How to Chain Agents Without Breaking Things
The simple rules for making multiple AI agents work together like a smooth team.
What Is Agent Chaining
An AI agent is a small program that uses a large language model to think, look things up, and take actions on its own. A single agent can answer a question, call an API, or write a paragraph. But it gets confused when you ask it to do too many different things at once.
"Chaining" agents means connecting them so the work passes from one to the next — like stations on an assembly line. Agent A finishes a job, hands its result to Agent B, which does the next step, then passes to Agent C. Each agent stays focused on one small, well-defined task.
A common example: a "scout" agent that finds web pages about a topic, then a "summarizer" agent that condenses them, then a "writer" agent that turns those summaries into a final report. The output of one becomes the input of the next.
Why One Big Agent Isn't Enough
A single super-smart agent that does everything sounds appealing, but it has real problems in practice. Its context window fills up with too much information. When something goes wrong, you have to figure out which step failed inside a giant mess. You can't easily swap out one piece for a better one. And costs climb because you send everything to the most powerful model every time.
Chained agents fix these problems. Each one is small, focused, and easy to test. They work like a relay race: each runner is specialized, and the handoff is where things can either go right or go wrong.
💡 Key Insight
The biggest mistake is making one mega-agent that does everything. Chained agents are easier to debug, cheaper to run, and more reliable — but only if you design the handoffs carefully. The handoff is the chain's weakest link.
Five Rules for a Chain That Doesn't Break
Most broken agent chains fail for the same handful of reasons. Follow these five rules and yours will stay standing:
- Break the work into small steps. Don't make one agent do five things. Find the natural hand-off points in your task. If you can't describe what one agent does in one short sentence, split it.
- Give each agent a single clear job. A short system prompt beats a long one. "You summarize web pages in two sentences" is better than "You do research, summarization, translation, and writing."
- Define the handoff format. Both agents need to agree on what the message between them looks like. A small JSON object with clear fields beats a free-form paragraph — the next agent can read the fields it needs and ignore the rest.
- Check the handoff. Before passing the result on, validate it. Did the previous agent actually finish? Is the output the right shape? Is anything missing? Catch errors at the handoff, not ten steps later.
- Make retries possible. When step 3 breaks, you should be able to re-run just step 3 — not the entire chain from the beginning. Save intermediate outputs so you can pick up where things went wrong.
A Two-Agent Chain in Python
Here's a simple example of two agents chained together. The first agent summarizes an article. The second agent takes that summary and translates it into Spanish. Each one does one job, and the summary variable is the handoff between them.
# A tiny two-agent chain: summarize, then translate. import json from openai import OpenAI client = OpenAI() def run_agent(system_prompt: str, user_prompt: str) -> str: # Step 1+2: one focused agent, one clear job. response = client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}, ], ) return response.choices[0].message.content # --- Agent 1: Summarizer --- def summarize(article: str) -> str: return run_agent( "You summarize articles in exactly 2 short sentences.", f"Summarize this article:\n\n{article}", ) # --- Agent 2: Translator --- def translate_to_spanish(summary: str) -> str: return run_agent( "You translate English text into natural Spanish.", f"Translate this summary to Spanish:\n\n{summary}", ) # --- The chain: summarize → translate --- article = "OpenAI was founded in 2015 with the goal of building safe AI..." summary = summarize(article) # Agent 1's output spanish = translate_to_spanish(summary) # Agent 2 uses Agent 1's output print("Summary:", summary) print("Spanish:", spanish)
The key thing to notice is the summary variable — that string is the handoff. Agent 2 doesn't know or care about the original article. It only sees what Agent 1 gave it. That separation is what makes the chain reliable: you can swap out the summarizer for a better one, or add a third agent that polishes the translation, without rewriting the rest of the code.
Knowledge Check
Test what you learned with this quick quiz.