Claude Agent SDK 详解:用代码构建 AI Agent 的生产级工具

Anthropic 推出的 Agent SDK 让开发者能以库的形式构建 AI Agent,继承 Claude Code 的全部能力,支持 Python 和 TypeScript。

Claude Agent SDK 把驱动 Claude Code 的核心能力——工具调用、Agent 循环、上下文管理——全部开放给了开发者。用几行代码就能构建一个能读文件、执行命令、编辑代码的自主 AI Agent。

这才是 Vibe Coding 走向生产的关键一步。


这是什么?

Claude Agent SDK(之前叫 Claude Code SDK)是 Anthropic 推出的开发工具包,让你能以库的形式构建生产级 AI Agent。

支持 Python 和 TypeScript。最大的优势是:SDK 内置了工具执行逻辑,不用自己实现工具调用,Agent 开箱即用。

你的 Agent 可以做什么?

能力说明
📂 读取文件自动读取本地文件内容
💻 运行命令在终端执行 Bash 命令
🌐 搜索网络进行网络搜索获取信息
✏️ 编辑代码自动修改和编写代码
🔧 更多工具Glob 文件匹配等内置工具

快速上手

1. 安装 Claude Code

SDK 需要 Claude Code 作为运行时环境:

# macOS/Linux
brew install claude-code

# 或者用 npm
npm install -g @anthropic-ai/claude-code

2. 安装 SDK

# Python
pip install claude-agent-sdk

# TypeScript
npm install @anthropic-ai/claude-agent-sdk

3. 设置 API Key

export ANTHROPIC_API_KEY=your-api-key

可以从 Anthropic Console 获取。

4. 跑第一个 Agent

Python 示例:

import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions

async def main():
    async for message in query(
        prompt="Find and fix the bug in auth.py",
        options=ClaudeAgentOptions(allowed_tools=["Read", "Edit", "Bash"])
    ):
        print(message)
        # Claude 会自动读取文件、找到 bug、修复它

asyncio.run(main())

这段代码创建了一个能自动查找并修复代码 Bug 的 Agent。


Claude Code 功能支持

Agent SDK 支持 Claude Code 基于文件系统的配置功能:

功能配置文件路径
Skills(技能).claude/skills/SKILL.md
Slash Commands.claude/commands/*.md
Memory(记忆)CLAUDE.md.claude/CLAUDE.md
Plugins(插件)通过 plugins 配置

启用这些功能需要在选项里设置:

# Python
setting_sources=["project"]

# TypeScript
settingSources: ['project']

多平台支持

除了 Anthropic 原生 API,还可以用第三方云服务:

平台环境变量
Amazon BedrockCLAUDE_CODE_USE_BEDROCK=1
Google Vertex AICLAUDE_CODE_USE_VERTEX=1
Microsoft FoundryCLAUDE_CODE_USE_FOUNDRY=1

跟其他 Claude 工具的区别


品牌使用指南

集成 Claude Agent SDK 的话,品牌使用是可选的。引用 Claude 时:

允许的叫法

不允许的叫法


许可和条款

使用受 Anthropic 商业服务条款 约束。


小结

Claude Agent SDK 是一个让开发者快速构建 AI Agent 的工具包:

  1. 开箱即用 - 内置文件读取、命令执行、代码编辑等工具
  2. 双语言支持 - Python 和 TypeScript 都能用
  3. 继承 Claude Code 能力 - 共享相同的核心技术
  4. 灵活部署 - 支持 Anthropic、AWS、Google Cloud、Azure
  5. 生产就绪 - 设计用于投入生产的 Agent 应用

如果你在探索 Vibe Coding 和 AI 辅助开发,这个 SDK 能帮你构建自己的自动化编码助手。


延伸阅读

查看原文