Conditional Routing and Branching
Use add_conditional_edges to dispatch based on state — the agent decides where to go next.
Why Conditional Edges?
Real agents make decisions. After a step, the next step depends on the current state. Conditional edges encode that logic.
Adding a Conditional Edge
Three ingredients: source node, router function, destination map:
def route_after_planning(state: AgentState) -> str:
if state.get('confidence', 0) > 0.8:
return 'execute'
return 'replan'
graph.add_conditional_edges(
'planner',
route_after_planning,
{
'execute': 'executor',
'replan': 'planner'
}
)All lessons in this course
- Why Graphs Beat Flat Chains
- Nodes, Edges and State
- Conditional Routing and Branching
- Persisting Graph State (Checkpoints)