LangGraph Agents

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
from typing import Literal, TypedDictfrom langgraph.graph import StateGraph, START, ENDclass State(TypedDict):    amount_inr: int    fraud_score: float    route: strdef route_refund(state: State) -> Literal["auto_refund", "human_review", "reject"]:    if state["fraud_score"] > 0.8:        return "reject"    if state["amount_inr"] > 5000 or state["fraud_score"] > 0.4:        return "human_review"    return "auto_refund"builder.add_conditional_edges(    "score",    route_refund,    {"auto_refund": "auto_refund", "human_review": "human_review", "reject": "reject"},)

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

ReturnEffect
"node_name"Run that node next
ENDFinish 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; Command when 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 Literal return types and a default branch to avoid it.