Tutorials · Claude Agent SDK · examples / pythonExample code
Example code
Claude Agent SDK examples: python
Open a file to read it highlighted, with links to where the tutorial uses it.
Claude Agent SDKContents
| Name | What it shows | Used in | Size |
|---|---|---|---|
| 01_hello.py | The simplest possible agent. One query(), read-only tools, print every message. | P1 §4 | 526 B · raw |
| 02_fix_bugs.py | The quickstart agent. Finds and fixes crash bugs in src/utils.py with acceptEdits. | P1 §5 | 1.4 KB · raw |
| 03_multi_turn.py | Multi-turn conversation with ClaudeSDKClient. The second turn relies on the first. | P1 | 1.2 KB · raw |
| 04_custom_tools.py | Custom tools via an in-process MCP server. | P2 | 3.4 KB · raw |
| 05_hooks.py | Hooks. PreToolUse blocks writes to .env files; PostToolUse logs every tool result. | P2 | 2.3 KB · raw |
| 06_structured_output.py | Structured output validated against a Pydantic schema. | P1 | 1.6 KB · raw |
| 07_streaming.py | Token-level streaming with include_partial_messages. | P1 | 1.6 KB · raw |
| 08_subagents.py | Programmatic subagents with AgentDefinition, plus SubagentStart/Stop hooks. | P2 §4 | 2.5 KB · raw |
| 09_sessions.py | Sessions: capture an ID, resume it, fork it, and inspect it on disk. | P2 §5 | 2.0 KB · raw |
| 10_permissions.py | Runtime approval with can_use_tool. Allows file creation, denies deletion, rewrites paths. | P2 | 2.6 KB · raw |
| 11_checkpointing.py | File checkpointing: let the agent edit a file, then rewind it. | P3 §1 | 1.8 KB · raw |
| 12_interrupt.py | Interrupt a running turn, drain it, then send a new one. | P1 | 1.5 KB · raw |
| 13_introspection.py | Introspection: init metadata, context usage, MCP status, server info, dynamic model switch. | P3 §3 | 2.4 KB · raw |
| 14_errors.py | Error handling: max_turns and max_budget_usd limits, ResultError, bad CLI path. | P1 | 1.5 KB · raw |
| 15_skills_and_prompts.py | Skills (dispatch /security-check), CLAUDE.md loading, and system prompt variants. | P2 §6 | 2.9 KB · raw |
| 16_cost_tracking.py | Cost and usage: per-step usage deduplicated by message_id, result totals, cache tokens. | P3 §2 | 2.0 KB · raw |
| 17_streaming_input.py | Streaming input mode via an async generator, plus /compact and ConversationReset via /clear. | P1 | 1.7 KB · raw |
| 18_otel_capture.py | Observability. Run a tiny OTLP/HTTP receiver on localhost, point the agent at it, | P3 §4 | 4.6 KB · raw |
| 19_sqlite_session_store.py | A custom SessionStore backed by SQLite, validated with the SDK's conformance suite, | P3 §5.1 | 6.3 KB · raw |
| 20_multi_tenant.py | Multi-tenant isolation. Two tenants in one process: separate cwd, separate config dir, no shared settings or memory. | P3 §5.2 | 4.0 KB · raw |
| 21_sandbox_and_stderr.py | Sandbox settings on a host WITHOUT bubblewrap: failIfUnavailable=True stops the run with a clear | P3 §6 | 1.6 KB · raw |
| 22_concurrency_sizing.py | Capacity planning. Run N sessions concurrently and measure the peak memory of the claude subprocesses. | P3 §5.3 | 2.4 KB · raw |
| demo_util.py | Shared helpers for the demo scripts: a compact message printer. | P1 §4 | 2.7 KB · raw |
| p1_goal_loop.py | A goal-based loop. A Stop hook refuses to let the agent stop until a | P4 §4.4 | 2.7 KB · raw |
| p1b_goal_loop_blocked.py | Same loop, but the goal has a criterion the prompt never mentioned, so the first stop is blocked. A Stop hook refuses to let the agent stop until a | P4 §4.4 | 3.2 KB · raw |
| p2_graph_pipeline.py | A code-owned graph. Your program is the orchestrator; each node is a query(). | P4 §7.3 | 4.2 KB · raw |
| p3_workflow_patterns.py | The classic workflow patterns as code: routing, prompt chaining with a gate, and voting. | P4 §3 | 3.6 KB · raw |
| p4_handoff.py | Handoffs. Control AND the conversation move between specialists; a router in code decides who's next. | P4 §7.6 | 3.1 KB · raw |
| p5_debate.py | A supervised swarm. Peers investigate competing hypotheses, share a blackboard, and a judge decides. | P4 §7.6 | 4.0 KB · raw |
| p6_agentic_rag.py | Agentic RAG. Retrieval is a tool the agent calls as often as it likes; the answer must cite sources. | P4 §6.2 | 4.4 KB · raw |
| p7_deferred_approval.py | Deferred approval. A PreToolUse hook pauses the run at a risky command; the process ends with | P4 §10 | 3.6 KB · raw |
| p8_eval_gate.py | Harness-as-code. An end-state eval that scores a CLAUDE.md change before you ship it. | P4 §5 | 1.9 KB · raw |
| p9_circuit_breaker.py | A circuit breaker on a goal loop. Part of the verifier's condition depends on something | P4 §4.5 | 2.9 KB · raw |