Skip to content

Courses / RAG Systems / Module 7

Module 7: Query understanding: rewriting, HyDE, multi-query, decomposition, routing

8 h · theory 2 · hands-on 3 · project 2 · revision 1

You will be able to

  • Rewrite conversational follow-ups into standalone queries
  • Use HyDE and multi-query where they help
  • Decompose multi-part questions
  • Route queries to the right index or tool

Capstone increment: Query rewriter and router in front of the retriever, each measured on the golden set.

Job-description skills: Query understanding · Lab tools: openai, pydantic

1. Users don’t write search queries

Real questions are follow-ups (“and for interns?”), vague (“leave rules”), multi-part (“compare the 2023 and 2025 travel policy”), phrased in different words from the documents, or not questions for your documents at all (“how many tickets did we close last week?”). Retrieving on the raw text fails on all of these.

Query understanding is a small pipeline in front of the retriever: rewrite, route, and sometimes expand or decompose. Each step costs a model call and latency, so each one has to earn its place on your golden set.

User turn“and for interns?”Rewritestandalone questionRouteclassify intentRetrievehybrid + re-rankAnswerwith citationsPolicies indexdefaultSQL / APInumbers, statusDecomposemulti-part questionsMulti-query / HyDEvague questionsBefore retrievalRoutes, each measured on the golden set
Every rewrite costs a model call and some latency. Keep a step only if it moves your retrieval metrics.

2. Rewrite follow-ups into standalone questions

In a conversation, the current turn often only makes sense with the previous ones. A condense-question step asks a fast model to rewrite the latest turn, given the recent history, into a standalone query that keeps every constraint (“How many sick leave days do interns get?”). Use structured output so the rewrite is a field you can log and evaluate.

Measure it on a labelled set of follow-ups: does retrieval on the rewrite find the right chunks? Watch for rewrites that drop a constraint (“for interns”) or add one the user never said.

Common misconception: “Just send the whole chat history as the query.” Old turns pull retrieval towards earlier topics. Rewrite to one standalone question instead.

3. HyDE and multi-query

HyDE (Gao and others, 2022) asks a model to write a hypothetical answer and embeds that instead of the question, because a fake answer looks more like real answers than a short question does. It helps when queries are short or phrased very differently from the documents; it can hurt when the model invents specifics that pull retrieval the wrong way.

Multi-query generates three to five rephrasings, retrieves for each and fuses the lists with RRF (often called RAG-Fusion). It improves recall on vague questions at the cost of several retrievals and one generation call. Step-back prompting asks a more general question first (“what is the leave policy?”) to fetch background before the specific one.

Tip: Apply expansion only to the segment that needs it. A router that sends precise queries straight to retrieval keeps latency low for most users.

4. Decompose multi-part questions

“How do annual leave rules differ between the 2023 and 2025 policies?” needs two retrievals, one per policy year, then an answer that compares them. Decomposition asks the model for sub-questions, retrieves for each, and answers with all the contexts, citing each.

Independent sub-questions can run in parallel. When one answer is needed to ask the next (“who approves claims for the team that owns billing?”), the question is multi-hop, and the agentic RAG patterns of Module 13 fit better.

5. Route to the right place

Not every question belongs in the vector index. Questions about counts and trends belong in SQL over the ticket database; status questions belong in an API; small talk needs no retrieval at all; some questions should go to a different index or carry filters (year, document type, region) extracted from the text, sometimes called self-querying.

Start with rules you can test, then add an LLM classifier with a fixed output schema for what rules can’t decide, or an embedding-similarity router over example queries per route. Always keep a default route and log the decision with its reason. Measure routing accuracy on labelled queries, and track it per route.

In production: Routers fail silently: a misrouted question gets a confident answer from the wrong source. Log the route with every answer and review a sample weekly.

Hands-on: coding window

The coding window and the full VS Code lab are available after you sign in.

Sign in to code

Labs

Lab 7.1: Condense-question rewriter

Build an LLM rewriter with structured output that turns the latest turn plus history into a standalone query. Label 20 follow-up questions and compare recall@5 with and without it.

Expected: Recall with and without rewriting, and three examples where the rewrite dropped or invented a constraint.

Lab 7.2: HyDE and multi-query

On the vague-question segment of the golden set, compare baseline, HyDE and multi-query (4 rephrasings with RRF). Record latency and cost per question for each.

Expected: A table of recall, latency and cost; a decision on which segment gets which method.

Lab 7.3: Router (capstone increment)

Add src/query/route.py routing to policies, tickets (SQL) or no retrieval, with year and doc-type filters, and a labelled set of 40 queries in tests/.

Expected: Routing accuracy of at least 90% in a pytest test that runs in CI.

Go deeper

Videos

Further reading

GitHub repositories

  • langchain-ai/rag-from-scratch: Notebooks 5 to 10: query translation (multi-query, RAG-Fusion, decomposition, step-back, HyDE) and routing.
  • texttron/hyde: The original HyDE code; read how the hypothetical documents are generated and averaged.
  • run-llama/llama_index: Router query engines and query transformations in the docs’ advanced retrieval section.

Checkpoint quiz

1. “And for interns?” retrieves nothing useful. Best fix?
2. When does HyDE help most?
3. What does multi-query cost you?
4. “How many incidents did we close last week?” should go to…
5. How do you know a router works?

Interview questions

  1. How do you handle follow-up questions in a conversational RAG system?
  2. When would you use HyDE, multi-query or decomposition? What does each cost?
  3. Design a router that sends questions to documents, SQL or an API.
  4. How do you extract metadata filters from a natural-language question safely?
  5. Your rewriter improved recall overall but broke one segment. What do you do?

Ask the professor

Sign in to ask questions about this module.

Discussion

Community discussion appears here once giscus is configured (see README).