claude-agent-sdk
Skill2 starsChanged 5 months agoMITView on GitHub
Expert implementation guidance for the Claude Agent SDK (Python). Use when users are building with the Agent SDK — creating custom tools, managing sessions, controlling permissions, streaming responses, integrating MCP servers, adding hooks, defining subagents, or deploying SDK-based agents. Also use for SDK architecture questions (query() vs ClaudeSDKClient, multi-turn vs one-shot, structured outputs, session persistence, cost tracking). Provides implementation patterns, decision guides, and pointers to working code in the SDK source — not just doc links.
6 other projects use a version of this.
Adopted by
Most recently taken up by
- liangdabiao/claudesdk-skilltheir own version, first seen May 23, 2026
- tdimino/claude-code-minoantheir own version, first seen Apr 16, 2026
- fcsouza/agent-skillstheir own version, first seen Mar 7, 2026
- JasonPaff/clarifytheir own version, first seen Feb 4, 2026
- JohnJBoren/claude-skills-ecosystemtheir own version, first seen Dec 26, 2025
- jahody/skillstheir own version, first seen Dec 19, 2025
Other versions6 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 6
- tdimino/claude-code-minoan★ 40same name
- liangdabiao/claudesdk-skill★ 35same name
- fcsouza/agent-skills★ 20same name
- JohnJBoren/claude-skills-ecosystem★ 2same name
- jahody/skills★ 0same name
- JasonPaff/clarify★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository11 other files it gives its agents.
- .claude/CLAUDE.mdCLAUDE.md
- agent-harnessSkill
- claude-codeSkill
- grill-with-docsSkill
- improve-codebase-architectureSkill
- learnSkill
- plugin-browserSkill
- prompt-engineeringSkill
- py-debugSkill
- tddSkill
- ts-debugSkill
The file
We split this skill into the rules it gives an agent
12 rules. 0 are common, 2 are distinctive, and 10 are very specific to this project.
0
Common
Most of the 5 edited versions we read keep these.
2
Distinctive
This author's own choices. Worth a look.
10
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 16
Use this skill when:
- Distinctive: line 41
Read specific example files: when you find a relevant match — these are the source of truth.
- Very specific: line 63
To verify current fields: search for ClaudeAgentOptions in the SDK source.
- Very specific: line 69
To see the exact types and fields: search the SDK source for these type names, or read examples/quick_start.py and …
- Very specific: line 85
Look up: read examples/quick_start.py for basic, options, and tools usage.
- Very specific: line 97
Look up: read examples/streaming_mode.py for multi-turn, concurrent, interrupt, and error handling patterns.
- Very specific: line 109
Look up: read examples/include_partial_messages.py.
- Very specific: line 122
Look up: read examples/mcp_calculator.py for a complete custom tools example.
- Very specific: line 159
Look up: read examples/hooks.py for PreToolUse, PostToolUse, permission decisions, and stop control.
- Very specific: line 171
Look up: read examples/agents.py for single and multi-agent patterns.
- Very specific: line 202
query(): if: Single task, no follow-up, simple pipeline - Very specific: line 203
ClaudeSDKClient: if: Multi-turn, conversation history, streaming UI, interrupts, session management
“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Claude Agent SDK
When to Use This Skill
Use this skill when:
- You're building with the Agent SDK in Python (one-off queries, persistent agents, multi-turn conversations)
- You need to decide between API patterns (e.g.,
query()vsClaudeSDKClient, streaming vs buffered) - You're implementing features like custom tools, session management, permissions, or hooks
- You're architecting agent applications (error handling, permissions, cost control, session branching)
- You want working code examples from real SDK code
- You need to troubleshoot Agent SDK issues or understand API behavior
How to Answer SDK Questions
The SDK evolves frequently. The patterns below capture stable concepts, but field names and exact APIs may have moved. Verify concrete API details against the live source before quoting them:
- Refresh the doc index when fetching docs. If you need to consult the official docs to answer (i.e. you'll hit step 1 below), run
bash ~/.claude/skills/claude-agent-sdk/scripts/fetch-agent-sdk-urls.shfirst to rewritereferences/agent-sdk-urls.mdfrom Anthropic's currentllms.txt. Skip the refresh for purely conceptual answers or when the existing snapshot is sufficient. - Check official docs for conceptual explanations: fetch from the URLs listed in agent-sdk-urls.md.
- Search the indexed repo for the feature in question:
mcp__claude-context__search_code( path="~/.claude/skills-references/claude-agent-sdk/claude-agent-sdk-python", query="<feature name or concept>" ) - Browse examples for working usage patterns:
Glob(~/.claude/skills-references/claude-agent-sdk/claude-agent-sdk-python/examples/*.py) - Read specific example files when you find a relevant match — these are the source of truth.
Core Concepts
These concepts are architecturally stable — the names and roles don't change, even if exact field names evolve.
ClaudeAgentOptions — The Central Configuration Object
Almost everything in the SDK is configured through ClaudeAgentOptions. Commonly used fields (verify exact names against the source before quoting in answers):
system_prompt— str or preset dict for customizing Claude's behaviorallowed_tools— tool whitelist (e.g.,["Read", "Write", "Bash"])max_turns— limit agent turnsmax_budget_usd— cost cappermission_mode—"default"|"acceptEdits"|"bypassPermissions"model— model overridecan_use_tool— permission callback for dynamic allow/denyhooks— lifecycle hooks dict (PreToolUse,PostToolUse, etc.)mcp_servers— MCP server configuration dictagents— subagent definitions dictcwd— working directory
To verify current fields: search for ClaudeAgentOptions in the SDK source.
Message Types — Processing SDK Responses
The SDK yields typed messages: AssistantMessage, ResultMessage, UserMessage, SystemMessage. Content blocks include TextBlock, ToolUseBlock, ToolResultBlock.
To see the exact types and fields: search the SDK source for these type names, or read examples/quick_start.py and examples/streaming_mode.py for handling patterns.
Implementation Patterns
Each pattern below describes when and why to use an approach. For exact, up-to-date code, follow the "look up" pointer to the corresponding SDK example file.
Pattern 1: One-Off Task — query()
When: Single task, no follow-up needed, fire-and-forget Why: Simplest API — async generator that yields messages, no session management
Key shape:
async for message in query(prompt="...", options=ClaudeAgentOptions(...)):- Yields
AssistantMessage(withTextBlockcontent) andResultMessage(with cost/status) - Without options, Claude has no tools and uses an empty system prompt
Look up: read examples/quick_start.py for basic, options, and tools usage.
Pattern 2: Multi-Turn Conversation — ClaudeSDKClient
When: Multi-turn workflows, context preservation, interactive sessions, interrupts Why: Maintains conversation history, supports streaming, session management
Key shape:
async with ClaudeSDKClient(options=...) as client:- Two-step per turn:
await client.query("...")thenasync for msg in client.receive_response(): - Context is preserved across turns automatically
Look up: read examples/streaming_mode.py for multi-turn, concurrent, interrupt, and error handling patterns.
Pattern 3: Streaming Partial Messages (Real-Time UI)
When: Chat UI, progress display, long-running tasks Why: See tokens and tool calls as they happen, build real-time experiences
Key shape:
- Set
include_partial_messages=TrueinClaudeAgentOptions StreamEventmessages arrive interspersed with regular messages- Known limitations with extended thinking and structured output
Look up: read examples/include_partial_messages.py.
Pattern 4: Custom Tools via MCP (Extend Claude)
When: Claude needs domain-specific capabilities (APIs, business logic)
Why: Type-safe in-process MCP tools with the @tool decorator
Key shape:
@tool(name, description, schema)decorator defines individual toolscreate_sdk_mcp_server(name, version, tools=[...])bundles them into a server- Register via
ClaudeAgentOptions(mcp_servers={"name": server}) - Tool names follow
mcp__<server>__<tool>pattern forallowed_tools
Look up: read examples/mcp_calculator.py for a complete custom tools example.
Pattern 5: Permission Controls (Security)
When: Production agents, untrusted input, approval workflows Why: Prevent unintended tool use with typed allow/deny responses
Key shape:
ClaudeAgentOptions(can_use_tool=callback)with typed returns (PermissionResultAllow/PermissionResultDeny)- Callback signature:
(tool_name, input_data, context) -> Allow | Deny PermissionResultAllow(updated_input=...)can modify tool inputsPermissionResultDeny(message="...")blocks with explanation
Options (most restrictive to most permissive):
allowed_tools=["Read", "Grep"]— Whitelist specific tools onlycan_use_tool=callback— Dynamic allow/deny/modify per tool callpermission_mode="acceptEdits"— Auto-accept file editspermission_mode="bypassPermissions"— Allow everything (dev only)
Look up: read examples/tool_permission_callback.py.
Pattern 6: Hooks (Lifecycle Interception)
When: Logging, validation, security gates, error recovery Why: Intercept agent behavior at key lifecycle points without changing the task prompt
Key shape:
ClaudeAgentOptions(hooks={"PreToolUse": [HookMatcher(matcher="Bash", hooks=[callback])]})- Callback signature:
(input_data: HookInput, tool_use_id, context: HookContext) -> HookJSONOutput - Return
hookSpecificOutputwithpermissionDecision: "allow"/"deny"to control execution
Available hook events:
PreToolUse— Before Claude calls a tool (can allow/deny/modify)PostToolUse— After tool execution (can add context, stop execution)UserPromptSubmit/SessionStart/SessionEnd— Session lifecycleStop— Before agent stops (can add context to continue)
Look up: read examples/hooks.py for PreToolUse, PostToolUse, permission decisions, and stop control.
Pattern 7: Subagents (Task Delegation)
When: Parallel tasks, isolated contexts, specialized domain agents Why: Delegate to focused agents with their own tools, prompts, and models
Key shape:
- Define via
AgentDefinition(description, prompt, tools, model) - Register in
ClaudeAgentOptions(agents={"name": definition}) - Invoke by asking the main agent to "use the
<name>agent to..."
Look up: read examples/agents.py for single and multi-agent patterns.
Pattern 8: Cost Control
When: Production budgets, dev experimentation limits Why: Hard-stop agent execution when cost exceeds threshold
Key shape:
ClaudeAgentOptions(max_budget_usd=0.10)ResultMessage.subtype == "error_max_budget_usd"when exceeded- Cost checked after each API call, so final cost may slightly exceed budget
Look up: read examples/max_budget_usd.py.
Pattern 9: System Prompt Customization
When: Custom personas, project-specific instructions, domain assistants Why: Control Claude's behavior and knowledge base
Four methods:
- String — Full replacement:
system_prompt="You are a pirate." - Preset — Use built-in:
system_prompt={"type": "preset", "preset": "claude_code"} - Preset + append — Extend built-in:
system_prompt={"type": "preset", "preset": "claude_code", "append": "Extra instructions."} - Empty (default) — No system prompt, vanilla Claude
Look up: read examples/system_prompt.py.
Decision Guide
Q: Should I use query() or ClaudeSDKClient?
query()if: Single task, no follow-up, simple pipelineClaudeSDKClientif: Multi-turn, conversation history, streaming UI, interrupts, session management
Q: How do I handle approvals and permissions?
- Whitelist (
allowed_tools) — Fast, predictable, best for production - Permission callback (
can_use_tool) — Dynamic logic, can modify inputs - Hooks (
PreToolUse) — Lifecycle-level control, chainable - Permission mode — Global setting for dev vs production
- Combination — Whitelist + callback for defense in depth
Learning Resources
- Official docs: agent-sdk-urls.md lists every Agent SDK (Python) page on code.claude.com with one-line descriptions;
WebFetchany URL for the live content. Regenerate the list viascripts/fetch-agent-sdk-urls.shwhen you need the latest set of pages. - Semantic search — Use
mcp__claude-context__search_codeon the indexed repo for any concept - Browse examples:
Glob(~/.claude/skills-references/claude-agent-sdk/claude-agent-sdk-python/examples/*.py)
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

