Routing Pattern: Send Each Request to the Right Agent
The routing pattern uses one cheap classification step to choose which specialist agent handles a request. Instead of one agent with a huge prompt that covers billing, technical support and sales, you write three focused agents and a router in front. The router reads the request, picks a label, and your code calls the matching specialist.
Why route
Specialists perform better because their prompts are short and specific, and their tool lists are small. Routing also lets you spend money wisely: easy requests go to a fast, cheap model, hard ones to a stronger model. Finally, routing makes behaviour auditable. You can log which route each request took and measure the quality per route.
Design the labels first
Router quality depends on the labels. Aim for a small set (3 to 7) of mutually exclusive categories with clear definitions, plus an explicit other label. Overlapping labels produce inconsistent routing. Write one sentence per label saying what belongs there and what does not.
Implementation
The router returns exactly one label. Code validates it against the allowed set and falls back to a general agent if the label is unknown. The specialists are a dictionary of system prompts here; in a real system each could have its own tools and model.
import anthropic
client = anthropic.Anthropic()
def call(system: str, user: str, model: str = "claude-sonnet-5-5") -> str:
r = client.messages.create(
model=model, max_tokens=2000, system=system,
messages=[{"role": "user", "content": user}],
)
return "".join(b.text for b in r.content if b.type == "text").strip()
ROUTER = """Classify the user's message into exactly one label.
billing: invoices, charges, refunds, payment methods.
technical: errors, bugs, how to use a feature.
sales: pricing questions, plans, demos.
other: anything else.
Reply with the label only, lowercase."""
SPECIALISTS = {
"billing": "You are a billing support agent. Be precise about amounts and dates. "
"Never promise a refund; explain the review process.",
"technical": "You are a technical support agent. Ask for the exact error message "
"if it is missing. Give numbered steps.",
"sales": "You are a sales assistant. Explain plans clearly. Do not invent prices; "
"say a sales rep will confirm them.",
"other": "You are a helpful general assistant. Keep answers short.",
}
def route(message: str) -> str:
label = call(ROUTER, message, model="claude-haiku-5-5").lower().strip(" .\n")
return label if label in SPECIALISTS else "other"
def handle(message: str) -> str:
label = route(message)
print("route:", label)
return call(SPECIALISTS[label], message)
print(handle("I was charged twice for my March subscription."))
print(handle("The export button throws a TypeError in Firefox."))
You should see route: billing then a billing-style answer, followed by route: technical and a step-by-step reply. The router uses claude-haiku-5-5, the smallest model, because classification is an easy job. The specialists use a stronger model.
Handling uncertainty
Some messages belong to two categories: "I was charged for a plan I cannot access". Options include returning a list of labels and calling several specialists, adding a clarify label that asks the user a question, or picking the primary label and letting the specialist hand off if needed. The simplest safe choice is a fallback route to a general agent, as in the code above.
Evaluating a router
A router is easy to test because its output is a label. Collect 50 to 100 real messages, label them by hand, and measure accuracy and a confusion table (which labels get mixed up). When two labels are often confused, merge them or sharpen their definitions. Re-run this test whenever you change the router prompt or add a label.
Common mistakes
- Too many labels. Past about seven, accuracy drops and definitions overlap.
- No fallback. A made-up label from the model will cause a
KeyErrorwithout the validation step. - Letting the router also answer. Keep it to classification so it stays cheap and testable.
- Forgetting that routers see user text, which may contain instructions. Constrain the output to the label set, and validate it in code.
- Never reviewing misroutes. Log the route and sample a few each week.
Routing combines well with other patterns: a router can send hard requests to an orchestrator-worker system and easy ones to a single call.
Cheap models, strong specialists
The router example already shows a useful cost pattern: a tiny model decides, a stronger model answers. Because every request passes through the router, its cost and latency are paid on every call, so keep its prompt short and its output to one word. The specialists are called once per request and can afford larger prompts and tools. If most traffic is simple, add a cheap "general" specialist on a small model, and send only the hard categories to the strongest one.
Routing on more than intent
Labels do not have to be topics. You can route on language, on the customer's plan, on estimated difficulty, or on whether the request needs a tool. Plain code can often route without a model: if the message contains an order number, go to the orders agent. Use rules first for the clear cases and a model for the ambiguous remainder. This hybrid is cheaper, faster and easier to test than routing everything with the model.
Comments
No comments yet. Have a question or a tip? Be the first to share it.