When to reach for it
- Coordination must be completely traceable and testable.
- Complex flows involving cycles and strict conditions must run stably in production.
An executable graph of agents, tools, and state.
Agent coordination, tools, and state transitions are modeled as an explicit, executable state graph consisting of nodes and edges. It composes the two workflow relaxations — conditional edges (Routing) and bounded cycles (Loop) — over a closed node set with a validated state schema.
The developer wires the node set and the admissible edges; a router node lets an LLM choose which edge to take at runtime.
| Step | Node | State | Next edge |
|---|---|---|---|
| 1 | classify | Category: technical | Conditional: to diagnose |
| 2 | diagnose | Cause: an expired certificate | Fixed: to draft |
| 3 | draft | Reply, version 1 | Fixed: to check |
| 4 | check | Failed: the reply names no fix. Attempts: 1 | Conditional: back to draft |
| 5 | draft | Reply, version 2, with the steps to renew | Fixed: to check |
| 6 | check | Passed | Conditional: end |
NODES = {"classify": classify, "billing": billing, "diagnose": diagnose,
"draft": draft, "check": check}
def next_node(current, state): # every admissible edge is written here
if current == "classify": # conditional edge
return {"billing": "billing", "technical": "diagnose"}[state["category"]]
if current in ("billing", "diagnose"):
return "draft"
if current == "draft":
return "check"
# bounded cycle: at most two drafts
if state["passed"] or state["attempts"] >= 2:
return None
return "draft"
def run(ticket):
state, node = {"ticket": ticket, "attempts": 0}, "classify"
while node:
# a node returns an update to the state
state = {**state, **NODES[node](state)}
node = next_node(node, state)
return stateWhen to reach for it
When it backfires
When the router is an LLM, it can return an edge label that was never registered. The graph then either crashes or silently falls through to a default that hides the failure.
Countermeasure
Validate the router's output against the registered edge set before transitioning. Reject and retry on mismatch; never inherit corrupted routing decisions.
Cycles are legal in this pattern. Without an explicit guard, a router that keeps choosing the same back-edge will loop until the process is killed.
Countermeasure
Set a hard recursion / step budget at the runtime level and an exit predicate the graph checks every iteration. Treat the budget as a correctness boundary, not a performance hint.
Search patterns, frameworks, and pages.