0PricingLogin
AI Agents · Lesson

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

  1. Why Graphs Beat Flat Chains
  2. Nodes, Edges and State
  3. Conditional Routing and Branching
  4. Persisting Graph State (Checkpoints)
← Back to AI Agents