Sub topic 6
The handoff pattern
This chapter assumes you can read Python, know what a tool call is, and understand that a model receives a list of messages on every turn. It builds on the earlier chapters about tool calling and about why work is split across agents.
What a handoff is
A handoff transfers control of the conversation from one agent to another. The new agent takes over the same conversation, so it sees the same message history. The user keeps talking to "the assistant" and may never notice the switch.
Mechanically, a handoff is a special kind of tool call. The model does not produce a final answer. It asks the runtime to change which agent is active. The runtime swaps the agent's instructions and tools and calls the model again.
A minimal implementation
Each agent declares which agents it may hand off to. The loop treats a handoff reply as a signal to change the active agent and continue.
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):
"""call_model(agent, messages) returns one of:
{"type": "final", "text": str}
{"type": "tool", "name": str, "args": dict}
{"type": "handoff", "target": str}
"""
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")Three details matter:
agent = agent.handoffs[reply["target"]]is the whole handoff. Nothing else changes.messagesis never replaced, so the new agent inherits the full history.- A handoff uses up a turn.
max_turnstherefore also limits how many times control can move.
Common errors
- Ping-pong loops. Agent A hands to B, and B hands back to A, because both instructions say "if this is not your area, hand off". Give each agent a clear scope and a rule for what to do when it is the right agent.
- Unknown targets. The model names an agent that is not in
handoffs, which raises aKeyErrorin the code above. Catch it and return an error message to the model so it can choose again. - Stale instructions. After a handoff the previous agent's instructions no longer apply. If the new agent needs a rule from the old one, put that rule in its own instructions.
- Leaky history. The whole history moves with the conversation. That includes tool results the new agent should not see. The chapter on trust boundaries covers this.
When handoffs fit
Handoffs suit conversations where the user should talk to one specialist at a time, such as support, intake, or onboarding. They fit less well when one request needs several specialists at once. The orchestrator pattern, covered next, handles that case.
Exercise
In the loop above, why does a handoff reply use continue instead of appending a message, and what does the new agent see on its first turn?
Show answer
A handoff changes only which agent is active, so there is no tool result to record. The new agent sees the same messages list as before, so it has the full history. It uses its own instructions and tools.