Section D: SDK Introduction

What is Agent SDK?

Claude Code as a programmable library - build progressively • Install setup ↗

Block 1
Client + Model
Block 2
+ Tools
Block 3
+ System Prompt
Block 4
+ MCP Servers
Block 5
+ Subagents
Block 6
Complete Agent
Block 1: Minimal Client + Model (Copy-Paste Ready!)
Start with the basics: import SDK, create client, run a query
from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions
import asyncio

async def main():
options = ClaudeAgentOptions(
model="claude-sonnet-4-5"
)

async with ClaudeSDKClient(options=options) as client:
await client.query("Hello!")
async for message in client.receive_response():
print(message)

asyncio.run(main())
Block 2: Add Allowed Tools
Enable built-in tools: Read, Write, Bash, WebSearch, etc.
options = ClaudeAgentOptions(
model="claude-sonnet-4-5",
allowed_tools=["Read", "Write", "Bash", "WebSearch"]
)

async with ClaudeSDKClient(options=options) as client:
await client.query("Search the web for Constructor Tech")
async for message in client.receive_response():
print(message)
Block 3: Add Custom System Prompt
Customize agent behavior with a system prompt
options = ClaudeAgentOptions(
model="claude-sonnet-4-5",
allowed_tools=["Read", "Write", "Bash", "WebSearch"],
system_prompt="You are a research assistant."
)
Block 4: Add MCP Servers
Connect external tools via Model Context Protocol
options = ClaudeAgentOptions(
model="claude-sonnet-4-5",
allowed_tools=["Read", "Write", "Bash", "WebSearch"],
system_prompt="You are a research assistant.",
mcp_servers={"browser": {
"command": "npx",
"args": ["@anthropic-ai/mcp-server-puppeteer"]
}}
)
Block 5: Add Subagents
Multi-agent orchestration with specialized subagents
options = ClaudeAgentOptions(
model="claude-sonnet-4-5",
allowed_tools=["Read", "Write", "Bash", "Task"],
system_prompt="Orchestrate research tasks.",
mcp_servers={"browser": {...}},
agents={
"scraper": {"description": "Scrapes sites"},
"researcher": {"description": "Finds news"}
}
)
Block 6: Complete Agent (Copy-Paste Ready!)
Full runnable script combining all features
from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions
import asyncio

async def main():
options = ClaudeAgentOptions(
model="claude-sonnet-4-5",
allowed_tools=["Read", "Write", "Bash", "Task"],
system_prompt="You are a research assistant.",
mcp_servers={"browser": {"command": "npx", "args": [...]}},
agents={"scraper": {...}, "researcher": {...}}
)

async with ClaudeSDKClient(options=options) as client:
await client.query("Research Constructor Tech")
async for message in client.receive_response():
print(message)

asyncio.run(main())