Sub topic 15
Cost and latency trade-offs
This chapter assumes you have read the chapters on the orchestrator and handoff patterns, and that you know what a token is. Every agent you add can improve quality, but it also adds model calls. Each call costs money and takes time.
Where the cost comes from
Cost in a multi-agent system is mostly token volume. You pay for input tokens (everything sent to the model) and output tokens (everything it writes). Three things drive that volume.
- Number of model calls. Each turn of each agent is one call.
- Context size. A handoff keeps the whole message history, so every later call re-sends it. Input cost grows with each turn.
- Instructions and tool descriptions. Each agent repeats its own instructions and tool schemas on every call.
An orchestrator adds a call for planning and a call for merging, on top of the calls the specialists make. In return, each specialist starts with a short history that contains only its task.
Where the latency comes from
Latency is the time the user waits. Model calls in a chain add up, because each agent waits for the previous one. A handoff chain of three agents costs at least three sequential calls. An orchestrator that calls two specialists one after the other has the same problem.
Parallel fan-out helps here. Independent subtasks can run at the same time, so the wait is roughly the slowest worker, not the sum of all workers. The cost stays the same, because you still pay for every call.
Measuring before tuning
Estimate cost per run from the token counts in your traces. This small function sums them. The prices are example values, so replace them with your provider's current rates.
from dataclasses import dataclass
@dataclass
class Call:
agent: str
input_tokens: int
output_tokens: int
def run_cost(calls: list[Call], in_price: float = 3.0, out_price: float = 15.0) -> float:
"""Prices are dollars per million tokens."""
total_in = sum(c.input_tokens for c in calls)
total_out = sum(c.output_tokens for c in calls)
return (total_in * in_price + total_out * out_price) / 1_000_000
calls = [
Call("triage", 800, 40),
Call("billing", 2400, 300),
Call("billing", 3100, 250),
]
print(f"${run_cost(calls):.4f}")Notice that the input tokens of the second billing call are larger than the first. History accumulates, so late turns are the most expensive.
Ways to reduce both
- Use a small model for routing. A triage agent only picks a destination, so a cheaper, faster model usually works.
- Pass a payload, not the history. The structured handoff payload from the context chapter shrinks input tokens on every later call.
- Cap turns. The
max_turnslimit puts a ceiling on the cost of a runaway loop. - Run independent work in parallel. This cuts latency without cutting cost.
- Skip agents you do not need. If one agent handles a request well, do not add a second one.
Quality against cost
Splitting work usually raises quality for hard tasks, because each agent has a focused prompt. It raises cost and latency at the same time. Compare the options on your own test set, and keep the extra agent only if the quality gain justifies the added calls.
Exercise
A triage agent hands off to a specialist, and the full history is re-sent on each of the specialist's five turns. Name two changes that lower cost, and one that lowers latency without lowering cost.
Show answer
To lower cost, pass a compact handoff payload instead of the full history, and use a smaller model for the triage agent. You could also cap turns. To lower latency without lowering cost, run independent subtasks in parallel, because you still pay for every call but the user waits only for the slowest one.