Agent SDK Anatomy

The core building blocks that turn a prompt into an autonomous, tool‑using loop — and how Python and TypeScript expose them.

Claude Agent SDK Python & TypeScript 13-slide anatomy

Claude Docs: Agent SDK overview.

Documentation reviewed: .

Module01 — Core model

The Agent Loop

An agent query uses the same loop as Claude Code: read context, decide, act, observe — repeat until Claude finishes or a limit stops the run.

Context in instructions + conversation tool definitions + results Claude responds AssistantMessage Tool executes built‑in / MCP / custom Result feeds back ToolResultBlock inside UserMessage
init reports session setup. ResultMessage reports how the run ended, with cost and usage. These are events your application receives.

Claude Docs: Agent loop and message types. Diagram uses Python message type names.

Module02 — Interception

Hooks: Pre / PostToolUse

Hooks sit inside the loop, not beside it — intercepting a call before it runs, or rewriting its result after, without touching the tool's own code.

Claude picks a tool PreToolUse allow · deny · ask defer · edit input Tool runs PostToolUse updatedToolOutput additionalContext Result to Claude
One field now covers both tool families: updatedToolOutput replaces output for any tool — MCP or built‑in. The older updatedMCPToolOutput is deprecated.

Claude Docs: Hook decisions and output fields.

Module03 — Packaged know-how

Skills

A skill packages instructions for a kind of task. Claude loads them when it invokes the skill; you can also request a skill by name.

Discovered

Listed, not loaded

Claude sees short descriptions of the skills available to this session. Full instructions load when a skill is invoked.

Loaded on demand

Pulled in mid‑task

When a request matches, Claude invokes the Skill tool and loads the relevant instructions into the active context — e.g. a docx skill only when a Word file is due.

Composable

Stack when needed

Several skills can combine in one task — a research skill to gather facts, then a document skill to format the result.

Skills must be discovered through your configured settings sources and enabled for the session. The skills option controls which ones Claude may invoke.

Claude Docs: Configure and invoke skills.

Module04 — Control

Permissions

Read these checks in order. A call can be approved, blocked, or sent for a decision before it reaches the last step.

  1. 1Hooks

    Can block, defer or edit a call. A hook’s approval still goes through deny and ask rules.

  2. 2Deny rules

    A matching deny rule blocks the call. The approval callback is skipped.

  3. 3Ask rules

    A match goes to the callback for a decision, or is denied in dontAsk mode.

  4. 4Permission mode

    May approve the call or apply extra checks. For example, acceptEdits approves eligible file edits.

  5. 5Allow rules

    An eligible match approves the call. Calls needing no approval also finish here.

  6. 6Your callback

    canUseTool / can_use_tool decides remaining calls. In dontAsk, they are denied instead.

Modes: default · acceptEdits · plan · auto · dontAsk · bypassPermissions.
Already-approved calls skip your callback. Use a PreToolUse hook for checks that must see every call.

Claude Docs: Permission order and exceptions. Some protected actions always need extra checks.

Module05 — Extensibility

Custom Tools

Wrap your own function so Claude can call it like any built‑in — no separate process to run or manage.

Your function Python / TS handler @tool / tool() name + description + schema Bundle the tools create_sdk_mcp_server() createSdkMcpServer() Add to query options Python: mcp_servers TypeScript: mcpServers

Claude Docs: Custom tools in Python and TypeScript. Tool handlers run inside your application process.

Module06 — Delegation

Subagents

A subagent works in its own context. The parent receives its returned result rather than the full working transcript, while the application can optionally observe intermediate activity.

Parent agent main context Subagent A own tools + prompt Subagent B own tools + prompt Subagent C own tools + prompt result returns to parent — working context stays isolated

Claude Docs: Subagent context and returned reports.

Module07 — Continuity

Sessions

A session_id identifies a conversation. Its history is saved locally by default, so a later run can pick up where it left off. You can also mirror it to external storage.

turn 1 session_id saved Resume continue latest or resume by ID Fork new session ID inherited history
A fork copies the conversation history. It does not copy your files: edits in the same working directory are visible to both sessions.

Claude Docs: Continue, resume and fork sessions.

Module08 — Connections

External MCP Servers

Connect local or remote MCP servers through stdio, SSE, or Streamable HTTP. Tool Search loads tool definitions when needed and is on by default where supported.

Your SDK agent find tools on demand stdio server local process SSE server legacy streaming transport Streamable HTTP current remote transport

Claude Docs: MCP transports · Tool Search defaults and supported configurations.

Module09 — Limits

Runtime Limits & Effort

Limit tool-use turns, set an estimated spending threshold, and choose how much reasoning Claude applies to each response.

maxTurns

Tool-use turn limit

Stops with error_max_turns. Keep earlier streamed messages: this error has no final result answer.

maxBudgetUsd

Budget threshold

Stops when estimated spend reaches the threshold, checked after each response. That response and running subagents can push the total higher.

effort

Reasoning depth

low to max — trades speed and token cost for reasoning depth. Available levels depend on the model.

For example, a $1 threshold can finish above $1. Leave headroom, and use billing records for the actual charge.

Claude Docs: Limits and effort · Cost estimates. Option names here are TypeScript.

Module10 — Memory

Context Compaction

When context fills up, the runtime summarizes older conversation history and keeps recent exchanges so Claude can continue. Some older details can be lost.

Older turns full detail, growing Summary key points from history Continue with summary + recent exchanges
PreCompact runs before compaction; compact_boundary marks it afterward. Put lasting project rules in CLAUDE.md, loaded through your settings sources.

Claude Docs: Compaction and persistent instructions.

Module11 — Two SDKs

Python vs TypeScript

Broadly the same agent runtime, exposed through different language idioms — with occasional differences in API shape and release timing.

Python
Session trackingClaudeSDKClient auto‑tracks across calls
Top-level optionssnake_case, e.g. max_turns
Custom tool@tool decorator
TypeScript
Session trackingquery() with continuation or resume options
Top-level optionscamelCase, e.g. maxTurns
Custom tooltool() helper
Python has exceptions: hook fields and AgentDefinition use names such as updatedInput and maxTurns. Check the type you are configuring.

Claude Docs: Python reference · TypeScript reference.

Module12 — Synthesis

The Full Picture

Every module exists to shape one thing: what happens inside the loop.

Agent Loop Hooks intercept Skills know‑how Permissions gate Custom tools extend Subagents delegate Sessions persist MCP servers connect Runtime control limits · memory

The pieces change what the loop can do — the loop is still the thing.

Claude Docs: Official Agent SDK overview. Each slide links to the details behind its explanation.

01 / 13
background
use ← → or click the edges to navigate