Trail content
9 minEnglish narration, Portuguese subtitlesPodcast (RSS)
Chapters
  1. 0:00Opening
  2. 0:46The idea
  3. 1:05Before and after
  4. 1:35A real case
  5. 2:41A challenge
  6. 3:24Why it works
  7. 4:28Safe mechanics
  8. 5:24The traps
  9. 7:25Practice
  10. 8:05Wrap-up
Transcript

Opening

A team builds a market report generator with five agents. A senior researcher, a data analyst, a writer, a reviewer, and an editor in chief. In the demo, the diagram looks impressive.

In production, only the researcher has a tool. The other four receive text, rewrite it, and pass it on. The report takes several minutes, costs several times what it should, and sometimes arrives with a number that differs from the source. Nobody knows in which of the five handoffs it changed. You met this smell in the last lesson. Today you'll collapse it, without losing what was actually useful.

The idea

Merge the agents that have no technical reason to be separate into a single agent, with the right tools, and a prompt that describes the task, not the job titles. And keep separate only what isolates context, restricts tools, or provides an independent review that was measured.

Before and after

Before, five roles in a queue. Four of them rewriting text.

After, the request reaches a single agent. And its prompt describes the task and the output format. Not characters.

The agent researches as many times as it needs, with search and page fetching.

And the review that mattered, every claim has a source, became a check in code.

The report comes out with a fraction of the calls.

A real case

Let's follow one number through the old design's five handoffs. The names and numbers are hypothetical.

The researcher finds, on a page: the contactless payments market moved one point two four billion dollars in twenty twenty-five, according to association X.

The analyst writes: about one point two billion, last year.

The writer: over a billion dollars a year.

The reviewer keeps it, with improved wording.

And the editor: moves billions every year.

No agent got it wrong on purpose. Each one summarized a little, and dropped what looked like a detail. Added up, the handoffs lost the source, the year, and the order of magnitude. And each intermediate agent read the previous text in full, and wrote it again in full. Four reads and four rewrites of the same content, to reach a text the researcher already had the data to write. In the new design, the agent writes the report once, with the sources right there in its own context.

A challenge

Here's one for you. In the old system, the final report said billions every year, where the source said one point two four billion. What's the most likely cause?

Each free text handoff summarized a little, and the precision eroded, step by step. Each agent rewrites without seeing the original source, and small losses add up. It wasn't the editor hallucinating. The editor only received text that already said over a billion. And a sixth agent to check the numbers would just be one more handoff, with the same loss. The check that works lives in code, against the recorded source.

Why it works

For a model, splitting into agents only helps for technical reasons. When agents share everything and only rewrite, each handoff costs a full read, and risks dropping details. When you merge, the model does in one reasoning sequence what it used to do in five calls, without rereading its own work as if it were someone else's. And what used to be review was often a verifiable criterion, which works better in code.

But splitting isn't always wrong. When several subagents research broad questions in parallel, each one reading a lot of material in its own context, and returning only the essentials, it can pay off. Even at a much higher token bill.

And when everyone works on the same material, like the same code, agents that can't see each other's context make decisions that contradict each other. So the rule is simple. Split when there's heavy, independent reading, a sensitive tool, or a measured review. Merge when everyone works on the same material.

Safe mechanics

Now the mechanics. Step zero, measure. Run the eval on the current system. Quality, cost per task, latency. And save each intermediate agent's output.

Compare each agent's input and output. If the output is nearly the same as the input, that agent is a candidate to go.

Try removing one at a time, and run the eval. The ones whose removal doesn't change quality, go.

Write the single agent's prompt, with the task, the format, and any useful rules the removed roles carried.

And turn the verifiable reviews into code. Sources, format, length, banned terms. Then keep separate only what has a reason, give the merged agent a budget, and measure again. Same or better quality, lower cost and latency.

The traps

Now the common mistake. Throwing out the rules along with the roles. The reviewer's prompt sometimes held a rule worth keeping, buried among character lines. Never cite data older than two years.

Say that in the eval, that rule caught problems in six percent of cases. What do you do with it? Not keep the reviewer as a whole agent. A date is verifiable without a model. And not drop it either, hoping the single agent will look for recent data on its own. Turn it into a code check on each source's date, and state the rule in the prompt, so the agent looks for recent sources from the start.

So before you remove each role, copy its concrete instructions into a list. Drop the character ones, like you are a demanding reviewer with twenty years of experience. And move the verifiable ones into the prompt, or into code.

The risks. Context overflow. If one of the agents isolated heavy reading, merging fills the context. Keep that one as a subagent.

Losing a review that mattered. Say you removed the analyst, and the eval didn't change. Then you removed the reviewer, and the score dropped on cases with tables. Don't put both back. The analyst can go. Find out what the reviewer fixed in tables, and pick the cheapest home for it. A rule, a check in code, or a separate evaluator.

Merging into a mega agent. If, while merging, you also give the agent the tools to email customers and publish on the website, you created the opposite smell. Merge only what's decorative. Tools with side effects keep their own scope, and approval.

And pushback from whoever designed it. Five agents with job titles look more sophisticated on a slide. Bring the eval comparison. Same quality, fewer calls, less time.

Practice

Let's practice. Five agents that answer public tenders. Which would you merge, and which would you keep separate?

The reader stays separate. It isolates the reading of forty documents. The summarizer goes. The reader can already return structured requirements.

The writer and the formatter become one. Formatting is part of writing, or the format becomes a template in code. And the compliance reviewer stays separate, if the eval shows it catches problems. It reviews with a rubric, and without the writer's reasoning.

Wrap-up

So here's the idea to take with you. Measure, remove one agent at a time, and keep only what moves the score. Copy the useful rules before deleting any role, and turn the verifiable ones into code. You end up with one agent that does the work, and a few separations you can explain.

In the next lesson, we'll look at the separation that does pay, when it's done right: the evaluator optimizer. See you there.

RefactoringMoving control

Collapse Agents

Fundir Agentes

Collapse Agents is an AI refactoring for LLM systems, in the Moving control group. Merge agents that only pass text along into one agent with the right tools.

Element
14/35
Level
Intermediate
Read
5 min
Updated
On this page 8
  1. 01Problem
  2. 02Solution
  3. 03Before and after
  4. 04Why it works
  5. 05Safe mechanics
  6. 06Risks
  7. 07Challenge
  8. 08Check your understanding
  9. ··Quick answers

Problem

Several agents only pass text along, without truly dividing the work, and cost multiplies. These are the Decorative Multi-Agent and Subagent Recursion smells.

A team builds a market report generator with five agents: Senior Researcher, Data Analyst, Writer, Reviewer, and Editor-in-Chief. In the demo, the diagram looks impressive. In production, only the Researcher has a tool. The other four receive text, rewrite it, and pass it on.

The report takes several minutes, costs several times what it should, and sometimes arrives with a number that differs from what the source said. Nobody knows in which of the five handoffs it changed.

Solution

Merge agents that have no technical reason to be separate into a single agent with the right tools and a prompt that describes the task, not the job titles. Keep separate only what isolates context, restricts tools, or provides a measured independent review.

Before and after

-agents:
-  - role: Senior Researcher       tools: [web_search]
-  - role: Data Analyst            tools: []
-  - role: Writer                  tools: []
-  - role: Reviewer                tools: []
-  - role: Editor-in-Chief         tools: []
-flow: sequential
+agent:
+  system: |
+    You produce market reports. Research with web_search, record
+    every claim with its source, and write the report in the structure below.
+    Before finishing, check that every claim has a source.
+  tools: [web_search, fetch_page]
+  output: Report   # schema with sections and citations
+  budget: { steps: 20 }
+checks:
+  - every_claim_has_source   # in code, after the response

Before: five roles in a queue, four of them rewriting text.

flowchart LR
  P(["1. Researcher"]):::llm --> A(["2. Analyst"]):::llm
  A --> R(["3. Writer"]):::llm
  R --> V(["4. Reviewer"]):::llm
  V --> E(["5. Editor"]):::llm

After: one agent with tools and a check in code.

flowchart LR
  T(["1. Request"]):::io --> A(["2. Report agent"]):::llm
  A --> W["3. web_search, fetch_page"]:::tool
  W --> A
  A --> C["4. Source check in code"]:::tool
  C --> OUT(["5. Report"]):::io
  1. The request reaches a single agent.
  2. The prompt describes the task and the output format, not characters.
  3. The agent researches as many times as it needs.
  4. The “review” that mattered (every claim has a source) became a check in code.
  5. The report comes out with a fraction of the calls.

Worked example: the telephone game

Follow one data point through the old design’s five handoffs. The Researcher finds on a page: “the contactless payments market moved R$ 1.24 billion in 2025, according to association X”. The names and numbers here are hypothetical.

Agent What comes out
Researcher R$ 1.24 billion in 2025, according to association X
Analyst about R$ 1.2 billion last year
Writer over a billion reais a year
Reviewer over a billion reais a year (“improved” wording)
Editor moves billions every year

No agent got it wrong on purpose. Each one summarized a little and dropped what looked like detail. Added up, the handoffs lost the source, the year, and the order of magnitude.

Now the cost math, with illustrative numbers. Suppose each intermediate agent reads about 4,000 tokens from the previous one and writes another 3,000. That is four full reads and four full rewrites of the same content, just to reach a text the Researcher already had the data to write. In the new design, the agent writes the report once, with the sources in its own context.

Check +20 XP on first tryIn the old system, the final report said 'billions every year' where the source said R$ 1.24 billion. What is the most likely cause?

Why it works

For a model, splitting into agents helps only for technical reasons: one agent’s context would get too full, one agent’s tools should not be with another, or an independent look improves the result. Human job titles are not one of those reasons. When agents share everything and only rewrite, each handoff costs a full read and risks dropping details.

When you merge, the model does in one reasoning sequence what it used to do in five calls, without rereading its own work as if it were someone else’s. And what was “review” was often a verifiable criterion, which works better in code.

The target is a ReAct Loop with Tool Use: one agent that reasons, calls a tool, reads the result, and decides the next step.

When splitting pays

Splitting is not always wrong. Anthropic reported, in its post on Claude’s multi-agent research system (June 2025), that several subagents researching in parallel helped on broad questions. The same post says this design spends far more tokens than a regular chat. The split paid off because each subagent read a lot of material in its own context and returned only the essentials.

That same month, Cognition published “Don’t Build Multi-Agents”, arguing the opposite for tasks like writing code: agents that cannot see each other’s context make decisions that contradict each other. Both positions fit this page’s rule. Split when there is heavy, independent reading. Merge when everyone works on the same material.

Reason to keep separate Example
Isolate heavy reading A subagent reads 40 PDFs and returns requirements in a schema
Restrict tools Only the payments agent can call transfer
Measured independent review An evaluator with a rubric that the eval shows catches errors
Parallelize independent work Five searches on different topics at the same time

Safe mechanics

  1. Measure. Run the eval on the current system: quality, cost per task, and latency. Also save each intermediate agent’s output.
  2. Compare each agent’s input and output. If the output is nearly the same as the input, the agent is a candidate to go.
  3. Try removing one at a time and run the eval. Agents whose removal does not change quality go.
  4. Write the single agent’s prompt with the task, the format, and any useful rules the removed roles carried.
  5. Turn verifiable reviews into code: sources, format, length, banned terms.
  6. Keep separate what has a reason: heavy reading (Extract Subagent), review with a rubric (Evaluator-Optimizer), sensitive tools (a restricted agent).
  7. Give the merged agent a budget.
  8. Measure again. Same or better quality, lower cost and latency.

Common mistake: throwing out the rules with the roles

The “Reviewer” prompt sometimes held a rule worth keeping, buried among character lines: “never cite data older than two years”. If you delete the agent without reading its prompt, the rule disappears.

Before removing each role, copy its concrete instructions into a list. Drop the character ones (“you are a demanding reviewer with 20 years of experience”) and move the verifiable ones into the single prompt or into a check in code.

Check +20 XP on first tryWhile merging, you notice the Reviewer rejected reports with data older than two years. In the eval, that rule caught problems in 6% of cases. What do you do with it?

Risks

  • Context overflow. If one of the agents isolated heavy reading, merging fills the context. Keep that one as a subagent.
  • Losing a useful review. Sometimes a reviewer made a difference in rare cases. An eval with hard cases shows this.
  • A prompt that becomes a Mega Agent. Merging everything, including tools with side effects, creates the opposite smell, the Mega Agent. Merge only what is decorative.
  • Pushback from whoever designed it. Five agents with job titles look more sophisticated on a slide. Bring the eval comparison: same quality, fewer calls, less time.

Challenge

Which of these agents would you merge, and which would you keep separate?

1. Reader: reads 40 tender PDFs and extracts requirements (tool: read_pdf)
2. Summarizer: summarizes the Reader's output into bullet points
3. Writer: writes the proposal from the bullet points
4. Compliance reviewer: checks the proposal against the requirements, with a rubric
5. Formatter: applies the company's document template
Show solution

The Reader stays separate: it isolates the reading of 40 PDFs. The Summarizer goes: the Reader can already return structured requirements. The Writer and the Formatter become one, because formatting is part of writing, or the format becomes a template in code. The Compliance reviewer stays separate if the eval shows it catches problems, because it reviews with a rubric and without the writer’s reasoning.

Reader (subagent, read_pdf) -> requirements in a schema
Writer (agent) -> proposal in the template
Reviewer (evaluator with rubric) -> list of unmet requirements

Check your understanding

Check +20 XP on first tryWhich of these reasons, on its own, does NOT justify keeping an agent separate?

Check +20 XP on first tryYou removed the Analyst from the pipeline and the eval did not change. You removed the Reviewer and the score dropped on cases with tables. What is the next step?

Check +20 XP on first tryAfter merging five agents into one, you also gave it the tools to email customers and publish on the website, which used to live in another flow. What is the risk?

Quick answers

What is Collapse Agents?

Collapse Agents is an AI refactoring for LLM systems, in the Moving control group. Merge agents that only pass text along into one agent with the right tools.

What problem does Collapse Agents solve?

It treats Subagent Recursion and Decorative Multi-Agent.

Which pattern does Collapse Agents lead to?

The ReAct Loop and Tool Use design pattern.

Go further

Relations

Target patterns 2
Shows up in scenarios 1
Part of the tracks 1
Search