Tool deep diveApps and agents

Claude Agent SDK: Claude Code's agent loop inside your own app

Python and TypeScript libraries that run Claude Code's agent loop, built-in tools, permissions, hooks, subagents, MCP and sessions in a process you operate. How it works, and how to put a production agent under control.

Checked 13 Oct 202612 min read

Vendor
Anthropic
Licence
Use under Anthropic's Commercial Terms; Python package code MIT, TypeScript package proprietary
Latest release
v0.2.165 · 8 Oct 2026 · Release notes (opens in a new tab)

Best for

Agents that read and edit files, run commands and call tools, with Claude Code's permissions, hooks and sessions built in

Avoid when

You need non-Claude models, or a single API call or a short tool loop where the client SDK's tool runner is simpler

Learn it hands-onClaude Agent SDK: Claude Code's agent loop inside your own app is taught in Agent Operations: See, Control, and Optimize Agents, with machine-verified labs.
On this page
  1. What it is and where it sits
  2. When to pick it
  3. How it works
  4. Members only: Set it up
  5. Members only: Production configuration
  6. Members only: Wiring it into the stack
  7. Members only: Operating it
  8. Members only: Alternatives compared
  9. Members only: Learn it hands-on

The Claude Agent SDK gives your application the same agent that runs in Claude Code: it plans, reads and edits files, runs shell commands, searches the web, calls MCP tools and delegates to subagents, with Claude Code's permission system, hooks and session history around it. You call query() with a prompt and options; the SDK drives the loop and streams messages back until the task is done or a limit stops it.

What it is and where it sits#

The SDK is a library in two languages: claude-agent-sdk for Python 3.10 or newer (0.2.165 on 8 October 2026) and @anthropic-ai/claude-agent-sdk for Node.js 18 or newer (0.3.296 on 9 October 2026). It was called the Claude Code SDK until Anthropic renamed it; old code imports claude_code_sdk and ClaudeCodeOptions. Use of the SDK is governed by Anthropic's Commercial Terms of Service, including when you build products for your own customers. The Python package's code carries an MIT licence file; the TypeScript package is proprietary.

Under the hood, query() spawns the claude CLI as a subprocess and talks to it over stdio. Both SDKs bundle a native Claude Code binary pinned to the SDK version, so updating the SDK updates the agent. The subprocess owns a shell, a working directory and session transcripts on local disk, which is why hosting it is closer to hosting a worker than a stateless API.

What comes built in:

  • Tools. Read, Write, Edit, Glob, Grep, Bash, WebSearch, WebFetch, ToolSearch, and orchestration tools such as Agent (subagents), Skill, AskUserQuestion, TaskCreate and TaskUpdate.
  • Custom tools and MCP. Your own functions become tools through an in-process MCP server; external MCP servers connect over stdio, HTTP or SSE.
  • Permissions. Allow and deny rules, permission modes and an approval callback decide whether each tool call runs.
  • Hooks. Callbacks at points such as PreToolUse, PostToolUse and SubagentStop can block, rewrite or log tool calls.
  • Subagents, sessions, skills and plugins. Specialized agents with their own prompts and tools; resumable and forkable conversations; skills, commands and CLAUDE.md memory loaded from .claude/ directories.

In the stack, it is an agent harness you host yourself. Anthropic's own comparison puts it between the Client SDK (you write the tool loop, or use its tool runner, for tools you define) and Managed Agents (Anthropic hosts the loop and the sandbox). It works with Claude models through the Anthropic API, Amazon Bedrock, Claude Platform on AWS, Google Cloud's Agent Platform and Microsoft Foundry.

When to pick it#

Pick the Agent SDK when:

  • The agent works on files and commands. Code review, migrations, test fixing, report generation from a data directory, log triage: the built-in file and shell tools do most of the work.
  • You want Claude Code's guardrails without rebuilding them. Permission modes, scoped rules like Bash(git log *), hooks and an approval callback are already there.
  • Tasks are long and multi-step. The SDK handles context compaction, prompt caching, parallel tool calls and subagents.
  • You already use Claude Code. Skills, subagent definitions, hooks and CLAUDE.md files your team wrote for Claude Code load into SDK agents.

Pick something else when:

  • You need other model providers. The SDK runs Claude models only. LangGraph or a gateway-based loop keeps you portable.
  • The job is one call or a short loop over your own tools. The Client SDK and its tool runner are less to host than a CLI subprocess per session.
  • You do not want to run the infrastructure. Managed Agents hosts the loop and a per-session sandbox for you.
  • You planned to let users sign in with their Claude subscription. Anthropic does not allow third-party products built on the SDK to offer claude.ai login or its rate limits unless previously approved. Use API keys or a cloud provider.

How it works#

1. Your prompt goes to the model. 2. The model thinks and plans the next step. 3. It calls a tool: run npm test. 4. A PreToolUse hook checks the call and allows it with exit code 0. 5. The tool runs. 6. The result returns and is added to the model context. 7. The model thinks again; on turn 2 it decides the work is done. 8. The answer goes out.Prompt inyour taskModel thinksplans the next stepAnswer outwhen the work is doneHook checks itPreToolUse checkTool runsruns npm testResult returnsadded to contextBash · npm testLoop until done

The loop. Claude receives your prompt with the system prompt, tool definitions and history; the SDK yields a SystemMessage with subtype init. Claude answers with text and tool calls; tools run (in parallel where possible); results go back; this repeats until Claude replies with no tool calls. The SDK then yields a final AssistantMessage and a ResultMessage holding the result text, token usage, an estimated total_cost_usd, num_turns, the session_id and a subtype that says whether it succeeded or hit a limit.

Limits. max_turns (maxTurns) counts tool-use round trips. max_budget_usd (maxBudgetUsd) stops the loop when estimated spend reaches the cap; the response that crosses it still completes, so spend can overshoot by one response plus whatever running subagents spend. Hitting either returns a ResultMessage with subtype error_max_turns or error_max_budget_usd. effort trades reasoning depth for tokens.

Context. Everything accumulates in one context window: system prompt, tool schemas, history and tool outputs. Stable parts are prompt-cached automatically. Near the limit, the SDK compacts older history into a summary and emits a compact_boundary system message; rules that must survive belong in CLAUDE.md, which is re-injected on every request.

Permissions, in order. For each tool call the SDK checks: hooks first (a hook can deny; a hook "allow" does not skip the next two steps), then deny rules (which win even in bypassPermissions mode), then ask rules, then the permission mode, then allow rules, then your can_use_tool callback. The modes:

ModeWhat happens to calls no rule settles
defaultGo to your can_use_tool callback; with no callback, they are denied
acceptEditsFile edits and common filesystem commands are approved; other shell commands follow the rules
planClaude explores and plans; edits and shell writes always go to your callback
dontAskDenied without prompting. Only rule-approved calls and calls that never need approval, such as reads inside the working directory, run
autoA model classifier reviews actions such as shell commands and network requests
bypassPermissionsApproved, except explicit ask rules and tools that need user interaction. TypeScript also needs allowDangerouslySkipPermissions: true; refused when running as root on Unix

Configuration loading. When you omit setting_sources (settingSources), the SDK reads the same files as the CLI: user, project and local settings, CLAUDE.md, and skills, agents, commands and hooks under .claude/. Pass an empty list to run only on what you configure in code.

Set it up

For cloud providers instead of an API key, set CLAUDE CODE USE BEDROCK=1 (Amazon Bedrock), CLAUDE CODE USE ANTHROPIC AWS=1 with ANTHROPIC AWS WORKSPACE ID (Claude Platform on AWS), CLAUDE CODE USE VERTEX=1 (Google Cloud) or CLAUDE CODE USE FOUNDRY=1 (Microsoft Foundry), plus that cloud's ...

Members only below this line

Get the full Claude Agent SDK: Claude Code's agent loop inside your own app deep dive free

What you get on this page:

  • 6 members sections

Free. You also get Stratiflux Weekly: one email a week, unsubscribe anytime.

Also in the members part

  1. Production configuration
  2. Wiring it into the stack
  3. Operating it
  4. Alternatives compared
  5. Learn it hands-on

Sources

Checked against these on 13 Oct 2026.

  1. Agent SDK overview (opens in a new tab)code.claude.com
  2. How the agent loop works (opens in a new tab)code.claude.com
  3. Agent SDK permissions and evaluation order (opens in a new tab)code.claude.com
  4. Agent SDK hooks (opens in a new tab)code.claude.com
  5. Agent SDK subagents (opens in a new tab)code.claude.com
  6. Agent SDK custom tools and MCP (opens in a new tab)code.claude.com
  7. Agent SDK sessions (opens in a new tab)code.claude.com
  8. Hosting the Agent SDK (opens in a new tab)code.claude.com
  9. Securely deploying AI agents (opens in a new tab)code.claude.com
  10. Agent SDK cost tracking (opens in a new tab)code.claude.com
  11. Agent SDK observability (opens in a new tab)code.claude.com
  12. Migrate from the Claude Code SDK (opens in a new tab)code.claude.com
  13. claude-agent-sdk-typescript repository (opens in a new tab)github.com