Peer handoff (swarm): agents pass the conversation to one another, with no central planner
In peer handoff, the agent holding the conversation can transfer it, with its state, to a better-suited peer. Use it for conversations that change topic, such as support, where a central planner would add delay.
Peer handoff in 60 seconds
- What: several agents, each with its own prompt and tools, where the current agent can transfer the conversation and its state to another.
- Who decides the path: a model, locally. The agent holding the conversation chooses the next agent from a short allowed list.
- Best for: support and service conversations that change topic, and flows where the user should feel one continuous chat.
- Weak spots: ping-pong loops between agents, lost context during transfer, and unclear ownership of the outcome.
- Compare: it sits between a router, which decides once up front, and a supervisor, which keeps control the whole time.
Where this type fits
Peer handoff belongs to the group of patterns where a model decides the path, with the single agent and the supervisor. The difference is that control moves. There is no central model holding the plan. The orchestration hub shows how it compares with the others.
The problem it solves
A conversation does not stay on one topic. A customer starts with a delivery question, mentions a disputed charge, and then asks about a refund to a different card. A single agent that covers all of this needs a large prompt and many tools. A router at the start cannot help, because the right specialist changes mid-conversation.
A supervisor can follow the changes, but it sits on every turn. It adds a model call each time and holds the whole conversation in its own context. For a live chat, that extra hop is felt as delay, and it makes the supervisor a bottleneck.
Peer handoff lets the specialist who is talking decide when to pass on. Each agent keeps a short prompt and only its own tools. The user stays in one conversation, and the next agent starts with the history and any notes the last one wrote. The cost is that nobody oversees the whole path, so you need rules to keep transfers sensible.
How it works
- Start with an entry agent. A triage agent usually takes the first message and handles simple cases itself.
- Offer handoff as a tool. Each agent has a "transfer to X" action for every peer it may pass to. The allowed list is fixed in code.
- Decide. During a turn, the agent either answers or calls a transfer, with a short reason and notes for the next agent.
- Transfer state. Code moves the conversation, the notes, and key facts to the new agent. The new agent becomes the active one.
- Continue. The new agent replies to the user. It may answer, use its tools, or transfer again.
- Cap and log. Code counts handoffs, blocks loops, and records each transfer with its reason.
The chain in the diagram is only one possible path. Real conversations can go back to triage or skip an agent. What stays constant is that each transfer is a visible event with a reason, and that code, not the model, enforces the cap.
State transfer is the part that decides whether the user feels a smooth call or a restart. Pass the conversation, but also a short structured note: who the customer is, what is solved, what is open, and any facts already checked. Without it, the new agent re-asks questions and users lose patience.
A worked example
A customer writes to Acme: "My parcel is late, and I also see a charge of 89.00 that I do not recognise."
- Triage agent: sees two issues. It checks the parcel, which is delayed by two days, and tells the customer. It then transfers to billing with the note
{"customer": "C-1042", "open": "unrecognised charge 89.00", "done": "delivery delay explained"}. - Billing agent: reads the note and looks up the charge. It finds two charges of 89.00 on the same day, one pending.
- Billing decision: this pattern looks like a possible duplicate payment from a stolen card, which billing may not resolve. It transfers to fraud with the note
{"charge": "89.00 x2", "reason": "possible unauthorised use"}. - Fraud agent: asks the customer to confirm whether they made either purchase. The customer says no. Fraud freezes the card through its tool, after a confirmation step, and opens a case.
- Handoff count: 2 of a maximum of 4. The log records each reason.
- Output: the customer had one conversation, never repeated their details, and ended with a case number.
If billing had tried to send the customer back to triage and triage back to billing, the cap and the loop check would have stopped the chain and offered a human agent instead.
Building it
MAX_HANDOFFS = 4 # stops endless transfers
MAX_TURNS = 20 # stops endless conversations
ALLOWED = { # who may pass to whom, fixed in code
"triage": {"billing", "fraud"},
"billing": {"triage", "fraud"},
"fraud": {"billing"},
}
def run_conversation(user_message: str, session):
active, handoffs, seen = "triage", 0, []
for _ in range(MAX_TURNS):
agent = AGENTS[active] # prompt + tools per agent
# Placeholder: returns {"type": "reply", "text": "..."} or
# {"type": "handoff", "to": "...", "reason": "...", "notes": {...}}
out = agent.step(session.history, session.notes, user_message)
if out["type"] == "reply":
session.history.append(("assistant", active, out["text"]))
return out["text"]
target = out["to"]
if target not in ALLOWED[active]:
session.notes["error"] = f"invalid handoff to {target}"
continue # ask the same agent again
# Block ping-pong: the same pair must not repeat back and forth.
if seen[-2:] == [target, active]:
return escalate_to_person(session, reason="handoff loop")
handoffs += 1
if handoffs > MAX_HANDOFFS:
return escalate_to_person(session, reason="too many handoffs")
log.info("handoff", frm=active, to=target, reason=out["reason"])
session.notes.update(out["notes"]) # state travels with the call
seen.append(active)
active = target
return escalate_to_person(session, reason="turn limit")
AGENTS, log, session, and escalate_to_person are placeholders. The allowed list and the caps live in code, so a model cannot talk its way past them.
Design decisions
- Allowed transfers: write an explicit map of who may pass to whom. A small, sparse map is easier to reason about than a full mesh.
- State package: pass the history plus a structured note with identity, resolved items, open items, and checked facts. Keep the note short and typed.
- Caps and loop checks: limit total handoffs and block the same pair from bouncing. When a cap is hit, escalate to a person instead of failing quietly.
- Entry and exit: name one entry agent and one place the conversation can end. Decide who owns the final outcome, so a case is never left with no owner.
- Permissions: give each agent only its own tools, and re-check the user's rights at every tool call. A handoff must not carry elevated permissions with it.
- Sensitive actions: keep confirmation steps inside the agent that holds the dangerous tool, and consider a human checkpoint before money or accounts change.
- Observability: give the whole conversation one id, and log every handoff with source, target, and reason. Without this, debugging is guesswork.
Measuring it
- Handoff accuracy: on a sample, the share of transfers that a reviewer agrees were to the right agent and at the right time.
- Handoffs per conversation: the mean and 95th percentile. A rising tail points to ping-pong or poorly drawn agent boundaries.
- Repeat-question rate: how often the new agent asks for something already in the notes. It measures state transfer quality.
- Resolution rate and escalation rate: the share of conversations resolved by agents, and the share sent to a person by a cap or loop check.
- Time to resolution: from first message to resolved, compared with a single-agent baseline.
- Cost per conversation: total tokens across agents, since each transfer carries history forward.
When to use it, and when not to
| Situation | Use this type? | Why |
|---|---|---|
| Live support where topics change mid-conversation | Yes | The agent in the chat can pass to the right specialist without a central hop. |
| A single topic with one set of tools | No | A single agent is simpler and cheaper. |
| One decision at the start decides everything | No | A router is enough and easier to test. |
| Goals that need a plan and combined results | No | A supervisor keeps the plan and merges the parts. |
| Flows with money or account changes | With care | Add confirmations and a human checkpoint inside the sensitive agent. |
| Teams that own different domains | Yes | Each team can own one agent and its tools behind a clear transfer contract. |
Trade-offs
| Dimension | Effect | What to do about it |
|---|---|---|
| Quality | Specialists with short prompts do well in their area. Poor transfers lose context and annoy users. | Pass a structured note and measure repeat questions. |
| Latency | No central hop, so turns stay quick. Each transfer adds some delay. | Let the receiving agent reply in the same turn when it can. |
| Cost | History is passed on at each transfer, so long chains cost more. | Summarise old turns when passing state, and cap handoffs. |
| Complexity | Many small agents and a transfer map to maintain. | Keep the map sparse and give each agent an owner. |
| Failure modes | Ping-pong, dropped context, and unclear ownership of the final outcome. | Add loop checks, cap transfers, and escalate to a person at the limit. |
Compared with related types
A router decides once, before any work, and the specialist never passes the request on. Peer handoff lets the decision recur during the conversation. A supervisor with workers keeps one planner in charge of the whole task and receives every result. Handoff gives up that oversight to gain speed and a smoother conversation.
Against a single agent, handoff pays off only when one agent's prompt and tools get too crowded. Start with one agent, and split it when you can name distinct specialities with different tools.
What this means for your role
Common mistakes
Further reading
- Orchestration patterns: the hub page that compares every pattern.
- Human checkpoint: how to bring a person into a long chain.
- Tool calling and MCP: how each agent gets a safe, limited tool set.
- Guardrails and safety: checks to apply around actions that an agent can take.
Peer handoff works best with few agents, a sparse transfer map, and firm caps. The conversation moves, but the rules stay in code.