Authorizations
API key for authenticationVisit the API Key Management Page to get your API KeyAdd to request header:
API versionSpecifies the Claude API version to useExample:
2023-06-01Body
Model name
claude-haiku-4-5-20251001- Claude 4.5 Fast Response Versionclaude-sonnet-4-5-20250929- Claude 4.5 Balanced Versionclaude-opus-4-1-20250805- Most Capable Claude 4.1 Flagship Modelclaude-opus-4-1-20250805-thinking- Claude 4.1 Opus Deep Thinking Versionclaude-sonnet-4-5-20250929-thinking- Claude 4.5 Sonnet Deep Thinking Version
List of messagesArray of messages for the model to generate the next response. Supports alternating Multi-turn conversation:Prefilled assistant response:
user and assistant roles.Each message contains:role: Role (userorassistant)content: Content (string or array of content blocks)
Maximum tokens to generateMaximum number of tokens to generate before stopping. The model may stop before reaching this limit.Different models have different maximum values.Minimum: 1
System promptSystem prompts set Claude’s role, personality, goals, and instructions.String format:Structured format:
Temperature parameter, range 0-1Controls randomness of output:
- Low values (e.g., 0.2): More deterministic, conservative
- High values (e.g., 0.8): More random, creative
Nucleus sampling parameter, range 0-1Uses nucleus sampling. Recommend using either
temperature or top_p, not both.Default: 1.0Top-K samplingSample from top K options only, removes “long tail” low probability responses.Recommended for advanced use cases only.
Enable streamingWhen
true, uses Server-Sent Events (SSE) to stream responses.Default: falseStop sequencesCustom text sequences that cause the model to stop generating.Maximum 4 sequences.Example:
["\n\nHuman:", "\n\nAssistant:"]MetadataMetadata object for the request.Includes:
user_id: User identifier
Tool definitionsList of tools the model can use to complete tasks.Function tool example:Supported tool types:
- Custom function tools
- Computer use tool (computer_20241022)
- Text editor tool (text_editor_20241022)
- Bash tool (bash_20241022)
Tool choice strategyControls how the model uses tools:
{"type": "auto"}: Auto-decide (default){"type": "any"}: Must use a tool{"type": "tool", "name": "tool_name"}: Use specific tool
Response
Unique message identifierExample:
"msg_013Zva2CMHLNnXjNJJKqJ2EF"Object typeAlways
"message"RoleAlways
"assistant"Content blocks arrayContent generated by the model, as an array of content blocks.Text content:Tool use:Content types:
text: Text contenttool_use: Tool invocation
Model that handled the requestExample:
"claude-sonnet-4-5-20250929"Stop reasonPossible values:
end_turn: Natural completionmax_tokens: Reached maximum tokensstop_sequence: Hit stop sequencetool_use: Invoked a tool
Stop sequence triggeredThe stop sequence that was generated, if any; otherwise
nullToken usage statistics
Usage Examples
Basic Conversation
Multi-turn Conversation
Using System Prompts
Streaming Response
Tool Use
Vision Understanding
Base64 Image
Best Practices
1. Prompt Engineering
Clear role definition:2. Error Handling
3. Token Optimization
4. Prefilling Responses
Streaming Response Handling
Python Streaming
JavaScript Streaming
Important Notes
-
API Key Security:
- Store API keys in environment variables
- Never hardcode keys in source code
- Rotate keys regularly
-
Rate Limiting:
- Be aware of API rate limits
- Implement retry mechanisms
- Use exponential backoff
-
Token Management:
- Monitor token usage
- Optimize prompt length
- Use appropriate max_tokens values
-
Model Selection:
- Opus: Complex tasks, deep thinking required
- Sonnet: Balanced performance and cost
- Haiku: Fast response, simple tasks
-
Content Filtering:
- Validate user input
- Filter sensitive information
- Implement content moderation