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
Module01 — Core model

The Agent Loop

Every SDK call runs the same cycle Claude Code runs internally: read context, decide, act, observe — repeat until the task is done or a limit stops it.

Context in SystemMessage · init Claude responds AssistantMessage Tool executes built‑in / MCP / custom Result feeds back ToolResultBlock inside UserMessage ends as ResultMessage — status + cost + usage
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.
Module03 — Packaged know-how

Skills

A skill is a folder of instructions for one kind of task — loaded only when it's relevant, instead of sitting in context the whole session.

Discovered

Listed, not loaded

Claude sees a name and one‑line description for every available skill up front — the full instructions stay out of context until needed.

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.

Module04 — Control

Permissions

Tool calls pass through hooks, rules and the active permission mode. Your callback handles calls that still need an application-level decision.

Tool call e.g. Bash(...) PreToolUse may block or rewrite deny / ask rules patterns + settings permissionMode default · plan · auto acceptEdits · dontAsk bypassPermissions canUse Tool() Run Stop
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 createSdkMcpServer() bundles tools together In the loop via mcpServers option
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
Module07 — Continuity

Sessions

Each run has a session_id. Sessions are stored locally by default and can be continued, resumed, forked, or mirrored to external storage.

turn 1 session_id saved Resume continue latest or resume by ID Fork new session ID inherited history
Module08 — Connections

External MCP Servers

Connect local or remote MCP servers through stdio, SSE, or Streamable HTTP. For large catalogs, Tool Search can defer detailed schemas until Claude needs them.

Your SDK agent optional Tool Search stdio server local process SSE server legacy streaming transport Streamable HTTP current remote transport
Module09 — Limits

Runtime Limits & Effort

Two hard limits bound an open-ended run; a third control trades speed and cost for reasoning depth.

maxTurns

Round‑trip cap

Caps tool‑use round trips. Hit it and the run stops with error_max_turns plus partial output.

maxBudgetUsd

Spend cap

Stops the query when the SDK’s cumulative client-side cost estimate reaches the threshold.

effort

Reasoning depth

low to max — trades speed and cost for reasoning depth, per turn.

Module10 — Memory

Context Compaction

As a conversation approaches its context limit, the runtime compacts earlier context into a summary while retaining the recent working state needed to continue.

Older turns full detail, growing Compaction observable via PreCompact Recent turns kept in full, untouched
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
Namingsnake_case options
Custom tool@tool decorator
TypeScript
Session trackingquery() with continuation or resume options
NamingcamelCase options
Custom tooltool() helper
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.

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