Contents

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:

A supervisor sits between the customer and three specialists, reading every message and rewriting every answer; the seat-booking specialist cannot transfer the customer to refunds.CustomerSupervisorreads every messagerewrites every answerextra hop each turnSeat bookingFAQRefundscannottransfer
Scroll to see all of it.
Every turn goes through the supervisor, and the specialist that spots a refund request cannot pass the customer on.

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:

  1. The entry agent takes the first message. Triage, which has no booking tools, reads the request and calls transfer_to_seat_booking with a short reason.
  2. 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.
  3. 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.
  4. The next message skips routing. The customer answers “ABC123, seat 12A”. The run loop sends it straight to Seat booking, which calls update_seat and confirms.
  5. A peer hands off directly. Seat booking does not handle compensation, so it calls transfer_to_refunds with 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

The customer talks only to the active agent, Seat booking. Triage can transfer to Seat booking, FAQ and Refunds and back; Seat booking can transfer directly to Refunds; Refunds can escalate to a human. A run loop and a shared state store sit underneath.talks only to the active agenttransfer_to_*peeredgeescalate_to_humanCustomerTriageentry agentSeat bookingactive nowFAQfaq_lookupRefundsissue_refundHumanRun loopswaps agent, filters history, capsShared statemessages · active_agent · context object123456
Scroll to see all of it.

Hover over a number, or tap it, to read what it is.

Solid arrows are allowed transfers; an arrow at both ends means the receiver can transfer back. Dashed lines are reads and writes of shared state. Only one agent is active at a time.
  1. 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.
  2. 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.
  3. Active agent. The only agent talking to the user. Its name is saved between turns, so the next message goes straight to it.
  4. 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.
  5. 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.
  6. 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

Avoid it when

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

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.

Further reading