claude-agent-sdk-python
Skill79 starsChanged 10 months agoApache-2.0View on GitHub
Build and test Python applications using the Claude Agent SDK. Use when creating agent applications, custom MCP tools, permission handlers, lifecycle hooks, or multi-turn conversations. Covers query(), ClaudeSDKClient, @tool decorator, create_sdk_mcp_server(), hooks, permissions, and independent app testing patterns.
- Installs packages
1 other project uses a version of this.
Adopted by
Most recently taken up by
- WalterSumbon/claude-agent-sdk-skilltheir own version, first seen Feb 14, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- WalterSumbon/claude-agent-sdk-skill★ 7same 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 repository31 other files it gives its agents.
- AGENTS.mdAGENTS.md
- backend/CLAUDE.mdCLAUDE.md
- CLAUDE.mdCLAUDE.md
- .claude/skills/oip-proposal/CLAUDE.mdCLAUDE.md
- docs/CLAUDE.mdCLAUDE.md
- frontend/CLAUDE.mdCLAUDE.md
- scripts/CLAUDE.mdCLAUDE.md
- .cursor/rules/development.mdcCursor rule
- browser-useSkill
- agent-browserSkill
- billing-automationSkill
- canvas-designSkill
- doc-expanderSkill
- docxSkill
- frontend-designSkill
- internal-commsSkill
- litho-budgeted-analysisSkill
- mcp-builderSkill
- oip-proposalSkill
- paypal-integrationSkill
- pci-complianceSkill
- pdfSkill
- pptxSkill
- skill-creatorSkill
- spec-driven-devSkill
- stripe-integrationSkill
- template-skillSkill
- theme-factorySkill
- webapp-testingSkill
- web-artifacts-builderSkill
- xlsxSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 178
Use scripts/run_agent_app.py to test agent applications in isolation:
- Very specific: line 188
Use scripts/create_agent_project.py to scaffold new agent projects:
- Distinctive: line 255
Test tools independently: before integrating with full agent
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Claude Agent SDK Python
Build production-ready agent applications with the Claude Agent SDK for Python.
Quick Reference
Installation
pip install claude-agent-sdkRequires Python 3.10+ and bundled Claude CLI v2.0.60+.
Core APIs
| API | Use Case |
query() | One-shot or simple multi-turn queries |
ClaudeSDKClient | Interactive streaming, bidirectional control |
@tool + create_sdk_mcp_server() | Custom in-process tools |
can_use_tool callback | Fine-grained permission control |
hooks | Lifecycle event interception |
Basic Patterns
One-Shot Query
import anyio
from claude_agent_sdk import query, AssistantMessage, TextBlock, ResultMessage
async def main():
async for msg in query(prompt="Explain Python decorators"):
if isinstance(msg, AssistantMessage):
for block in msg.content:
if isinstance(block, TextBlock):
print(block.text)
elif isinstance(msg, ResultMessage):
print(f"Cost: ${msg.total_cost_usd:.4f}")
anyio.run(main)Interactive Client
import anyio
from claude_agent_sdk import (
ClaudeSDKClient,
ClaudeAgentOptions,
AssistantMessage,
TextBlock,
ResultMessage,
)
async def main():
options = ClaudeAgentOptions(
system_prompt="You are a helpful assistant",
allowed_tools=["Read", "Write", "Bash"],
permission_mode="acceptEdits",
max_turns=10,
)
async with ClaudeSDKClient(options=options) as client:
await client.query("Analyze main.py")
async for msg in client.receive_response():
if isinstance(msg, AssistantMessage):
for block in msg.content:
if isinstance(block, TextBlock):
print(block.text)
elif isinstance(msg, ResultMessage):
print(f"Done. Cost: ${msg.total_cost_usd:.4f}")
anyio.run(main)Custom Tools
from typing import Any
from claude_agent_sdk import tool, create_sdk_mcp_server, ClaudeAgentOptions
@tool("add", "Add two numbers", {"a": float, "b": float})
async def add(args: dict[str, Any]) -> dict[str, Any]:
result = args["a"] + args["b"]
return {"content": [{"type": "text", "text": f"Result: {result}"}]}
@tool("divide", "Divide two numbers", {"a": float, "b": float})
async def divide(args: dict[str, Any]) -> dict[str, Any]:
if args["b"] == 0:
return {
"content": [{"type": "text", "text": "Error: Division by zero"}],
"is_error": True
}
return {"content": [{"type": "text", "text": f"Result: {args['a'] / args['b']}"}]}
calculator = create_sdk_mcp_server(
name="calculator",
version="1.0.0",
tools=[add, divide]
)
options = ClaudeAgentOptions(
mcp_servers={"calc": calculator},
allowed_tools=["mcp__calc__add", "mcp__calc__divide"],
permission_mode="acceptEdits",
)Configuration Reference
ClaudeAgentOptions
ClaudeAgentOptions(
# Model
model="claude-sonnet-4-20250514",
fallback_model=None,
# Prompts
system_prompt="You are a helpful assistant",
# Limits
max_turns=10,
max_budget_usd=1.00,
max_thinking_tokens=8000,
# Environment
cwd="/path/to/project",
env={"CUSTOM_VAR": "value"},
# Tools
tools=["Read", "Write", "Bash"], # or {"type": "preset", "preset": "claude_code"}
allowed_tools=["Read", "Write"], # whitelist
disallowed_tools=["WebFetch"], # blacklist
mcp_servers={"name": config}, # custom tools
# Permissions
permission_mode="acceptEdits", # default, acceptEdits, plan, bypassPermissions
can_use_tool=my_permission_callback,
# Sessions
continue_conversation=False,
resume="session-id",
fork_session=False,
# Hooks
hooks={"PreToolUse": [HookMatcher(matcher="*", hooks=[my_hook])]},
)Permission Modes
| Mode | Behavior |
"default" | Prompt for all actions |
"acceptEdits" | Auto-approve file edits |
"plan" | Planning mode (no execution) |
"bypassPermissions" | Auto-approve all (use with caution) |
Advanced Patterns
For detailed examples of:
- Permission callbacks: See references/permissions.md
- Lifecycle hooks: See references/hooks.md
- Testing patterns: See references/testing.md
- Production patterns: See references/production.md
Independent App Testing
Use scripts/run_agent_app.py to test agent applications in isolation:
# Run a simple agent app
python scripts/run_agent_app.py path/to/app.py
# Run with custom options
python scripts/run_agent_app.py path/to/app.py --cwd /project --max-turns 5Use scripts/create_agent_project.py to scaffold new agent projects:
# Create a basic agent project
python scripts/create_agent_project.py my_agent --template basic
# Create with custom tools
python scripts/create_agent_project.py my_agent --template custom-tools
# Create interactive chat app
python scripts/create_agent_project.py my_agent --template chatMessage Types
| Type | Description |
AssistantMessage | Claude's responses (text, thinking, tool use) |
UserMessage | User input or tool results |
ResultMessage | Session summary (always last) |
SystemMessage | System events |
Content Blocks
from claude_agent_sdk import TextBlock, ThinkingBlock, ToolUseBlock, ToolResultBlock
# In AssistantMessage.content
for block in msg.content:
if isinstance(block, TextBlock):
print(block.text)
elif isinstance(block, ThinkingBlock):
print(f"Thinking: {block.thinking}")
elif isinstance(block, ToolUseBlock):
print(f"Tool: {block.name}, Input: {block.input}")Error Handling
from claude_agent_sdk import (
ClaudeSDKError,
CLINotFoundError,
ProcessError,
SDKJSONDecodeError,
)
try:
async with ClaudeSDKClient(options) as client:
await client.query("Do something")
async for msg in client.receive_response():
process(msg)
except CLINotFoundError:
print("Claude CLI not found. Install from https://claude.ai")
except ProcessError as e:
print(f"CLI failed with exit code: {e.exit_code}")
except SDKJSONDecodeError:
print("Failed to parse CLI response")Best Practices
- Always use context managers for
ClaudeSDKClientto ensure cleanup - Set
max_budget_usdto prevent runaway costs - Use
permission_mode="acceptEdits"for file operations without prompts - Return
is_error: Truefrom custom tools on failure - Use structured outputs for predictable JSON responses
- Test tools independently before integrating with full agent
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.

