Slides · Retrieval-augmented generation

RAG evaluation in depth

RAG evaluation in depth

“Is it fine, yes or no?” needs more than one number. This lesson builds an evaluation that says which stage is responsible and how far to trust the score.

By the end you can:

  • design an evaluation that scores retrieval and generation separately, then end to end,
  • compute recall@k, precision@k and MRR by hand,
  • turn misses into a failure taxonomy,
  • build a golden set with unanswerable questions and use an LLM judge with care,
  • run controlled experiments that track quality, latency and cost together.

Concept

Three complaints, one number

  • One user got a wrong page.
  • One got an old page.
  • One got the right page, read badly.

A single quality score blends all three. Each needs a different fix: retrieval, source freshness, generation. You need numbers that point at a stage.

Diagram

The evaluation, stage by stage

Diagram: A golden set feeds a retrieval evaluation and a generation evaluation, whose results combine into an end-to-end evaluation that gates...

Golden set

Retrieval eval:
recall, MRR, nDCG

Generation eval:
faithfulness, relevance

End-to-end eval

CI gate or dashboard

Production traces

Diagram summary: A golden set feeds a retrieval evaluation and a generation evaluation, whose results combine into an end-to-end evaluation that gates releases in CI or a dashboard. Production traces and feedback flow back into the golden set.

Think of an inspection of the kitchen, the plating and the whole meal. A single satisfaction number cannot say whether the ingredients or the cooking was at fault.

Concept

Three retrieval metrics, three questions

  • Recall@k: was the evidence found in the top k? The first metric to read: if the evidence is not retrieved, generation cannot fix it.
  • Precision@k: how much of the top k is relevant? Low precision means a noisy prompt.
  • MRR: how high did the first relevant chunk rank? Useful when one good chunk is enough.

nDCG@k additionally rewards putting highly relevant items earlier and handles graded relevance.

All of these need labelled relevant chunks for each query.

Math

The formulas

The worked examples below use the hit rate, which many teams simply call recall@k.

hit rate=queries with a relevant chunk in the top ktotal queriesrecall@k=relevant chunks in the top kall relevant chunks for that queryprecision@k=relevant results in the top kkMRR=1N∑i=1N1ranki,with 1ranki=0 when nothing relevant is found\begin{aligned} \text{hit rate} &= \frac{\text{queries with a relevant chunk in the top } k}{\text{total queries}} \\ \text{recall@}k &= \frac{\text{relevant chunks in the top } k}{\text{all relevant chunks for that query}} \\ \text{precision@}k &= \frac{\text{relevant results in the top } k}{k} \\ \mathrm{MRR} &= \frac{1}{N} \sum_{i=1}^{N} \frac{1}{\mathrm{rank}_i}, \quad \text{with } \tfrac{1}{\mathrm{rank}_i} = 0 \text{ when nothing relevant is found} \end{aligned}
SymbolMeaning
kkHow many top results you look at
hit rate\text{hit rate}Share of queries with at least one relevant chunk in the top k; often reported as recall@k
recall@k\text{recall@}kPer query: how many of the relevant chunks were found; average over queries
NNNumber of queries
ranki\mathrm{rank}_iPosition of the first relevant result for query ii, starting at 1
1/ranki1 / \mathrm{rank}_iReciprocal rank: 1 for first place, 12\frac{1}{2} for second, 13\frac{1}{3} for third

Note that MRR looks only at the first relevant result; later relevant results do not count.

Source1 Mean reciprocal rank

Worked example

Worked example: four queries, top 5

First relevant chunk is at rank 1, rank 3, not found, and rank 2.

QueryRank of first relevantIn top 5?Reciprocal rank
A1yes1.000
B3yes0.333
Cnoneno0
D2yes0.500
recall@5 (hit rate)=34=0.75MRR=1+0.333+0+0.54=1.8334=0.458\begin{aligned} \text{recall@}5\ (\text{hit rate}) &= \frac{3}{4} = 0.75 \\ \mathrm{MRR} &= \frac{1 + 0.333 + 0 + 0.5}{4} = \frac{1.833}{4} = 0.458 \end{aligned}

Recall@5 says three of four were found. MRR adds how high they ranked, which matters if only the top few go into the prompt.

Source1 Mean reciprocal rank

Worked example

Worked example: precision and what k changes

One query returns five results. Relevant ones are at ranks 1, 2, 3 and 5.

precision@5=45=0.8precision@3=33=1.0precision@1=11=1.0\begin{aligned} \text{precision@}5 &= \frac{4}{5} = 0.8 \\ \text{precision@}3 &= \frac{3}{3} = 1.0 \\ \text{precision@}1 &= \frac{1}{1} = 1.0 \end{aligned}
  • The same list looks perfect at k=3k = 3 and still carries one noisy chunk at k=5k = 5.
  • Choose k to match how many chunks you really put in the prompt.
  • A single query gives precision. Recall over the set needs the fraction of queries (or relevant chunks) found.

Worked example

Worked example: which metric moves?

Five queries, top 3 for recall. First relevant ranks: 1, none, 4, 2, none.

recall@3=25=0.40MRR=1+0+0.25+0.5+05=0.35\text{recall@}3 = \frac{2}{5} = 0.40 \qquad \mathrm{MRR} = \frac{1 + 0 + 0.25 + 0.5 + 0}{5} = 0.35

Now an engineer improves one query: rank 4 becomes rank 1.

recall@3=35=0.60MRR=1+0+1+0.5+05=0.50\text{recall@}3 = \frac{3}{5} = 0.60 \qquad \mathrm{MRR} = \frac{1 + 0 + 1 + 0.5 + 0}{5} = 0.50

Recall@3 rose because the chunk crossed into the top 3. MRR rose because it moved up. If a chunk moves from rank 2 to rank 1, recall@3 does not change at all, but MRR does. Use both.

Concept

Generation metrics

  • Faithfulness (groundedness): is every claim supported by the retrieved context? Split the answer into claims and check each, with a judge model or an entailment model.
  • Answer relevance: does it address the question?
  • Answer correctness: does it match the gold answer?
  • Citation accuracy: do cited sources support the cited claims?
  • Abstention quality: does it say “I don’t know” when the context lacks the answer?

The Ragas documentation defines faithfulness as supported claims divided by total claims. The Ragas paper presents metrics for the retriever, the faithful use of context and generation quality.

Sources2 RAGAs: Automated Evaluation of Retrieval Augmented Generation3 Faithfulness

Compare

The failure taxonomy

Retrieved gold?Answer faithful?Answer correct?Diagnosis
NoanyNoRetrieval failure
YesYesNoSource wrong or ambiguous, or the question is ambiguous
YesNoNoGeneration failure
NoNoYes, by luckFrom model memory, not the corpus: risky
YesYesYesHealthy

Quick manual check: paste the gold passage into the prompt. If the answer is now correct, the retriever was the problem.

Worked example

Worked example: four misses, four rows

Illustrative results from a run.

MissGold chunk retrieved?Faithful?Correct?Row and first fix
M1Non/aNoRetrieval: check chunking and add hybrid search
M2YesYesNoSource or ambiguity: read the gold chunk; fix the document
M3YesNoNoGeneration: tighten grounding, trim noisy context
M4NoNoYesMemory, not corpus: add the question to the regression set and require citations

M4 looks like a win and is not. It passes today and fails when the model changes.

Diagram

A golden set is a living asset

Diagram: A golden set starts from real queries, labels gold chunks, and splits into a frozen regression set and a rotating fresh set.

Real queries and experts

Label gold chunks

Frozen regression set

Rotating fresh set

Run on every change

Slice results

New failures

Diagram summary: A golden set starts from real queries, labels gold chunks, and splits into a frozen regression set and a rotating fresh set. It runs on every change, results are sliced to expose a failing group, and new failures are labelled and added back.

Concept

Golden set rules

  • Start from real logged queries and subject-matter experts. Tens of questions to begin, a few hundred is better.
  • Cover types: factual, multi-hop, ambiguous, unanswerable, exact-ID, long-form.
  • Label gold chunks, not just answers, so retrieval can be scored.
  • Synthetic questions scale the set, but they tend to be easier and echo the chunk’s wording. Review a sample by hand.
  • Slice by query type, document type, tenant and language. An average can hide a failing slice.

Concept

LLM-as-judge: useful, biased

A judge model scores answers at scale. The MT-Bench paper reports that strong judges reach over 80% agreement with human preferences, and it names position, verbosity and self-enhancement bias as limitations.

PitfallWhat happensMitigation
Position biasFavours an answer by where it appearsSwap the order and compare
Verbosity biasFavours longer answersFixed rubric, length-aware checks
Self-enhancementFavours its own answers, and plausibly those of its own model familyJudge from a different family
InstabilityScores differ across runsRepeat or vote; calibrate against human labels

Report agreement with human labels for your own rubric. A judge you have not calibrated is an opinion.

Source4 Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena

Diagram

Cancelling position bias

Diagram: The judge scores both orders.

yes

no

Answers A and B

Judge sees A then B

Judge sees B then A

Same winner both times?

Record the winner

Record a tie or ask a human

Diagram summary: The judge scores both orders. If the same answer wins either way it is recorded as the winner; if the winner flips with the order, the result is a tie or goes to a human.

Concept

Do not rely on temperature 0 for a stable judge

  • Temperature 0 alone does not guarantee identical output. The Thinking Machines article attributes it mainly to kernels that are not batch-invariant, so results depend on server load.
  • Some current models restrict sampling parameters. The Claude Sonnet 5.5 migration guide says temperature, top_p and top_k return an error and should be removed.
  • So stability comes from design: a fixed rubric, structured output, repeated judging or a majority vote, and human calibration.

Check your model’s current documentation before choosing settings.

Sources5 Defeating Nondeterminism in LLM Inference6 Claude Sonnet 5.5 migration guide

Process

One change at a time

  1. Pick one change: chunk size, embedder, reranker or prompt.
  2. Run it on the same golden set as the baseline.
  3. Read quality, latency and cost together.
  4. Report uncertainty: on a small set one question is a visible jump.
  5. Slice the results; look for a group that got worse.
  6. Gate the deploy on thresholds you set before you looked.

Changing three things in one release leaves you unable to say which one helped.

Worked example

Worked example: a gain that costs

Illustrative experiment on 250 golden queries: a larger embedding model.

MeasureBaselineCandidateChange
recall@50.740.78+0.04
p95 latency0.9 s1.1 s+0.2 s (about 22 percent)
Cost per 1,000 queries$2.00$2.60+30 percent
queries gained=0.04×250=10latency ratio=1.10.9=1.22cost ratio=2.602.00=1.30\begin{aligned} \text{queries gained} &= 0.04 \times 250 = 10 \\ \text{latency ratio} &= \frac{1.1}{0.9} = 1.22 \\ \text{cost ratio} &= \frac{2.60}{2.00} = 1.30 \end{aligned}

Ten queries out of 250 is a small shift; check which queries flipped before calling it real. Decide using a rule written beforehand, for example “ship if recall@5 gains at least 3 points on every slice, with latency under 1.2 s and cost within 25 percent”. By that rule this candidate fails on cost, however good the recall looks.

Concept

Online signals feed the offline set

  • Thumbs up or down, copy and edit rate, follow-up rephrasing, escalation to a human.
  • “No answer” rate, citation clicks, latency and cost per query.
  • Sample low-signal traces for human review and add the failures to the golden set.
  • Wire the golden set into CI, so a change that drops recall@k on a slice fails the build.

Tools such as Ragas, TruLens, DeepEval, LangSmith and Arize Phoenix exist, and features change often, so check current documentation before choosing.

Check yourself

Check: compute and diagnose

Six queries, first relevant rank in the top 5: 1, 1, 3, none, none, 5. (a) Compute recall@3 (hit rate) and MRR. (b) For one of the “none” queries the gold chunk was missing from the retrieved list and the answer was wrong. Which taxonomy row is it, and what do you try first?

Answer

(a) Recall@3 =36=0.50= \frac{3}{6} = 0.50 (ranks 1, 1 and 3 qualify; rank 5 is outside the top 3). MRR =1+1+0.333+0+0+0.26=2.5336=0.422= \frac{1 + 1 + 0.333 + 0 + 0 + 0.2}{6} = \frac{2.533}{6} = 0.422. (b) Gold not retrieved and answer wrong is a retrieval failure; try chunking, hybrid search or metadata filters first.

Check yourself

Check: spot the flaws

A team builds a 60-question set by asking a model to write one question per chunk, runs a pairwise judge that always shows the baseline answer first, and picks the release threshold after seeing the new results. They report “answer quality up 3 points”. List three problems and a fix for each.

Answer
  1. Synthetic questions copied from chunks are easy and echo their wording; add reviewed real queries and unanswerable ones. 2. Always showing the baseline first invites position bias; judge both orders and treat a flip as a tie or send it to a human. 3. A threshold chosen after seeing results is fitted to them; set the rule beforehand. Also: 3 points on 60 questions is about two flips (each is 1.7 points), so confirm on a larger set and report the retrieval and generation metrics separately.

Summary

Recap

  • Evaluate stages separately, then end to end.
  • Recall@k: found it? MRR: how high? Precision@k: how noisy?
  • Faithfulness, relevance, correctness, citation accuracy, abstention.
  • The taxonomy names who is responsible for each miss.
  • Golden sets need unanswerable questions, gold chunks, a frozen and a fresh part.
  • Judges are biased: calibrate against human labels.
  • One change at a time; track quality, latency and cost; report uncertainty.

References

References & Sources (1 of 2)

  1. Mean reciprocal rank (opens in a new tab)

    Wikipedia2026Article

    Used for MRR averages the reciprocal of the rank of the first correct answer per query, scores 0 when there is none, and considers only the first relevant result.

  2. RAGAs: Automated Evaluation of Retrieval Augmented Generation (opens in a new tab)

    Shahul Es, Jithin James, Luis Espinosa-Anke, Steven Schockaert2024EACL 2024 (System Demonstrations)Paper

    Used for Ragas proposes reference-free metrics covering the retriever's ability to find focused context, the model's faithful use of it, and generation quality.

  3. Faithfulness (opens in a new tab)

    Ragas2026Documentation

    Used for Faithfulness is the number of response claims supported by the retrieved context divided by the total number of claims.

  4. Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena (opens in a new tab)

    Lianmin Zheng, Wei-Lin Chiang, Ying Sheng, Siyuan Zhuang, et al.2023NeurIPS 2023 Datasets and Benchmarks TrackPaper

    Used for Strong LLM judges reach over 80 percent agreement with human preferences, and position, verbosity and self-enhancement biases are limitations.

  5. Defeating Nondeterminism in LLM Inference (opens in a new tab)

    Horace He2025Article

    Used for Temperature 0 does not by itself make LLM inference repeatable, because kernel results can depend on server load.

References

References & Sources (2 of 2)

  1. Claude Sonnet 5.5 migration guide (opens in a new tab)

    Anthropic2026Documentation

    Used for The Claude Sonnet 5.5 guide says temperature, top_p and top_k return an error and should be removed.