← Claude Docs

How the agent loop works - Claude Code Docs

Claude Docs · April 8, 2026
The Agent SDK lets you embed Claude Code’s autonomous agent loop in your own applications. The SDK is a standalone package that gives you programmatic control over tools, permissions, cost limits, and output. You don’t need the Claude Code CLI installed to

Detailed Analysis

Anthropic's Claude Code SDK exposes the autonomous agent loop that powers Claude Code as a standalone, embeddable package, giving developers programmatic control over the same execution engine without requiring the Claude Code CLI. The loop follows a deterministic cycle: Claude receives a prompt alongside tool definitions and conversation history, evaluates the current state, produces text and/or tool call requests, and the SDK executes those tools and feeds results back to Claude — repeating until Claude issues a response containing no tool calls. Each full cycle constitutes a "turn," and the SDK surfaces this progression as a typed stream of messages: a `SystemMessage` at session initialization, `AssistantMessage` after each Claude response, `UserMessage` after each tool execution, optional `StreamEvent` messages for real-time streaming, and a terminal `ResultMessage` carrying the final output, token usage, cost, and session ID. The architecture is intentionally transparent, allowing developers to observe exactly what Claude is doing at each stage rather than treating the loop as a black box.

The practical significance of this design lies in the granular control it affords over agentic behavior. Developers can cap execution with `max_turns` or `maxTurns` — which counts only tool-use turns — or impose a spend ceiling via `max_budget_usd` / `maxBudgetUsd`, both of which are critical guardrails for production deployments where open-ended prompts can otherwise trigger unbounded chains of tool calls. Hooks allow interception, modification, or blocking of tool calls before execution, enabling custom permission logic layered on top of the SDK's built-in `canUseTool()` checks. The research context corroborates that this loop is implemented as a `while`-loop in `src/query.ts`, which inspects the API's `stop_reason` field — continuing to iterate when `stop_reason` equals `"tool_use"` and terminating when it does not — a clean architectural decision that keeps the core logic readable while production wrappers add streaming, concurrency, compaction, and sub-agent coordination around it.

This transparent, message-stream-based design reflects a broader trend in AI development toward making agentic systems observable and compositional rather than opaque. Earlier generations of AI integrations were largely request-response: a developer sent a prompt and received text. The agent loop paradigm treats an LLM as a reasoning engine inside an execution environment, capable of multi-step planning across many tool calls — exemplified in the documentation's "Fix the failing tests in auth.ts" scenario, which spans four turns including running tests, reading source files, editing code, and re-running tests before producing a final result. The ability to embed this loop programmatically, instrument each message type, and intercept tool calls is what allows teams to build reliable, auditable pipelines rather than brittle prompt chains.

The SDK's architecture also anticipates the scaling challenges inherent in complex agentic tasks. The inclusion of session compaction — signaled by the `compact_boundary` message subtype — indicates that the loop is designed to handle conversations long enough to approach context limits, automatically summarizing history to preserve continuity without developer intervention. Sub-agent support, noted in the research context, extends this further by enabling a lead agent to coordinate parallel workstreams, a pattern increasingly common in software engineering agents that must simultaneously handle testing, documentation, and code modification. These features position Claude Code's SDK not merely as a developer tool but as an infrastructure primitive for building multi-agent systems at production scale.

The release of this SDK documentation signals Anthropic's intent to move Claude from an assistant model to a platform on which agentic software is built. By exposing the internal mechanics of the agent loop — turn counting, message typing, hook interception, budget controls — Anthropic is making a bet that the developer community will want to build on top of Claude's reasoning and tool-use capabilities rather than simply querying it. This mirrors moves by competitors to offer similar agentic frameworks, but the emphasis on cost visibility, granular turn control, and a clearly typed message stream suggests a particular focus on production reliability and operational transparency, concerns that have historically been underserved in early-generation AI developer tooling.

Read original article →