Lesson 3.4 · 8 min read · about 55 min to master at a very slow pace

Beyond Basic RAG: Query Rewriting, Filters and Freshness

The upgrades that matter most once basic RAG works: rewriting conversational queries, splitting multi-part questions, metadata and permission filters, keeping the index fresh, and when an agent should decide what to search.

Not startedJump to quiz

Query rewriting

In chat, users write follow-ups: "And for contractors?" Embedding that alone retrieves nothing useful. Before retrieval, ask a small, fast LLM to rewrite the query as a standalone question using the conversation:

History: "What is the meal allowance in Germany?"
Follow-up: "And for contractors?"
Rewritten: "What is the meal allowance in Germany for contractors?"

This one step fixes a large share of failures in conversational RAG.

Multi-part and vague questions

  • Decomposition: "Compare the 2025 and 2026 travel policies" becomes two searches, one per year, whose results are merged.
  • Multi-query: generate 3–4 phrasings of the question, search each, and fuse the results with RRF. Helps when users and documents use different vocabulary.
  • HyDE (hypothetical document embeddings): have the LLM write a short hypothetical answer, and search with its embedding, which often looks more like the documents than the question does.

Each adds an LLM call (latency and cost), so add them only when your evals show the failure they fix.

Filters and permissions

Metadata filters narrow the search before ranking: year, product, country, document type. Permissions are a filter you must never skip: an HR assistant must not retrieve salary documents for a user who cannot open them.

Enforce access in retrieval, not in the prompt

"Do not reveal confidential documents" in the prompt is not access control: the model has already seen the text and can be talked into repeating it. Filter by the user's permissions in the search query, so forbidden chunks are never retrieved.

Keeping the index fresh

  • Re-index documents when they change (track a hash or modified date); delete chunks of removed documents.
  • Store version and date; prefer or filter to the latest when questions are about "current" rules.
  • Monitor index lag: the time between a document changing and the index reflecting it.

Agentic RAG

Instead of one fixed retrieval step, give the LLM a search tool and let it decide what to search, read the results, and search again if needed (Section 4). This handles research-style questions ("Which suppliers had late deliveries in both Q1 and Q3?") but is slower, costlier and harder to evaluate. Use a fixed pipeline for the common questions and an agent for the hard tail.

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

First passDoes query rewriting change what the user sees?

No, the user still sees their own message. The rewritten query is used only for retrieval (and logged, so you can debug it). Some teams also pass the rewritten query to the answering model for clarity.

Going deeperWhat about structured data like sales figures?

Embeddings are poor at "total sales in Q3 for Slovenia". For numbers in databases, give the model a tool that runs a safe, read-only query (text-to-SQL with guardrails, or predefined parameterised queries), and keep RAG for text. Many real assistants combine both.

Cheatsheet

Query rewriting

Turn follow-ups into standalone questions with a small LLM before retrieval. Big win for chat.

Multi-query vs HyDE

Multi-query: several phrasings, fused with RRF. HyDE: search with the embedding of a hypothetical answer.

Prompt is not access control

Filter by user permissions in the search itself, so forbidden chunks are never retrieved.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1applyAfter "What's the meal allowance in Germany?", a user asks "And for contractors?" Retrieval returns nothing relevant. What is the fix?
Q2conceptHow does HyDE (hypothetical document embeddings) improve retrieval?
Q3diagnoseAn HR assistant's prompt says "never reveal salary data", yet a user extracts salary figures with a clever question. What is the real flaw?
Q4applyUsers ask "Compare our 2025 and 2026 travel policies." Basic retrieval returns only 2026 chunks. What helps most?
Q5interpretWhen is agentic RAG (letting the model decide what to search) worth its extra cost?