RAGEngineering Hub
Chapter 10 · concept 1 of 6 · show all
  1. 1What RAG is, and why
    1. 1.1Why models need outside knowledge, and what hallucination means
    2. 1.2What RAG is: anatomy of a request
    3. 1.3RAG vs fine tuning vs long context and prompt caching
    4. 1.4Use cases and the quality, latency and cost triangle
    5. Chapter overview
  2. 2Tokens, cost and your first RAG
    1. 2.1Tokens and context windows for RAG
    2. 2.2The RAG lifecycle and your first RAG in miniature
    3. Chapter overview
  3. 3Getting documents in
    1. 3.1Content sources and file formats
    2. 3.2Parsing, layout and OCR
    3. 3.3Tables, images and mixed content
    4. 3.4Cleaning, normalization and quality gates
    5. 3.5Metadata, document identity and versioning
    6. 3.6Updates, deletes, reindexing and ingestion pipelines
    7. Chapter overview
  4. 4Chunking
    1. 4.1Why we chunk, and the main chunking strategies
    2. 4.2Structure aware chunking and tables
    3. 4.3Chunk size, overlap and token based sizing
    4. 4.4Contextual chunking and late chunking
    5. 4.5Parent child and multi representation indexing
    6. 4.6Chunk metadata, provenance and the chunking debugging playbook
    7. Chapter overview
  5. 5Embeddings
    1. 5.1Embeddings and similarity from first principles
    2. 5.2What embeddings cannot do
    3. 5.3Choosing an embedding model: size, cost, language
    4. 5.4Embedding model migration and index rebuilds
    5. Chapter overview
  6. 6Vector stores and scores
    1. 6.1Vector databases, ANN indexes and choosing a store
    2. 6.2Score calibration and thresholds
    3. 6.3Late interaction and multivector retrieval
    4. Chapter overview
  7. 7Finding the right chunks
    1. 7.1Query understanding, rewriting and expansion
    2. 7.2Dense retrieval and keyword search with BM25
    3. 7.3Hybrid retrieval and rank fusion
    4. 7.4topK, no good match, and metadata filtering
    5. 7.5Routing, diversity, deduplication and caching
    6. 7.6Reranking: two stage, cross encoder and LLM rerankers
    7. Chapter overview
  8. 8When retrieval fails
    1. 8.1Retrieval failure analysis playbook
    2. Chapter overview
  9. 9Building the answer
    1. 9.1Context selection, packing and citations
    2. 9.2Grounding prompts and abstention
    3. 9.3Lost in the middle and context management
    4. 9.4Conflicts, staleness and freshness rules
    5. 9.5Hallucination checks and answer verification
    6. 9.6Conversational RAG, answer UX and RAG APIs
    7. Chapter overview
  10. 10Measuring
    1. 10.1Why measure, and what to measure
    2. 10.2Building a golden dataset
    3. 10.3Retrieval metrics: precision, recall, MRR, nDCG
    4. 10.4Faithfulness and answer correctness
    5. 10.5LLM as judge, and judging the judge
    6. 10.6Offline and online evals, regression tests and slices
    7. Chapter overview
  11. 11Safety and operations
    1. 11.1Prompt injection and poisoned documents
    2. 11.2Access control, privacy and governance
    3. 11.3Index lifecycle operations
    4. 11.4Observability and tracing
    5. 11.5Latency, cost, caching and model routing
    6. 11.6Reliability, degraded modes and launch readiness
    7. Chapter overview
  12. 12Scale and advanced RAG
    1. 12.1Scaling, multi tenancy and enterprise architecture
    2. 12.2Choosing an architecture: naive, advanced, agentic, graph
    3. 12.3Agentic RAG and RAG as an MCP tool
    4. 12.4GraphRAG, structured data and multimodal RAG
    5. Chapter overview

Why measure, and what to measure

Without a fixed test, every change is a guess. What to measure at each stage of a RAG system, and three changes from this course where the obvious number told the wrong story.

A real question

You changed the chunk size and the answers feel better. Are they?

You cannot tell by feel. A fixed set of questions with known answers, run before and after every change, turns “feels better” into a number, and shows which questions got worse.

The problem

Why this matters

Try a few questions by hand; if they look good, the change is good.
The belief

Try a few questions by hand; if they look good, the change is good.

In this course, reranking left recall@3 at 47 of 47 but moved 8 answers up to rank 1. Diversity re-ranking looked harmless and lost 3 answers. Only a fixed test, read per question, showed either.
What really happens

In this course, reranking left recall@3 at 47 of 47 but moved 8 answers up to rank 1. Diversity re-ranking looked harmless and lost 3 answers. Only a fixed test, read per question, showed either.

Why not the simpler way

RAG systems have dozens of settings and every one interacts with the others. Teams that do not measure end up tuning by anecdote: the CEO's favourite question, the last bug report. Changes that fix one case silently break three others. Measurement is what lets you change things with confidence.

The idea

The idea in plain words

What to measure, by stage:

  • Retrieval: is the evidence in the top k (recall@k), how high (MRR, nDCG), how much noise (precision) (RAG-41).
  • Generation: is the answer correct (matches the golden answer), faithful (supported by the context), cited, and does it abstain when it should (RAG-42).
  • System: latency per stage, tokens and cost per question, error and abstention rates.

A doctor does not ask "do you feel better?" only. They also measure temperature and blood pressure, the same way each time.

How it works

Step by step

  1. A golden set: questions with a known answer and the exact evidence sentence (RAG-40). The same set, every time.
    1

    Fix the test

    A golden set: questions with a known answer and the exact evidence sentence (RAG-40). The same set, every time.

  2. Retrieval: was the evidence found, at what rank? Generation: is the answer correct, grounded and cited? Plus cost and latency.
    2

    Measure each stage

    Retrieval: was the evidence found, at what rank? Generation: is the answer correct, grounded and cited? Plus cost and latency.

  3. Before and after, per question. A total hides questions that broke while others improved.
    3

    Run it on every change

    Before and after, per question. A total hides questions that broke while others improved.

In detail

Offline evaluation runs the golden set on demand and in CI (RAG-44). Online evaluation watches real traffic: feedback, sampled reviews, abstention and flag rates.

Read per question. Store each question's ranks and verdicts for every run. A diff between runs lists questions that improved and questions that broke.

Noise. Rounding, sampling temperature and small sets move numbers. Re-run the baseline twice to see how much a "change" can move with nothing changed.

The code

Try it in Python

def compare(before: dict[str, int | None], after: dict[str, int | None], k: int = 3):
    """before/after: question id -> rank of the evidence chunk (None if not found)."""
    hit = lambda r: r is not None and r <= k
    fixed = [q for q in before if not hit(before[q]) and hit(after[q])]
    broke = [q for q in before if hit(before[q]) and not hit(after[q])]
    moved_up = [q for q in before if before[q] and after[q] and after[q] < before[q]]
    print(f"top-{k}: {sum(map(hit, before.values()))} -> {sum(map(hit, after.values()))}")
    print(f"fixed {fixed}  broke {broke}  moved up {len(moved_up)}")

See it

What we measured

Measured · changes from this course, by two different numbers

Polder corpus, 34 recursive 512 token chunks, 47 golden questions. Numbers from RAG-21, RAG-30 and RAG-31.

Reranking hybrid top 10: recall@34747
Reranking hybrid top 10: answer first3340
MMR: distinct files in top 53.74.1
MMR: answers in top 34441
bge-small re-run, nothing changed4443

Measured 2026-11-04 with corpus/scripts/measure-parts-4-7.mts.

Try it on a real example

Three changes from this course, judged by the number you might check first

Change

Hybrid top 10, reranked

recall@3: 47 → 47 · answers at rank 1: 33 → 40

The first number says “no effect”. The second says it was the biggest gain in Part 5.

Choosing

Use it when, avoid it when

Use it when
  • A golden set from day one, run on every change
  • Separate numbers for retrieval, generation, cost and latency
Avoid it when
  • Judging changes by a few hand-picked questions
  • Reading one total without the per-question diff

What it costs · Writing 30 to 50 questions takes a day. Retrieval runs take seconds; answer runs cost model calls.

In production

Start measuring on day one with 30 to 50 questions; a small honest set beats none. Make the run cheap and fast so people use it: our retrieval run takes seconds. Answer-level checks need model calls; run those before releases, and retrieval checks on every commit.

Watch out

Common mistakes

  • One score tells you if it is better. Recall@3 hid the biggest gain in Part 5.
  • Evaluation comes at the end. It is what makes every earlier step safe to change.
  • A one-question change is real. It can be rounding; re-run the baseline.

Check yourself

Remember this

Remember these

  • A fixed golden set turns opinions into comparisons.
  • Measure retrieval and generation separately, plus cost and latency.
  • Pick metrics that can show the change you expect: rank 1, not only top 3.
  • Compare per question; small sets have noise of a question or two.

Say it in an interview

  1. 01I run a fixed golden set before and after every change and compare per question.
  2. 02I measure retrieval, generation, cost and latency separately.
  3. 03Reranking left recall@3 unchanged but moved 8 answers to rank 1; the metric has to fit the change.
  4. 04Diversity improved a variety number and lost 3 answers; I watch the number users feel.
  5. 05With 47 questions, I treat one-question differences as possible noise.

Quick quiz

Three questions

Question 1

Q1

Reranking the hybrid top 10 kept recall@3 at 47 of 47. Did it help?

Question 2

Q2

Why measure retrieval and answers separately?

Question 3

Q3 · from RAG-05

What are the stages of a RAG request?

Saved on this device. No account needed.