Build an Anthropic Messages API Request
The Anthropic Messages API is the primary interface for all Claude models. Unlike the OpenAI Chat Completions API, the Messages API separates the system prompt into a dedicated top-level field rather than placing it in the messages array. This structural difference is significant: Claude uses the system field as a higher-priority instruction channel that the model is strongly trained to follow, making system-level constraints more reliable than embedding them as user messages. This example shows a complete Messages API request for a document analysis task with the system field, a user message containing a document, and generation parameters. The max_tokens field is required in the Anthropic API (unlike OpenAI where it is optional) — omitting it causes a validation error. The model field must use the exact model ID including the full version string like "claude-3-5-sonnet-20241022". Key differences from OpenAI: temperature range is 0.0–1.0 (not 0.0–2.0), there is no presence_penalty or frequency_penalty parameter, and streaming is enabled via stream: true rather than a separate streaming endpoint. The Anthropic API also supports extended thinking for claude-3-7-sonnet, which enables multi-step reasoning with visible thought traces.
{
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 1024,
"system": "You are an expert document analyst. Extract key information accurately and present it in a structured format.",
"messages": [
{
"role": "user",
"content": "Please summarize the key points from this meeting notes document and identify action items:\n\nMeeting: Q4 Planning Session\nDate: November 15, 2024\nAttendees: Alice, Bob, Carol\n\nDiscussion: The team reviewed Q3 results showing 15% growth. For Q4, priorities are: (1) launch new API features by Dec 1, (2) reduce support tickets by 20%, (3) complete security audit. Bob will lead the API work, Carol owns support improvements, Alice manages the audit."
}
]
}FAQ
- Why does Claude use a separate system field instead of a system role in messages?
- Anthropic designed the API with a separate system field to give developers a clear, reliable channel for instructions. Claude is strongly trained to treat the system field as authoritative, making constraints there more consistent than embedding them as user-role messages.
- Is max_tokens required in the Anthropic API?
- Yes. Unlike OpenAI where max_tokens is optional (defaulting to model maximum), the Anthropic API requires max_tokens in every request. Omitting it returns a 400 validation error.
- What models are available in the Anthropic API?
- The main models are Claude 3.5 Sonnet (best balance of speed and intelligence), Claude 3.5 Haiku (fastest, most affordable), and Claude 3 Opus (most capable for complex tasks). Always use the full model ID with date suffix to ensure consistent behavior.
Related Examples
Claude's system prompt is the highest-priority instruction channel in the Anthro...
Count Tokens for a Claude RequestThe Anthropic API provides a dedicated token counting endpoint that returns the ...
Build an OpenAI Chat Completion RequestThe Chat Completion API is the primary interface for all GPT models and the foun...