Sub topic 9
Choosing handoffs, orchestrators, or both
This chapter assumes you have read the chapters on the handoff pattern and the orchestrator pattern. You should be comfortable with the Agent dataclass and the run loop from earlier, and with what it means for a tool to return a string. Here the goal is a decision method. Given a workflow, you will pick handoffs, an orchestrator, or a hybrid, and you will know what to check when the choice turns out to be wrong.
The core difference: who holds control
Both patterns split work across agents. They differ in who owns the conversation after a delegation.
- In a handoff, control moves. The new agent becomes the active agent and talks to the user. The previous agent is out of the loop unless something hands control back.
- In an orchestrator, control stays. The manager calls a specialist like a tool, gets a string back, and decides what to do next. The specialist never talks to the user.
This one difference drives most of the trade-offs below.
| Property | Handoff | Orchestrator |
|---|---|---|
| Who talks to the user | The current specialist | Always the manager |
| History seen by the next agent | Usually the shared history | Only the task text it is given |
| Who combines results | Nobody, the last agent answers | The manager |
| Parallel work | Awkward, one active agent at a time | Natural, manager calls several workers |
| Cost of an extra step | One more model call by the new agent | A worker run plus a manager call to read the result |
| Failure of a specialist | Conversation is stuck with that agent | Manager can retry or use another worker |
| Control flow is decided by | Each agent, step by step | The manager |
Decision questions
Work through these questions in order. Stop at the first one that gives a clear answer.
- Does the user need to keep talking to the specialist? A refund agent that asks follow-up questions about an order is a conversation. Use a handoff. If the specialist only produces a result, use a tool-style call.
- Do you need to combine outputs from several specialists into one answer? A report that needs research, then writing, then a final edit needs a single place to merge. Use an orchestrator.
- Can subtasks run independently? If yes, an orchestrator can fan them out. Handoffs serialize work.
- Does the specialist need the whole conversation? A billing agent that must see what the user already said benefits from a handoff with shared history. A worker that needs only "summarize this document" is better isolated behind an orchestrator, because it sees less and costs less.
- Do you need one place to enforce policy? An orchestrator is a natural checkpoint for limits, approvals, and logging. With handoffs, every agent must enforce them itself.
Worked example: two designs for one product
Suppose a support product handles three kinds of requests: billing questions, technical problems, and refund requests. Each has its own instructions and tools.
A handoff design works well here. A triage agent reads the first message and hands off to the right specialist. The specialist then holds the rest of the conversation. The user asks follow-ups, and the specialist answers using its own tools.
Now suppose a different product: "Prepare a competitor brief." This needs research on three companies, a draft, and a style pass. The user does not want to chat with the researcher. They want one document. Use an orchestrator that calls research three times and write once.
A runnable comparison
The code below runs both patterns with a scripted fake model, so you can see the control flow without an API key. It uses the Agent and run definitions from earlier chapters, repeated here so the file runs on its own.
from dataclasses import dataclass, field
from typing import Callable
@dataclass
class Agent:
name: str
instructions: str
tools: dict[str, Callable] = field(default_factory=dict)
handoffs: dict[str, "Agent"] = field(default_factory=dict)
def run(start: Agent, messages: list, call_model, max_turns: int = 10):
agent = start
for _ in range(max_turns):
reply = call_model(agent, messages)
if reply["type"] == "final":
return agent.name, reply["text"]
if reply["type"] == "handoff":
agent = agent.handoffs[reply["target"]]
continue
result = agent.tools[reply["name"]](**reply["args"])
messages.append({"role": "tool", "name": reply["name"], "content": str(result)})
raise RuntimeError("max_turns exceeded")
def as_tool(worker: Agent, call_model):
def tool(task: str) -> str:
_, text = run(worker, [{"role": "user", "content": task}], call_model)
return text
return tool
def fake_model(agent, messages):
"""Scripted behavior keyed on agent name and what is in the history."""
seen = [m.get("name") for m in messages if m["role"] == "tool"]
if agent.name == "triage":
return {"type": "handoff", "target": "refunds"}
if agent.name == "refunds":
return {"type": "final", "text": "Refund issued."}
if agent.name == "researcher":
return {"type": "final", "text": "Facts: A leads, B is cheaper."}
if agent.name == "writer":
return {"type": "final", "text": "Brief: A leads, B is cheaper."}
if agent.name == "orchestrator":
if "research" not in seen:
return {"type": "tool", "name": "research", "args": {"task": "Compare A and B"}}
if "write" not in seen:
return {"type": "tool", "name": "write", "args": {"task": "Write a brief"}}
return {"type": "final", "text": "Done: " + messages[-1]["content"]}
# Handoff design: control moves to the refunds agent.
refunds = Agent("refunds", "Handle refunds.")
triage = Agent("triage", "Route requests.", handoffs={"refunds": refunds})
print(run(triage, [{"role": "user", "content": "Refund order 1042"}], fake_model))
# Orchestrator design: control stays with the manager.
researcher = Agent("researcher", "Find facts.")
writer = Agent("writer", "Write briefs.")
manager = Agent(
"orchestrator",
"Split, delegate, combine.",
tools={"research": as_tool(researcher, fake_model), "write": as_tool(writer, fake_model)},
)
print(run(manager, [{"role": "user", "content": "Competitor brief"}], fake_model))The first call prints ('refunds', 'Refund issued.'). The agent that answered is the refunds agent, so control moved. The second prints ('orchestrator', 'Done: Brief: A leads, B is cheaper.'). The orchestrator answered, and the specialists only produced strings.
Hybrids
Real systems often need both. Two hybrid shapes are common.
Handoff first, orchestrator inside. A triage agent hands off to a "research assistant" that the user converses with. That assistant is itself an orchestrator and calls a search worker and a summarizer as tools. The user sees a conversation, and the assistant gets the benefit of isolated workers.
Orchestrator first, handoff for the tail. A manager gathers facts with workers, then hands off to a human-facing agent for a long interactive step, such as walking a user through a refund form. The manager's work becomes a compact payload for that agent. The earlier chapter on context passing covers how to build it.
Keep hybrids shallow. Each layer adds a model call, a place for context to be lost, and another trace to read. If you need more than two levels, check whether a single orchestrator with more tools would do.
Common mistakes and edge cases
- Handoffs used for pure computation. If the "specialist" never needs to talk to the user, the handoff leaves the user's conversation owned by an agent whose instructions were not written for chat. Use a tool call instead.
- Orchestrator as a bottleneck. The manager reads every result, so long worker outputs fill its context. Ask workers for short, structured results.
- Ping-pong handoffs. Agent A hands to B, and B hands back to A, because both think the other is the right owner. Keep
max_turnsset, and write handoff instructions that say when not to hand off. - Lost context after a handoff. If the history is shared, the new agent sees everything, including material it should not. If you pass a summary instead, the new agent may miss a detail. Decide on purpose, and test both.
- Triage that does too much. A triage agent with many tools starts solving problems instead of routing. Keep it to routing only.
- Hidden cost of isolation. Workers that start with a fresh history cannot see earlier user preferences. Include what they need in the task text.
Checking your choice
Before committing, run ten to twenty realistic requests through your design and record three things per run: how many model calls it took, which agent produced the final answer, and whether the answer was correct. A handoff design that often ends in the wrong specialist points to a weak triage prompt, not a wrong pattern. An orchestrator design with many calls and short worker outputs may mean the subtasks are too small to justify separate agents. A later chapter on tracing shows how to capture these numbers.
Exercise
A travel product must (1) look up flight options, (2) look up hotel options, (3) combine them into one itinerary, and (4) let the user ask follow-up questions about the itinerary. Which pattern would you choose, and why? Name the two stages if you use a hybrid.
Show answer
Use a hybrid. Stages 1 to 3 suit an orchestrator: the flight and hotel lookups are independent, can run in parallel, and one manager must merge them into a single itinerary. Stage 4 is an ongoing conversation, so after the itinerary is built the manager hands off to an itinerary assistant that holds the chat, receiving a compact payload (the itinerary, constraints, and open questions) instead of the full worker histories.
Exercise
Your triage agent hands a user to the billing agent, which hands back to triage, which hands to billing again, until the run fails with "max_turns exceeded". Name one cause and one fix.
Show answer
Cause: both agents' instructions leave the ownership of the request unclear, so each believes the other should handle it. Fix: state in the billing agent's instructions that it must answer billing questions itself and may hand back only for non-billing topics, and keep max_turns as a guard. Passing the reason for the handoff in the payload also helps the receiving agent avoid bouncing the request back.