Course contentsShow
← Machine Learning and Deep Learning
Lesson 2019 of 3,53843. Retrieval-Augmented Generation: Advanced TechniquesFree lesson

Query Routing and Classification

Determining which retrieval strategy or knowledge source to use based on query type and intent classification.

Query Routing and Classification

What You'll Learn

You'll learn how to automatically classify incoming queries and route them to the most appropriate retrieval strategy or knowledge source—ensuring your RAG system uses the right tool for each question.

The Problem: One Size Doesn't Fit All

Not all queries should follow the same retrieval path. A factual question like "What is the capital of France?" needs different handling than a complex analytical query like "Compare the economic policies of three different administrations." Yet naive RAG systems treat them identically.

Query routing solves this by acting as an intelligent dispatcher—analyzing each query's intent and characteristics, then directing it to the optimal retrieval strategy, knowledge base, or even skipping retrieval entirely when the LLM already knows the answer.

How Query Classification Works

Think of it like a restaurant host who reads the customer (query) and seats them at the right table (retrieval strategy):

  1. Intent Classification: Categorize the query type (factual lookup, comparison, summarization, calculation)
  2. Complexity Assessment: Determine if it needs multi-step retrieval, single-pass vector search, or keyword matching
  3. Domain Detection: Identify which knowledge base or document collection is most relevant
  4. Retrieval Strategy Selection: Route to dense retrieval, hybrid search, or even external APIs

For example:

  • Simple factual queries → BM25 keyword search
  • Semantic queries → Dense vector retrieval
  • Recent events → Time-filtered vector search
  • Math problems → Skip retrieval, use LLM directly

The classifier itself is often an LLM prompted to categorize queries, or a lightweight fine-tuned model for speed.

Why It Matters

Query routing dramatically improves both accuracy and efficiency. You avoid expensive vector searches when unnecessary, use specialized indexes when beneficial, and prevent irrelevant context from polluting generation.

Key Takeaway: Query routing and classification intelligently directs each question to the retrieval strategy or knowledge source best suited to answer it, optimizing both quality and resource usage.