Swarm / Handoffs
Also known as: handoff pattern, handoff orchestration, agent transfer
- Who decides
- Peers
- Shape
- Mesh
Intent
Several specialised agents share one conversation, and exactly one of them is active at a time and talks to the user. When a request leaves its area, the active agent itself passes control, with the conversation, to a better-suited peer by calling a tool such as transfer_to_refunds.
About the name. “Swarm” is also used for agents that coordinate through a shared space, like ants, and for a lead agent launching many sub-agents. On this page it means only handoffs between peers, the meaning it has in agent frameworks.
Problem
You are building the support chat for an airline. A customer writes: “My flight was cancelled. Can I pick a seat on the new one, and can I get compensation?”
One agent with every policy and every tool mixes them up, so you split the work into three specialists (seat booking, FAQ, refunds) and put a supervisor in front of them. The supervisor reads each message, calls a specialist, reads its answer and writes the reply. Three things go wrong:
- Every turn pays an extra hop. Even when the customer is just giving the seat specialist a seat number, the message goes through the supervisor and back, which adds a model call and a delay to each turn.
- Answers are paraphrased. The supervisor rewrites what the specialist said, and details such as a fare rule or a deadline get lost or changed on the way.
- Specialists cannot pass the customer on. The seat-booking agent sees that the customer now wants compensation, but it can only report back to the supervisor. It has no way to hand the customer to the refunds agent.
Solution
Remove the hub and let the specialists pass the conversation to each other. Each agent has its own instructions, a few tools of its own and a list of peers it may transfer to. Each allowed transfer is an ordinary tool, so the model decides when to hand off the same way it decides when to call any tool. Your code, the run loop, carries out the transfer and remembers who is active.
On the cancelled-flight request, a run looks like this:
- The entry agent takes the first message. Triage, which has no booking tools, reads the request and calls
transfer_to_seat_bookingwith a short reason. - The run loop swaps the agent. It adds a tool result (“Transferred to Seat booking”) so the history stays valid and makes Seat booking the active agent. Seat booking’s instructions replace Triage’s.
- The receiver continues in the same turn. Seat booking asks for the confirmation number. The turn ends when an agent replies without calling a tool.
- The next message skips routing. The customer answers “ABC123, seat 12A”. The run loop sends it straight to Seat booking, which calls
update_seatand confirms. - A peer hands off directly. Seat booking does not handle compensation, so it calls
transfer_to_refundswith a one-line summary. Refunds offers to open a compensation case without asking for the booking again.
The developer decides which agents exist and which transfers are allowed; the active agent decides when to use one. In a Supervisor, control returns to the central agent after every step and only it talks to the user, while here the receiver takes over and answers the user directly until it hands off again.
Structure
Hover over a number, or tap it, to read what it is.
- Agents. Peers, each owning one area: its instructions, a few tools and the list of agents it may transfer to. None of them manages the others; one is the entry agent that gets the first message.
- Handoff tools. One tool per allowed target, usually named
transfer_to_<agent>. Its name and description are all the model has to choose a target, and an argument can carry a reason or a summary. - Active agent. The only agent talking to the user. Its name is saved between turns, so the next message goes straight to it.
- Run loop. Code that calls the active agent, runs its tools, detects a handoff, swaps the agent, filters the history the receiver sees and stops runs that exceed a limit on turns or handoffs.
- Shared state. The message history, the name of the active agent and a context object holding facts that must survive every handoff, such as a booking number.
- Escape hatch. A transfer to a human or back to the entry agent. Every agent needs at least one, so a user is never stuck with an agent that cannot help.
Use it when / Avoid it when
Use it when
- The conversation is long and its topic drifts. Support desks, booking and onboarding chats move from one expert’s area to another’s as the user talks.
- The right specialist only shows up mid-conversation. A classifier that reads the first message would often guess wrong.
- Specialists should answer the user themselves. No hub rewrites their replies, and follow-up messages reach them without a routing call.
- Areas are clear and do not overlap. The model must tell “seat change” from “refund” from a one-line tool description.
Avoid it when
- The first message says who should handle it. Use Routing, which classifies once and costs less.
- One request spans several areas at once. Use Parallelization or Orchestrator–Workers, since handoffs handle one area after another.
- One agent must own the final answer or enforce a policy in one place. Use a Supervisor that calls the specialists as tools.
- One agent with a handful of tools already works. Stay with a single ReAct agent until its prompt or tool list becomes too large.
How it fails
- Ping-pong
-
Two agents transfer the user back and forth several times in one turn, and the cost climbs until a limit stops the run.
Mitigation: cap handoffs per turn in the run loop and stop when the same pair keeps swapping.
- Sticky agent
-
The user changes topic, and the active agent tries to answer anyway because it is still active.
Mitigation: list in every prompt what the agent does not handle, with the transfer to use for each case.
- Lost context
-
The receiver asks for the confirmation number the user already gave.
Mitigation: keep key facts in the shared context object, where a filtered or summarised history cannot drop them.
- Dead end
-
An agent has no transfer that fits, and the user is stuck with it.
Mitigation: give every agent an exit to the entry agent or to a human.
Related patterns
- Supervisor: the main alternative; control returns to a central agent after every step, and only that agent talks to the user.
- Routing: a router classifies once and never decides again, while a triage agent can ask questions first and the receiver keeps the conversation.
- ReAct (tool-use loop): each agent runs this loop inside, and its handoff tools are tools like any other.
- Human-in-the-Loop: a person can be a handoff target, and approval steps can guard the risky tools of any agent.
Try it
Describe the agents, what each one handles and does not handle, and who it may transfer to. The generator writes one system prompt per agent, the handoff tool descriptions and a checklist for the run loop.
Open the generator on this pattern
Further reading
- Orchestrating Agents: Routines and Handoffs (OpenAI Cookbook, 2024): the recipe that named the pattern, with a minimal run loop.
- Benchmarking Multi-Agent Architectures (LangChain, 2025): handoffs, a supervisor and a single agent compared on the same tasks.
- AI agent orchestration patterns (Microsoft Azure Architecture Center, 2026): when to use handoffs, and their usual pitfalls.