By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A normal edge always goes to the same place. With a conditional edge, a router function reads the state and returns a label; a mapping turns that label into the next node. Branches and loops are both just labels, and the decision lives in a named, testable function instead of inside a prompt or a deeply nested if.
The equivalent script is a while loop with a chain of ifs inside it. It works for the happy path and becomes unmaintainable exactly when support gets hard: a retry for a flaky tool, a clarification turn, a refund above a threshold, a pause for approval. Each special case adds a branch, and none of them is a named decision you can test on its own.
In a graph the loop is an edge, and the decision is a small function from state to label. You can unit-test the router with plain dicts, draw the branch structure for a review, add a checkpoint after each pass, and change the retry policy without touching the lookup node.
from typing import Literal, TypedDict
from langgraph.graph import END, START, StateGraph
class LookupState(TypedDict):
order_id: str
lookups: int
found: bool
def lookup(state: LookupState) -> dict:
# A real node would call a read-only tool or a model here.
lookups = state["lookups"] + 1
return {"lookups": lookups, "found": lookups >= 2}
def route_after_lookup(state: LookupState) -> Literal["lookup", "done"]:
return "done" if state["found"] else "lookup"
builder = StateGraph(LookupState)
builder.add_node("lookup", lookup)
builder.add_edge(START, "lookup")
builder.add_conditional_edges(
"lookup", route_after_lookup, {"lookup": "lookup", "done": END}
)
graph = builder.compile()
result = graph.invoke({"order_id": "4521", "lookups": 0, "found": False})
print(result)Every run has a recursion_limit — 25 supersteps by default. Each step of the graph counts, including loop passes, and exceeding the limit raises GraphRecursionError instead of hanging forever. Set it per invocation in the config: graph.invoke(inputs, {"recursion_limit": 10}), or alongside a checkpointer config: {"configurable": {"thread_id": "..."}, "recursion_limit": 10}.
The limit catches bugs; it is not a retry policy. A good loop keeps a meaningful progress condition in its state — an attempts counter, whether the last lookup returned data, how many consecutive failures there have been — and routes to a fallback or END when that condition says stop. Then a GraphRecursionError means something genuinely unexpected happened, and the checkpoint history shows you which node repeated.
attempts field in the same node that performs the work, and assert on it in tests.found, last_tool_succeeded, refund_approved: a boolean the router can read without guessing.recursion_limit at a sane value, log when it fires, and treat that log as a bug report about your loop.Key takeaways
A conditional edge asks a router for a label and a mapping turns that label into the next node.
Branches and loops are both edges; the router is a pure function you can unit-test.
recursion_limit (default 25 supersteps) is a backstop that raises GraphRecursionError; it is not a retry policy.
A safe loop carries a progress condition in state and has a fallback for when attempts run out.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.