

How it works
- A node (or nodes) computes values and writes them into the graph
state. - Define a pure routing function that reads that
stateand returns a routing key (for example,"search","answer", or"clarify"). - Register conditional edges with a
path_mapthat maps routing keys to target node IDs. - The router only chooses the path; nodes perform work and update
state.

Anatomy of conditional routing
You need:- A graph
statewith explicit keys (for example,intent). - One or more nodes that compute or set values in
state. - A pure routing function that reads
stateand returns a route key. - A conditional edge that maps keys to target node IDs.
- A “choose action” node runs an LLM or classifier and sets
state["intent"]. - A router function reads
state["intent"]and returns that value. - The builder registers conditional edges mapping intent values to node IDs.
state["intent"] = "search"). The router must remain pure and only read state and return a routing key.

state; the router routes on those values.

What can you route on?
Use conditional routing for a wide range of signals:
You can also implement more advanced behaviors: fallback logic, confidence thresholds, multi-step evaluations — as long as the routing function returns a known key, LangGraph will map it to the correct node.

Minimal example flow
- A “Choose Action” node sets
state["intent"]via an LLM or classifier. - A router reads
state["intent"]and returns the routing key. - Execution continues at the node mapped to that key.
state manually to different intent values and verifying the graph follows different paths.

Chatbot example
Example runtime flow:- User: “Can you look up the LangGraph docs?”
- Node 1: Classify intent → sets
state["intent"] = "search" - Router: Sends execution to
search_node search_node: Calls a search tool and stores results instategenerate_node: Summarizes found documents into a response
clarify, the router can direct to a node that prompts the user for more details. This pattern scales: add more intent types and nodes as needed.

Best practices
- Use explicit state keys such as
intent,decision, orroute_choice. - Keep routing functions pure: avoid side effects and do not call LLMs inside them.
- Log routing decisions to make flows observable and debuggable.
- Avoid embedding routing logic inside nodes; use
add_conditional_edgesto keep routing separate. - Design routers as small, pluggable functions so they can be reused across graphs and contexts (role-based routing, language switching, escalation).

Do not perform side effects or call external services (including LLMs) inside your router function. Put that work in preceding nodes and let the router only read state and return a key.
add_conditional_edges keeps graphs maintainable and easier to reason about — a must for production systems.

Design routers as pure, pluggable functions so they can be reused across graphs and contexts — for example, role-based routing, language switching, or escalation flows.
Summary
The router is the brain of a LangGraph: it decides which work to run next but does not perform the work itself. By learning how to set and read state and return routing keys from small, pure functions, you can build smart branching flows that respond to user needs, tool outputs, and more. Conditional routing adds intelligence without unnecessary complexity and scales to advanced use cases like loops and persistent state.Links and references
- LangChain LLMChain documentation
- LangGraph conditional routing concepts and best practices (internal docs and examples)