Router pattern

1 min read
On trails:AGPATstep 2/6

A router agent is a delegation pattern, manager and worker. The router only does routing and management, and the specialised agents do the work.

A query goes to a router agent that only classifies it, then to one of three specialist agents, which produce the answer. Query Router classify, delegate Billing agent Tech agent Sales agent Answer
The router classifies and hands over. It never answers.

In LangGraph

The router is one function that decides and never answers, wired in with a single conditional edge. In a real system that function is an LLM call that classifies the query. Here it is a keyword stub.

Python
from typing import Literal, TypedDictfrom langgraph.graph import StateGraph, START, END class State(TypedDict):    query: str    answer: str def route(state: State) -> Literal["billing", "tech", "sales"]:    # In practice an LLM classifies the query. It only decides, it never answers.    q = state["query"].lower()    if "invoice" in q:        return "billing"    return "tech" if "error" in q else "sales" def billing(state: State): return {"answer": "billing agent handled it"}def tech(state: State):    return {"answer": "tech agent handled it"}def sales(state: State):   return {"answer": "sales agent handled it"} builder = StateGraph(State)for name, node in [("billing", billing), ("tech", tech), ("sales", sales)]:    builder.add_node(name, node)    builder.add_edge(name, END)builder.add_conditional_edges(START, route)graph = builder.compile()

LangGraph’s docs also show a router that fans out to several agents in parallel with Send and synthesises their answers. This one picks a single specialist.

Why it pays off

  • Maintainable. A new domain is a new specialist and a new route, not more rules in one giant prompt.
  • Performance and cost. Each specialist carries only its own prompt and tools, so each call stays small.
  • Testable. Every agent can be evaluated alone, and so can the router’s classification (see agent evaluationAgent evaluationTest the flow an agent took and what it left behind, against the goal and the guardrails.Opens in a new tab).

The catch

The router is only as good as its classification. A wrong route lands the query on an agent with the wrong tools, and that agent cannot fix it. Anthropic says the same of routing: it needs accurate classification. Test the router on its own.

Compare handoffHandoff patternAgents in a fixed sequence, each passing its output to the next.Opens in a new tab, where the order is fixed and nobody chooses. The same idea, applied to context and tools, is the dynamic expert agentThe dynamic expert agentPersonalize with what the agent knows about the user, then assemble the right expert for the question.Opens in a new tab.


Sources