Sub topic 17
Multi-agent designs and their trade-offs
This chapter assumes you can write a single-agent loop with tool calling, a step limit, and error handling. It also assumes you know how the context window limits what a model can see. Here you will learn when splitting work across several agents helps, which patterns exist, and what the split costs.
What "multi-agent" means
A multi-agent design uses more than one model-driven loop to finish a task. Each agent has its own system prompt, its own tools, and its own message history. They exchange results as text or structured data.
Treat "agent" loosely here. A sub-agent is often the same model with a different prompt and a different tool list. The separation is in the context and the permissions, not in the model.
Why split a task
There are four reasons that hold up in practice:
- Context isolation. A worker can read 40 pages of search results and return a 10-line summary. The orchestrator's history stays small.
- Specialization. A narrow prompt with three tools is easier for a model to follow than a broad prompt with thirty.
- Least privilege. A research agent gets read-only tools. Only a separate, tightly checked agent gets the tool that sends email or writes to a database.
- Parallelism. Independent subtasks can run at the same time, which lowers wall-clock latency.
Organizational convenience is not a reason. If the task fits comfortably in one context with a modest tool set, a single agent is usually cheaper, faster, and easier to debug.
Common patterns
| Pattern | How it works | Best for |
|---|---|---|
| Orchestrator and workers | One agent plans and delegates. Workers run subtasks and return results. | Research, code changes across many files |
| Pipeline | Agent A's output feeds agent B, then C, in a fixed order. | Draft, review, format |
| Router | A small first step picks which specialist handles the request. | Support triage, mixed request types |
| Critic and generator | One agent produces work. Another checks it against criteria. | Code review, policy checks |
| Peer debate | Several agents answer independently and a judge compares them. | High-stakes answers where errors are costly |
A pipeline with fixed order is really a workflow, as covered in the previous chapter. The orchestrator pattern is the one that keeps the model in charge of decisions, so it carries the most risk and the most flexibility.
A minimal orchestrator
The code below shows the mechanics with a scripted model so it runs without an API key. Each worker builds a fresh message list, so nothing from one worker leaks into another. Failures come back as text, as in the tool-registry pattern from earlier chapters.
from concurrent.futures import ThreadPoolExecutor
def fake_model(messages):
# Scripted stand-in for a real model call.
role = messages[0]["content"]
task = messages[-1]["content"]
return {"content": f"[{role}] findings for: {task}"}
def make_worker(system_prompt, call_model):
def worker(task: str) -> str:
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": task},
]
return call_model(messages)["content"]
return worker
def run_team(plan, workers, max_parallel=3):
# plan is a list of (worker_name, task) pairs.
def run_one(item):
name, task = item
if name not in workers:
return name, f"error: unknown worker {name}"
try:
return name, workers[name](task)
except Exception as exc:
return name, f"error: {exc}"
with ThreadPoolExecutor(max_workers=max_parallel) as pool:
return list(pool.map(run_one, plan))
workers = {
"search": make_worker("search specialist", fake_model),
"data": make_worker("data analyst", fake_model),
}
plan = [
("search", "Find recent reports on battery prices"),
("data", "Summarize the 2024 price table"),
("legal", "Check licensing"),
]
for name, result in run_team(plan, workers):
print(name, "->", result)The third task names a worker that does not exist. The orchestrator receives an error string instead of a crash, so a real orchestrating model could read it and re-plan. In a full system, plan would come from a model call that returns structured output, validated against a schema.
What it costs
Splitting adds failure modes that a single agent does not have.
- Token cost multiplies. Every agent pays for its own system prompt and history. Teams of agents commonly use several times the tokens of one agent on the same task.
- Latency can rise. Sequential delegation adds a full model round trip per hop. Parallel workers help only when subtasks are truly independent.
- Information is lost at the handoff. A worker's summary drops detail. If the orchestrator needed a number the worker omitted, the error appears later and far from its cause.
- Errors compound. If each stage is right 90% of the time, a chain of four is right about 66% of the time.
- Debugging is harder. A wrong final answer might come from a bad plan, a bad worker, or a bad summary. Without traces per agent, you cannot tell which.
- Shared state conflicts. Two workers editing the same file or record can overwrite each other.
Design rules that reduce the pain
- Start with one agent. Split only after you can point to a measured problem: context overflow, tool confusion, or a permission boundary.
- Give each handoff a schema. Ask workers for structured results with named fields, such as
answer,sources, andconfidence. This limits information loss and makes validation possible. - Pass what the worker needs, not everything. Send the subtask and the relevant facts. Do not forward the whole history.
- Bound everything. Set a step limit per worker, a limit on delegation depth, and a total token or cost budget. Without a depth limit, agents can spawn agents indefinitely.
- Keep write access narrow. Give mutating tools to as few agents as possible, and put human approval in front of them.
- Trace each agent separately. Give every run an ID and record which agent produced each message. The observability chapter covers this in more detail.
Choosing a design
Ask these questions in order:
- Does the task fit in one context with a manageable tool set? Use one agent.
- Is the sequence of steps known ahead of time? Use a workflow, such as a pipeline.
- Is the problem that one step reads far more than it needs to keep? Use a worker with its own context that returns a summary.
- Do you need a hard permission boundary? Separate the agents by tool access.
- Do independent subtasks make latency the bottleneck? Run workers in parallel.
- Is an error very costly? Add a critic or a second independent attempt, and accept the extra cost.
If none of these apply, a multi-agent design is probably overhead.
Common mistakes
- Creating a "manager" agent that only forwards the request to one worker. It adds a round trip and no value.
- Letting workers talk freely to each other. Free-form chatter is hard to bound and hard to trace. Route messages through the orchestrator.
- Using a critic that sees the same prompt and context as the generator. It tends to approve the same mistakes. Give it explicit criteria and, ideally, different information.
- Measuring only the final answer. Evaluate each worker on its own test tasks too, so you can find which part regressed.
Exercise
A single agent researches a topic by calling a search tool 15 times, and by the end its context is full of raw search results. It then starts to miss details from the user's original request. Which multi-agent change addresses this most directly, and what two controls should you add to keep it safe?
Show answer
Delegate the searching to a worker agent with its own fresh context. The worker reads the raw results and returns a short structured summary, so the orchestrator's history stays small and the original request stays in view. Add a step limit for the worker, plus a schema or validation on its returned summary. A depth limit on delegation and a total cost budget are also reasonable controls.
Exercise
Why does a four-stage pipeline in which every stage is 90% reliable give worse results than a single 90% reliable step, and what does this suggest about when to split a task?
Show answer
Errors multiply across stages: 0.9 x 0.9 x 0.9 x 0.9 is about 0.66, so the chain succeeds only about two thirds of the time. Each added agent must therefore earn its place with a concrete benefit, such as context isolation or a permission boundary, or with checks between stages that catch errors before they pass on.