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 file and code agents running in Python and TypeScript

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:

ToolWhat it does
ReadReads files in the working directory
WriteCreates new files
EditMakes targeted changes to files
BashRuns terminal commands
GlobFinds files by pattern
GrepSearches file contents with regex
WebSearchRuns web queries
WebFetchFetches page content
AskUserQuestionAsks 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.

  1. Install the package. For TypeScript, run npm install @anthropic-ai/claude-agent-sdk with Node.js 18 or later. For Python, run pip install claude-agent-sdk or uv add claude-agent-sdk with Python 3.10 or later.
  2. Set your API key. Export ANTHROPIC_API_KEY in your process environment. The SDK does not read .env files by itself, so load one first if you keep the key there.
  3. 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);
}
  1. 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.
  2. 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.

ProductWhat it isWho runs the agent loopUse it when
Messages API (Client SDK)Direct access to the Claude APIYou doYou want full control over prompts, tools, and retries
Claude Agent SDKClaude Code's agent as a libraryThe SDK does, in your processYou want the Claude Code loop inside your own app or service
Claude Code CLITerminal app for interactive codingThe CLI doesA person drives the agent at a prompt
Managed AgentsAnthropic-hosted agentsAnthropic doesYou 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

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.