Claude Agent SDK 开发指南
原 Claude Code SDK 已正式更名为 Claude Agent SDK,能力范围覆盖编码任务之外的 AI Agent 构建。本指南涵盖 Python/TypeScript 快速入门、核心概念、与 Managed Agents 的对比以及通过 QCode 接入的方式。
更新于 2026-10-20
什么是 Claude Agent SDK
官方改名
官方迁移指南显示,改名同时重组了文档,安装用的包名也一并更换;两个名字指的是同一套库(核于 2026-10-20)。
为何改名
官方给出的理由是能力范围:这套库用于编码任务之外的 AI Agent 构建(核于 2026-10-20)。
统一工具接口
与 Claude Code 相同的工具集、Agent 循环与上下文管理,可作为库嵌入 Python 或 TypeScript 应用,在你自己的进程中运行。
生产就绪
内置文件读写、编辑、命令执行与网页搜索工具,并附带会话管理、权限控制、Hooks、子代理与 MCP 接入。
多语言支持
官方维护两个包:Python 包为 claude-agent-sdk,TypeScript 包为 @anthropic-ai/claude-agent-sdk,两者持续更新。
核心概念
Tools(工具)
Agent 可调用的函数集合,用装饰器声明,自动生成 JSON Schema 供 Claude 理解和调用。
Agent Loop(代理循环)
Claude 思考 → 选择工具 → 执行 → 观察结果 → 继续思考的循环,直到任务完成。
Memory / Context(记忆)
会话内短期记忆(对话历史)与跨会话长期记忆(文件/数据库持久化)的统一管理。
Permissions(权限)
细粒度权限控制:文件系统、网络、代码执行、外部 API 等访问范围均可独立配置。
Python 快速入门
三步完成第一个 Claude Agent;接入 QCode 只需配置 QCode 的 API Key 与 Anthropic 端点。
步骤 1:安装 SDK
python3 -m venv .venv && source .venv/bin/activate
pip install claude-agent-sdk
步骤 2:创建 Agent
import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions
async def main():
options = ClaudeAgentOptions(
system_prompt="You are an expert Python developer",
permission_mode="acceptEdits",
)
async for message in query(
prompt="Create a Python web server", options=options
):
print(message)
asyncio.run(main())
步骤 3:定义自定义工具
import asyncio
from typing import Any
from claude_agent_sdk import (
ClaudeAgentOptions, create_sdk_mcp_server, query, tool
)
@tool("get_weather", "Get current weather for a city", {"city": str})
async def get_weather(args: dict[str, Any]) -> dict[str, Any]:
return {
"content": [
{"type": "text",
"text": f"Weather in {args['city']}: 22 C, sunny"}
]
}
weather_server = create_sdk_mcp_server(
name="weather",
version="1.0.0",
tools=[get_weather],
)
options = ClaudeAgentOptions(
mcp_servers={"weather": weather_server},
allowed_tools=["mcp__weather__get_weather"],
)
async def main():
async for message in query(
prompt="What's the weather in Beijing and Shanghai?",
options=options,
):
print(message)
asyncio.run(main())
TypeScript 快速入门
Node.js / Bun 环境下的 Claude Agent 开发,完整类型支持。
步骤 1:安装 SDK
npm install @anthropic-ai/claude-agent-sdk
步骤 2:创建 Agent
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Create a Python web server",
options: { systemPrompt: "You are an expert Python developer" },
})) {
console.log(message);
}
步骤 3:完整示例
import { tool, createSdkMcpServer, query } from "@anthropic-ai/claude-agent-sdk";
import { z } from "zod";
const getTemperature = tool(
"get_temperature",
"Get the current temperature at a location",
{
latitude: z.number().describe("Latitude coordinate"),
longitude: z.number().describe("Longitude coordinate")
},
async (args) => {
return {
content: [{ type: "text", text: `Temperature at ${args.latitude}` }]
};
}
);
const weatherServer = createSdkMcpServer({
name: "weather",
version: "1.0.0",
tools: [getTemperature]
});
for await (const message of query({
prompt: "What's the temperature in Beijing?",
options: {
mcpServers: { weather: weatherServer },
allowedTools: ["mcp__weather__get_temperature"]
}
})) {
console.log(message);
}
Agent SDK vs Managed Agents
| 对比维度 |
Agent SDK
|
Managed Agents
|
|---|---|---|
| 运行环境 | 自托管(本地/自有服务器) | Agent 循环由 Anthropic 托管,会话可放在托管云沙箱或自有基础设施的沙箱中。 |
| 扩容方式 | 手动管理实例 | 自动弹性伸缩 |
| 适用场景 | 原型开发、高度定制场景 | 企业生产级 Agent 部署 |
通过 QCode.cc 使用
将 ANTHROPIC_BASE_URL 设为 https://api.qcode.cc/api,用 QCode 的 API Key 即可调用 Agent SDK;可调型号见 /models。Managed Agents 由 Anthropic 托管 Agent 循环,会话可放在托管云沙箱或自有基础设施的沙箱中(官方文档 2026-10-20 抓取),本站没在这条链路上验过,也不代它承诺。
# Python SDK
ANTHROPIC_BASE_URL=https://api.qcode.cc/api
ANTHROPIC_API_KEY=your-qcode-api-key
# TypeScript SDK
ANTHROPIC_BASE_URL=https://api.qcode.cc/api
ANTHROPIC_API_KEY=your-qcode-api-key
# Point the endpoint at your own gateway: the SDK reads it from env
from claude_agent_sdk import ClaudeAgentOptions
options = ClaudeAgentOptions(
env={
"ANTHROPIC_BASE_URL": "https://api.qcode.cc/api",
},
)
Claude 型号 Fable 5 / Opus 4.8 / Sonnet 4.6 在 QCode 可调,Haiku 4.5 等其他型号是否可调以 /models 为准(核于 2026-10-20)。
工具调用(Function Calling)完整支持
流式输出(Streaming)支持
与官方 Messages API 同形:Agent SDK 只改 ANTHROPIC_BASE_URL