Anthropic Claude SDK Cheatsheet 2026
Quick reference for Anthropic's Claude API: client setup, Messages API, system instructions, token streams, and multi-modal payloads.
Initialization
When to Use
When executing chat, context-heavy, or multi-modal analysis queries using Anthropic's Claude Messages API.
Common Mistakes
Passing system-level instructions in the general 'messages' array rather than using the dedicated top-level 'system' parameter.
Shortcut / Pro-Tip
Use Claude's long 200k context window to ingest complete files or logs in a single query.Example
import Anthropic from '@anthropic-ai/sdk'; const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });Output Example
{
"id": "msg_013Z95E5Z",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello there! I am Claude."
}
]
}Messages API
When to Use
When executing chat, context-heavy, or multi-modal analysis queries using Anthropic's Claude Messages API.
Common Mistakes
Passing system-level instructions in the general 'messages' array rather than using the dedicated top-level 'system' parameter.
Shortcut / Pro-Tip
Use Claude's long 200k context window to ingest complete files or logs in a single query.Example
const msg = await anthropic.messages.create({ model: 'claude-3-5-sonnet-latest', max_tokens: 1024, messages: [{ role: 'user', content: 'Hi' }] });Output Example
{
"id": "msg_013Z95E5Z",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello there! I am Claude."
}
]
}Streaming
When to Use
When executing chat, context-heavy, or multi-modal analysis queries using Anthropic's Claude Messages API.
Common Mistakes
Passing system-level instructions in the general 'messages' array rather than using the dedicated top-level 'system' parameter.
Shortcut / Pro-Tip
Use Claude's long 200k context window to ingest complete files or logs in a single query.Example
const stream = await anthropic.messages.create({ model: 'claude-3-5-sonnet-latest', max_tokens: 1024, messages: [...], stream: true });Output Example
{
"id": "msg_013Z95E5Z",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello there! I am Claude."
}
]
}When to Use
When executing chat, context-heavy, or multi-modal analysis queries using Anthropic's Claude Messages API.
Common Mistakes
Passing system-level instructions in the general 'messages' array rather than using the dedicated top-level 'system' parameter.
Shortcut / Pro-Tip
Use Claude's long 200k context window to ingest complete files or logs in a single query.Example
stream.on('text', (text) => console.log(text));Output Example
{
"id": "msg_013Z95E5Z",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello there! I am Claude."
}
]
}System & Params
When to Use
When executing chat, context-heavy, or multi-modal analysis queries using Anthropic's Claude Messages API.
Common Mistakes
Passing system-level instructions in the general 'messages' array rather than using the dedicated top-level 'system' parameter.
Shortcut / Pro-Tip
Use Claude's long 200k context window to ingest complete files or logs in a single query.Example
system: "You are a specialized mathematical calculator. Respond only with numbers."Output Example
{
"id": "msg_013Z95E5Z",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello there! I am Claude."
}
]
}When to Use
When executing chat, context-heavy, or multi-modal analysis queries using Anthropic's Claude Messages API.
Common Mistakes
Passing system-level instructions in the general 'messages' array rather than using the dedicated top-level 'system' parameter.
Shortcut / Pro-Tip
Use Claude's long 200k context window to ingest complete files or logs in a single query.Example
temperature: 0.5, top_p: 0.9Output Example
{
"id": "msg_013Z95E5Z",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello there! I am Claude."
}
]
}Multimodal
When to Use
When implementing visual, optical document, or multimodal diagram analysis with Claude Vision.
Common Mistakes
Sending raw visual file paths instead of encoding them as Base64 text payloads alongside metadata headers.
Shortcut / Pro-Tip
Ensure you explicitly specify the media_type (e.g., image/jpeg or image/png) in your payload object.Example
{ role: 'user', content: [{ type: 'image', source: { type: 'base64', media_type: 'image/jpeg', data: b64Data } }] }Output Example
{
"id": "msg_013Z95E5Z",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello there! I am Claude."
}
]
}Claude API Best Practices
1Keep System Prompts Dedicated
Use Anthropic's top-level 'system' parameter to specify guidelines and constraints, rather than mixing them into the messages array.
2Format Vision Payloads Correctly
Encode image assets in Base64 format and supply the proper media type (image/jpeg, image/png, image/webp) alongside prompt content blocks.
3Prefill Claude's Responses
Inject text in the final 'assistant' message block to prefill Claude's response, guiding its output style or skipping initial conversational fluff.
4Keep APIs Server-Secured
Always route Claude API calls through secure server endpoints. Never expose ANTHROPIC_API_KEY to browser clients.
5Set max_tokens Appropriately
Always specify the max_tokens parameter manually; Anthropic requires this parameter to be explicitly declared on every call.
Common Claude API Errors & Solutions
Missing required parameter 'max_tokens'
Anthropic's API strictly requires declaring 'max_tokens' in every request. Set an integer value (e.g. max_tokens: 1024).
400 Invalid parameter: system not allowed inside messages
The system instructions must be placed in the top-level 'system' string parameter, not as a message with role 'system'.
429 Too Many Requests (Rate limit)
Your requests are hitting rate limits. Implement exponential backoff delays or ask Anthropic for a tier limit increase.
Base64 validation error on image payload
Ensure you are sending the raw base64 string without data-URI prefixes (e.g. remove 'data:image/jpeg;base64,') from the content.
Overlapping message roles
The 'messages' array must strictly alternate between 'user' and 'assistant' roles, starting with 'user'.
Common Claude API Interview Questions
Q1What is Anthropic's Messages API?
The Messages API is the modern endpoint structure for communicating with Claude models. It replaces the legacy Text Completions endpoint and supports multi-modal image inputs and system instructions.
Q2How can you prefill Claude's responses and why is it useful?
By adding a message with the role 'assistant' at the end of the messages array, you can force Claude to start its reply with specific text (e.g., '{' to enforce clean JSON formatting).
Q3What is the context window length of Claude 3.5 Sonnet?
Claude 3.5 Sonnet supports a very large 200,000 token context window, allowing you to upload complete repositories, codebases, or extensive books.
Q4How does Claude handle images in its Vision API?
Claude supports sending images encoded as Base64 blocks with media types like image/png, image/jpeg, image/webp, or image/gif within the content array alongside text blocks.
Q5What is prompt caching in Anthropic's API?
Prompt caching is a feature that allows you to cache frequently used context (like system instructions or documents) on Anthropic's servers, reducing latency and cost by up to 90% for subsequent requests.
Generated from LearnHubly Developer Cheatsheets
Access interactive sandbox tests, tools, and developer code bases at https://www.learnhubly.com