# Building Agents with the Claude Agent SDK：用 Claude Agent SDK 构建自主 Agent

> 原文链接：https://claude.com/blog/building-agents-with-the-claude-agent-sdk
> 作者/来源：Thariq Shihipar / Anthropic
> 阅读日期：2026-04-02

## 一句话总结
介绍从 Claude Code SDK 更名为 Claude Agent SDK 的背景及其设计哲学——通过赋予 agent 专业人员日常使用的工具（bash、文件系统、代码执行），而非仅限于 API 调用，实现更自然的自主 agent 构建。

## 核心论点

文章的核心设计哲学是：**让 agent 使用与人类专业人员相同的工具**。与仅限于 API 调用的传统方法不同，Claude Agent SDK 让 agent 可以访问 bash 命令、文件系统和代码执行——这使得 agent 的工作方式更接近人类处理任务的方式。从 Claude Code SDK 更名为 Claude Agent SDK 反映了这一认知的扩展：这些能力远不止于编码场景，而是适用于金融分析、个人助理、客服和研究等多种专业领域。

SDK 的核心架构是一个**四阶段反馈循环**：Gather Context（收集上下文）→ Take Action（执行动作）→ Verify Work（验证工作）→ Iterate（迭代改进）。每个阶段都有多种实现机制。上下文收集支持 agentic search（文件系统导航）、semantic search、subagent 并行任务和自动 context compaction（用于长时间运行的操作）。动作执行支持 custom tools、bash scripting、code generation 和 MCP（Model Context Protocol）集成。工作验证支持 rules-based feedback（如 linting）、visual feedback（用于 UI 相关任务）和 LLM-as-judge（用于模糊规则评估）。

SDK 的一个重要设计选择是集成 **Model Context Protocol（MCP）**，为连接外部服务提供标准化接口。这意味着开发者可以通过 MCP 扩展 agent 的能力边界，而无需为每个外部服务编写定制集成代码。

## 关键概念

- **Claude Agent SDK**：Anthropic 提供的 agent 开发工具包，从原来的 Claude Code SDK 更名而来，支持构建具有文件系统访问、终端命令和迭代问题求解能力的自主 agent。
- **Agent Feedback Loop（Agent 反馈循环）**：四阶段架构——Gather Context → Take Action → Verify Work → Iterate，构成 agent 自主运行的核心循环。
- **Agentic Search**：通过文件系统导航和语义搜索主动收集信息，而非被动接收上下文。
- **Context Compaction（上下文压缩）**：在长时间运行的操作中自动压缩上下文，保持 context window 在可用范围内。
- **Model Context Protocol（MCP）**：标准化的外部服务连接协议，让 agent 可以通过统一接口访问各种外部工具和服务。
- **LLM-as-Judge**：使用 LLM 评估 agent 输出，特别适用于难以用确定性规则表达的"模糊"质量标准。
- **Code as Execution（代码即执行）**：让 agent 通过编写和执行代码来完成任务，提高可靠性——某些任务用代码表达比用自然语言指令更精确。

## 实践建议
1. 优先设计清晰的 tool 接口，注重 context 效率——工具描述的质量直接影响 agent 的工具选择正确性
2. 考虑哪些任务适合通过代码表达以提高可靠性（如数据处理、文件操作），而非纯自然语言指令
3. 建立代表性的测试数据集，用于程序化评估 agent 在真实场景中的表现
4. 利用 MCP 集成标准化外部服务连接，减少定制化集成的维护成本
5. 在 agent 工作流中融入验证环节（linting、visual feedback、LLM-as-judge），不要跳过 Verify Work 阶段
6. 对长时间运行的 agent 任务启用 context compaction，防止 context window 溢出

## 独到观点
文章最独特的贡献在于明确了 Anthropic 的 agent 设计哲学：**赋予 agent 与人类相同的工具，而非设计 agent 专用接口**。这与许多 agent framework 为 agent 设计专门的抽象层的做法形成对比。四阶段反馈循环（Gather → Act → Verify → Iterate）虽然概念上简洁，但它将验证（Verify）作为必需环节嵌入核心循环的做法值得注意——这与 [LangChain: Improving with Harness Engineering](32-langchain-improving-with-harness.md) 中 self-verification 的发现一致。从 Code SDK 到 Agent SDK 的更名本身也是一个有意义的信号：编码 agent 的核心能力（文件系统访问、终端命令、迭代执行）实际上是通用 agent 能力的基础。

## 与其他文章的关联
- SDK 的定位大致处于 [LangChain: Frameworks, Runtimes, and Harnesses](34-langchain-frameworks-runtimes-harnesses.md) 分类中 framework 和 harness 之间
- Agent Feedback Loop 中的 Verify Work 阶段与 [LangChain: Improving with Harness Engineering](32-langchain-improving-with-harness.md) 的 Build-and-Self-Verify Loop 理念一致
- Context compaction 机制与 [12-Factor Agents](25-humanlayer-12-factor-agents.md) Factor 3"Own Your Context Window"的 context engineering 实践相关
- LLM-as-judge 的验证方法与 [Anthropic: Demystifying Evals](29-anthropic-demystifying-evals.md) 中的 model-based grader 对应
- [Anthropic: Multi-Agent Research System](35-anthropic-multi-agent-research.md) 可能就是基于此 SDK 构建的多 agent 系统
- MCP 集成为 [12-Factor Agents](25-humanlayer-12-factor-agents.md) Factor 11"Trigger from Anywhere"的多通道触发提供了标准化的技术方案
- [OpenHands: Learning to Verify AI Code](33-openhands-verify-ai-code.md) 的 verification stack 可以视为 Verify Work 阶段的更深入实现
