Sub topic 9
Choosing Handoffs, Orchestrators, or Both
This chapter assumes you have read the earlier chapters on the handoff pattern and the orchestrator pattern. You should be comfortable with the Agent class and the run loop from those chapters, and with the idea that a tool is a function the model can request.
The core difference
The two patterns differ in who holds control and who sees the history.
- In a handoff, the active agent changes. The new agent takes over the conversation and sees the same message history. The first agent is out of the loop.
- In an orchestrator, one manager agent stays in control. It calls specialists as tools. Each specialist sees only the task text it receives and returns a string.
| Question | Handoff | Orchestrator |
|---|---|---|
| Who talks to the user at the end? | The last agent that received control | Always the manager |
| What does the next agent see? | Full history, or a payload you choose | Only the task string |
| Can one agent combine several results? | Not naturally | Yes, this is its job |
| Where is the main failure risk? | Ping-pong loops between agents | A weak manager that splits work badly |
Decision criteria
Ask these questions in order.
- Does the task need results from several specialists combined into one answer? If yes, use an orchestrator. A handoff has no place to merge results.
- Is the conversation a sequence of stages, where each stage needs a different persona, tools, or policy? If yes, use handoffs. A support flow that moves from triage to billing is an example.
- Does the specialist need the whole conversation to do its job? Handoffs carry history by default. An orchestrator must copy relevant facts into the task string.
- Must you isolate a specialist from the rest of the context? Use an orchestrator. A worker that sees only its task cannot be confused or manipulated by earlier messages.
A hybrid design
Real systems often combine both. A triage agent hands off to a domain agent. That domain agent acts as an orchestrator for its own specialists.
The code below shows the hybrid using the Agent, run, and as_tool definitions from earlier chapters.
def build_hybrid(policy: Agent, calculator: Agent, call_model) -> Agent:
billing = Agent(
name="billing",
instructions="Resolve billing issues. Use the specialist tools and combine their answers.",
tools={
"check_policy": as_tool(policy, call_model),
"calculate_refund": as_tool(calculator, call_model),
},
)
return Agent(
name="triage",
instructions="Route the user to the right agent.",
handoffs={"billing": billing},
)Calling run(build_hybrid(...), messages, call_model) starts at triage. When the model returns a handoff to billing, billing becomes the active agent. Its tool calls then run workers with fresh histories.
Common errors
- Using an orchestrator for a simple routing task. The manager adds a model call and latency to every request. If one agent can answer after routing, use a handoff.
- Handing off when you need a merged answer. The user then receives only the last agent's view.
- Handing off back and forth. Two agents that each think the other is better suited will loop until
max_turnsstops them. Give each agent clear, non-overlapping instructions. - Starving a worker of context. A task string like "check the refund" fails if the worker needs the order number. Put the needed facts in the task.
Exercise
A travel assistant must look up flights, look up hotels, and return one combined itinerary. Which pattern fits, and why?
Show answer
An orchestrator. The manager calls a flight specialist and a hotel specialist as tools, then merges both results into one itinerary. A handoff would pass control to one specialist and leave no agent responsible for combining the results.
Exercise
In the hybrid code, what does the billing agent's check_policy worker see when it runs?
Show answer
Only the task string the billing agent passes to it. as_tool starts the worker with a fresh history that contains just that one user message.