Course Content
LangGraph Agents
7 sections · 49 lessons
How do conditional edges work in LangGraph, and when do you use them?
What you need to know
Python
1from typing import Literal, TypedDict2from langgraph.graph import StateGraph, START, END34class State(TypedDict):5 amount_inr: int6 fraud_score: float7 route: str89def route_refund(state: State) -> Literal["auto_refund", "human_review", "reject"]:10 if state["fraud_score"] > 0.8:11 return "reject"12 if state["amount_inr"] > 5000 or state["fraud_score"] > 0.4:13 return "human_review"14 return "auto_refund"1516builder.add_conditional_edges(17 "score",18 route_refund,19 {"auto_refund": "auto_refund", "human_review": "human_review", "reject": "reject"},20)Parts of a conditional edge
- Source node —
"score": the edge is checked after this node runs. - Routing function — takes state (and optionally config or runtime), returns where to go.
- Path map — optional dict or list of targets. Without it, LangGraph cannot draw the possible branches, and the diagram shows edges to every node.
What the function may return
| Return | Effect |
|---|---|
"node_name" | Run that node next |
END | Finish the run |
["a", "b"] | Run both in the next super-step, in parallel |
[Send("worker", {...}), ...] | Run a node several times with custom inputs |
Prebuilt routing
tools_condition from langgraph.prebuilt returns "tools" if the last AI message has tool calls, otherwise END. It is the standard edge in a hand-built tool loop.
A real-life example
A marketplace's refund graph scores each request, then routes. In the first month, 72% of requests went to auto_refund, 25% to human_review and 3% to reject. When the fraud team wanted to lower the approval threshold from Rs 5,000 to Rs 3,000 during a festival sale, it was a one-line change in route_refund, covered by a unit test that passes three small state dicts and checks the three routes. No prompt was touched.
Follow-up questions to expect
- "Conditional edge or
Command?" — Conditional edge when routing is a separate rule from the node's work;Commandwhen the node itself already computed where to go. - "Can the routing function be async?" — Yes, but keep it cheap. If it needs a model call, make that a node that writes a field.
- "What if it returns a name that is not in the path map?" — The run fails with an error. Use
Literalreturn types and a default branch to avoid it.