
Branching Logic
How to make your graph smart.
Branching Logic
A linear chain is dumb. It does the same thing for every input. A graph is smart. It adapts.
Visualizing Branches
graph LR
Start --> Condition{User Intent?}
Condition -- "Refund" --> RefundNode
Condition -- "Tech Support" --> SupportNode
Condition -- "General" --> ChatNode
The Condition Function
In LangGraph, logic lives in a plain Python function.
def choose_route(state):
user_input = state["messages"][-1]
# Simple keyword check
if "refund" in user_input:
return "billing"
else:
return "support"
Adding the Conditional Edge
You attach this function to a node.
graph.add_conditional_edges("start_node", choose_route)