✨
Claude Code Agent SDK
Claude Code Agent SDK · v1.0.0
507 下载
0 安装
2.5k 收藏
1 浏览
简介
Claude Code Agent SDK
原始描述
Claude Agent SDK documentation — build production AI agents with Claude Code as a library in Python or TypeScript. Use when building, configuring, or debuggi...
通过你的 Agent 添加
安装目录:claude-code-agent-sdk/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。
点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。
或者手动安装
Claude Code~/.claude/skills/claude-code-agent-sdk/
Codex CLI~/.codex/skills/claude-code-agent-sdk/
DeepSeek Harness~/.agents/skills/claude-code-agent-sdk/
包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可;
目录名不用换,也不要把包直接解压到 skills 根目录。
技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。
技能正文
name: claude-code-agent-sdk
description: Claude Agent SDK documentation — build production AI agents with Claude Code as a library in Python or TypeScript. Use when building, configuring, or debugging agents with the Claude Agent SDK.
Claude Code Agent SDK
使用 Claude Code 作为库构建生产级 AI 代理。支持 Python 和 TypeScript。
Covers
- Quickstart and setup
- Agent loop and message types
- Tool configuration and built-in tools
- Hooks for intercepting agent behavior
- Session management (resume, fork, continue)
- MCP server integration
- Sub-agents and parallel execution
- Permissions and security
- Custom tools,
- Streaming and structured outputs
- Cost tracking and observability
- Claude Code features (skills, commands, memory)
- Hosting and deployment
- Python and TypeScript API references.
Quick Navigation
Reference files are split by topic in references/. Load only what you need:
Core
- overview.md — SDK 概览、安装、认证、内置工具、权限模式
- quickstart.md — 快速开始:构建 bug 修复代理
- agent-loop.md — 代理循环:消息类型、工具执行、上下文窗口、压缩
Agent Control
- hooks.md — Hooks:PreToolUse/PostToolUse/Stop 等、匹配器、回调函数
- permissions.md — 权限:allowedTools/disallowedTools、权限模式
- user-input.md — 用户输入:批准提示、AskUserQuestion
Sessions & State
- sessions.md — 会话管理:continue/resume/fork、ClaudeSDKClient
- file-checkpointing.md — 文件检查点:跟踪和还原文件更改
Tools & Integration
- custom-tools.md — 自定义工具:进程内 MCP 服务器
- mcp.md — MCP:连接外部工具 + 工具搜索扩展到数千工具
Sub-agents & Parallelism
- subagents.md — 子代理:AgentDefinition、上下文隔离、并行任务 + 待办事项跟踪
Streaming & Output
- streaming-output.md — 流式响应 + 输入模式 + 结构化输出(JSON Schema/Zod/Pydantic)
Configuration & Features
- claude-code-features.md — Claude Code 功能:settingSources、CLAUDE.md、skills
- modifying-system-prompts.md — 系统提示:claude_code 预设 vs 自定义
- skills.md — Agent Skills
- slash-commands.md — 斜杠命令
- plugins.md — 插件系统
Operations
- hosting.md — 托管 + 安全部署:Docker、云、CI/CD、隔离、凭证管理
- cost-tracking.md — 成本跟踪:token 使用、预算
- observability.md — 可观测性:OpenTelemetry
- migration-guide.md — 迁移指南
API Reference
- python.md — Python SDK API 参考
- typescript.md — TypeScript SDK API 参考(含 V2 弃用说明)
Key Patterns
Basic agent
from claude_agent_sdk import query, ClaudeAgentOptions
async for message in query(
prompt="Fix the bug in auth.py",
options=ClaudeAgentOptions(allowed_tools=["Read", "Edit", "Bash"])
):
print(message)
With hooks
options = ClaudeAgentOptions(
hooks={"PreToolUse": [HookMatcher(matcher="Bash", hooks=[validate_command])]}
)
With MCP servers
options = ClaudeAgentOptions(
mcp_servers={"playwright": {"command": "npx", "args": ["@playwright/mcp@latest"]}}
)
With sub-agents
options = ClaudeAgentOptions(
allowed_tools=["Read", "Glob", "Agent"],
agents={"reviewer": AgentDefinition(
description="Code reviewer",
prompt="Review code quality",
tools=["Read", "Glob", "Grep"]
)}
)
Multi-turn with sessions
async with ClaudeSDKClient(options=options) as client:
await client.query("Analyze the auth module")
async for msg in client.receive_response(): print(msg)
await client.query("Now refactor it") # auto-continues
async for msg in client.receive_response(): print(msg)
- hooks.md — Hooks:PreToolUse/PostToolUse/Stop 等、匹配器、回调函数
- permissions.md — 权限:allowedTools/disallowedTools、权限模式
- user-input.md — 用户输入:批准提示、AskUserQuestion
Sessions & State
- sessions.md — 会话管理:continue/resume/fork、ClaudeSDKClient
- file-checkpointing.md — 文件检查点:跟踪和还原文件更改
Tools & Integration
- custom-tools.md — 自定义工具:进程内 MCP 服务器
- mcp.md — MCP:连接外部工具 + 工具搜索扩展到数千工具
Sub-agents & Parallelism
- subagents.md — 子代理:AgentDefinition、上下文隔离、并行任务 + 待办事项跟踪
Streaming & Output
- streaming-output.md — 流式响应 + 输入模式 + 结构化输出(JSON Schema/Zod/Pydantic)
Configuration & Features
- claude-code-features.md — Claude Code 功能:settingSources、CLAUDE.md、skills
- modifying-system-prompts.md — 系统提示:claude_code 预设 vs 自定义
- skills.md — Agent Skills
- slash-commands.md — 斜杠命令
- plugins.md — 插件系统
Operations
- hosting.md — 托管 + 安全部署:Docker、云、CI/CD、隔离、凭证管理
- cost-tracking.md — 成本跟踪:token 使用、预算
- observability.md — 可观测性:OpenTelemetry
- migration-guide.md — 迁移指南
API Reference
- python.md — Python SDK API 参考
- typescript.md — TypeScript SDK API 参考(含 V2 弃用说明)
Key Patterns
Basic agent
from claude_agent_sdk import query, ClaudeAgentOptions
async for message in query(
prompt="Fix the bug in auth.py",
options=ClaudeAgentOptions(allowed_tools=["Read", "Edit", "Bash"])
):
print(message)
With hooks
options = ClaudeAgentOptions(
hooks={"PreToolUse": [HookMatcher(matcher="Bash", hooks=[validate_command])]}
)
With MCP servers
options = ClaudeAgentOptions(
mcp_servers={"playwright": {"command": "npx", "args": ["@playwright/mcp@latest"]}}
)
With sub-agents
options = ClaudeAgentOptions(
allowed_tools=["Read", "Glob", "Agent"],
agents={"reviewer": AgentDefinition(
description="Code reviewer",
prompt="Review code quality",
tools=["Read", "Glob", "Grep"]
)}
)
Multi-turn with sessions
async with ClaudeSDKClient(options=options) as client:
await client.query("Analyze the auth module")
async for msg in client.receive_response(): print(msg)
await client.query("Now refactor it") # auto-continues
async for msg in client.receive_response(): print(msg)
- custom-tools.md — 自定义工具:进程内 MCP 服务器
- mcp.md — MCP:连接外部工具 + 工具搜索扩展到数千工具
Sub-agents & Parallelism
- subagents.md — 子代理:AgentDefinition、上下文隔离、并行任务 + 待办事项跟踪
Streaming & Output
- streaming-output.md — 流式响应 + 输入模式 + 结构化输出(JSON Schema/Zod/Pydantic)
Configuration & Features
- claude-code-features.md — Claude Code 功能:settingSources、CLAUDE.md、skills
- modifying-system-prompts.md — 系统提示:claude_code 预设 vs 自定义
- skills.md — Agent Skills
- slash-commands.md — 斜杠命令
- plugins.md — 插件系统
Operations
- hosting.md — 托管 + 安全部署:Docker、云、CI/CD、隔离、凭证管理
- cost-tracking.md — 成本跟踪:token 使用、预算
- observability.md — 可观测性:OpenTelemetry
- migration-guide.md — 迁移指南
API Reference
- python.md — Python SDK API 参考
- typescript.md — TypeScript SDK API 参考(含 V2 弃用说明)
Key Patterns
Basic agent
from claude_agent_sdk import query, ClaudeAgentOptions
async for message in query(
prompt="Fix the bug in auth.py",
options=ClaudeAgentOptions(allowed_tools=["Read", "Edit", "Bash"])
):
print(message)
With hooks
options = ClaudeAgentOptions(
hooks={"PreToolUse": [HookMatcher(matcher="Bash", hooks=[validate_command])]}
)
With MCP servers
options = ClaudeAgentOptions(
mcp_servers={"playwright": {"command": "npx", "args": ["@playwright/mcp@latest"]}}
)
With sub-agents
options = ClaudeAgentOptions(
allowed_tools=["Read", "Glob", "Agent"],
agents={"reviewer": AgentDefinition(
description="Code reviewer",
prompt="Review code quality",
tools=["Read", "Glob", "Grep"]
)}
)
Multi-turn with sessions
async with ClaudeSDKClient(options=options) as client:
await client.query("Analyze the auth module")
async for msg in client.receive_response(): print(msg)
await client.query("Now refactor it") # auto-continues
async for msg in client.receive_response(): print(msg)
- streaming-output.md — 流式响应 + 输入模式 + 结构化输出(JSON Schema/Zod/Pydantic)
Configuration & Features
- claude-code-features.md — Claude Code 功能:settingSources、CLAUDE.md、skills
- modifying-system-prompts.md — 系统提示:claude_code 预设 vs 自定义
- skills.md — Agent Skills
- slash-commands.md — 斜杠命令
- plugins.md — 插件系统
Operations
- hosting.md — 托管 + 安全部署:Docker、云、CI/CD、隔离、凭证管理
- cost-tracking.md — 成本跟踪:token 使用、预算
- observability.md — 可观测性:OpenTelemetry
- migration-guide.md — 迁移指南
API Reference
- python.md — Python SDK API 参考
- typescript.md — TypeScript SDK API 参考(含 V2 弃用说明)
Key Patterns
Basic agent
from claude_agent_sdk import query, ClaudeAgentOptions
async for message in query(
prompt="Fix the bug in auth.py",
options=ClaudeAgentOptions(allowed_tools=["Read", "Edit", "Bash"])
):
print(message)
With hooks
options = ClaudeAgentOptions(
hooks={"PreToolUse": [HookMatcher(matcher="Bash", hooks=[validate_command])]}
)
With MCP servers
options = ClaudeAgentOptions(
mcp_servers={"playwright": {"command": "npx", "args": ["@playwright/mcp@latest"]}}
)
With sub-agents
options = ClaudeAgentOptions(
allowed_tools=["Read", "Glob", "Agent"],
agents={"reviewer": AgentDefinition(
description="Code reviewer",
prompt="Review code quality",
tools=["Read", "Glob", "Grep"]
)}
)
Multi-turn with sessions
async with ClaudeSDKClient(options=options) as client:
await client.query("Analyze the auth module")
async for msg in client.receive_response(): print(msg)
await client.query("Now refactor it") # auto-continues
async for msg in client.receive_response(): print(msg)
- hosting.md — 托管 + 安全部署:Docker、云、CI/CD、隔离、凭证管理
- cost-tracking.md — 成本跟踪:token 使用、预算
- observability.md — 可观测性:OpenTelemetry
- migration-guide.md — 迁移指南
API Reference
- python.md — Python SDK API 参考
- typescript.md — TypeScript SDK API 参考(含 V2 弃用说明)
Key Patterns
Basic agent
from claude_agent_sdk import query, ClaudeAgentOptions
async for message in query(
prompt="Fix the bug in auth.py",
options=ClaudeAgentOptions(allowed_tools=["Read", "Edit", "Bash"])
):
print(message)
With hooks
options = ClaudeAgentOptions(
hooks={"PreToolUse": [HookMatcher(matcher="Bash", hooks=[validate_command])]}
)
With MCP servers
options = ClaudeAgentOptions(
mcp_servers={"playwright": {"command": "npx", "args": ["@playwright/mcp@latest"]}}
)
With sub-agents
options = ClaudeAgentOptions(
allowed_tools=["Read", "Glob", "Agent"],
agents={"reviewer": AgentDefinition(
description="Code reviewer",
prompt="Review code quality",
tools=["Read", "Glob", "Grep"]
)}
)
Multi-turn with sessions
async with ClaudeSDKClient(options=options) as client:
await client.query("Analyze the auth module")
async for msg in client.receive_response(): print(msg)
await client.query("Now refactor it") # auto-continues
async for msg in client.receive_response(): print(msg)
Basic agent
from claude_agent_sdk import query, ClaudeAgentOptions
async for message in query(
prompt="Fix the bug in auth.py",
options=ClaudeAgentOptions(allowed_tools=["Read", "Edit", "Bash"])
):
print(message)
options = ClaudeAgentOptions(
hooks={"PreToolUse": [HookMatcher(matcher="Bash", hooks=[validate_command])]}
)
With MCP servers
options = ClaudeAgentOptions(
mcp_servers={"playwright": {"command": "npx", "args": ["@playwright/mcp@latest"]}}
)
With sub-agents
options = ClaudeAgentOptions(
allowed_tools=["Read", "Glob", "Agent"],
agents={"reviewer": AgentDefinition(
description="Code reviewer",
prompt="Review code quality",
tools=["Read", "Glob", "Grep"]
)}
)
Multi-turn with sessions
async with ClaudeSDKClient(options=options) as client:
await client.query("Analyze the auth module")
async for msg in client.receive_response(): print(msg)
await client.query("Now refactor it") # auto-continues
async for msg in client.receive_response(): print(msg)
options = ClaudeAgentOptions(
mcp_servers={"playwright": {"command": "npx", "args": ["@playwright/mcp@latest"]}}
)
options = ClaudeAgentOptions(
allowed_tools=["Read", "Glob", "Agent"],
agents={"reviewer": AgentDefinition(
description="Code reviewer",
prompt="Review code quality",
tools=["Read", "Glob", "Grep"]
)}
)
Multi-turn with sessions
async with ClaudeSDKClient(options=options) as client:
await client.query("Analyze the auth module")
async for msg in client.receive_response(): print(msg)
await client.query("Now refactor it") # auto-continues
async for msg in client.receive_response(): print(msg)
async with ClaudeSDKClient(options=options) as client:
await client.query("Analyze the auth module")
async for msg in client.receive_response(): print(msg)
await client.query("Now refactor it") # auto-continues
async for msg in client.receive_response(): print(msg)
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。