techlist.io

Prompt Chaining

1 posts

Anthropic2024-12-196 min readCurated summary

Building effective AI agents

This post, from Anthropic, argues that the most effective LLM agent systems are built from simple, composable patterns rather than complex frameworks. It distinguishes predefined workflows from autonomous agents and advises starting with the simplest solution, adding complexity only when measured results justify it. The authors cover five workflow patterns, when to use full agents, and how to design tools for them. ### Workflows Versus Agents - Workflows orchestrate LLMs and tools through predefined code paths. - Agents let the LLM dynamically direct its own processes and tool usage, keeping control over how it accomplishes tasks. - Both are grouped as "agentic systems," but the distinction is architecturally important. ### When to Use Agents and Frameworks - Agentic systems often trade latency and cost for better task performance, so the tradeoff must be justified. - Workflows suit well-defined tasks that need predictability, while agents suit open-ended problems that need flexibility and model-driven decisions at scale. - Many applications only need a single LLM call optimized with retrieval and in-context examples. - Frameworks such as the Claude Agent SDK, Strands Agents SDK, Rivet, and Vellum simplify setup, but their abstractions can hide prompts and responses, making debugging harder and tempting developers to add unnecessary complexity. - The authors recommend starting with LLM APIs directly, and understanding the framework's underlying code if you do use one. ### The Augmented LLM Building Block - The foundation of agentic systems is an LLM enhanced with retrieval, tools, and memory, which it can use actively by generating search queries, selecting tools, and deciding what to retain. - Key considerations are tailoring these capabilities to the use case and exposing them through a clear, well-documented interface. - The Model Context Protocol is one way to integrate third-party tools using a simple client implementation. ### Core Workflow Patterns - **Prompt chaining:** Decomposes a task into sequential LLM calls, with optional programmatic "gates" between steps. It trades latency for accuracy. Examples include writing marketing copy and then translating it, or drafting an outline, checking it against criteria, and then writing the document. - **Routing:** Classifies an input and sends it to a specialized follow-up task, which separates concerns and allows specialized prompts. Examples include directing customer service queries to different processes, or sending simple questions to a smaller model like Claude Haiku 4.5 and hard ones to Claude Sonnet 4.5. - **Parallelization:** Runs LLM calls simultaneously and aggregates the outputs programmatically, in two variations: - *Sectioning* splits a task into independent subtasks, such as running a guardrail model alongside the core response model, or evaluating different aspects of a model's output separately. - *Voting* runs the same task multiple times for diverse outputs, such as several prompts reviewing code for vulnerabilities. - **Orchestrator-workers:** A central LLM dynamically breaks down a task, delegates subtasks to worker LLMs, and synthesizes the results. Unlike parallelization, subtasks aren't predefined. This suits coding tasks where the required file changes depend on the input, and multi-source search. - **Evaluator-optimizer:** One LLM generates a response while another evaluates it and provides feedback in a loop. This works best when evaluation criteria are clear and iterative refinement measurably helps, as with literary translation or multi-round search. ### Agents in Production - Agents begin with a command or interactive discussion with the user, then plan and operate independently, checking "ground truth" from the environment, such as tool results or code execution, at each step. - They can pause for human feedback at checkpoints or blockers, and should include stopping conditions, such as a maximum number of iterations. - Structurally, agents are often just LLMs using tools in a loop, so the design of toolsets and their documentation is critical. - Agents suit open-ended problems where the number of steps is unpredictable, but their autonomy raises costs and the risk of compounding errors, so extensive sandbox testing and guardrails are essential. - Anthropic's own examples include a coding agent for SWE-bench tasks and the "computer use" reference implementation. ### Combining Patterns and Guiding Principles - The patterns are not prescriptive. They are starting points to shape and combine for specific use cases. - Success depends on measuring performance and iterating. Added complexity should only be kept when it demonstrably improves outcomes. - The authors suggest three principles for agents: keep the design simple, make planning steps transparent, and carefully craft the agent-computer interface (ACI) through thorough tool documentation and testing. ### Agents in Practice - **Customer support:** Suits agents because conversations need external data (customer records, order history, knowledge base articles), actions like refunds can be handled programmatically, and success is measurable through user-defined resolutions. Some companies charge only for successful resolutions. - **Coding agents:** Suit agents because solutions can be verified with automated tests, agents can iterate on test feedback, and output quality can be measured objectively. Anthropic's agent can solve real GitHub issues from SWE-bench Verified based on the pull request description alone, though human review remains essential. ### Prompt Engineering Your Tools - Tool definitions deserve as much prompt-engineering effort as the main prompts, since Claude invokes tools through structured tool-use blocks in the API. - Some formats are much harder for LLMs to produce than others. For example, diffs require counting lines in chunk headers before writing code, and code inside JSON requires escaping newlines and quotes. - Format guidelines: - Give the model enough tokens to reason before committing to an output. - Stick to formats common in internet text. - Avoid formatting overhead like line counting or string escaping. - Designing the agent-computer interface deserves as much care as the human-computer interface. Put yourself in the model's shoes, write documentation like a docstring for a junior developer (with examples, edge cases, and input requirements), test the tools on many inputs, and "poka-yoke" them so mistakes are harder to make. - In the SWE-bench agent, the team spent more time on tools than on the prompt. Requiring absolute file paths eliminated errors that occurred after the agent changed directories. ### Practical Takeaway Start with the simplest prompts and direct API calls, and measure results carefully. Add workflows or autonomous agents only when simpler approaches fall short, and invest heavily in tool design and testing, since they largely determine whether an agent succeeds.