Skip to main content
LearnMLAcademy
Intermediate10 min read

Advanced RAG: Chunking, Retrieval, Reranking & Evaluation

Optimize RAG pipelines with chunking, rerankers, and hybrid search, with token walkthroughs, prompt design tasks, and retrieval exercises.

What you will master in this lesson
Intuitive core mechanisms & practical real-world intuition
Step-by-step mathematical breakdown & visual diagrams
Runnable Python implementation & practical trade-offs

Basic RAG works. Why does retrieval still fail?

A document may be split at the wrong place, an exact product code may be missed by semantic search, or a relevant passage may appear in the top 20 but be pushed out by a reranker.

Advanced RAG is mostly about improving and measuring the path from source document → candidate passages → final context.

Chunking decides what unit can be retrieved

If chunks are too large, one vector may represent several unrelated ideas and consume too much context. If chunks are too small, the answer may be split away from the surrounding explanation.

Chunking choices

Chunking choices
ChoicePotential benefitPotential cost
Smaller chunksFocused retrievalCan lose surrounding context
Larger chunksMore context stays togetherMay mix topics and waste token budget
OverlapPreserves information across boundariesDuplicates text and increases index/context cost

Chunk boundaries change what retrieval can prove

Too small

…up to 5 days…

High focus, but region, date, and exceptions are lost

Useful unit

India leave policy · 2026 · carry forward up to 5 days

Question and conditions stay together

Too large

Leave + payroll + travel + expenses · 4 pages

Useful text is diluted and consumes more context

precision may rise · completeness fallsbalanced evidence unitsurrounding context rises · noise/cost rise
Chunk size and overlap are evaluation choices. The useful unit preserves the answer together with its scope; overlap can rescue boundary text but duplicates index and context content.

Hybrid retrieval uses more than one signal

A query such as “INV-009381 late-payment policy” contains both an exact identifier and a semantic description. Lexical search is strong for the code; dense retrieval is strong for related meaning. Hybrid retrieval combines both candidate sources.

Reranking asks a more expensive second question

  1. Retrieve broadly

    Use dense, lexical, or hybrid retrieval to get perhaps the top 20 candidates.

  2. Rerank narrowly

    Apply a more precise scoring model to those candidates.

  3. Assemble context

    Keep the strongest evidence that fits the context budget.

  4. Generate

    Ask the LLM to answer using the selected evidence.

This separate illustrative pipeline uses two candidate lists of up to 20 results and then selects five context passages. These are tunable example counts, not recommended defaults. Read the narrowing path as a sequence of decisions about which evidence deserves the limited context space.

Retrieve broadly, rerank deeply, send only the strongest context

10,000 chunks

authorized searchable corpus

Dense top 20

semantic candidates

Sparse top 20

exact lexical candidates

Hybrid fusion + metadata scope

combine complementary signals

Reranker

deeper query-document scoring

Top 5 context → LLM

smallest strong evidence set

All counts are illustrative and developer-chosen; production candidate depths must be evaluated.

Measure retrieval separately from generation

Suppose an evaluation query has 2 known relevant chunks. If only 1 appears in the top 3 retrieved results:

Recall@3 = relevant chunks retrieved in top 3 / all known relevant chunks = 1 / 2 = 0.50
Recall@k measures whether retrieval found the known relevant items. It does not prove the final generated answer is correct.

If Recall@3 improves from 0.50 to 1.00 for that case, retrieval found both labelled relevant chunks in the top 3. Generation, citation support, latency, and safety still need separate evaluation.

Why the next lesson matters

We can now measure retrieval quality. How do we evaluate the whole LLM application—including answer quality, latency, structure, safety, and product constraints?

LLM Evaluation →

Summary / Key Takeaways

  • Chunk size determines the unit of retrieval; overlap can preserve boundary context but duplicates content.
  • Hybrid retrieval combines lexical and dense signals when exact terms and semantic meaning both matter.
  • Reranking applies a more precise second-stage score to a smaller candidate set.
  • Debug the first stage where the relevant evidence disappears instead of changing unrelated components.
  • Recall@k measures retrieval coverage of known relevant items; 1 of 2 relevant chunks retrieved gives Recall@3 = 0.50 in the example.
  • Better retrieval metrics do not by themselves prove the final LLM answer is correct.

Apply this concept in a real hands-on project

You have studied the idea. Now use it in working Python code, see the calculation and output, and explain what changed. Each project includes step-by-step instructions, full source and tests.

Practice challenge: predict an outcome first, run the code, and explain the difference.

Track Your Progress0% curriculum done

Finished "Advanced RAG: Chunking, Retrieval, Reranking & Evaluation"?

Mark this lesson as completed to update your progress bar and track your milestones across the full curriculum.

Overall Progress0 / 167 lessons

Was this tutorial helpful?

Your feedback helps us continuously refine and improve our open-access tutorials.

Knowledge Check

Progress: 0/5

Test your active recall. Choose one answer for each question, then check it to reveal the step-by-step reasoning.

Question 1 of 5conceptual
Why can overlapping chunks help retrieval?
Question 2 of 5practical selection
A user query contains an exact product code plus a natural-language description. Which retrieval design is a strong candidate?
Question 3 of 5numerical/formula
A labelled query has 2 known relevant chunks. One appears in the top 3. What is Recall@3?
Question 4 of 5interpretation
What does improving Recall@3 from 0.5 to 1.0 prove?
Question 5 of 5debugging/diagnostic
The relevant chunk is retrieved in top 20 but disappears after reranking. Where should debugging focus next?

Knowledge Check Review & Explanations

Review the questions, verified correct answers, and conceptual explanations for this lesson.

1Why can overlapping chunks help retrieval?▼

Correct Answer: They can preserve context that would otherwise be split across a boundary

Overlap can retain boundary context, though it also duplicates text and consumes storage/context.

2A user query contains an exact product code plus a natural-language description. Which retrieval design is a strong candidate?▼

Correct Answer: Hybrid lexical + dense retrieval

Lexical retrieval can preserve exact code matching while dense retrieval captures semantic description.

3A labelled query has 2 known relevant chunks. One appears in the top 3. What is Recall@3?▼

Correct Answer: 0.50

Recall@3 is relevant items retrieved divided by total known relevant items: 1/2 = 0.5.

4What does improving Recall@3 from 0.5 to 1.0 prove?▼

Correct Answer: Retrieval found all labelled relevant chunks in the top 3 for that case

Recall localizes retrieval quality; generation and claim support still require separate evaluation.

5The relevant chunk is retrieved in top 20 but disappears after reranking. Where should debugging focus next?▼

Correct Answer: Reranker scores/features and candidate ordering

The retrieval stage succeeded; the first observed failure occurs at reranking.

Keep moving through the path

Continue Learning

Free Download: ML Interview Cheatsheet

100 essential Machine Learning interview questions and answers covering foundations, algorithms, evaluation, deep learning, Python, system design and interview strategy. Enter your email to download it instantly.

Next