Routing with a router node
A router is a classification step that sends each question to one specialist and then steps aside.
Last updated: 27 Sep, 2026 · LangChain 1.4
The supervisor in lesson 30 is a model deciding every turn. A router is a single classification step, often one model call or plain rules, that sends the question on and steps out of the way. With plain rules it is ordinary Python. It is cheaper and more predictable, and it cannot combine answers.
Pick one to watch it run, step by step.
The two specialists come from the previous lesson: orders_agent looks up orders and policies_agent answers from the policy documents. Here a router sends each question to one of them.
The route function
def route(question):
if some_rule(question): # one classification step
return "orders", orders_agent
return "policies", policies_agent # the default specialistRouting by an order id
route picks a specialist by looking for an order id. Nothing decides here except a regular expression.
import re
from specialists import last_reply, orders_agent, policies_agent
def route(question):
if re.search(r"\b[A-Z]\d+\b", question): # an order id, like A17
return "orders", orders_agent
return "policies", policies_agent # everything elseThe answer function
answer hands the question to the chosen agent and returns which one replied.
def answer(question):
name, agent = route(question) # pick one specialist
return name, last_reply(agent, question) # pass the question straight throughRunning three questions through the router
Three questions through the router: two that fit one specialist, and one that needs two.
from router import answer
for question in ["Where is A17?", "Is shipping free?", "Where is A17, and how long does a refund take?"]:
name, reply = answer(question)
print(f"{name:<8} {reply.splitlines()[-1]}")Where the router hands off, and where it slips
- The first two questions reached the right agent with no model deciding anything; a rule did it.
- The third shows the limit: it mentions an order, so the whole sentence went to the orders agent alone.
- That agent read "refund" as a refund request, a tool it does not have, so neither half was answered.
- A router makes one choice: a question that needs two agents needs the supervisor from lesson 30, or a router that fans out and merges the answers, which the docs build with LangGraph.
Router vs supervisor
| Router | Supervisor | |
|---|---|---|
| Decision | One step, up front | The model, every turn |
| Cost | One call, or plain rules | A model call per turn |
| Two areas at once | Cannot combine | Asks each and joins |
| Predictable | Same route every time with rules | Depends on the model |
When a router beats a supervisor
- Sending each question to exactly one specialist by its topic.
- A cheap first pass that escalates only the hard cases to a supervisor.
Related
- Previous: Subagents as tools
- Next: Testing an agent
- Reference: Multi-agent
- Route questions that contain "refund" and an order id to the policies agent, and test both kinds.
- Add a third specialist for account questions and a rule for it.
- Print how many model calls
answermakes for one question.
Every expert started right here.