Building Your Own Agents with the Claude Agent SDK
The Claude Agent SDK gives you Claude Code's agent loop as a library โ for your own programs, not just the ready-made CLI.
What the SDK is
The Claude Agent SDK is a library for Python and TypeScript. It gives you the same agent loop, the same built-in tools, and the same context management that power Claude Code โ but programmable inside your own code instead of the terminal.
The agent loop comes for free
Normally, working with an LLM means building a loop yourself: send a request, check whether the model wants to use a tool, run the tool, send the result back, repeat. The SDK handles this for you: you call query() with a prompt, and the response arrives as a stream of messages โ reasoning, tool calls, tool results โ until the task is done.
Defining tools
Built-in tools like Read, Write, Edit, Bash, Glob, Grep, and WebSearch work immediately, with nothing to implement yourself. allowed_tools sets what the agent may use; permission_mode controls how much approval it needs. You can also connect custom MCP servers whenever the agent needs to reach an external system.
SDK or the ready-made CLI?
For interactive development and one-off tasks, the Claude Code CLI is enough. The SDK is built for CI/CD pipelines, custom applications, and production automation โ the same capabilities, embedded in your own program instead of the terminal.
EXAMPLE
In your own code: `query(prompt="Read README.md and summarize the three most important points", options=ClaudeAgentOptions(allowed_tools=["Read"]))` โ the agent reads the file itself and replies as a stream of messages.
๐ ๏ธ EXERCISE โ TRY IT YOURSELF
Use the Claude Agent SDK (Python or TypeScript) to build a small agent that reads a file with an intentional bug and fixes it.
- Install the SDK (pip install claude-agent-sdk or npm install @anthropic-ai/claude-agent-sdk) and set your ANTHROPIC_API_KEY.
- Write a small file with an obvious bug (e.g. division by zero) and call query() with allowed_tools=["Read","Edit"].
- Follow the message stream and watch the agent read, reason, and edit the file โ without you having written the tool loop yourself.
โ SELF-CHECK
- โ Did you write a tool-call loop yourself anywhere โ or did the SDK handle it?
- โ What happens if you set permission_mode to "plan" instead of "acceptEdits"?
- โ When would you use the Claude Code CLI instead of the SDK for the same task?
QUICK QUIZ
What does the Claude Agent SDK handle automatically that you'd otherwise have to build yourself against the raw API?
SOURCES
- Claude Agent SDK: Overview โ code.claude.com
- Claude Agent SDK: Quickstart โ code.claude.com
- GitHub: claude-agent-sdk-python โ github.com