Simple definition#
Advanced RAG adds steps before, during and after retrieval to find better context: fix the question (pre-retrieval), search in more than one way (retrieval), and filter and order what was found (post-retrieval). Agentic RAG goes further and lets an LLM decide when, where and how many times to search.
The advanced RAG pipeline#
flowchart LR
Q["User question<br/>+ chat history"] --> PRE
subgraph PRE["Pre-retrieval"]
direction TB
Q1["Rewrite / condense"] --> Q2["Expand: multi-query, HyDE"]
Q2 --> Q3["Route: which source?"]
end
PRE --> RET
subgraph RET["Retrieval"]
direction TB
V["Vector search"] --> F["Fusion — RRF"]
K["Keyword search BM25"] --> F
F --> MF["Metadata & ACL filters"]
end
RET --> POST
subgraph POST["Post-retrieval"]
direction TB
RR["Rerank — cross-encoder"] --> CMP["Compress / dedupe"]
CMP --> ORD["Order & cite"]
end
POST --> G["Generate"]
Pre-retrieval: improve the query#
| Technique | What it does | Example |
|---|---|---|
| Query rewriting / condensing | Turns a follow-up into a standalone query using history | “What about for contractors?” → “What is the parental leave policy for contractors in Germany?” |
| Multi-query expansion | LLM generates 3–5 paraphrases; retrieve for each; merge | Catches different wording in documents |
| HyDE (Hypothetical Document Embeddings) | LLM writes a hypothetical answer; embed that and search | Answers look more like documents than questions do |
| Query decomposition | Splits multi-part questions into sub-questions | “Compare the 2025 and 2026 travel policies” → two retrievals |
| Step-back prompting | Ask a more general question first | “Which leave types exist?” before a specific case |
| Translation | Translate the query into the corpus language | User asks in French, documents in English |
| Routing | Choose the right index, source or tool | HR question → HR index; “my open tickets” → ticket API |
| Metadata extraction | Pull filters out of the question | “in 2025 for Germany” → year=2025, country=DE |
Hybrid search#
Dense (vector) search matches meaning; sparse (keyword/BM25) search matches exact terms. Enterprise questions need both: “error E-4031 when exporting invoices” has an exact code and a meaning.
flowchart LR
Q["Query"] --> D["Dense search<br/>top 50 by cosine"]
Q --> S["BM25 search<br/>top 50 by keywords"]
D --> RRF["RRF<br/>score = Σ 1 / (k + rank)<br/>k ≈ 60"]
S --> RRF
RRF --> T["Fused top 20"]
Reciprocal Rank Fusion uses ranks, not raw scores, so it combines systems whose scores are on different scales. A document ranked 1st by BM25 and 3rd by vectors gets 1/61 + 1/63 ≈ 0.032, beating a document ranked 2nd by only one system (1/62 ≈ 0.016).
Alternatives: weighted score fusion (normalise then blend), or learned sparse models such as SPLADE that combine both ideas.
Reranking#
Retrieval models (bi-encoders) embed query and document separately — fast but approximate. A reranker (cross-encoder) reads the query and a candidate together and outputs a relevance score — slower but much more accurate.
flowchart LR
A["Corpus<br/>millions of chunks"] -->|"Stage 1: fast<br/>bi-encoder + BM25"| B["Top 50 candidates<br/>high recall"]
B -->|"Stage 2: accurate<br/>cross-encoder reranker"| C["Top 5<br/>high precision"]
C --> LLM["LLM prompt"]
- Typical gain: one of the largest single quality improvements in RAG.
- Options: hosted rerank APIs (Cohere, Voyage, cloud providers), open-weight cross-encoders (BGE reranker, ms-marco models), or an LLM as reranker (expensive).
- Cost: adds ~50–300 ms; rerank only the top 20–100.
- ColBERT (late interaction) sits between bi- and cross-encoders: token-level matching with pre-computed document vectors.
Contextual retrieval#
Chunks lose context when cut from their document. Contextual retrieval uses an LLM at ingestion time to write one or two sentences situating each chunk (“This chunk is from ACME’s Q2 2026 10-Q, discussing revenue in the EMEA segment…”) and prepends it before embedding and BM25 indexing. Combined with reranking, this was reported to cut retrieval failures by roughly two thirds. Prompt caching makes the ingestion cost modest.
Post-retrieval: make the context better#
- Rerank and keep the top 3–8 chunks.
- Deduplicate near-identical chunks (versions, copies).
- Compress — extract only the relevant sentences, or summarise long chunks.
- Order — most relevant first (and possibly last), to counter “lost in the middle”.
- Expand — fetch parent sections or neighbouring chunks for context (small-to-big).
- Prefer fresh — use dates and version metadata; tell the model to prefer the newest source.
- Number sources for citations.
Self-correcting RAG#
Corrective RAG (CRAG) grades retrieved documents for relevance; if they are poor, it rewrites the query or falls back to another source (such as web or another index).
Self-RAG trains or prompts the model to decide whether retrieval is needed, to critique retrieved passages, and to check that its answer is supported.
flowchart TD
Q["Question"] --> R["Retrieve"]
R --> G{"Grade relevance<br/>LLM or classifier"}
G -->|"relevant"| GEN["Generate"]
G -->|"ambiguous"| RW["Rewrite query"] --> R
G -->|"irrelevant"| ALT["Search another source"] --> GEN
GEN --> H{"Answer grounded<br/>in sources?"}
H -->|"yes"| OUT["Answer + citations"]
H -->|"no"| RW
Put a limit on loops (for example two rewrites) so cost and latency stay bounded.
Agentic RAG#
In agentic RAG, retrieval becomes a tool that an agent can call as often as it likes, alongside other tools.
flowchart TD
U["Question: 'Did our Q3 incident<br/>breach any customer SLA?'"] --> A{"Agent<br/>plans next step"}
A -->|"search incidents"| T1["Tool: search_postmortems"]
A -->|"search contracts"| T2["Tool: search_contracts"]
A -->|"get metrics"| T3["Tool: query_sla_metrics SQL"]
T1 --> O["Observations"]
T2 --> O
T3 --> O
O --> A
A -->|"enough evidence"| ANS["Answer with citations"]
| Classic / advanced RAG | Agentic RAG | |
|---|---|---|
| Retrieval | Once, fixed pipeline | As many times as needed, chosen by the model |
| Sources | Usually one index | Many: indexes, SQL, APIs, web |
| Questions handled | Single-hop | Multi-hop, comparative, analytical |
| Latency / cost | Predictable, low | Higher, variable |
| Control | High | Lower — needs step limits and evaluation |
Use agentic RAG when questions genuinely need multiple steps or sources; keep a fixed pipeline for simple Q&A, where it is cheaper, faster and easier to test.
What to add first#
Ranked by typical impact per effort:
- Better parsing and structure-aware chunking with heading context.
- Hybrid search (BM25 + vectors).
- Reranking.
- Metadata filters (date, product, region, ACL).
- Query rewriting for multi-turn chat.
- Contextual retrieval.
- Multi-query / HyDE / decomposition for hard questions.
- Agentic or graph-based retrieval for multi-hop questions.
Measure each change with RAG evaluation; not every technique helps every corpus.
Common mistakes#
Review questions#
What is hybrid search and why use it? Running dense vector search and sparse keyword (BM25) search together and fusing results, typically with Reciprocal Rank Fusion. Vectors capture meaning; BM25 captures exact terms such as codes and names. Together they outperform either alone on enterprise text.
What does a reranker do and why not use it for the initial search? A cross-encoder that scores each query–document pair jointly, which is far more accurate than comparing separate embeddings. It is too slow to run over the whole corpus, so it reranks the top 20–100 candidates from fast retrieval.
Explain HyDE. Hypothetical Document Embeddings: the LLM writes a plausible answer to the query, and that text is embedded and used for search. Because it resembles the style of documents more than a short question does, it can retrieve better matches.
What is agentic RAG and when would you use it? An agent that uses retrieval as a tool, deciding which sources to query and iterating until it has enough evidence. Use it for multi-hop or analytical questions across multiple sources; avoid it for simple Q&A where a fixed pipeline is cheaper and more predictable.
How would you improve a RAG system with poor answer quality? Measure retrieval recall first. If retrieval is weak: fix parsing and chunking, add hybrid search, reranking, metadata filters, query rewriting, contextual retrieval. If retrieval is good but answers are poor: improve the prompt, context ordering and compression, model choice, and add faithfulness checks.
Summary#
- Pre-retrieval fixes the question; retrieval searches in multiple ways; post-retrieval filters and orders.
- Hybrid search + reranking is the highest-value upgrade for most systems.
- Contextual retrieval restores context lost by chunking.
- Corrective/self-RAG and agentic RAG add loops and tools for hard questions — with step limits.
- Add techniques one at a time and measure.
References#
- Cormack, Clarke, Büttcher, Reciprocal Rank Fusion outperforms Condorcet and individual rank learning methods, 2009.
- Gao et al., Precise Zero-Shot Dense Retrieval without Relevance Labels (HyDE), 2022.
- Yan et al., Corrective Retrieval Augmented Generation, 2024.
- Asai et al., Self-RAG, 2023.
- Khattab & Zaharia, ColBERT, 2020.
- Anthropic, Introducing Contextual Retrieval, 2024.