Module 2 · Orchestration topologies
Peer-to-peer networks and handoffs
5 min read
In a network topology there is no boss: any agent can hand control to another. This transfer is called a handoff. It's the natural topology for conversational assistants where the request evolves during the exchange — a customer support flow moving from billing to technical issues, for example.
Anatomy of a handoff
A handoff is three things:
- The decision: the current agent judges that another is better suited.
- The transfer of control: execution moves to the target agent.
- The transfer of context: the target agent receives what it needs to continue without asking everything again.
In LangGraph, a node performs the first two by returning a Command:
return Command(
goto="technical", # transfer of control
update={"transcript": ["billing → technical"], # transfer of context
"handoffs": state["handoffs"] + 1},
)
Command combines state update and routing in a single return value. It's more expressive than a conditional edge: the decision and its consequences stay in the same place.
The type annotation is mandatory
def specialist(state: SupportState) -> Command[Literal["billing", "technical", "__end__"]]:
The Command[Literal[...]] annotation declares the possible destinations to LangGraph. Without it, the graph can neither be drawn nor validated at compile time. And remember: a node that routes via Command must not also have a fixed outgoing edge. The two mechanisms would add up: the Command destination and the fixed edge's destination would both run.
The complete example: support with two specialists
The file examples/05_handoffs.py implements a triage agent and two specialists, billing and technical.
Triage: rules first, model second
def triage(state: SupportState) -> Command[Literal["billing", "technical"]]:
text = state["request"].lower()
# Deterministic rules first: free, instant, testable
if any(w in text for w in ("invoice", "refund", "payment")):
target, reason = "billing", "billing keyword"
else:
route = extract("Route the request to the right team.", state["request"], Route, model=WORKER_MODEL)
target, reason = route.agent, route.reason
return Command(goto=target, update={"transcript": [f"triage → {target} ({reason})"]})
Obvious cases are handled by a rule; only ambiguous cases cost a model call — and a call to an economical model, since choosing between two options doesn't need the most powerful one.
Specialists: answer or transfer
class SpecialistReply(BaseModel):
answer: str
handoff_to: Literal["billing", "technical", "none"]
def specialist(state: SupportState) -> Command[Literal["billing", "technical", "__end__"]]:
reply = extract(..., SpecialistReply, model=WORKER_MODEL)
can_transfer = reply.handoff_to not in ("none", name) and state["handoffs"] < MAX_HANDOFFS
if can_transfer:
return Command(goto=reply.handoff_to,
update={"transcript": [f"{name} → {reply.handoff_to}"],
"handoffs": state["handoffs"] + 1})
return Command(goto=END, update={"transcript": [f"{name}: {reply.answer}"]})
The transfer decision is a structured output (handoff_to is a Literal): the model cannot invent a nonexistent destination. And it is the code that decides whether the transfer is allowed (can_transfer), not the model.
Offline run:
$ ATLAS_FAKE_LLM=1 python examples/05_handoffs.py "Ma facture est fausse depuis le bug d'hier"
triage → billing (mot-clé de facturation)
billing → technical
technical : Ticket ouvert, correction sous 24 h.
(The companion code's keywords and messages are in French; the flow is what matters.)
The three risks of agent networks
1. Endless ping-pong
A hands off to B, which hands off to A, which hands off to B… Each agent sincerely believes the other is more competent. Two cumulative protections:
- A business counter (
handoffs≤MAX_HANDOFFS): beyond it, the agent must answer or escalate to a human. - LangGraph's recursion limit:
{"recursion_limit": 10}in the config. It's a global safety net: exceeding it raises aGraphRecursionError. Don't rely on it alone, since it stops execution abruptly instead of producing an answer.
2. Lost context
If the target agent only receives the initial request, it asks again for what has already been established. Pass a condensed history (here, transcript) rather than the raw conversation: the target agent needs the established facts, not every turn.
3. Degraded observability
Without a center, reconstructing "who did what" is harder. Log every handoff with its reason — the router's reason field and the transcript entry exist for that — and trace them (module 5).
Supervisor or network?
| Criterion | Supervisor | Network |
|---|---|---|
| Sub-tasks known in advance | ✅ | ➖ |
| Evolving conversation | ➖ | ✅ |
| Traceability, audit | ✅ | ⚠️ needs instrumentation |
| Guaranteed termination | By construction | Through counters |
| Latency | Central coordination | Direct transfers |
Real systems are often hybrid: a network of a few conversational agents, some of which delegate internally to supervisor-style subgraphs.
Key takeaways
- A handoff = decision + transfer of control + transfer of context; in LangGraph, a
Command(goto=…, update=…).- Annotate the return type
Command[Literal[...]]and don't add a fixed edge to a node that routes itself.- Destinations are constrained by a schema, and the code decides whether a transfer is allowed.
- Protect against ping-pong with a business counter, and keep
recursion_limitas a safety net.
Hands-on
- Run the example with and without the word "bug" in the request; follow the transfers.
- Set
MAX_HANDOFFSto 0: check that agents answer instead of transferring. - Add a third
salesspecialist and a test that checks no run exceeds 3 transfers.