Topic 3 of 5 · 5 min read
Routing a question to the right index or tool
Goal: Design a router with explicit routes and a fallback, and find the misrouted query in a routing log.
A router classifies each query by intent and sends it to the one source that can answer it: a document index, a database, an API, or nowhere. Searching the manuals for "how many orders did we ship yesterday?" returns plausible paragraphs and no number.
Check yourself · 1 of 3
Which question should a router send to a SQL source rather than the document index?
Why it matters
Real systems have several sources. Policies live in documents, order counts in SQL, live shipping status behind an API. Greetings and "thanks" need no retrieval at all. A single vector index for everything gives wrong answers on the structured questions and wastes a search on the trivial ones.
How to do it
- List the routes explicitly, each with a one-line description and an example:
docs,sql,order_api,none, and afallback. - Classify with structured output. Use a JSON schema with an enum so the model can only return a known route. See the structured-output docs in the references.
- Add confidence and a fallback. When confidence is low, or the query fits two routes, take the fallback: search the default docs index, or ask the user a clarifying question.
- Allow multiple routes when a question needs both, such as a policy and a number. Route lists compose with decomposition.
- Log every decision with the query, route, confidence and result count. Review the log weekly; misroutes show up as empty or irrelevant results.
const RouteSchema = {
type: "object",
properties: {
route: { enum: ["docs", "sql", "order_api", "none"] },
confidence: { type: "number" },
},
required: ["route", "confidence"],
additionalProperties: false,
};
Before and after
Weak: everything goes to the vector index.
Query: "status of order 88213" -> docs index -> 3 chunks about the returns policy
Fixed: the router recognises an identifier and a status request.
Query: "status of order 88213" -> route=order_api (0.96) -> {"status":"shipped"}
What goes wrong
- Overlapping route descriptions. If
docsandsqlboth say "questions about sales", the model flips between them. Write non-overlapping descriptions with examples of the boundary. - No fallback. An off-list query is forced into the nearest route and fails quietly.
- Routing on keywords instead of intent. "Cancel" in "cancel policy" and "cancel my order" needs different routes.
- Stale routes: a new source ships but the router prompt is never updated, so its questions go to the docs index.
Examples
Four routes, one fallback
Routes: docs for policy text, sql for aggregate numbers, order_api for one order's live status, none for chit-chat. A low-confidence result routes to docs and the answer says what was searched.
Spotting a misroute in the log
Log line: q="how many refunds last month" route=docs conf=0.55 results=4 top_score=0.31. A count question went to the document index with a weak score. It belongs to sql; the route description for sql needs a counting example.
Key points
- A router picks the source that can answer: documents, SQL, an API, or no retrieval, instead of searching one index for everything.
- Use structured output with an enum of routes so the model can only return a route you handle.
- Always define a fallback and use it on low confidence or ambiguity rather than forcing the nearest route.
- Write non-overlapping route descriptions with boundary examples; overlapping ones cause flip-flopping.
- Log query, route, confidence and result count; misroutes show up as empty or irrelevant results.
Common mistakes
- Sending every query to one vector index regardless of what it asks.
- Letting the router return free text, which then fails to match any known route.
- Having no fallback route for low-confidence or unmatched queries.
- Writing overlapping route descriptions and never checking the routing log.
Check yourself · 2 of 3
Why define the router's output as a JSON schema with an enum of routes?
Check yourself · 3 of 3
The routing log shows "cancel my subscription" routed to docs and "cancel policy details" routed to order_api. What is the likely cause?
Practice items, not counted towards your mastery.
Start here
- Precise Zero-Shot Dense Retrieval without Relevance Labels (HyDE) (opens in a new tab)PaperFree
Introduces searching with a generated hypothetical answer instead of the raw question.
- Query Rewriting for Retrieval-Augmented Large Language Models (opens in a new tab)PaperFree
Shows rewriting the query before retrieval as its own trainable step in a RAG pipeline.
- Measuring and Narrowing the Compositionality Gap in Language Models (Self-Ask) (opens in a new tab)PaperFree
Explains why multi-part questions fail and how asking sub-questions closes the gap.
More for this topic
- Structured model outputs (OpenAI API docs) (opens in a new tab)DocsFree
How to force model output to match a JSON schema, which routers and filter extractors depend on.
- Tool use overview (Claude docs) (opens in a new tab)DocsFree
How a model chooses among tools, the same decision a query router makes.