构建高效的智能体
Building effective agents
文章目录
Note: Much of the tooling landscape described in this post has changed since December 2024.
注意:自 2024 年 12 月以来,本文所描述的许多工具生态已经发生了变化。
For our current approach, see how we built Claude Managed Agents and the Managed Agents documentation.
关于我们当前的做法,请参阅我们如何构建 Claude Managed Agents 以及 Managed Agents 文档。
Over the past year, we've worked with dozens of teams building large language model (LLM) agents across industries.
在过去一年里,我们与数十个跨行业构建大语言模型(LLM)智能体(agent)的团队合作过。
Consistently, the most successful implementations weren't using complex frameworks or specialized libraries.
我们发现,最成功的实现始终没有使用复杂的框架或专门的库。
Instead, they were building with simple, composable patterns.
相反,它们是用简单、可组合的模式构建的。
In this post, we share what we’ve learned from working with our customers and building agents ourselves, and give practical advice for developers on building effective agents.
在本文中,我们分享从与客户合作以及自己构建智能体的过程中学到的东西,并为开发者提供构建高效智能体的实用建议。
What are agents?
什么是智能体?
"Agent" can be defined in several ways.
「智能体」可以有多种定义方式。
Some customers define agents as fully autonomous systems that operate independently over extended periods, using various tools to accomplish complex tasks.
一些客户将智能体定义为完全自主的系统,它们能在较长时间内独立运行,使用各种工具完成复杂任务。
Others use the term to describe more prescriptive implementations that follow predefined workflows.
另一些人则用这个词来描述更循规蹈矩的实现,即遵循预定义的工作流(workflow)。
At Anthropic, we categorize all these variations as agentic systems, but draw an important architectural distinction between workflows and agents:
在 Anthropic,我们把所有这些变体都归类为智能体系统(agentic systems),但在架构上对工作流与智能体做了重要区分:
Workflows are systems where LLMs and tools are orchestrated through predefined code paths.
工作流是 LLM 和工具通过预定义的代码路径进行编排(orchestration)的系统。
Agents, on the other hand, are systems where LLMs dynamically direct their own processes and tool usage, maintaining control over how they accomplish tasks.
而智能体则是 LLM 动态决定自身流程和工具使用的系统,并对如何完成任务保持控制权。
Below, we will explore both types of agentic systems in detail.
下文我们将详细探讨这两类智能体系统。
In Appendix 1 (“Agents in Practice”), we describe two domains where customers have found particular value in using these kinds of systems.
在附录 1(“实践中的智能体”)中,我们描述了客户在使用这类系统时发现具有特别价值的两个领域。
When (and when not) to use agents
何时(以及何时不)使用智能体
When building applications with LLMs, we recommend finding the simplest solution possible, and only increasing complexity when needed.
在使用 LLM 构建应用时,我们建议找到尽可能最简单的解决方案,并且只在需要时增加复杂度。
This might mean not building agentic systems at all.
这可能意味着完全不构建智能体系统。
Agentic systems often trade latency and cost for better task performance, and you should consider when this tradeoff makes sense.
智能体系统通常以延迟和成本换取更好的任务表现,而你应该考虑这种权衡何时有意义。
When more complexity is warranted, workflows offer predictability and consistency for well-defined tasks, whereas agents are the better option when flexibility and model-driven decision-making are needed at scale.
当需要更高复杂度时,工作流能为定义明确的任务提供可预测性和一致性,而当需要在大规模场景下具备灵活性和模型驱动的决策时,智能体则是更好的选择。
For many applications, however, optimizing single LLM calls with retrieval and in-context examples is usually enough.
然而,对许多应用而言,通过检索(retrieval)和上下文示例(in-context examples)优化单次 LLM 调用通常就足够了。
When and how to use frameworks
何时以及如何使用框架(framework)
There are many frameworks that make agentic systems easier to implement, including:
有许多框架能让智能体系统更易于实现,包括:
The Claude Agent SDK;
Claude Agent SDK;
Strands Agents SDK by AWS;
AWS 的 Strands Agents SDK;
Rivet, a drag and drop GUI LLM workflow builder; and
Rivet,一个拖拽式的 GUI LLM 工作流构建器;以及
Vellum, another GUI tool for building and testing complex workflows.
Vellum,另一个用于构建和测试复杂工作流的 GUI 工具。
These frameworks make it easy to get started by simplifying standard low-level tasks like calling LLMs, defining and parsing tools, and chaining calls together.
这些框架简化了调用 LLM、定义和解析工具以及将调用串联起来等标准的底层任务,从而让入门变得容易。
However, they often create extra layers of abstraction that can obscure the underlying prompts and responses, making them harder to debug.
然而,它们往往会引入额外的抽象层,可能掩盖底层的提示词(prompt)和响应,使其更难调试。
They can also make it tempting to add complexity when a simpler setup would suffice.
当更简单的设置就足够时,它们还可能诱使人们增加复杂性。
We suggest that developers start by using LLM APIs directly: many patterns can be implemented in a few lines of code.
我们建议开发者从直接使用 LLM API 开始:许多模式只需几行代码就能实现。
If you do use a framework, ensure you understand the underlying code.
如果你确实使用了框架,请确保理解底层代码。
Incorrect assumptions about what's under the hood are a common source of customer error.
对底层机制的错误假设是客户出错的常见来源。
See our cookbook for some sample implementations.
请参阅我们的 cookbook 获取一些示例实现。
Building blocks, workflows, and agents
构建模块(building block)、工作流和智能体
In this section, we’ll explore the common patterns for agentic systems we’ve seen in production.
在本节中,我们将探讨在生产环境中见过的智能体系统(agentic system)的常见模式。
We'll start with our foundational building block—the augmented LLM—and progressively increase complexity, from simple compositional workflows to autonomous agents.
我们将从基础构建模块——增强型 LLM(augmented LLM)——开始,逐步增加复杂性,从简单的组合式工作流到自主智能体(autonomous agent)。
Building block: The augmented LLM
构建模块:增强型 LLM
The basic building block of agentic systems is an LLM enhanced with augmentations such as retrieval, tools, and memory.
智能体系统的基本构建模块是一个经过增强的 LLM,这些增强包括检索、工具(tool)和记忆(memory)。
Our current models can actively use these capabilities—generating their own search queries, selecting appropriate tools, and determining what information to retain.
我们当前的模型可以主动使用这些能力——生成自己的搜索查询、选择合适的工具,并决定要保留哪些信息。

The augmented LLM
增强型 LLM
We recommend focusing on two key aspects of the implementation: tailoring these capabilities to your specific use case and ensuring they provide an easy, well-documented interface for your LLM.
我们建议关注实现中的两个关键方面:将这些能力定制到你的具体用例,并确保它们为你的 LLM 提供易于使用且文档完善的接口。
While there are many ways to implement these augmentations, one approach is through our recently released Model Context Protocol, which allows developers to integrate with a growing ecosystem of third-party tools with a simple client implementation.
虽然实现这些增强能力有许多方式,但一种途径是通过我们最近发布的 Model Context Protocol,它允许开发者通过简单的客户端实现,与不断增长的第三方工具生态集成。
For the remainder of this post, we'll assume each LLM call has access to these augmented capabilities.
在本文剩余部分,我们将假设每次 LLM 调用都能访问这些增强能力。
Workflow: Prompt chaining
工作流:提示词链(prompt chaining)
Prompt chaining decomposes a task into a sequence of steps, where each LLM call processes the output of the previous one.
提示词链将一个任务分解为一系列步骤,其中每次 LLM 调用都处理上一次调用的输出。
You can add programmatic checks (see "gate” in the diagram below) on any intermediate steps to ensure that the process is still on track.
你可以在任何中间步骤上添加程序化检查(参见下图中的“gate”),以确保流程仍在正常推进。

The prompt chaining workflow
提示词链工作流
When to use this workflow: This workflow is ideal for situations where the task can be easily and cleanly decomposed into fixed subtasks.
何时使用此工作流:当任务可以轻松且清晰地分解为固定子任务时,此工作流非常理想。
The main goal is to trade off latency for higher accuracy, by making each LLM call an easier task.
主要目标是通过让每次 LLM 调用完成更简单的任务,以延迟换取更高的准确率。
Examples where prompt chaining is useful:
提示词链有用的示例:
Generating Marketing copy, then translating it into a different language.
生成营销文案,然后将其翻译成另一种语言。
Writing an outline of a document, checking that the outline meets certain criteria, then writing the document based on the outline.
撰写文档大纲,检查大纲是否满足某些标准,然后基于大纲撰写文档。
Workflow: Routing
工作流:路由(Routing)
Routing classifies an input and directs it to a specialized followup task.
路由会对输入进行分类,并将其导向专门化的后续任务。
This workflow allows for separation of concerns, and building more specialized prompts.
这种工作流允许关注点分离,并构建更专门的提示词。
Without this workflow, optimizing for one kind of input can hurt performance on other inputs.
若没有这种工作流,针对某一类输入进行优化可能会损害在其他输入上的表现。

The routing workflow
路由工作流
When to use this workflow: Routing works well for complex tasks where there are distinct categories that are better handled separately, and where classification can be handled accurately, either by an LLM or a more traditional classification model/algorithm.
何时使用这种工作流:路由适用于这样的复杂任务——其中存在更适合分开处理的明确类别,并且分类可以被准确完成,无论是通过 LLM 还是更传统的分类模型/算法。
Examples where routing is useful:
路由有用的示例:
Directing different types of customer service queries (general questions, refund requests, technical support) into different downstream processes, prompts, and tools.
将不同类型的客户服务查询(一般问题、退款请求、技术支持)引导到不同的下游流程、提示词和工具中。
Routing easy/common questions to smaller, cost-efficient models like Claude Haiku 4.5 and hard/unusual questions to more capable models like Claude Sonnet 4.5 to optimize for best performance.
将简单/常见的问题路由到更小、更具成本效益的模型(如 Claude Haiku 4.5),将困难/不常见的问题路由到能力更强的模型(如 Claude Sonnet 4.5),以优化获得最佳性能。
Workflow: Parallelization
工作流:并行化
LLMs can sometimes work simultaneously on a task and have their outputs aggregated programmatically.
LLM 有时可以同时处理一项任务,并以编程方式将其输出聚合起来。
This workflow, parallelization, manifests in two key variations:
这种工作流,即并行化,表现为两种关键变体:
Sectioning: Breaking a task into independent subtasks run in parallel.
分段(sectioning):将一个任务拆分为可并行运行的独立子任务。
Voting: Running the same task multiple times to get diverse outputs.
投票(voting):多次运行同一任务以获得多样化的输出。

The parallelization workflow
并行化(parallelization)工作流
When to use this workflow: Parallelization is effective when the divided subtasks can be parallelized for speed, or when multiple perspectives or attempts are needed for higher confidence results.
何时使用此工作流:当拆分出的子任务可以通过并行化来提升速度,或者需要多种视角或多次尝试以获得置信度更高的结果时,并行化就很有效。
For complex tasks with multiple considerations, LLMs generally perform better when each consideration is handled by a separate LLM call, allowing focused attention on each specific aspect.
对于涉及多个考量因素的复杂任务,当每个考量因素由单独的 LLM 调用处理时,LLM 通常表现更好,因为这样可以聚焦于每个具体方面。
Examples where parallelization is useful:
并行化有用的示例:
Sectioning:
分段:
Implementing guardrails where one model instance processes user queries while another screens them for inappropriate content or requests.
实现护栏(guardrails),即一个模型实例处理用户查询,而另一个实例筛查其中是否有不当内容或请求。
This tends to perform better than having the same LLM call handle both guardrails and the core response.
这通常比让同一个 LLM 调用同时处理护栏和核心响应表现更好。
Automating evals for evaluating LLM performance, where each LLM call evaluates a different aspect of the model’s performance on a given prompt.
自动化评测(eval)以评估 LLM 性能,其中每个 LLM 调用评估模型在给定提示词上表现的不同方面。
Voting:
投票:
Reviewing a piece of code for vulnerabilities, where several different prompts review and flag the code if they find a problem.
审查一段代码是否存在漏洞,其中几个不同的提示词会审查代码,并在发现问题时标记出来。
Evaluating whether a given piece of content is inappropriate, with multiple prompts evaluating different aspects or requiring different vote thresholds to balance false positives and negatives.
评估给定内容是否不当,使用多个提示词评估不同方面,或要求不同的投票阈值以平衡误报和漏报。
Workflow: Orchestrator-workers
工作流:编排器-工作者(Orchestrator-workers)
In the orchestrator-workers workflow, a central LLM dynamically breaks down tasks, delegates them to worker LLMs, and synthesizes their results.
在编排器-工作者工作流中,一个中心 LLM 动态地分解任务,将其委派给工作者 LLM,并综合它们的结果。

The orchestrator-workers workflow
编排器-工作者工作流
When to use this workflow: This workflow is well-suited for complex tasks where you can’t predict the subtasks needed (in coding, for example, the number of files that need to be changed and the nature of the change in each file likely depend on the task).
何时使用此工作流:此工作流非常适合你无法预测所需子任务的复杂任务(例如在编码中,需要更改的文件数量以及每个文件中更改的性质很可能取决于任务)。
Whereas it’s topographically similar, the key difference from parallelization is its flexibility—subtasks aren't pre-defined, but determined by the orchestrator based on the specific input.
尽管在拓扑结构上相似,但与并行化的关键区别在于其灵活性——子任务不是预先定义的,而是由编排器根据具体输入决定的。
Example where orchestrator-workers is useful:
编排器-工作者有用的示例:
Coding products that make complex changes to multiple files each time.
每次对多个文件进行复杂更改的编码产品。
Search tasks that involve gathering and analyzing information from multiple sources for possible relevant information.
涉及从多个来源收集和分析信息以寻找可能相关信息的搜索任务。
Workflow: Evaluator-optimizer
工作流:评估器-优化器(evaluator-optimizer)
In the evaluator-optimizer workflow, one LLM call generates a response while another provides evaluation and feedback in a loop.
在评估器-优化器工作流中,一次 LLM 调用生成响应,而另一次则在循环中提供评估和反馈。

The evaluator-optimizer workflow
评估器-优化器工作流
When to use this workflow: This workflow is particularly effective when we have clear evaluation criteria, and when iterative refinement provides measurable value.
何时使用此工作流:当我们有明确的评估标准,并且迭代改进能带来可衡量的价值时,此工作流尤其有效。
The two signs of good fit are, first, that LLM responses can be demonstrably improved when a human articulates their feedback; and second, that the LLM can provide such feedback.
良好适配的两个标志是:第一,当人类清晰表达其反馈时,LLM 响应能够得到可证明的改进;第二,LLM 能够提供这样的反馈。
This is analogous to the iterative writing process a human writer might go through when producing a polished document.
这类似于人类作者在产出精修文档时可能经历的迭代写作过程。
Examples where evaluator-optimizer is useful:
评估器-优化器有用的示例:
Literary translation where there are nuances that the translator LLM might not capture initially, but where an evaluator LLM can provide useful critiques.
文学翻译,其中存在译者 LLM 起初可能无法捕捉的细微差别,但评估器 LLM 可以提供有用的批评。
Complex search tasks that require multiple rounds of searching and analysis to gather comprehensive information, where the evaluator decides whether further searches are warranted.
复杂的搜索任务需要多轮搜索和分析来收集全面的信息,由评测器决定是否需要进行进一步搜索。
Agents
智能体
Agents are emerging in production as LLMs mature in key capabilities—understanding complex inputs, engaging in reasoning and planning, using tools reliably, and recovering from errors.
随着 LLM 在关键能力上日趋成熟——理解复杂输入、进行推理与规划、可靠地使用工具以及从错误中恢复——智能体正在生产环境中涌现。
Agents begin their work with either a command from, or interactive discussion with, the human user.
智能体开始工作时,要么接受人类用户下达的命令,要么与人类用户进行交互式讨论。
Once the task is clear, agents plan and operate independently, potentially returning to the human for further information or judgement.
一旦任务明确,智能体便独立地规划与执行,必要时会回到人类那里获取更多信息或判断。
During execution, it's crucial for the agents to gain “ground truth” from the environment at each step (such as tool call results or code execution) to assess its progress.
在执行过程中,智能体必须在每一步从环境中获取“真值(ground truth)”(例如工具调用结果或代码执行结果),以评估自身的进展。
Agents can then pause for human feedback at checkpoints or when encountering blockers.
随后智能体可以在检查点处、或在遇到阻碍时暂停,以等待人类反馈。
The task often terminates upon completion, but it’s also common to include stopping conditions (such as a maximum number of iterations) to maintain control.
任务通常在完成后终止,但为了保持控制,也常会设置停止条件(例如最大迭代次数)。
Agents can handle sophisticated tasks, but their implementation is often straightforward.
智能体可以处理复杂的任务,但其实现往往相当直接。
They are typically just LLMs using tools based on environmental feedback in a loop.
它们通常只是 LLM 在一个循环中基于环境反馈来使用工具。
It is therefore crucial to design toolsets and their documentation clearly and thoughtfully.
因此,清晰地、审慎地设计工具集及其文档至关重要。
We expand on best practices for tool development in Appendix 2 ("Prompt Engineering your Tools").
我们将在附录 2(“Prompt Engineering your Tools”)中详述工具开发的最佳实践。

Autonomous agent
自主智能体
When to use agents: Agents can be used for open-ended problems where it’s difficult or impossible to predict the required number of steps, and where you can’t hardcode a fixed path.
何时使用智能体:智能体可用于开放式问题,这类问题难以或无法预测所需的步骤数量,也无法硬编码一条固定路径。
The LLM will potentially operate for many turns, and you must have some level of trust in its decision-making.
LLM 可能会运行很多轮次,你必须对其决策有一定程度的信任。
Agents' autonomy makes them ideal for scaling tasks in trusted environments.
智能体的自主性使其非常适合在可信环境中扩展任务规模。
The autonomous nature of agents means higher costs, and the potential for compounding errors.
智能体的自主性意味着更高的成本,以及错误不断累积的可能性。
We recommend extensive testing in sandboxed environments, along with the appropriate guardrails.
我们建议在沙箱(sandbox)环境中进行广泛测试,并配置适当的防护措施。
Examples where agents are useful:
智能体有用的示例:
The following examples are from our own implementations:
以下示例来自我们自己的实现:
A coding Agent to resolve SWE-bench tasks, which involve edits to many files based on a task description;
一个编码智能体(coding agent),用于解决 SWE-bench 任务;这些任务涉及根据任务描述对多个文件进行编辑;
Our “computer use” reference implementation, where Claude uses a computer to accomplish tasks.
我们的“computer use”参考实现,其中 Claude 使用计算机来完成任务。

High-level flow of a coding agent
编码智能体的高层流程
Combining and customizing these patterns
组合和定制这些模式
These building blocks aren't prescriptive.
这些构建块并非硬性规定。
They're common patterns that developers can shape and combine to fit different use cases.
它们是常见模式,开发者可以对其进行调整和组合,以适应不同的用例。
The key to success, as with any LLM features, is measuring performance and iterating on implementations.
与任何 LLM 功能一样,成功的关键在于衡量性能并迭代实现方式。
To repeat: you should consider adding complexity only when it demonstrably improves outcomes.
再重复一次:只有当复杂性可证明地改善结果时,你才应考虑增加它。
Summary
总结
Success in the LLM space isn't about building the most sophisticated system.
在 LLM 领域,成功不在于构建最复杂的系统。
It's about building the right system for your needs.
而在于构建适合你自身需求的系统。
Start with simple prompts, optimize them with comprehensive evaluation, and add multi-step agentic systems only when simpler solutions fall short.
从简单的提示词开始,用全面的评测对其进行优化,只有当更简单的方案力有不逮时,才加入多步骤的智能体系统。
When implementing agents, we try to follow three core principles:
在实现智能体时,我们尽量遵循三条核心原则:
Maintain simplicity in your agent's design.
保持智能体设计的简洁性。
Prioritize transparency by explicitly showing the agent’s planning steps.
优先保证透明性,明确展示智能体的规划步骤。
Carefully craft your agent-computer interface (ACI) through thorough tool documentation and testing.
通过详尽的工具文档与测试,精心设计你的智能体-计算机接口(agent-computer interface,ACI)。
Frameworks can help you get started quickly, but don't hesitate to reduce abstraction layers and build with basic components as you move to production.
框架可以帮助你快速起步,但当转向生产环境时,不要犹豫,减少抽象层并使用基础组件进行构建。
By following these principles, you can create agents that are not only powerful but also reliable, maintainable, and trusted by their users.
遵循这些原则,你可以创建出不仅强大,而且可靠、可维护并受用户信任的智能体。
Acknowledgements
致谢
Written by Erik S. and Barry Zhang.
作者:Erik S. 和 Barry Zhang。
This work draws upon our experiences building agents at Anthropic and the valuable insights shared by our customers, for which we're deeply grateful.
这项工作借鉴了我们在 Anthropic 构建智能体的经验,以及我们的客户分享的宝贵见解,对此我们深表感谢。
Appendix 1: Agents in practice
附录 1:智能体实践
Our work with customers has revealed two particularly promising applications for AI agents that demonstrate the practical value of the patterns discussed above.
我们与客户合作的工作揭示了 AI 智能体(AI agents)的两个尤其有前景的应用,这些应用展示了上述模式(patterns)的实际价值。
Both applications illustrate how agents add the most value for tasks that require both conversation and action, have clear success criteria, enable feedback loops, and integrate meaningful human oversight.
这两个应用都说明了,对于既需要对话又需要行动、具有明确的成功标准(success criteria)、能够形成反馈循环(feedback loops)并纳入有意义的人工监督(human oversight)的任务,智能体如何创造最大价值。
A. Customer support
A. 客户支持
Customer support combines familiar chatbot interfaces with enhanced capabilities through tool integration.
客户支持将熟悉的聊天机器人界面与通过工具集成(tool integration)增强的能力结合起来。
This is a natural fit for more open-ended agents because:
这自然适合更开放式的智能体(open-ended agents),因为:
Support interactions naturally follow a conversation flow while requiring access to external information and actions;
客户支持交互天然遵循对话流程,同时需要访问外部信息和执行操作;
Tools can be integrated to pull customer data, order history, and knowledge base articles;
可以集成工具来拉取客户数据、订单历史和知识库(knowledge base)文章;
Actions such as issuing refunds or updating tickets can be handled programmatically; and
诸如发放退款或更新工单之类的操作可以通过编程方式处理;以及
Success can be clearly measured through user-defined resolutions.
成功可以通过用户定义的解决结果(resolution)来清晰衡量。
Several companies have demonstrated the viability of this approach through usage-based pricing models that charge only for successful resolutions, showing confidence in their agents' effectiveness.
多家公司已经通过基于用量的定价模式证明了这一方法的可行性——该模式仅对成功解决的结果收费,显示出它们对自身智能体有效性的信心。
B. Coding agents
B. 编码智能体
The software development space has shown remarkable potential for LLM features, with capabilities evolving from code completion to autonomous problem-solving.
软件开发领域已展现出 LLM 功能的巨大潜力,其能力从代码补全演进到自主问题解决。
Agents are particularly effective because:
智能体尤其有效,因为:
Code solutions are verifiable through automated tests;
代码解决方案可通过自动化测试进行验证;
Agents can iterate on solutions using test results as feedback;
智能体可以利用测试结果作为反馈来迭代解决方案;
The problem space is well-defined and structured; and
问题空间定义明确、结构清晰;并且
Output quality can be measured objectively.
输出质量可以客观衡量。
In our own implementation, agents can now solve real GitHub issues in the SWE-bench Verified benchmark based on the pull request description alone.
在我们自己的实现中,智能体现在仅凭 pull request 描述,就能解决 SWE-bench Verified 基准测试中真实的 GitHub issue。
However, whereas automated testing helps verify functionality, human review remains crucial for ensuring solutions align with broader system requirements.
然而,尽管自动化测试有助于验证功能,人工审查对于确保解决方案与更广泛的系统要求保持一致仍然至关重要。
Appendix 2: Prompt engineering your tools
附录 2:为你的工具编写提示词
No matter which agentic system you're building, tools will likely be an important part of your agent.
无论你构建的是哪种智能体系统,工具都很可能是你的智能体中重要的一部分。
Tools enable Claude to interact with external services and APIs by specifying their exact structure and definition in our API.
工具让 Claude 能够与外部服务和 API 交互:在我们的 API 中精确指定这些工具的结构和定义即可。
When Claude responds, it will include a tool use block in the API response if it plans to invoke a tool.
Claude 作出响应时,如果打算调用某个工具,就会在 API 响应中包含一个工具调用块。
Tool definitions and specifications should be given just as much prompt engineering attention as your overall prompts.
工具的定义和规范,应当得到与你整体提示词同等程度的提示词工程关注。
In this brief appendix, we describe how to prompt engineer your tools.
在这篇简短的附录中,我们介绍如何为你的工具做提示词工程。
There are often several ways to specify the same action.
同一个动作往往有多种指定方式。
For instance, you can specify a file edit by writing a diff, or by rewriting the entire file.
例如,你可以通过写一个 diff 来指定文件编辑,也可以通过重写整个文件来指定。
For structured output, you can return code inside markdown or inside JSON.
对于结构化输出,你可以在 markdown 里返回代码,也可以在 JSON 里返回代码。
In software engineering, differences like these are cosmetic and can be converted losslessly from one to the other.
在软件工程中,这类差异只是表面形式,可以无损地从一种转换为另一种。
However, some formats are much more difficult for an LLM to write than others.
然而,有些格式对 LLM 来说比其他格式难写得多。
Writing a diff requires knowing how many lines are changing in the chunk header before the new code is written.
写 diff 要求在写出新代码之前,就知道 chunk header 中有多少行发生了变化。
Writing code inside JSON (compared to markdown) requires extra escaping of newlines and quotes.
在 JSON 里写代码(相比 markdown)需要对换行符和引号做额外的转义。
Our suggestions for deciding on tool formats are the following:
我们对决定工具格式的建议如下:
Give the model enough tokens to "think" before it writes itself into a corner.
给模型足够的 token 来“思考”,以免它把自己逼入死角。
Keep the format close to what the model has seen naturally occurring in text on the internet.
保持格式贴近模型在互联网文本中自然见过的形式。
Make sure there's no formatting "overhead" such as having to keep an accurate count of thousands of lines of code, or string-escaping any code it writes.
确保没有格式“开销”,例如必须准确计算数千行代码,或者对它写出的任何代码进行字符串转义。
One rule of thumb is to think about how much effort goes into human-computer interfaces (HCI), and plan to invest just as much effort in creating good agent-computer interfaces (ACI).
一条经验法则是,思考在人机界面(HCI)上投入了多少精力,并计划在创建良好的智能体-计算机界面(ACI)上投入同样多的精力。
Here are some thoughts on how to do so:
以下是一些关于如何做到这一点的想法:
Put yourself in the model's shoes.
站在模型的角度思考。
Is it obvious how to use this tool, based on the description and parameters, or would you need to think carefully about it?
根据描述和参数,使用这个工具是否显而易见,还是你需要仔细思考?
If so, then it’s probably also true for the model.
如果是这样,那么对模型来说可能也是如此。
A good tool definition often includes example usage, edge cases, input format requirements, and clear boundaries from other tools.
一个好的工具定义通常包括示例用法、边缘情况、输入格式要求,以及与其他工具的明确界限。
How can you change parameter names or descriptions to make things more obvious?
你可以如何修改参数名或描述,让事情更明显?
Think of this as writing a great docstring for a junior developer on your team.
把这想象成在为你团队中的初级开发者写一份出色的 docstring。
This is especially important when using many similar tools.
在使用许多相似工具时,这一点尤其重要。
Test how the model uses your tools: Run many example inputs in our workbench to see what mistakes the model makes, and iterate.
测试模型(model)如何使用你的工具:在我们的 workbench 中运行许多示例输入,看看模型会犯哪些错误,并迭代。
Poka-yoke your tools.
为你的工具做防错设计(Poka-yoke)。
Change the arguments so that it is harder to make mistakes.
修改参数,让犯错变得更难。
While building our agent for SWE-bench, we actually spent more time optimizing our tools than the overall prompt.
在为 SWE-bench 构建我们的智能体时,我们实际上花在优化工具上的时间比花在整个提示词上的还多。
For example, we found that the model would make mistakes with tools using relative filepaths after the agent had moved out of the root directory.
例如,我们发现,在智能体移出根目录后,模型会在使用相对文件路径(relative filepath)的工具上犯错。
To fix this, we changed the tool to always require absolute filepaths—and we found that the model used this method flawlessly.
为了解决这个问题,我们修改了工具,使其始终要求绝对文件路径(absolute filepath)——结果我们发现模型完美地使用了这种方法。