Building Your Own Agent with the Agent SDK
On this page10 items
In 10 seconds
You can use the engine inside Claude Code as a library from your own program.
You don’t have to write the tool round-trips yourself, and permissions, subagents and hooks all work as they are.
In No.52 we looked at the tool-use round-trip. If writing that round-trip yourself feels like a chore, that’s exactly where the Agent SDK comes in.
How is it different?#
The official documentation lays this out well, so here it is as is.
| What you want to do | What to use |
|---|---|
| Build an agent without writing the tool round-trips yourself | Agent SDK |
| Work interactively in the terminal | Claude Code CLI (Chapter 5) |
| Call the API directly and write the round-trips yourself | Client SDK (No.51, No.52) |
| Run long-running agents without managing the environment | Managed Agents |
The Agent SDK runs the same machinery as Claude Code inside your own process. Think of it as Claude Code without the terminal UI.
What carries over as is#
Almost everything covered in Chapter 5 works unchanged.
- Built-in tools (reading, writing, running commands, web search)
- Hooks (No.41)
- Subagents (No.23)
- MCP (Chapter 6)
- Permissions (No.39)
- Sessions (resuming where you left off, forking)
- Skills, commands and memory (loaded automatically from
.claude/and~/.claude/) - Plugins (No.41)
The fact that your .claude/ settings apply unchanged is a big deal. Skills and subagents you built for Claude Code can be used in your own agent too.
Supported languages#
Python and TypeScript only.
If you want to use it from another language, you can launch the CLI as a child process (-p and --output-format json). That way you can invoke the same agent behavior from any language.
When to choose the Agent SDK#
The deciding question is whether the task is agentic.
Tasks the Client SDK handles fine:
- A single call produces the answer (classification, summarization, extraction)
- No tools, or just one or two with a simple round-trip
- Guaranteeing the shape with structured output is enough (No.52)
Tasks the Agent SDK suits:
- The steps aren’t decided in advance
- Tools are called back and forth many times
- Reading files, writing them, running things, and checking the results
- Permission control is needed
Taking the quotation example: if all you do is extract fields from a PDF, the Client SDK is enough. If the agent has to look through a folder, judge differences between formats, and switch to a different process when needed, that’s the Agent SDK.
About authentication#
Use API key authentication.
The official docs are explicit: unless previously approved, Anthropic does not allow third-party developers to offer claude.ai login or usage limits in their products. Agents built with the Agent SDK are no exception.
If you put it into your own service, the premise is that you pay for the usage. The cost design from No.53 becomes your business design.
There are rules about naming, too#
This is surprisingly easy to overlook, so it’s worth noting. There are official branding guidelines.
Allowed: “Claude Agent”, “Claude”, and “(your product name) Powered by Claude”.
Not allowed: “Claude Code”, “Claude Code Agent”, or a look that imitates Claude Code.
The principle is that your product keeps its own brand. For internal tools this rarely matters, but check before releasing anything publicly.
Try it: decide between the Client SDK and the Agent SDK (3 minutes)
Prompt
“`
I’ll describe what I want to build. Decide whether the Client SDK (calling the API directly)
or the Agent SDK (using Claude Code’s machinery) is a better fit.Include the following in your reasoning:
・Whether the steps are decided in advance
・How many tool round-trips are needed
・Whether it deals with files or commandsWhat I want to build: (write it here)
“`Expected result: You get a verdict with reasons, centered on the number of round-trips.
If it doesn’t work: If the answer is “it could go either way”, start with the Client SDK. Moving over once you feel it’s not enough wastes less effort.
Common pitfalls#
Choosing the Agent SDK from the start. Using it for something a single call can handle just adds weight. Start with the Client SDK.
Trying to let users log in with claude.ai. That isn’t allowed. Use an API key.
Skipping permission settings. Your own agent writes files and runs commands too. Everything in Chapters 3 and 5 applies directly.
Related pages#
- No.23 Subagents and Agent Teams — how to split up work
- No.39 Permission Modes, Sandboxing and Rewinding — designing permissions
- No.53 Optimizing API Costs — designing costs
Summary#
- You can use Claude Code’s engine as a library, in Python and TypeScript
- Your
.claude/settings apply unchanged, so skills and subagents can be shared - The dividing line is how many tool round-trips there are
- Authenticate with an API key. You can’t let users sign in with claude.ai