Claude's API is a structured request/response surface: messages, tools, cache breakpoints, and a stop_reason you have to loop on. The managed Agents stack sits on top of that same loop.
โโโโโโ โโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโ โโโโโโโ โโโโโโโ โโโ โโโโโโโ โโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โโโ โโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโ โโโโโโ โโโ โโโโโโ โโโโโโ โโโ โโโ โโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโ โโโ โโโโโโ โโโโโ โโโ โโโ โโโโโโ โโโ โโโโโโโ โโโ โโโ โโโโโโโ โโโโโโโโโโ โโโโโโ โโโ โโโโโโโโโโ โโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโ โโโ โโโโโโโโโโโ โโโโโโ โโโโโโโโโ โโโ โโโ โโโโโโโโโโโ โโโโโโ โโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โโโ โโโโโโโ โโโโโโโ โโโโโโโโ
The Messages API is a stateless POST that carries the whole conversation. The response is a typed content[] array, not a string. max_tokens is required. system is a top-level field, not a message role.
max_tokens is required. Omitting it throws a validation error, there is no safe default. system is a top-level parameter, not a message with role: "system". Passing it inside messages will error. Both of these burn junior developers on first contact.
POST /v1/messages, Stateless, full-history request. GA. The core of every Claude integration.
POST /v1/messages/batches, Async bulk processing at 50% cost reduction. GA. For eval pipelines, bulk classification, and offline processing.
POST /v1/messages/count_tokens, Count tokens before sending. GA. Essential for cache planning and rate-limit budgeting.
POST /v1/files, Upload files once, reference across many calls. Beta. Eliminates re-encoding large documents on every request.
Claude's response is not a string. It is a content[] array of typed blocks. Accessing response.content[0].text directly will explode when the first block is a tool_use block, which happens whenever Claude decides to call a tool. Always check content[0].type before accessing fields.
{ type: "text", text: string }. The most common block for plain responses.{ type: "tool_use", id, name, input }. Indicates Claude wants to call a function. Your code must execute it and return results.{ type: "thinking", thinking: string }. Extended reasoning trace, only on models and requests that enable it. Not visible in standard responses.Tool use is commonly described as "function calling" but that framing misses the architecture. It is a multi-turn protocol where Claude signals intent, your application executes, and the result flows back into the conversation. The model itself never runs any code. You do. Claude just decides when and how to call.
Pass a tools[] array with name, description, and input_schema. The description is read by Claude, a vague description produces poor tool selection. Be explicit about when the tool should and should not be called.
The response content will contain a ToolUseBlock with { id, name, input }. The id is critical, it links the tool call to the tool result in the next turn.
Run the function, hit the API, query the database, whatever the tool requires. This happens entirely in your application. Claude is waiting with its context intact.
Append the assistant message (Claude's full content array) and a user message with { type: "tool_result", tool_use_id, content }. The tool_use_id must match the id from step 2.
Claude may call multiple tools in sequence or parallel. The loop continues until stop_reason is "end_turn". A single messages.create call is never enough for agentic tasks, build the while loop, not a one-shot call.
The tool use loop is your responsibility, not Anthropic's. Claude is stateless between calls. The growing messages[] array is the entire agent state. Every tool call round-trip costs input tokens proportional to history length, this is where agentic costs compound. Design tools that return concise, structured results.
Long outputs without streaming mean one thing: gateway timeouts and users staring at a blank screen. Claude's streaming mode uses Server-Sent Events (SSE) with a structured event sequence. It is not just chunked text, it is a typed event stream that includes tool use blocks, thinking deltas, and usage stats.
client.messages.stream() context manager. stream.text_stream for text-only iteration. Both sync and async supported.
client.messages.stream() with .on("text", cb) event handler. Typed event objects. Promise-based final message via stream.finalMessage().
Any HTTP client that handles text/event-stream. Use when you need the raw event sequence for custom routing (e.g., proxy to WebSocket).
Works together. Tool use blocks arrive as streamed JSON deltas. Accumulate input_json_delta events, parse on content_block_stop.
Prompt caching is the single biggest cost optimization available to heavy Claude users, and most teams are not using it. The idea is simple: mark a stable prefix of your prompt, system context, tool definitions, RAG documents, and Anthropic will cache it server-side. Cache reads cost approximately one-tenth of normal input token pricing.
Automatic caching, Add a single top-level cache_control: { type: "ephemeral" } to the request. Anthropic applies the breakpoint automatically and moves it forward as the conversation grows. Best for multi-turn conversations where the accumulated history should be cached between turns.
Explicit breakpoints, Place cache_control directly on individual content blocks for precise control. Supports up to four breakpoints per request. Use when you have a known stable prefix (large system prompt, product catalog, codebase context) followed by dynamic per-user content.
The cache prefix must be byte-for-byte identical across calls. Any dynamic value before the cache_control breakpoint, a timestamp, a user ID, unsorted JSON keys, will invalidate the cache on every call. Never interpolate volatile data before the last breakpoint. Also: minimum cacheable prefix is ~1024โ4096 tokens depending on model. Shorter prefixes silently skip caching with no error, just cache_creation_input_tokens: 0.
{ type: "ephemeral" }, Default 5-minute TTL. Enough for bursty request windows where many users share the same system prompt.{ type: "ephemeral", ttl: "1h" }, Extended TTL for longer-lived contexts. Useful when your system prompt is stable over a session that spans 30โ60 minutes.The 2026 Claude model family has four tiers in active production, plus invitation-only models under Project Glasswing. The right model is not the most capable one, it is the most capable one that fits your latency, cost, and quality requirements for the specific task.
| Model | ID | Sweet Spot | Tradeoff | Status |
|---|---|---|---|---|
| Claude Fable 5 | claude-fable-5 |
Long-running agents, hardest reasoning, enterprise work | Highest cost, highest latency, not for latency-sensitive paths | GA |
| Claude Opus 5 | claude-opus-5 |
Complex agentic coding, multi-step code review, deep reasoning | High cost; use where quality justifies it | GA |
| Claude Sonnet 4.6 | claude-sonnet-4-6 |
Everyday code gen, PR reviews, support agents, the default workhorse | Best cost/quality balance for most production workloads | GA |
| Claude Haiku 4.5 | claude-haiku-4-5-20251001 |
High-volume classification, streaming autocomplete, cheap routing | Lower reasoning depth; use only for well-defined, simple tasks | GA |
| Claude Mythos 5 | claude-mythos-5 |
Research frontier tasks, Project Glasswing invitation-only | Not generally available; requires approved access | INVITE |
A practical two-tier setup: Sonnet for the hot path (user-facing, latency-sensitive, most requests), Opus or Fable for the cold path (background agents, complex code review, deep analysis). Use the Token Counting API before the hot path to route requests that will need heavy reasoning to the cold path automatically.
All GA models available. IAM-integrated auth. Same API surface, Bedrock billing. Best for teams already on AWS with VPC data residency requirements.
GA models on Vertex. Google Cloud IAM auth. Useful when the rest of the stack is GCP and you want unified billing and compliance.
GA models via Azure-integrated Microsoft Foundry. Entra ID auth. For teams in the Microsoft enterprise ecosystem.
Latest models and features first. Anthropic billing and support. x-api-key header or Workload Identity Federation for keyless auth.
Building a tool use loop yourself is the low-level API. Anthropic now ships a higher-level managed agent runtime that handles state, sandboxed execution environments, and versioned agent definitions. It is still Beta as of August 2026. If it sticks, they own the execution layer, not just the model.
An Agent definition bundles a model, a system prompt, a set of tools and skills, and environment configuration into a versioned, reusable object. Once defined, you run sessions against it rather than reconstructing the full context on every call.
A Session is a running instance of an Agent in a managed cloud sandbox. Anthropic handles state persistence, tool routing, and environment setup. The client streams events via GET /v1/sessions/{id}/events/stream, an SSE endpoint that emits agent actions, tool calls, and output in real time.
messages[] array through your application.Claude Code (Anthropic's CLI) is itself built on this stack. When you run Claude Code as an agent, it uses the same tool use loop, session state model, and skills system described here, just packaged into a CLI workflow with bash, text_editor, and file system client tools. The Claude Code Max subscription uses subscription billing rather than API token pricing, which radically changes the economics for high-volume interactive workloads.
| API | Endpoint | Purpose | GA / Beta |
|---|---|---|---|
| Files | POST /v1/files |
Upload once, reference across many calls. Eliminates repeated encoding. | BETA |
| Skills | POST /v1/skills |
Create versioned, reusable agent capabilities. | BETA |
| Agents | POST /v1/agents |
Define reusable, versioned agent configurations. | BETA |
| Sessions | POST /v1/sessions |
Run stateful agent sessions in managed cloud sandboxes. | BETA |
| Environments | POST /v1/environments |
Configure sandbox templates for agent sessions. | BETA |
Claude API billing is per input and output token. Costs compound fast in agentic loops because every tool call round-trip re-sends the full conversation history as input tokens. A 10-tool-call agent session with a 4000-token system prompt can easily consume 50,000+ input tokens per run.
POST /v1/messages/count_tokens to gate expensive requests. If a request will consume >10k tokens, decide whether to proceed, warn the user, or summarize the context first.The Messages API is stateless by design. Your application owns the conversation state. Build the growing messages[] array correctly and never assume Claude remembers previous calls.
Tool use is a loop, not a single call. Claude signals intent via stop_reason: "tool_use". You execute. You return tool_result with the matching tool_use_id. Repeat until "end_turn". One call is never enough for agentic tasks.
Prompt caching is the single most impactful cost optimization. A stable system prompt longer than ~1024 tokens should always have a cache_control breakpoint. Verify it's working by checking usage.cache_read_input_tokens.
Stream everything that could be long. Vercel and Netlify function timeouts will clip long non-streamed responses. Use client.messages.stream() for code gen, docs, and any agentic output that might exceed 60 seconds.
Model routing is not optional at scale. Use Haiku for classification and routing, Sonnet for everyday generation, Opus for complex agents, and Fable for the hardest long-running work. Never default to the largest model for tasks that don't need it.
The Managed Agents stack is Anthropic's play to own the agent runtime, not just the model. Agents, Sessions, Skills, and Environments are Beta today, but track them. If they stabilize, the case for building your own agent state management weakens significantly.
Tool result size is a hidden cost driver. Every tool result comes back as input tokens. Design tools to return the minimum information Claude needs, not raw API dumps. A well-formatted 100-token summary beats a 5000-token JSON blob every time.
Sources: Anthropic API Overview ยท Tool Use Guide ยท Prompt Caching ยท Models Overview ยท Streaming Guide