AI & LLM
Updated for 2026

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

import Anthropic from '@anthropic-ai/sdk'; const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
Initialize the Anthropic client SDK using environment credentials.

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

Console / Terminal
{
  "id": "msg_013Z95E5Z",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello there! I am Claude."
    }
  ]
}

Messages API

const msg = await anthropic.messages.create({ model: 'claude-3-5-sonnet-latest', max_tokens: 1024, messages: [{ role: 'user', content: 'Hi' }] });
Send a standard non-streaming Messages API request to the current Sonnet model.

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

Console / Terminal
{
  "id": "msg_013Z95E5Z",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello there! I am Claude."
    }
  ]
}

Streaming

const stream = await anthropic.messages.create({ model: 'claude-3-5-sonnet-latest', max_tokens: 1024, messages: [...], stream: true });
Initiate an Anthropic streaming event-emitter stream for real-time text delivery.

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

Console / Terminal
{
  "id": "msg_013Z95E5Z",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello there! I am Claude."
    }
  ]
}
stream.on('text', (text) => console.log(text));
Listen and respond to raw incoming text stream segments as they arrive.

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

Console / Terminal
{
  "id": "msg_013Z95E5Z",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello there! I am Claude."
    }
  ]
}

System & Params

system: "You are a specialized mathematical calculator. Respond only with numbers."
Set high-priority system-level instructions using the top-level 'system' parameter.

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

Console / Terminal
{
  "id": "msg_013Z95E5Z",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello there! I am Claude."
    }
  ]
}
temperature: 0.5, top_p: 0.9
Apply custom sampling temperature or top_p variables to control response diversity.

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.9

Output Example

Console / Terminal
{
  "id": "msg_013Z95E5Z",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello there! I am Claude."
    }
  ]
}

Multimodal

{ role: 'user', content: [{ type: 'image', source: { type: 'base64', media_type: 'image/jpeg', data: b64Data } }] }
Submit Base64 encoded JPEG or PNG image binary data to Claude's vision system.

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

Console / Terminal
{
  "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

Error

Missing required parameter 'max_tokens'

Solution

Anthropic's API strictly requires declaring 'max_tokens' in every request. Set an integer value (e.g. max_tokens: 1024).

Error

400 Invalid parameter: system not allowed inside messages

Solution

The system instructions must be placed in the top-level 'system' string parameter, not as a message with role 'system'.

Error

429 Too Many Requests (Rate limit)

Solution

Your requests are hitting rate limits. Implement exponential backoff delays or ask Anthropic for a tier limit increase.

Error

Base64 validation error on image payload

Solution

Ensure you are sending the raw base64 string without data-URI prefixes (e.g. remove 'data:image/jpeg;base64,') from the content.

Error

Overlapping message roles

Solution

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.