Query Routing

Definition

Directing a query to one of several downstream paths based on its intent. Sami Maameri’s framing is deliberately unglamorous: “routers are essentially just If/Else statements we can use to direct the control flow.”

Routing decides which index, tool, prompt or engine handles a query — as distinct from Query Classification, which assigns a query to a taxonomy label. Classification is a labelling task; routing is a control-flow decision that may or may not use a classifier to make it.

Why It Matters

Not every query should hit the vector index. Some are better served by structured filters, some by a keyword engine, some by a tool call, some by no retrieval at all. A single fixed pipeline forces one answer for every query; a router lets a system be several pipelines wearing a trenchcoat. It is the mechanism behind the “should we retrieve?” decision in LLM Guardrails and the per-query strategy choice in Agentic Search.

Router Types

TypeHow it decidesCost
LLM completion routerModel outputs one of a set of wordsLLM call
LLM function-calling routerModel picks a route via function callingLLM call
Semantic routerEmbedding similarity against example utterancesEmbedding only — cheap
Zero-shot classification routerZero-shot model assigns a label from a setModel inference
Language classification routerDetects query language, routes accordinglyCheap (langdetect)
Keyword routerMatches keywords against route listsTrivial
Logical routerDiscrete checks — string length, file type, value comparisonsTrivial

The list is ordered roughly by cost, and the practical lesson is that the cheap end is underused: language detection and logical checks resolve a large share of routing decisions without any model call.

Implementations

  • LlamaIndex — LLM Selector router, Pydantic Router
  • Haystack (deepset)ZeroShotTextRouter, TextClassificationRouter, ConditionalRouter, FileTypeRouter
  • LangChain
  • semantic-router (standalone Python package), OpenAI Encoder, Hugging Face models, langdetect

Articles