Sub topic 17
Multi-agent designs and their trade-offs
This chapter assumes you can write a tool-calling agent loop, as in the earlier chapters on tool calling and the agent loop. It also assumes you know how context windows limit what a model can see. A multi-agent design splits one task across several model-driven loops. Each loop has its own prompt, tools, and history.
Why split an agent
One agent with many tools and a long history can fail in predictable ways. Its prompt grows crowded, it picks the wrong tool among twenty, and its context fills with details that matter only to one subtask. Splitting the work addresses these problems:
- Focus: each agent gets a short prompt and a few tools.
- Context isolation: a subagent can read many files and return only a short summary.
- Permissions: a read-only agent never needs write tools.
Splitting also has costs. Each agent makes its own model calls, so cost and latency rise. Information is lost at every handoff. Failures are harder to trace across agents.
Common patterns
| Pattern | How it works | Good for |
|---|---|---|
| Orchestrator and workers | A lead agent breaks the goal into subtasks and calls worker agents as if they were tools | Tasks that decompose cleanly |
| Pipeline | Agent A's output feeds agent B, then C | Fixed stages such as draft, then review |
| Critic | One agent produces work, another checks it | Output that is easy to verify |
| Parallel workers | Several agents explore independent branches at once | Research and search |
A worker as a tool
The simplest design reuses what you already have. Wrap a subagent in a function and register it in the tool registry. The orchestrator then calls it like any other tool.
import json
def make_subagent_tool(call_model, system_note: str):
def subagent(task: str) -> str:
# The worker starts with a fresh history, so it sees only the task.
reply = call_model([
{"role": "user", "content": f"{system_note}\n\nTask: {task}"}
])
return json.dumps({"summary": reply["content"]})
return subagent
def fake_worker(messages):
return {"content": "Found 3 relevant files.", "tool_call": None}
TOOLS = {"research": make_subagent_tool(fake_worker, "You search code.")}
print(TOOLS["research"](task="Find uses of get_weather"))The worker's history stays private. The orchestrator receives only the returned summary, which keeps its own context small.
Trade-offs to weigh
- Cost. Every extra agent multiplies model calls and tokens. Measure before and after.
- Handoff loss. A worker knows only what the task text tells it. Pass the facts it needs, not just a goal.
- Error compounding. A wrong summary from one worker can mislead the orchestrator. Ask workers to return evidence, such as file paths or quotes.
- Runaway loops. Agents that call each other need step limits at every level, plus a total budget.
- Debugging. Trace each agent's run under one shared run ID.
Exercise
An orchestrator calls a worker agent to search a codebase. The worker reads 40 files, but the orchestrator's answer is wrong because it lacks a detail the worker saw. Name the cause and one fix.
Show answer
The cause is handoff loss. The worker's history was private, and its summary dropped the detail. One fix is to require the worker to return specific evidence, such as file paths and quoted lines, in its result. Another is to have the orchestrator ask the worker a follow-up question.