Multi-Agentmulti-agentswarmhandoffpeerdecentralised

Swarm / Peer Pattern

Peer agents hand a conversation and its context to whichever agent has the right capability, with no central controller — flexible for routing-heavy dialogues, risky without hop limits.

Interview question
Progress

You have not marked these as done yet. Read on anyway if they feel familiar; come back to them if something here does not land.

Key points

  • A hand-off is a tool call that switches the active agent instead of returning data.
  • Carry a small context object and recent turns, not the full history.
  • Tool sets stay with agents — that is the permission boundary.
  • Restrict the hand-off graph; add a hop limit and an escalation exit.
  • Evaluate on outcomes and invariants, not exact paths.

Mechanism: the hand-off

In a swarm each agent has a normal tool set plus hand-off tools: transfer_to_billing(), transfer_to_refunds(). Calling one does not return a result; it changes which agent runs the next turn. The conversation history (or a filtered slice of it) and a small context object travel with it.

This makes swarms natural for customer-support style flows where the active specialist changes as the conversation reveals what the user needs. Triage hands to billing; billing discovers a refund is required and hands to refunds; refunds resolves and hands back to triage for anything else.

Hand-offs in a support swarm
transfertransfertransferescalateUserHop limit 5TriageBillingRefundsHuman agent
UserLLMAgentToolDataDecisionHumanGuardrail

What travels and what does not

Decide explicitly what a hand-off carries. Full history is simple but leaks irrelevant detail and grows without bound. A context object ({customer_id, order_id, verified: true}) plus the last N turns is usually enough and keeps each agent's prompt small. Tool sets do not travel: refunds has issue_refund, triage does not, which is where the security benefit comes from (Tool Permissions and Least Privilege).

Also decide who may hand to whom. A fully connected graph of 8 agents has 56 possible transfers, most of them nonsensical. Restrict edges to the real routing graph and the model has fewer wrong choices.

A hand-off as a tool that switches the active agent
1class Handoff:
2 def __init__(self, to: str): self.to = to
3
4def transfer_to_refunds(reason: str) -> Handoff:
5 """Hand the conversation to the refunds specialist. Use only after the customer is verified."""
6 return Handoff("refunds")
7
8def run_swarm(agents: dict, start: str, msgs: list, ctx: dict, max_hops: int = 5):
9 active, hops = start, 0
10 while True:
11 out = agents[active].step(msgs, ctx) # returns text or a Handoff
12 if isinstance(out, Handoff):
13 hops += 1
14 if hops > max_hops:
15 return escalate_to_human(msgs, ctx) # termination guard
16 active = out.to
17 continue
18 return out

Risks specific to swarms

There is no single place that knows the plan, so ping-pong hand-offs, dropped tasks and duplicated actions are all easy. Every swarm needs a hop budget, an idempotency key for side effects that may be attempted by two agents (Idempotency), and a trace that records the agent name on every span so you can reconstruct who did what (Trace Inspection: Debugging from a Trace).

Evaluation is also harder: the "right" path through the swarm is not unique. Evaluate on outcomes plus path constraints (e.g. "refund never issued before verification") rather than on exact hand-off sequences.

When to use — and when not to

Use it when
  • Conversational flows where the needed specialist changes mid-dialogue.
  • Specialists need very different permission levels.
  • Routing decisions are local and do not need a global plan.
Avoid it when
  • Tasks with a fixed order — a pipeline is simpler and traceable.
  • Work requiring a global plan or merge step — use a supervisor.
  • Side-effect-heavy tasks where duplicated actions are costly.

Failure modes

Reference · show
  • Infinite transfer loops between two agents.
  • Task silently dropped at a hand-off because context did not carry it.
  • Two agents both perform the side effect.
  • Traces without agent identity make incidents unreconstructable.

Tradeoffs

Complexity
low → high
Latency
low → high
Cost
low → high
Reliability
poor → strong
Debuggability
hard → easy

Check yourself

Answer out loud or on paper first, then compare. These come from the interview questions linked to this lesson.

  1. What is the difference between a router architecture and a supervisor architecture, and when would you use each?

    Show a strong answer

    A Router Architecture makes one decision at the start: classify the input and dispatch it to one of several specialised handlers, each of which completes the task on its own. The router is usually a cheap classifier or a small model with structured output; it runs once and exits the picture. A Supervisor Architecture stays in the loop: it decomposes the task, delegates sub-tasks to workers, receives their results, and decides what to do next, possibly many times.

    Use a router when the inputs fall into distinct categories that need different handling and each category can be handled end-to-end by one path: support intents, document types, languages. Use a supervisor when a single task genuinely requires coordinating several capabilities whose results feed each other, such as research → analysis → drafting where the analysis result changes what to research next.

    The supervisor is much more expensive: every round trip is an LLM call, context grows with worker outputs, and the supervisor becomes a single point of failure. Routers are cheap, fast, and testable as classifiers.

    Full interview question, with follow-ups and red flags →
  2. A team split a single agent into a supervisor with four workers and quality dropped. What are the likely causes and what would you do?

    Show a strong answer

    The usual causes: the supervisor loses information when summarising the task for workers and their results for itself; workers lack context the single agent had (the user's original phrasing, earlier tool results); hand-offs use free-form prose so details drift; the supervisor makes extra round trips and hits iteration or budget caps; and errors compound because a worker's mistake is taken as truth by the supervisor. See Supervisor Pattern and Agent-to-Agent Communication.

    I would pull traces for the regressions and compare the single-agent run with the multi-agent run on the same input: where did the needed fact disappear? Then fix the boundary: pass structured messages including the original request, make workers return typed outputs with confidence and citations, cap supervisor rounds, and give the supervisor the raw worker outputs rather than its own summaries when they fit the budget.

    If after that the multi-agent version is not measurably better on the golden set, revert. The single agent was a working baseline; the split has to earn its place.

    Full interview question, with follow-ups and red flags →
  3. How do you evaluate a router?

    Show a strong answer

    Labeled set, confusion matrix, per-class precision and recall, calibration of the confidence used for the unsure threshold.

    Full interview question, with follow-ups and red flags →