Claude Agent SDK Patterns
Skill4 starsChanged 10 months agoMITView on GitHub
Claude Agent SDK patterns for building AI agents with Claude Code capabilities. Use when creating agents, custom tools, hooks, or MCP integrations.
- Installs packages
In this repository12 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- delegation-templatesSkill
- deploymentSkill
- frontend-patternsSkill
- Google ADK PatternsSkill
- LLM Evaluation PatternsSkill
- LLM Observability PatternsSkill
- memory-workflowSkill
- orchestrator-workflowsSkill
- react-native-best-practicesSkill
- skill-creatorSkill
- testing-strategySkill
The file
Files
Claude Agent SDK Patterns - Quick Reference
The Claude Agent SDK (claude-agent-sdk) enables building AI agents with Claude Code's capabilities: file operations, code execution, tool use, and multi-turn conversations.
Package: pip install claude-agent-sdk
⚠️ SDK Disambiguation
| SDK | Package | Purpose | Use When |
| Claude Agent SDK | claude-agent-sdk | Build agents with Claude Code capabilities | You need file ops, code execution, tools, multi-turn |
| Anthropic SDK | anthropic | Direct Claude API access | Simple chat, messages API, basic tool calling |
This skill covers the Claude Agent SDK - for agentic applications that need Claude Code's full capabilities (file system access, bash execution, MCP tools).
If you need the Anthropic SDK (basic Claude API):
- Use Context7:
/anthropics/anthropic-sdk-python - See Anthropic docs: https://docs.anthropic.com/en/api/
Modules
- 01-sdk-basics.md - Core SDK Usage
- Installation and setup
query()vsClaudeSDKClient- Message handling and streaming
- ClaudeAgentOptions configuration
- 02-tool-calling.md - Custom Tools
- @tool decorator pattern
- MCP server creation
- Tool registration and naming
- Error handling in tools
- 03-hooks.md - Hooks & Permissions
- PreToolUse hooks (validation, blocking)
- PostToolUse hooks (review, context injection)
- Permission callbacks (can_use_tool)
- Stop/interrupt patterns
- 04-streaming-behavior.md - Streaming Details (validated 2026-01-01)
- StreamEvent vs AssistantMessage timing
- Real-time text delta streaming
- Tool use/result message sequence
- Interrupt behavior and client reuse
- Session persistence and resume
When to Use Claude Agent SDK
| Need | Use Agent SDK? | Why |
| Simple chat completion | No | Use anthropic SDK directly |
| File operations | Yes | Built-in Read/Write/Edit tools |
| Code execution | Yes | Bash tool with sandboxing |
| Multi-turn agents | Yes | Session management |
| Custom tools | Yes | @tool decorator + MCP |
| Tool validation | Yes | PreToolUse hooks |
Quick Lookup
Common Issues:
- Messages not appearing? -> Check
receive_response()iteration - Tool not found? -> Verify
allowed_toolsincludes MCP prefix - Hook not firing? -> Check
HookMatcherpattern - Session lost? -> Use
ClaudeSDKClientcontext manager
For current syntax, use Context7:
get-library-docs: /anthropics/claude-agent-sdk-python (topic: "query options")Two Usage Patterns
1. One-Shot Query (Stateless)
from claude_agent_sdk import query, AssistantMessage, TextBlock
async for message in query(prompt="What is 2 + 2?"):
if isinstance(message, AssistantMessage):
for block in message.content:
if isinstance(block, TextBlock):
print(block.text)2. Interactive Client (Stateful)
from claude_agent_sdk import ClaudeSDKClient
async with ClaudeSDKClient() as client:
await client.query("What's the capital of France?")
async for msg in client.receive_response():
# Handle response
pass
# Follow-up in same session
await client.query("What's its population?")
async for msg in client.receive_response():
passCore Concepts
Message Types
| Type | Contains | When |
AssistantMessage | Claude's response, tool uses | Every turn |
UserMessage | User prompts, tool results | With replay |
SystemMessage | Claude Code events | System events |
ResultMessage | Cost, usage, output | End of query |
StreamEvent | Partial updates | With include_partial_messages=True |
Content Blocks
| Block | Found In | Contains |
TextBlock | AssistantMessage | .text |
ToolUseBlock | AssistantMessage | .name, .input, .id |
ThinkingBlock | AssistantMessage | .thinking |
ToolResultBlock | UserMessage | .content, .tool_use_id |
Anti-Patterns
Forgetting async context manager
# Wrong - session may not clean up
client = ClaudeSDKClient()
await client.query(...)
# Right
async with ClaudeSDKClient() as client:
await client.query(...)Not iterating response
# Wrong - messages never consumed
await client.query("Hello")
# Right
await client.query("Hello")
async for msg in client.receive_response():
...MCP tool naming
# Wrong - missing mcp__ prefix
allowed_tools=["my_tool"]
# Right
allowed_tools=["mcp__server_name__tool_name"]Project Conventions
- Always use context manager for
ClaudeSDKClient - Set
max_turnsto prevent runaway agents - Set
max_budget_usdfor cost control - Use hooks for security-sensitive operations
- Name MCP servers descriptively (e.g.,
"calculator","database")
Resources
- GitHub: https://github.com/anthropics/claude-agent-sdk-python
- Claude Code Docs: https://docs.anthropic.com/en/docs/claude-code
Context7 for live docs:
/anthropics/claude-agent-sdk-python- SDK reference
Last Updated: 2026-01-01
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.

