Skip to content
mentoor

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?

Choose one answer

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

  1. List the routes explicitly, each with a one-line description and an example: docs, sql, order_api, none, and a fallback.
  2. 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.
  3. 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.
  4. Allow multiple routes when a question needs both, such as a policy and a number. Route lists compose with decomposition.
  5. 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 docs and sql both 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?

Choose one answer

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?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic