# Anthropic building effective agents

<https://www.anthropic.com/research/building-effective-agents>

## agents vs workflows

* workflows 有预定义的 code path，调用 LLMs 和 tools。
* agents 由 LLMs 动态控制流程、工具使用。

## 何时（以及何时不）使用 agents

* 建议采用最简单的解决方案。当需要<mark style="color:green;">大规模的灵活性</mark>、模型驱动的决策时，agents 是更好的选择。
* 对于许多应用，通过<mark style="color:red;">检索、上下文示例</mark>优化单次 LLM 调用通常已经足够。

## agent 框架

* 如 LangChain 的 [LangGraph](https://langchain-ai.github.io/langgraph/)，Amazon Bedrock 的 [AI Agent framework](https://aws.amazon.com/bedrock/agents/)，[Rivet](https://rivet.ironcladapp.com/)（构建 LLM workflows 的拖拽式 GUI）,[Vellum](https://www.vellum.ai/)（构建和测试复杂 workflows 的 GUI）。
* 框架可能掩盖了底层的 prompts ​​and responses，更建议开发者直接使用 LLM APIs，除非理解框架底层代码。
* [常见 agent workflows 最小实现示例代码](https://github.com/anthropics/anthropic-cookbook/tree/main/patterns/agents)

## 代理系统的常见模式

### Building block: The augmented LLM

<figure><img src="https://www.anthropic.com/_next/image?url=https%3A%2F%2Fwww-cdn.anthropic.com%2Fimages%2F4zrzovbb%2Fwebsite%2Fd3083d3f40bb2b6f477901cc9a240738d3dd1371-2401x1000.png&#x26;w=3840&#x26;q=75" alt=""><figcaption></figcaption></figure>

anthropic 提供了 [AI 系统连接内容库、商业工具、开发环境的标准协议](https://www.anthropic.com/news/model-context-protocol) 以及 [客户端实现](https://modelcontextprotocol.io/quickstart/client#building-mcp-clients)。

### Workflow: Prompt chaining

<figure><img src="https://www.anthropic.com/_next/image?url=https%3A%2F%2Fwww-cdn.anthropic.com%2Fimages%2F4zrzovbb%2Fwebsite%2F7418719e3dab222dccb379b8879e1dc08ad34c78-2401x1000.png&#x26;w=3840&#x26;q=75" alt=""><figcaption></figcaption></figure>

\*Gate 用于检查输出确保流程正常进行。

* 适用于可以轻松、清晰地分解为固定子任务的任务，主要目标是通过使每次 LLM 调用都成为更简单的任务，以牺牲延迟换取更高的准确度。
* 示例
  * 生成文案，再翻译。
  * 先写文档大纲、再检查是否符合特定标准，最后根据大纲撰写文档。

### Workflow: Routing

<figure><img src="https://www.anthropic.com/_next/image?url=https%3A%2F%2Fwww-cdn.anthropic.com%2Fimages%2F4zrzovbb%2Fwebsite%2F5c0c0e9fe4def0b584c04d37849941da55e5e71c-2401x1000.png&#x26;w=3840&#x26;q=75" alt=""><figcaption></figcaption></figure>

* 适用于可以对输入进行分类，且最好分别处理的任务。通过构建更具针对性的提示等方式专门处理。
* 示例
  * 将不同类型的客户服务查询（一般问题、退款请求、技术支持）路由到不同的下游流程、提示和工具。
  * 将简单/常见问题路由到较小的模型，将困难/罕见的问题路由到更强大的模型，以优化成本和速度。

### Workflow: Parallelization

<figure><img src="https://www.anthropic.com/_next/image?url=https%3A%2F%2Fwww-cdn.anthropic.com%2Fimages%2F4zrzovbb%2Fwebsite%2F406bb032ca007fd1624f261af717d70e6ca86286-2401x1000.png&#x26;w=3840&#x26;q=75" alt=""><figcaption></figcaption></figure>

* 分为两类
  * Sectioning：将任务分解为多个独立、可并行的子任务，以提高速度。
  * Voting：多次运行同一任务以获得多样化的、更高置信度的结果。
* 示例
  * Sectioning：一个实例处理用户请求；另一个实例进行安全 guardrails。
  * Voting：评价内容是否不当，多个提示评估不同的方面，或采用不同的阈值来平衡 false positives and negatives。

### Workflow: Orchestrator-workers

<figure><img src="https://www.anthropic.com/_next/image?url=https%3A%2F%2Fwww-cdn.anthropic.com%2Fimages%2F4zrzovbb%2Fwebsite%2F8985fc683fae4780fb34eab1365ab78c7e51bc8e-2401x1000.png&#x26;w=3840&#x26;q=75" alt=""><figcaption></figcaption></figure>

* 中央 LLM 动态分解任务、分派给 worker LLMs、合成结果。与并行的区别在于子任务不是预定义的。
* 示例：从多个来源收集、分析信息（源，查询、分析方式可能取决于具体任务）。

### Workflow: Evaluator-optimizer

<figure><img src="https://www.anthropic.com/_next/image?url=https%3A%2F%2Fwww-cdn.anthropic.com%2Fimages%2F4zrzovbb%2Fwebsite%2F14f51e6406ccb29e695da48b17017e899a6119c7-2401x1000.png&#x26;w=3840&#x26;q=75" alt=""><figcaption></figcaption></figure>

* 适用于 LLM 可以对响应进行反馈、且反馈可以显著改进响应的任务。
* 示例
  * 翻译。
  * 需要多轮搜索、分析的任务，评估器决定是否需要进一步搜索。

### Agents

<figure><img src="https://www.anthropic.com/_next/image?url=https%3A%2F%2Fwww-cdn.anthropic.com%2Fimages%2F4zrzovbb%2Fwebsite%2F58d9f10c985c4eb5d53798dea315f7bb5ab6249e-2401x1000.png&#x26;w=3840&#x26;q=75" alt=""><figcaption></figcaption></figure>

* 适用于无法硬编码固定路径的开放式任务，且 LLM 的决策有一定的可信度。
* 示例

  <figure><img src="https://www.anthropic.com/_next/image?url=https%3A%2F%2Fwww-cdn.anthropic.com%2Fimages%2F4zrzovbb%2Fwebsite%2F4b9a1f4eb63d5962a6e1746ac26bbc857cf3474f-2400x1666.png&#x26;w=3840&#x26;q=75" alt=""><figcaption></figcaption></figure>

## 总结

* 实现代理的核心原则
  * 保持 agent 设计的简单性。
  * 优先考虑透明度，显式地展示 agent 的规划步骤。
  * 通过全面的工具文档、测试来仔细设计 agent-computer 接口。

## 示例

* agent 为需要对话和行动、具有明确的成功标准、支持反馈循环、集成了有效的人工监督的任务带来了最大价值。
* 客户支持

  聊天机器人界面，可以集成工具来提取客户数据、订单、知识库等；可以以编程的方式进行退款等操作，容易衡量是否成功。
* coding

  自动化测试、根据测试结果迭代优化，问题明确、容易衡量输出质量。

## 工具的提示工程

* 对 LLM 来说某些格式比其他格式更难编写。建议：接近互联网文本中的自然格式；没有格式化开销。
* 良好的工具定义通常包括示例用法、边界 case、输入格式要求、<mark style="color:green;">与其他工具的明确界限</mark>。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nanyang.gitbook.io/meng-yuan/ji-qi-xue-xi/agent/building-effective-agents.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
