Applications and Integration
Messages API Mechanics
Exam objective: Applying Messages API mechanics in application code and API requests, including message structure, tool use, streaming, vision, thinking, prompt caching, structured output, and stop reasons, to produce the intended behavior, and choosing between realtime and batch processing.
What the exam tests
Applying Messages API mechanics in code and requests: message structure, tool use, streaming, vision, thinking, prompt caching, structured output and stop reasons, plus the choice between realtime and batch processing. Expect a broken request, response or loop and a question about which change produces the intended behavior.
Key rules
Messages. The API is stateless: send the whole history each time, adding each response's content unchanged as an assistant turn. Claude 4.6 and later reject a prefilled final assistant turn.
Stop reasons are part of successful responses; errors are 4xx/5xx. end_turn: use the reply. max_tokens and model_context_window_exceeded: truncated. stop_sequence: check which fired. tool_use: run the tool. pause_turn: send the assistant content back. refusal: HTTP 200, read stop_details.
Tool use. Answer every client tool_use block with a tool_result carrying its tool_use_id, all in the next user message, results before any text. Failures get is_error: true. Detailed tool descriptions matter most for performance.
[{"role": "assistant", "content": [{"type": "tool_use", "id": "toolu_01", "name": "get_weather", "input": {"city": "Paris"}}]},
{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "toolu_01", "content": "18 C"}]}]
tool_choice is auto, any, tool or none. Claude Opus 5.5, Sonnet 5.5, Fable 5.1/5.2 and Mythos 5.1/5.2 reject any and tool; so does manual extended thinking. disable_parallel_tool_use goes inside tool_choice.
Streaming. message_start, then per block content_block_start, deltas, content_block_stop, then message_delta (carries stop_reason; usage is cumulative) and message_stop. Tool input arrives as partial JSON strings. Errors can arrive mid-stream, and unknown event types must be tolerated.
Vision. Image blocks use base64, URL or Files API file_id. Put images before text. Oversized images are downscaled, which costs fine detail; larger images cost more visual tokens up to the tier cap.
Thinking. Configuration differs by model. On Claude 5 and later it is on by default and display defaults to "omitted". In a tool loop, return thinking and redacted_thinking blocks unmodified. Lower effort to reduce cost; max_tokens is the hard ceiling and includes thinking.
Prompt caching. Prefix order is tools, system, messages. Use a top-level cache_control (automatic) or up to 4 block breakpoints. Default lifetime is 5 minutes, optionally "ttl": "1h". Below the model's minimum length nothing is cached and no error is raised.
Structured output. output_config.format with a JSON schema shapes the reply; strict: true on a tool guarantees its inputs. Objects need additionalProperties: false. A refusal or max_tokens stop can still yield non-matching output.
Batches. Asynchronous, 50% cheaper, usually under an hour, expiring after 24 hours. No stream. Match results by custom_id.
Traps
- One user message per tool result, or text placed before the results.
- Treating a truncated tool call as a tool error; retry with higher
max_tokens. - Reading
stop_reasonfrommessage_start, or parsing eachinput_json_deltaalone. - A cache breakpoint on a block with a timestamp: every request writes, none reads.
- Filtering content by type and silently losing
redacted_thinking. - Changing thinking settings,
tool_choiceor images and expecting message caches to survive. - Sending parallel requests before the first response starts and expecting cache hits.
- Assuming batch results keep input order.
Decide quickly
| Situation | Choice | Why |
|---|---|---|
| User watches the reply | Streaming | Text appears as generated |
| Long output, no need for events | Stream and collect the final message | Avoids HTTP timeouts |
| Large offline job | Message Batches | Cheaper, latency tolerant |
| Reply must be schema-valid JSON | output_config.format | Constrained decoding |
| Tool arguments must be valid | strict: true | Guarantees the input schema |
| Same long prefix, varying suffix | Breakpoint at end of prefix | Hash stays identical |
| Reuse gaps of 5 to 60 minutes | 1-hour TTL | Entry survives the gap |
| Same image across many turns | Files API file_id | Small payloads |