Overview
Every request to the Claude API is built around themessages array. This is the core data structure that carries the conversation — who said what, in what order, and what type of content was exchanged.
Understanding the messages array is the foundation of everything else in this reference.
Structure
Themessages array is a list of objects. Each object represents one turn in the conversation and has two required fields:
A basic messages array looks like this:
Roles
The Claude API uses two roles inside the messages array:The
system role does not go inside the messages array. System instructions are passed as a separate top-level system field in the request body. See the System Prompts guide for details.Multi-turn Conversations
The Claude API is stateless — Claude has no memory between requests. Every time you send a request, Claude only knows what you include in that request. To maintain context across a conversation, your application must pass the full conversation history with every request:This is how every LLM API works…context is not stored server-side. Your application is responsible for maintaining and passing conversation history.
Alternating Roles
Messages must follow a strict alternating pattern: user → assistant → user → assistant → user The array must always:- Start with a
usermessage - Alternate between
userandassistantroles
Content Types
Thecontent field accepts two formats:
Plain Text
The simplest format — a string directly:Structured Content Blocks
An array of typed content objects. Used for tool results, images, and multi-part messages:Token Accumulation
Every message in the history consumes input tokens. As a conversation grows longer, the input token count increases with every request — because you are sending the full history each time.- Turn 1: 17 input tokens
- Turn 2: 45 input tokens (history + new message)
- Turn 3: 89 input tokens (longer history + new message)
- Cost — input tokens are billed on every request
- Context limits — the total conversation history must fit within the model’s context window
Context Window
The context window is the maximum number of tokens a model can process in a single request — including both the input (your messages) and the output (Claude’s response).
200,000 tokens is approximately 150,000 words, enough for several novels worth of conversation history. For most applications this limit is rarely reached, but it is worth planning for in long-running or document-heavy use cases.
What Happens When the Limit Is Reached
If your messages array exceeds the context window, the API returns an error:Next Steps
Tokens
Understand how tokens are counted and how they affect cost
System Prompts
Learn how to set Claude’s behavior before the conversation starts
Tool Use
See how structured content blocks are used in tool use responses
API Reference
Full endpoint reference for the messages endpoint