The Claude Agent SDK gives developers the same agent harness that powers Claude Code as a library for Python and TypeScript. It handles the agentic loop, tools, permissions, hooks, subagents and MCP so you can build custom agents for support, research, operations and more.
Why an SDK?
Claude Code is an agent for coding. But businesses want agents for support tickets, document processing, research, operations. The Claude Agent SDK exposes Claude Code's engine as a library, so you can build those agents without reinventing the hard parts.
- Python:
pip install claude-agent-sdk - TypeScript:
npm install @anthropic-ai/claude-agent-sdk
A tiny agent
import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions
async def main():
options = ClaudeAgentOptions(
system_prompt="You are a helpful operations analyst.",
allowed_tools=["Read", "Grep", "Glob"],
)
async for message in query(prompt="Summarise the CSVs in ./reports", options=options):
print(message)
asyncio.run(main())
Notice: you set the role, restrict tools to read-only, give a task and stream messages back. The SDK runs the loop of think โ call tool โ observe โ repeat for you.
How the agentic loop works
Under the hood (and if you call the Messages API yourself), each response has a stop_reason:
tool_useโ run the requested tool(s), append the results to the conversation, call Claude again.end_turnโ Claude is finished; return the answer.
stop_reason. (A cap is fine as a safety backstop.)Production essentials
- Tool descriptions are how the model picks tools, so make them specific, with inputs, examples and boundaries.
- Few, focused tools per agent: around 4โ5 scoped to the role beats 20 generic ones.
- Hooks for business rules that must never be broken (refund limits, approvals).
- Structured errors: tell the agent whether a failure is retryable, and distinguish "couldn't reach the system" from "searched and found nothing".
- Human handoff: when escalating, pass a self-contained summary (customer, issue, what was tried, recommended action).
Check yourself
Three quick questions to check it stuck.
Ask the tutor
Stuck on something in this lesson? Ask the AI tutor. It answers from this lesson and points you to others that help. Answers are written by AI and can be wrong. Do not include personal details, passwords or company secrets.
Frequently asked
What is the Claude Agent SDK?
The Claude Agent SDK is a Python and TypeScript library that exposes the agent harness behind Claude Code, including the agentic loop, tools, permissions, hooks, subagents and MCP, for building custom agents.
How does an agent know when it is finished?
In the Messages API the response stop_reason tells you: tool_use means run the tools and continue, end_turn means the model has finished. Do not rely on parsing text like "done".
What can I build with the Claude Agent SDK?
Customer support agents, research assistants, document processing pipelines, internal operations bots and coding agents, all with controllable tools and guardrails.