By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Harbor Supply's support graph has outgrown one agent. Billing can read charges, shipping can read delivery status, and policy can read refund rules, but no single prompt holds all three toolkits well. A supervisor fixes the prompt problem by making the choice explicit: one node decides which specialist runs next, the specialist returns what it found, and the loop continues until the question is answered. That is a clean topology. It is also a loop with a model inside it, and an unbounded model loop is a production incident with a bill attached.
The applied version of the pattern is the one you can operate: the shared state carries a turn budget, the set of workers already asked, and a log of every handoff and its reason. The supervisor can stop for three distinct reasons — the question is answered, the budget is spent, or a worker failed — and each reason is data rather than an exception. That is the difference between a demo that usually finishes and a graph a support team can page on.
The supervisor node itself is small. It reads state, checks the budget, picks one worker that has not answered yet, and returns both the target and the reason. In LangGraph that is a conditional edge or a Command(goto=..., update=...); either way, the routing policy is a pure function over state and can be tested without a model. The model can supply the judgement — which unanswered question matters most — but the budget, the asked set and the log are deterministic state the graph owns.
from dataclasses import dataclass, field
from typing import Literal
WORKERS = ("billing", "shipping", "policy")
Turn = Literal["billing", "shipping", "policy", "end"]
@dataclass
class SupervisorState:
turns: int = 0
asked: list[str] = field(default_factory=list)
handoffs: list[dict[str, str]] = field(default_factory=list)
def decide(state: SupervisorState, budget: int) -> tuple[Turn, str]:
"""Deterministic stand-in for a supervisor model: one worker or end."""
if state.turns >= budget:
return "end", "turn budget exhausted"
for worker in WORKERS:
if worker not in state.asked:
return worker, f"{worker} question unanswered"
return "end", "all workers answered"
def run(budget: int) -> tuple[SupervisorState, str]:
state = SupervisorState()
while True:
turn, reason = decide(state, budget)
if turn == "end":
return state, reason
state.handoffs.append({"goto": turn, "reason": reason})
state.asked.append(turn)
state.turns += 1
for budget in (2, 5):
state, stop = run(budget)
route = " -> ".join(handoff["goto"] for handoff in state.handoffs)
print(budget, state.turns, f"[{route}]", stop)Both runs start identically and end differently because the budget, not luck, decides. With two turns the run stops after shipping and says so; with five it reaches policy and reports that every worker answered. The stop reason is the difference between an answer you can trust at the edges and one that merely did not crash. In a real graph the same reason is written to state and surfaced in the reply so the support agent knows which specialist did not weigh in.
errors entry keyed by worker, and let the supervisor decide whether to retry, skip or escalate.next_worker and end_reason; shared-state ownership is what makes the loop traceable.Send fan-out is cheaper and deterministic; the supervisor pattern earns its cost when the next step depends on what was just learned.A supervisor is only as good as the record it leaves. Four signals turn the loop from a black box into a readable trace, and all four belong in state rather than in log messages alone: the number of turns, the ordered handoff log with reasons, the worker errors, and the named end reason. With those, an incident review can answer "why did the run stop" and "who never answered" from the final state, without replaying a model.
| Signal | Where it lives | Question it answers |
|---|---|---|
turns and max_turns | State, written by the supervisor | Did the run stop because work was done or budget ran out? |
handoff_log (goto and reason) | Reducer channel in state | Which worker ran when, and why was it chosen? |
worker_errors | Worker-owned channel | Which specialist failed, and did the run continue? |
end_reason | State, written once | Was the answer complete, partial or escalated? |
Key takeaways
A supervisor is a router with a ledger: one target per turn, a reason for every handoff, and a shared state that the next turn and the final report both read.
Carry a turn budget in state and evaluate it before routing, so a spent budget produces a named stop instead of a recursion-limit crash.
Track the asked workers and write one channel per writer; this is what stops the model from re-routing to a specialist that already answered.
Treat worker failure as state: catch it at the boundary, record it, and let the supervisor retry, skip or escalate deliberately.
Make the stop reason and handoff log visible in the answer — a partial answer with an explicit budget stop is operable, while a silent truncation is an incident waiting to be reported.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.