Agent SDK
The Claude Agent SDK exposes Claude Code’s full agentic engine as a programmable library. Available in Python (claude-agent-sdk) and TypeScript (@anthropic-ai/claude-agent-sdk).
What It Is
The same infrastructure that powers Claude Code, but programmable. Instead of interacting through a terminal, you build applications that use Claude’s agent loop: reading files, running commands, editing code, searching the web - all orchestrated by the model.
Two Levels
There’s an important distinction between the Client SDK and the Agent SDK:
Client SDK (anthropic) | Agent SDK (claude-agent-sdk) | |
|---|---|---|
| You manage | The tool loop, tool execution, message history | Just the prompt and configuration |
| Claude manages | Nothing - you call the API | The full agent loop with built-in tools |
| Best for | Custom tool implementations, fine-grained control | Applications that need Claude Code’s capabilities |
The Agent Loop
Same loop as Claude Code:
prompt → Claude thinks → tool call → execute → observe → repeat → final response
The SDK handles all of this. You provide a prompt, and query() streams back messages as Claude works through the task.
Core API
Python
from claude_agent_sdk import query
async for message in query(
prompt="Find all TODO comments in this project and create a summary",
options={
"model": "claude-sonnet-4-6",
"permissionMode": "default",
}
):
if message.type == "text":
print(message.text, end="")
elif message.type == "tool_use":
print(f"Using tool: {message.tool}")
TypeScript
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Find all TODO comments in this project and create a summary",
options: {
model: "claude-sonnet-4-6",
permissionMode: "default",
},
})) {
if (message.type === "text") {
process.stdout.write(message.text);
}
}
Built-in Tools
The Agent SDK comes with the same tools as Claude Code:
| Tool | Purpose |
|---|---|
| Read | Read file contents |
| Edit | Targeted file edits |
| Write | Create new files |
| Bash | Run shell commands |
| Glob | Find files by pattern |
| Grep | Search file contents |
| Agent | Spawn subagents |
| WebSearch | Search the web |
| WebFetch | Fetch web pages |
Custom Tools via MCP
Add custom tools by creating in-process MCP servers:
from claude_agent_sdk import query, create_sdk_mcp_server
server = create_sdk_mcp_server("my-tools")
@server.tool
def lookup_user(user_id: str) -> str:
"""Look up a user by their ID."""
return db.get_user(user_id).to_json()
async for msg in query(
prompt="Look up user 123 and summarize their recent activity",
options={"mcpServers": [server]},
):
print(msg)
Subagents
Define specialized agents with isolated context, restricted tools, and model overrides:
from claude_agent_sdk import query, AgentDefinition
security_agent = AgentDefinition(
name="security-reviewer",
instructions="Review code for security vulnerabilities. Focus on injection, auth bypass, and data leaks.",
model="claude-opus-4-6",
tools=["Read", "Grep", "Glob"],
)
async for msg in query(
prompt="Review the auth module for security issues",
options={"agents": [security_agent]},
):
print(msg)
Non-Coding Uses
The Agent SDK isn’t limited to coding tasks:
- Deep research - search the web, read pages, synthesize findings
- Content creation - generate documents from templates and data
- Data processing - read files, transform data, write outputs
- Note-taking - capture and organize information from multiple sources
Key Features
| Feature | Description |
|---|---|
| Hooks | PreToolUse, PostToolUse, Stop, SessionStart, SessionEnd lifecycle events |
| Sessions | Capture session_id, resume with full context |
| Extended thinking | Internal reasoning blocks for complex problems |
| Computer use | GUI automation via screenshots + mouse/keyboard |
| MCP native | stdio, HTTP, SSE transports with wildcard permissions |
| Providers | Anthropic, Bedrock, Vertex AI, Azure AI Foundry |
Next
- Agent SDK quickstart - build a file-processing agent
- Claude Code features - the features that the SDK exposes programmatically