This article mainly discusses Anthropic's annual summary and design principles for building large language models (LLMs) and agents. The article was written by Anthropic and its content includesCharacteristics of successful solutions、Definition of agents、When to use agents、Use of frameworks、Building blocks and workflows、Workflow patterns、Application scenarios of agentsas well aspractical casesand other sections. The article emphasizes the importance of simplicity, transparency, and a well-designed agent-computer interface (ACI), and provides best practices for tool development and detailed information on prompt engineering for plugin tools. Based on the above content, Anthropic shares how to build valuable agents and provides practical suggestions for developers.
Agent Building Guide
December 20, 2024
Over the past year, Anthropic has collaborated with multiple industry teams to build large language model (LLM) agents. The most successful solutions did not use complex frameworks or specialized software packages. Instead, they were built using simple, composable modules. In this article, Anthropic shares the lessons learned from working with customers and building agents itself, and provides developers with relevant suggestions on how to build effective agents.
What is an Agent?
What is an Agent? "Agent" can have multiple definitions. Some customers define an Agent as a fully autonomous system that can operate independently for a long time and use various tools to complete complex tasks. Others describe an Agent as following a predefined workflow and being more prescriptive. At Anthropic, all these variants are categorized asagentic systems, but inworkflowsandagentsan important architectural distinction is drawn between:
- Workflowsare systems in which LLMs and tools are orchestrated based on predefined code paths.
- Agentsare systems in which the LLM dynamically plans its own process and tool use, and can control how tasks are completed.
Below, we will explore these two types of agentic systems in detail. In Appendix 1 ("Agents in Practice"), two areas where customers have found these systems to be particularly valuable are introduced.
When (and When Not) to Use Agents?
When building LLM applications, it is recommended to look for the simplest possible solution and only increase complexity when needed. This may mean not building an agentic system at all. Agentic systems usually incur latency and cost for better task performance, and it is necessary to consider whether this trade-off makes sense.
When more complexity is needed, workflows provide predictability and consistency for well-defined tasks, while Agents are a better choice when large-scale flexibility and model-driven decision-making are needed. However, for many applications, optimizing a single LLM call, together with retrieval and contextual examples, is usually sufficient.
When and How to Use Frameworks?
There are many frameworks that can make agentic systems easier to implement, including:
- LangChain's LangGraph;
- Amazon Bedrock'sAI agent framework;
- Rivet, a drag-and-drop GUI LLM workflow builder;
- Vellum, a GUI tool for building and testing complex workflows.
These frameworks simplify the operational process by streamlining standardized underlying tasks such as calling LLMs, quickly writing and parsing related tool plugins, and chaining calls. However, they create additional layers of abstraction, which can obscure the underlying prompts and responses, making debugging more difficult. They may add complexity to work that developers could accomplish with a simple setup.
We recommend that developers first use the LLM API directly: many common patterns can be implemented in just a few lines of code. If you do want to use a framework, make sure you understand the underlying code. Incorrect assumptions about the underlying content are a common source of customer errors.
See ourofficial manualfor some example implementations.
Building Blocks, Workflows, and Agents
In this section, we will explore common patterns of agent systems encountered in production. We will start with the basic building block—the augmented LLM—and gradually increase complexity, from simple compositional workflows to autonomous agents.
Building Block: The Augmented LLM
The basic building block of an agent system is an LLM enhanced with capabilities such as retrieval, tools, and memory. Today's models can use these capabilities automatically—autonomously generating search queries, selecting appropriate tools, and deciding which information to retain.
We recommend focusing on two key aspects of implementation: customizing for specific use cases based on the usage scenario, and ensuring that the LLM is provided with a simple and well-documented interface. Although there are many ways to implement these enhancements, one approach is to use Anthropic's recently released modelContext Protocol(Model Context Protocol), which enables developers, through a simpleclient implementation, to integrate with various third-party tool ecosystems that use this protocol.
For the remainder of this article, we will assume that each LLM call has access to these enhanced capabilities.
Workflow: Prompt Chaining Workflow
Prompt chaining breaks a task into a series of steps, where each LLM call processes the output of the previous call. You can add programmatic checks (see the "gate" in the figure below) at any intermediate step to ensure the process proceeds as expected.
- Applicable Scenarios: This workflow is ideal for scenarios where a task can be easily and clearly decomposed into fixed subtasks. The main purpose is to trade off response speed and higher accuracy by making each LLM call easier.
- Examples Where Prompt Chaining Applies:
- Generate marketing copy, then translate it into different languages.
- Write an outline for a document, check whether the outline meets certain criteria, and then write the document based on the outline.
Workflow: Routing Workflow
Routing classifies the input and directs it to subsequent specialized tasks. The workflow allows separation of concerns and the construction of more specialized prompts. Without this workflow, optimization for one type of input may harm performance on other inputs.
- Applicable Scenarios: Routing is suitable for complex tasks that have clear categories, are suitable for separate handling, and whose classification can be handled accurately by an LLM or a more traditional classification model/algorithm.
- Applicable Examples:
- Route different types of customer service queries (general questions, refund requests, technical support) to different downstream processes, prompts, and tools.
- Route simple/common questions to smaller models, such as Claude 3.5 Haiku, and route difficult/unusual questions to more powerful models, such as Claude 3.5 Sonnet, to optimize cost and speed.
Workflow: Parallelization Workflow
LLMs can sometimes complete a task simultaneously, and their outputs can be aggregated programmatically. This workflow is reflected in two key variants:
- Sectioning: Break a task into independent subtasks and run them in parallel.
- Voting: Run the same task multiple times to obtain different outputs.
- Applicable Scenarios: Parallelization is effective when the divided subtasks can be parallelized to improve speed, or when multiple perspectives need to be tried to obtain more reliable results. For complex tasks with multiple considerations, LLMs perform better when each consideration is handled with a separate LLM call.
- Applicable Examples:
- Sectioning:
- Security guardrails, where one model handles the user query while another screens for inappropriate content or requests. This usually performs better than having the same LLM call handle both security guardrails and the core response at the same time.
- Automated evaluation is used to evaluate an LLM's performance under a given prompt, with each LLM used for a different aspect of evaluating model performance.
- Voting:
- Review code for vulnerabilities; if issues are found, multiple different prompts review and flag the code.
- Evaluate whether given content is inappropriate, using multiple prompts to assess different aspects or set different voting thresholds to balance testing accuracy.
- Sectioning:
Workflow: Orchestrator-Workers Workflow
In the orchestrator-workers workflow, a central LLM dynamically breaks down tasks, delegates them to worker LLMs, and synthesizes their results.
- Applicable Scenarios: Suitable for complex tasks where the required subtasks cannot be predicted (for example, in coding, the number of files that need to be changed and the changes within each file may depend on the task itself). Although its flowchart is very similar to Parallelization, the key difference is that it is more flexible - the subtasks are not predefined, but are determined by the Orchestrator based on the specific input.
- Applicable Examples:
- Coding products that make complex changes to multiple files each time.
- Search tasks that involve collecting and analyzing information from multiple sources to find potentially relevant information.
Workflow: Evaluator-Optimizer Workflow
In this workflow, one LLM call is responsible for generating a response, while another provides evaluation and feedback in a loop.
- Applicable Scenarios: This workflow is particularly effective when there are clear evaluation criteria and the value of iterative refinement can be measured. Good adaptability has two signs: first, when humans express feedback, the LLM's response can improve noticeably; second, the LLM is able to provide such feedback. This is similar to the iterative writing process that a human writer may experience when producing a polished document.
- Applicable Examples:
- Literary translation, where there are nuances that the translation LLM may not initially capture, but the evaluation LLM can provide useful suggestions for improvement.
- Complex search tasks that require multiple rounds of searching and analysis to collect comprehensive information, where the LLM responsible for evaluation decides whether further searching is needed.
Agent
As LLMs mature in key capabilities such as understanding complex inputs, reasoning and planning, using tools, and recovering from errors, agents are beginning to emerge in production.
The work of an agent begins with a command from a human user, or with an interactive discussion with a human user. Once the task is clear, the agent independently plans and acts, and may need to ask the human back for more information or judgment. During execution, it is crucial for the agent to obtain the "ground truth" from the environment at each step (such as tool call results or code execution) to assess its progress. The agent can then pause to obtain human feedback when it encounters an obstacle. Tasks usually terminate upon completion, but often also include termination conditions (such as a maximum number of iterations) to maintain control.
Agents can handle complex tasks, but their implementation is usually simple. They are usually just an LLM using tools in a loop based on environmental feedback. Therefore, a well-designed and clear toolset and documentation are crucial. Appendix 2 ("Prompt Engineering your Tools") details the best practices for tool development.
(Autonomous agents)
- Applicable scenarios: Agents can be used for open-ended problems where the number of steps required is difficult or impossible to predict, and where a fixed path cannot be prescribed. The LLM may run multiple loops, and you must have a certain degree of trust in its decision-making ability. The autonomy of agents makes them particularly ideal for executing tasks in trusted environments. The autonomous nature of agents means higher costs and the potential for accumulating errors. Extensive testing in a sandbox environment is recommended, with appropriate safeguards in place.
- Applicable examples: The following are some examples from our own implementation:
- A coding agent for solvingSWE-bench tasks, which involve editing multiple files based on the task description;
- "Computer use" reference manual, in which Claude uses a computer to complete tasks.
(Advanced workflow for coding agents)
Composition and customization
These paradigms are not strictly prescribed. They are common patterns that developers can build and combine to suit different use cases. As with any LLM feature, the key to success is measuring performance and iterating on implementation. To repeat: complexity should only be considered when it clearly improves results.
Summary
Success in the LLM field is not about building the most complex system. It is about building the right system for the need. Start with simple prompts, optimize with comprehensive evaluations, and only add multi-step agent systems when simpler solutions are insufficient.
When implementing agents, we try to follow three core principles:
- Ensure the agent design issimple。
- Prioritizeby clearly showing the agent's planning steps。
- Through comprehensive tools,documentation, and testing,, carefully craft your agent-computer interface (ACI) interface.
Frameworks can help you get started quickly, but when moving to production, do not hesitate to reduce abstraction layers and build as much as possible with basic components. By following these principles, you can create agents that are not only powerful but also reliable, maintainable, and trusted by users.
Acknowledgments
Written by Erik Schluntz and Barry Zhang. This work draws on our experience building agents at Anthropic and valuable insights shared by our customers, for which we are deeply grateful.
Getthe original PDF of the "Agent Building Guide", scan the code to follow and reply:241222
Appendix 1: Agents in Practice
Our collaboration with customers revealed two applications where AI agents are particularly promising, demonstrating the practical value of the above patterns. Both applications illustrate that agents are most valuable for tasks that require conversation and action, have clear success criteria, can provide feedback loops, and incorporate valuable human oversight.
A. Customer Support
Customer support combines the familiar chatbot interface and enhances its capabilities through tool integration. This is a natural scenario for more open-ended agents because:
- It follows conversational flows, interactions are natural, and it simultaneously requires access to external information and actions;
- Tools can be integrated to retrieve customer data, order history, and knowledge base articles;
- Actions such as issuing refunds or updating tickets can be handled programmatically;
- Through user-defined solutions, it can be clearly measured whether the agents resolved the issue.
Some companies have demonstrated the viability of this approach through usage-based pricing models that charge only for successful resolutions, showing confidence in the effectiveness of their agents.
B. Coding Agents
The field of software development shows significant potential for LLM capabilities, with functions evolving from code completion to autonomous problem solving. Agents are particularly effective because:
- Code problem solving can be verified through automated testing;
- Agents can use test results as feedback to iterate solutions;
- Problem definitions are clear and structured;
- Output quality can be measured objectively.
In our own implementation, the agent is based on theSWE-bench verifiedbenchmark and can independently solve real GitHub issues. However, although automated testing helps verify functionality, human review is still crucial to ensure that solutions meet broader system requirements.
Appendix 2: Prompt Engineering Your Tools
No matter which agent system you are building, tool plugins may 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. When Claude responds, if it plans to call a tool, it will include atool use blockin the API response. Tool definitions and specifications should receive the same prompt engineering attention as the overall prompt. In this brief appendix, we describe how to prompt engineer tools.
There are usually several ways to specify the same operation. For example, file edits can be specified by writing a diff or rewriting the entire file. For structured output, code can be returned in Markdown or JSON. In software engineering, these differences are superficial and can be converted losslessly from one format to another.
However, some formats are harder for LLMs to write than others. Writing a diff requires knowing how many lines are changing in the block header before writing the new code. Writing code in JSON (compared with Markdown) requires additional escaping of newline characters and quotes.
Our recommendations for deciding on tool format are as follows:
- Give the model enough tokens to "think" before it goes down a dead end.
- Keep the format close to text that naturally appears on the internet.
- Ensure there is no formatting "overhead", such as having to accurately count thousands of lines of code, or string-escape any code it writes.
One lesson is that as much effort as is put into the human-computer interface (HCI) should be put into creating a good agent-computer interface (ACI). Here are some ideas for how to do this:
- Put yourself in the model's shoes. Based on the description and parameters, is it obvious to use this tool, or does it require careful thought? A good tool definition usually includes example usage, edge cases, input format requirements, and clear boundaries with other tools.
- How can you change parameter names or descriptions to make the task more obvious? Think of this as writing readable documentation for a junior developer on your team. This is especially important when using many similar tools.
- Test how the model uses your tool: run multiple example inputs in ourworkbenchto see what mistakes the model makes, and iterate.
- Implementmistake-proofing measuresfor your tool. Change the parameters to make it harder to make mistakes.
When buildingSWE-benchagents, Anthropic actually spends more time optimizing tools than optimizing the overall prompt. For example, Anthropic found that the model makes mistakes when using tools with relative file paths, especially after the agent moves out of the root directory. To solve this problem, the tool was changed to always require absolute file paths, and we found that the model used this approach perfectly.