DevInterviewMasterStart free →
Agentic AI PatternsFree to read

Routing Pattern

Send each question to the right expert

Picture calling a company helpline. A friendly voice asks, "Press 1 for billing, 2 for tech support, 3 for sales." That menu is a router : it figures out what you need and sends you to the right person. Routing does the same for an LLM — it looks at the input first, decides what kind of request it is, then hands it to the specialist built for that kind.

Key points

The one-line definition

Routing is a workflow where a classifier (often an LLM) reads the input, decides which category it belongs to, and forwards it to the specialised handler for that category — a dedicated prompt, set of tools, or model tuned for that job.

Note: Classify first, then dispatch to the right specialist. Like a receptionist.

The router sends each input down one branch

USER MESSAGE │ ▼ ┌─────────────────┐ │ ROUTER 🚦 │ │ (LLM classifier)│ │ what kind is it?│ └───┬────┬────┬───┘ refund │ │ │ sales ┌────────┘ │ └────────┐ ▼ ▼ ▼ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ REFUND │ │ TECH │ │ SALES │ │ handler │ │ SUPPORT │ │ handler │ └────┬─────┘ └────┬─────┘ └────┬─────┘ │ │ │ └─────────────┼─────────────┘ ▼ ✅ ANSWER

Chaining vs Routing (the key difference)

PROMPT CHAINING (straight line) ROUTING (pick a branch) ───────────────────────────── ───────────────────────

INPUT INPUT │ │ ▼ ▼ ┌───────┐ ┌──────────┐ │ step1 │ │ ROUTER │ └───┬───┘ └─┬──┬──┬──┘ ▼ ▼ ▼ ▼ ┌───────┐ A B C (one wins) │ step2 │ └───┬───┘ same steps different handler ▼ every time per input type ┌───────┐ │ step3 │ └───────┘

A tiny code example (read it like English)

Step one asks the LLM to label the message. Step two simply looks up the matching specialist and runs it. The router never answers the question itself.

HANDLERS = {
    "refund":  lambda m: llm(f"You are a refunds agent. Help: {m}"),
    "tech":    lambda m: llm(f"You are tech support. Help: {m}"),
    "sales":   lambda m: llm(f"You are a sales rep. Help: {m}"),
}

def route(message):
    # 1) classify the input into a known category
    label = llm(
        f"Classify as one word (refund/tech/sales): {message}"
    ).strip().lower()

    # 2) dispatch to the specialist (fall back if unknown)
    handler = HANDLERS.get(label, HANDLERS["tech"])
    return handler(message)

When should you use routing?

ScenarioRecommendationWhy
Inputs fall into clearly different types needing different handling✅ Use routingEach type gets a focused specialist instead of one bloated prompt.
You want to send easy questions to a cheap model, hard ones to a strong model✅ Use routingRouting by difficulty saves money without losing quality.
Every input needs the exact same processing❌ Don't routeThere's nothing to branch on; just call one prompt.
The categories overlap heavily and are hard to tell apart⚠️ Be carefulA wrong classification sends the user to the wrong specialist.

Routing mistakes beginners make

MistakeConsequenceFix
No fallback for when the classifier returns something unexpected.The code crashes or silently drops the user's request.Always have a default handler for unknown or ambiguous labels.
Too many fuzzy, overlapping categories.The router misclassifies and sends users to the wrong specialist.Use a few clear, distinct categories with good descriptions/examples.
Letting the router also try to answer the question.It does two jobs poorly and you lose the specialist benefit.Keep the router's only job to classify; let specialists do the answering.

Remember these lines

Key takeaways

Frequently Asked Questions

What is Routing Pattern?

Picture calling a company helpline. A friendly voice asks, "Press 1 for billing, 2 for tech support, 3 for sales." That menu is a router : it figures out what you need and sends you to the right person.

How does Routing Pattern work?

Routing is a workflow where a classifier (often an LLM) reads the input, decides which category it belongs to, and forwards it to the specialised handler for that category — a dedicated prompt, set of tools, or model tuned for that job.

What are the key takeaways about Routing Pattern?

Routing reads the input, classifies it, and sends it to the right specialised handler. Each handler has its own prompt, tools, or even model tuned for that category. Unlike chaining's straight line, routing picks one branch per input. Always include a fallback handler and keep categories clear and distinct.

Browse all Agentic AI Patterns topics →

Practice this on DevInterviewMaster

Read the full Routing Pattern breakdown with interactive demos, quizzes, and Hinglish notes.

Open the interactive topic →

800+ system-design, LLD, coding, and design-pattern topics. Unlock everything with Pro (₹499, one-time) or Ultimate (₹999, one-time) — lifetime access, no subscription.