Search⌘ K
AI Features

The Claude Agent SDK

Explore how the Claude Agent SDK simplifies building complex agent loops by integrating built-in and custom tools, managing permissions, and enabling sub-agent delegation. Understand how to maintain sessions with ClaudeSDKClient, use hooks to control tool calls, and configure agents for efficient multi-step workflows in customer-facing applications.

A Messages API tool loop is code we write and maintain ourselves. We send the conversation, read stop_reason, run each tool, return the results, and repeat. That control is valuable, and it also means every customer project rebuilds the same loop, the same permission checks, and the same logging.

The Claude Agent SDK is a library that runs Claude Code’s agent loop inside our own Python application. It gives us the loop, a set of built-in tools, permissions, hooks, sessions, and sub-agents, all configured through one options object. We decide what the agent may do, and the SDK runs the turns.

The SDK installs with pip install claude-agent-sdk and bundles the Claude Code command-line tool it drives, so no separate installation is needed. The examples here use version 0.2.163 with Python.

What the Agent SDK packages

The SDK brings Claude Code’s capabilities into a library. Six of them matter most when building agents for customers:

  • Agent loop: The SDK sends requests, runs tools, and feeds results back until the task is done.

  • Built-in tools: Ready-made tools such as Read, Write, Edit, Bash, Glob, Grep, WebSearch, and WebFetch.

  • Custom tools: Our own Python functions, exposed to Claude as tools.

  • Permissions: Rules that decide which tools run automatically, which are blocked, and which need approval.

  • Hooks: Callbacks that run our code at points in the agent’s work, such as just before a tool call.

  • Sessions and sub-agents: Conversations that keep context across turns, and specialized agents the main agent can delegate to.

The table below compares how each capability works with the Messages API and with the Agent SDK.

Messages API and Agent SDK compared

Capability

Messages API

Agent SDK

Agent loop

Our code sends requests, reads stop_reason, and repeats

The SDK runs the turns until the task is done

Tools

We define JSON schemas and run every tool call

Built-in tools plus custom tools from @tool functions

Permissions

Checks we write in our own code

allowed_tools, disallowed_tools, and permission_mode

Hooks

Logic we add around each tool call

Callbacks for events such as PreToolUse

Sessions

We store and resend the full history

ClaudeSDKClient keeps context across turns

Sub-agents

We build any delegation ourselves

AgentDefinition entries in the agents option

Do you find this helpful?

The trade is control for speed. The Messages API suits narrow workflows that need full control of each call. The Agent SDK suits agents that take many steps or need sessions, and many engagements use both.

These capabilities share one entry point, the query() function.

Running an agent with query()

...