Claude Agent SDK: Turning the Claude Code Agent Loop into a Library
The Claude Agent SDK puts the Claude Code agent loop inside your Python or TypeScript app. Learn what it is, how it works, and how to build your first agent.
By Galactic News Editorial · Published 2026-11-28T02:06:21+00:00 · Updated 2026-11-28T02:06:21+00:00
Claude Agent SDK: Turning the Claude Code Agent Loop into a Library
The Claude Agent SDK is Anthropic's library for running the Claude Code agent inside your own Python or TypeScript application. You get the same agent loop, built-in tools, permissions, and context management that power Claude Code. You call query() with a prompt, and the SDK streams back messages while Claude reads files, runs commands, and edits code. This guide explains what the SDK is, how it differs from the Messages API and the CLI, and how to build your first agent.
The idea is simple. Claude Code is a terminal app for interactive coding. The SDK is that same agent with the terminal removed. Your code drives it instead of a person typing at a prompt.
What the Claude Agent SDK Is
Anthropic first shipped the library as the Claude Code SDK in June 2025. It renamed the library to the Claude Agent SDK on September 29, 2025. The rename reflects the scope. The same harness can power research, support, and finance agents, not only coding agents. Old tutorials import @anthropic-ai/claude-code or claude-code-sdk. Those are the old package names. The current packages are @anthropic-ai/claude-agent-sdk on npm and claude-agent-sdk on PyPI.
Both packages bundle a native Claude Code binary for your platform. You do not install Claude Code separately. You do not run a hosted service. The harness runs in your own process, and deployment is your responsibility.
The public API is deliberately small. One primitive, query(prompt, options), returns an async iterator over messages. Your code loops over the messages as Claude works. Each message can be text, a tool call, a tool result, or a final result with usage and cost data.
Authentication uses an Anthropic API key, set as ANTHROPIC_API_KEY in the process environment. The library itself is free. You pay for Claude usage through the API. Anthropic notes that apps built on the SDK should bill through the API rather than offer a claude.ai login, unless Anthropic has approved it.
Both packages are still 0.x releases. The API keeps moving, so pin your version in production and read the changelog before upgrading.
Claude Agent SDK File and Code Agents: How the Loop Works
The agent loop is the core idea. Claude plans its own steps, calls tools, reads the results, and repeats until the task is done or it needs your input. You do not write that loop. You describe the task and set the guardrails.
The SDK ships with built-in tools, so your agent can start working without you writing tool handlers:
| Tool | What it does |
|---|---|
| Read | Reads files in the working directory |
| Write | Creates new files |
| Edit | Makes targeted changes to files |
| Bash | Runs terminal commands |
| Glob | Finds files by pattern |
| Grep | Searches file contents with regex |
| WebSearch | Runs web queries |
| WebFetch | Fetches page content |
| AskUserQuestion | Asks for clarification |
Beyond tools, the SDK carries over the rest of the Claude Code feature set. Subagents spawn specialized agents for focused subtasks, each with its own tools and permissions. Hooks run your code at key points in the agent lifecycle, so you can block dangerous operations, log tool calls, or require human approval. Sessions keep context across exchanges and can be resumed or forked later. MCP connects external tools and data sources through the Model Context Protocol. Permissions control which tools run automatically and which need approval. Skills, commands, and memory load from your project's .claude/ directory and from ~/.claude/, just like the CLI.
Install and Build Your First Agent
The official quickstart builds a bug-fixing agent. The steps below follow it.
- Install the package. For TypeScript, run
npm install @anthropic-ai/claude-agent-sdkwith Node.js 18 or later. For Python, runpip install claude-agent-sdkoruv add claude-agent-sdkwith Python 3.10 or later. - Set your API key. Export
ANTHROPIC_API_KEYin your process environment. The SDK does not read .env files by itself, so load one first if you keep the key there. - Write the agent. The Python version is short:
import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions
async def main():
async for message in query(
prompt="Find the bug in utils.py and fix it.",
options=ClaudeAgentOptions(
allowed_tools=["Read", "Edit", "Bash"],
),
):
print(message)
asyncio.run(main())The TypeScript version uses the same shape:
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Find the bug in utils.ts and fix it.",
options: { allowedTools: ["Read", "Edit", "Bash"] },
})) {
console.log(message);
}- Run it and watch the stream. Messages arrive as Claude reads the file, finds the bug, edits it, and reports back. Print each message to see the plan, the tool calls, and the result.
- Tighten permissions. Start with a narrow tool list and a working directory the agent may change. Widen access only when you know what the agent will do with it.
Agent SDK vs Messages API vs Claude Code CLI
Anthropic offers four ways to use Claude, and the names overlap. The table below separates them.
| Product | What it is | Who runs the agent loop | Use it when |
|---|---|---|---|
| Messages API (Client SDK) | Direct access to the Claude API | You do | You want full control over prompts, tools, and retries |
| Claude Agent SDK | Claude Code's agent as a library | The SDK does, in your process | You want the Claude Code loop inside your own app or service |
| Claude Code CLI | Terminal app for interactive coding | The CLI does | A person drives the agent at a prompt |
| Managed Agents | Anthropic-hosted agents | Anthropic does | You want hosted execution without running the harness |
The rule of thumb: use the Messages API when the job is a single model call with tools you define. Use the Agent SDK when the job is a multi-step task across files, commands, and the web. If your language is neither Python nor TypeScript, run the CLI as a subprocess with the -p flag and --output-format json.
Limits to Know Before Production
- The API moves. Both packages are 0.x. Pin versions, and test before upgrading.
- You host the harness. The SDK spawns a claude CLI subprocess in your process. Scaling, restarts, and observability are your job.
- It is Claude-only. The SDK does not swap in other providers. For model choice, use the Messages API or another framework.
- Every loop step bills usage. Long agent runs consume tokens on every turn. Set turn limits and watch the result message's usage data.
- Permissions are your safety net. Bash and Edit can change files and run commands. Restrict the working directory and the allowed tool list before you point an agent at a real repo.
Bottom Line
The Claude Agent SDK turns Claude Code from a terminal tool into a building block. Install one package, call query(), and you have a file-and-code agent with tools, permissions, sessions, and hooks included. It fits best when the task spans files and commands and you want the loop handled for you. For single model calls or other providers, the Messages API is the better fit. Start with a narrow tool list, pin the version, and watch the first runs closely.