Claude API Mechanics
Official scope (Exam Guide v1.0): Messages, tools, streaming, vision, thinking, caching, invoking Claude through third-party vendors, data access patterns, batch, and realtime vs batch trade-offs. The most concrete Domain 2 skill.
Request
| Field | Notes |
|---|---|
model | A model ID. Every ID is a pinned snapshot, including dateless IDs from the 4.6 generation on |
max_tokens | Required. A ceiling, not a target. Thinking counts inside it |
messages | Full history every call; the API is stateless |
system | Top-level field, not a message role |
tools, tool_choice, stream, thinking, output_config | Optional |
Response and stop reasons
content is an array of blocks (text, tool_use, thinking, ...). Read by type. usage reports tokens. Check stop_reason before using the answer.
stop_reason | Meaning | Action |
|---|---|---|
end_turn | Finished | Use it |
max_tokens | Hit your limit; truncated | Raise the limit or continue; incomplete tool_use means retry higher |
stop_sequence | Your stop sequence fired | Read stop_sequence |
tool_use | Wants a client tool | Run it; return a tool_result for every tool_use block |
pause_turn | A server-tool loop hit its iteration limit (default 10) | Send the assistant content back unchanged to continue |
refusal | Declined; a normal HTTP 200 | Read stop_details; retry on a fallback model |
model_context_window_exceeded | Filled the context window | Treat as truncated |
A loop must handle any stop reason. Empty end_turn replies are usually caused by adding text after tool_result blocks: send tool_result blocks alone.
Tool-use loop
tool_use block (id, name, input) → your code runs it → user message with tool_result and matching tool_use_id → repeat while stop_reason is tool_use. Claude may request several tools at once; return all results together.
Streaming
stream: true returns server-sent events: message_start → per block content_block_start, deltas, content_block_stop → message_delta (carries stop_reason) → message_stop. Tool inputs arrive as partial JSON: parse at block stop. An error can arrive after a 200 has started; handle mid-stream error events separately.
Prompt caching
- Caches a prefix in order tools → system → messages. Up to 4 breakpoints; automatic caching (one top-level
cache_control) moves a breakpoint forward as the conversation grows. Lookback window: 20 blocks. - Minimum cacheable length: 512 tokens on the current 5.x models (1,024 to 4,096 on older ones). Below it, nothing is cached and no error is returned.
- TTL 5 minutes (write 1.25x base input) or 1 hour (write 2x). Reads 0.1x base input by default (lower on some newest models).
- Changing tool definitions invalidates everything;
tool_choiceor images invalidate messages; thinking or effort changes invalidate messages; switching fast/standard speed invalidates system and messages. - Proof:
cache_creation_input_tokens,cache_read_input_tokens,input_tokens(total input is the sum of all three).
Batch
Message Batches API: 50% off, up to 100,000 requests or 256 MB per batch, most finish within 1 hour, expire if not done in 24 hours, results kept 29 days. Results can come back in any order: match on custom_id. Result types: succeeded, errored, canceled, expired (the last three are not billed).
Vision
- Image blocks by base64, URL or Files API
file_id. On Amazon Bedrock and Google Cloud only base64 is available. - JPEG, PNG, GIF (first frame), WebP. Up to 600 images per API request (100 for 200k-context models); 10 MB per image on the API (5 MB on Bedrock and Google Cloud); 32 MB request limit often hits first.
- Cost: 28x28-pixel patches,
ceil(w/28) x ceil(h/28)tokens; oversized images are downscaled. Images before the question works best. - Limits: will not identify people; approximate counting and coordinates; small (<200 px) or rotated images are error-prone; cannot generate images.
Thinking (verified, summary; detail in 5.2)
Thinking tokens are billed as output and count inside max_tokens. Adaptive thinking plus effort on current models; manual budget_tokens only on older ones. Return thinking blocks unmodified during tool use.
Third-party platforms
- Claude runs on the Claude API, Claude Platform on AWS, Microsoft Foundry (Anthropic-operated) and Amazon Bedrock, Google Cloud (partner-operated).
- Model IDs differ: Bedrock uses
anthropic.claude-opus-5-5; the others use the Claude API ID. - Partner-operated platforms set their own lifecycle dates; feature availability differs. Example: the current Bedrock endpoint lacks Agent Skills, the MCP connector, structured outputs, Files API inputs and server tools; fast mode is first-party (and Foundry on Azure) only.
Data access patterns
Stateless API: resend history. Files API uploads once and references by file_id (500 MB per file, 1 TB per org; files are workspace-scoped, so never accept file_ids from end users). Large documents and many images hit request-size limits first.
Realtime vs batch (design guidance, matches the guide's sample question)
Someone waiting: realtime (stream it). Nobody waiting and cost matters: batch.
Exam traps
max_tokensoptional (it is required) or treated as a target length.systemas a message role.- A 200 means success (check
stop_reason;refusalis also a 200). - Caching a 40-token prompt and expecting savings.
- Matching batch results by position instead of
custom_id. - Expecting URL images on Bedrock or identical model IDs on every platform.
References
- Handling stop reasons — checked Oct 16, 2026
- Errors — checked Oct 16, 2026
- Prompt caching — checked Oct 16, 2026
- Batch processing — checked Oct 16, 2026
- Vision — checked Oct 16, 2026
- Files API — checked Oct 16, 2026
- Models overview — checked Oct 16, 2026
- Claude in Amazon Bedrock — checked Oct 16, 2026
Check your understanding
Answer cut off mid-sentence, HTTP 200. What should the code have checked?Show answerHide answer
Answer
stop_reason(max_tokens).Your cache counters are both zero. Most likely cause?Show answerHide answer
Answer
Prefix below the minimum cacheable length.Overnight job, 50k prompts. Interface and how to match results?Show answerHide answer
Answer
Message Batches API;custom_id.The same app moves from the Claude API to Bedrock and breaks. Two things to check?Show answerHide answer
Answer
Model ID format and feature availability (for example URL images, Files API, beta features).