Katkat

Evaluating a RAG System

By the Katkat team · Updated · 9 min read

Quick overview

Learn how to evaluate a RAG system: recall@k and MRR for retrieval, faithfulness for answers, building golden sets, and balancing offline and online tests.

Lesson 21 of 88 · AI Engineering Interview Course

After this lesson you will be able to explain how to measure a retrieval-augmented generation (RAG) system by scoring retrieval and generation separately. You will be able to compute recall@k and MRR by hand, define faithfulness and say how it is checked, and describe how to build a golden set. You will also be able to explain how offline and online evaluation work together.

Evaluate the two stages separately

A RAG system has a retrieval stage (find chunks) and a generation stage (write an answer from those chunks). If you only score the final answer, you cannot tell which stage broke. A wrong answer might come from the right document never being retrieved, or from the right document being retrieved and then ignored. The fixes are completely different. See Retrieval-Augmented Generation, End to End for the pipeline itself.

FailureWhereTypical symptom
Retrieval missRetrievalThe needed passage is not in the top-k
Noisy contextRetrievalRelevant passage is present but buried among irrelevant ones
Unfaithful answerGenerationAnswer contains claims not supported by the context
Off-target answerGenerationAnswer is faithful but does not address the question
Wrong refusal or wrong answer to an unanswerable questionBothBot answers when it should say "I don't know", or the reverse

Good evaluation gives each row its own metric, so a regression points to a stage.

Retrieval metrics

Retrieval metrics need a label saying which documents or chunks are relevant for each test query. Then you compare against the ranked list the retriever returned.

  • Recall@k: of all relevant items for a query, the fraction that appear in the top k. It matters most because the generator can only use what it is given. Recall@k is a ceiling on answer quality.
  • Precision@k: of the k items returned, the fraction that are relevant. It tells you how much noise you are putting in the prompt.
  • Hit rate@k: the fraction of queries with at least one relevant item in the top k. Useful when each question has a single supporting passage.
  • MRR (mean reciprocal rank): for each query, take 1 divided by the rank of the first relevant result (0 if none is found), then average over queries. It rewards putting the right chunk near the top.
  • nDCG: handles graded relevance (highly relevant vs somewhat relevant) and discounts lower ranks. Worth naming, but recall and MRR cover most RAG discussions.

Worked example

Five test queries. The first relevant chunk appears at these ranks in the top 10:

QueryRank of first relevant chunk
Q11
Q23
Q3not in top 10
Q42
Q51
  • Reciprocal ranks: 1, 1/3, 0, 1/2, 1.
  • Sum = 1 + 0.333 + 0 + 0.5 + 1 = 2.833.
  • MRR = 2.833 / 5 ≈ 0.567.
  • Hit rate@1 = 2/5 = 0.40. Hit rate@3 = 4/5 = 0.80.

Now a multi-evidence query with 3 relevant chunks, where the top 5 contains 2 of them. Recall@5 = 2/3 ≈ 0.67 and precision@5 = 2/5 = 0.40.

Trade-offs

Raising k almost always raises recall and lowers precision. A bigger k also costs more tokens, more latency, and can distract the model (see Context Windows). MRR is the right lens when the system shows only a few chunks or a reranker orders them; recall@k is the right lens when you pass a fixed top-k to the model. Report several k values, not one.

Answer faithfulness and answer quality

Faithfulness (also called groundedness) asks: is every claim in the answer supported by the retrieved context? It is not the same as correctness. An answer can be faithful to an outdated document and still be wrong. It can also be correct from the model's memory but unfaithful, which is a risk because the answer cannot be traced to a source.

A common way to compute it:

  1. Split the answer into atomic claims (usually with an LLM).
  2. For each claim, check whether the retrieved context supports it (an LLM judge or an entailment model).
  3. Faithfulness = supported claims / total claims.

For example, an answer with 8 claims where 6 are supported scores 6/8 = 0.75. The two unsupported claims are what you read when debugging.

Faithfulness is one of several answer-level measures:

MetricQuestion it answersNeeds a reference answer?
FaithfulnessAre claims backed by the context?No
Answer relevanceDoes the answer address the question?No
Context precision / relevanceHow much of the retrieved context was actually useful?Sometimes
Answer correctnessDoes it match the ground truth?Yes
Citation accuracyDo the cited sources really support the cited claims?No
Abstention qualityDoes it decline when the context lacks the answer?Needs unanswerable cases

Always include unanswerable questions. A system that never says "I don't know" can score well on faithfulness only if its claims happen to be supported, and it will still fail users on gaps in the knowledge base.

Golden sets

A golden set is a fixed, versioned collection of test cases you trust enough to compare system versions. Each case typically holds a question, the IDs of the relevant source documents (and ideally the supporting passage), and a reference answer or key facts the answer must contain.

How to build one:

  • Start from real queries (logs, support tickets, user interviews). Invented questions tend to resemble the documents too closely.
  • Stratify: include easy lookups, multi-hop questions, ambiguous questions, numeric or table questions, and unanswerable ones. Label the category so you can slice results.
  • Label at document level, not chunk level. If relevance is tied to chunk IDs, every change to chunk size invalidates your labels. Store the source document and the supporting span, and map to chunks automatically. See Chunking Documents for Retrieval.
  • Use LLM-generated questions carefully. They are cheap and good for coverage, but they often share wording with the passage, which flatters keyword and embedding retrieval. Have a human review a sample and mix in real queries.
  • Keep it separate from tuning. If you tune prompts and chunking against the same cases you report on, the score is optimistic. Hold out a subset.

How big?

Size sets how much a difference means. Suppose measured recall@5 is 0.80 on 100 queries. A rough standard error is sqrt(0.8 × 0.2 / 100) = sqrt(0.0016) = 0.04, so an approximate 95% interval is ±0.08, or 0.72 to 0.88. A change from 0.80 to 0.83 on this set is noise. With 400 queries the standard error halves to 0.02. Small golden sets are fine for catching big regressions but poor for ranking close variants. Comparing two versions on the same queries (paired comparison) is more sensitive than comparing two independent scores.

Using an LLM as the judge

Faithfulness and relevance usually need a judge model, because string matching cannot tell whether a paraphrase is supported. This is practical but must be validated: label 50 to 100 cases by hand, compare the judge's verdicts, and track agreement. Known judge biases include favouring longer answers and favouring outputs from its own model family. Use a clear rubric, ask for a verdict per claim rather than a single holistic score, and re-check agreement whenever you change the judge model or prompt. LLM Evaluation and LLM-as-a-Judge covers this in depth.

Offline vs online evaluation

OfflineOnline
DataGolden set, fixedLive traffic
SpeedMinutes, runs in CIDays to collect enough signal
SignalsRecall@k, MRR, faithfulness, correctnessThumbs up/down, re-asks, escalation to a human, citation clicks, task completion
StrengthRepeatable, isolates a stage, safeReflects real users and real query drift
WeaknessCan go stale; only covers what you thought ofNoisy, confounded, slower, and exposes users to regressions

Use both in sequence. Offline evaluation is the gate: a change must not drop recall or faithfulness on the golden set. Passing changes go to an online stage, such as a canary or an A/B test (see Online Evaluation and A/B Tests). Then close the loop: sample production traces, find failures (thumbs-down, low judge scores, escalations), label them, and add them to the golden set so it tracks what users really ask.

Online metrics are indirect. A re-ask might mean a bad answer or a follow-up question, and a missing thumbs-down does not mean success. Treat them as signals to investigate, and pair them with periodic human review of sampled traces.

Reading the metrics to diagnose problems

PatternLikely causeFirst things to try
Low recall@kChunking, embedding model, query phrasingChange chunking, add hybrid search, query rewriting
Good recall@k, poor MRRRight chunk found but ranked lowAdd a reranker
Good recall, low faithfulnessGenerator ignores or embellishes contextTighten the prompt, require citations, lower temperature
High faithfulness, low correctnessRetrieved context is wrong, stale, or incompleteFix the corpus, freshness, or document parsing
Answers unanswerable questionsNo abstention behaviourAdd refusal instructions and abstention test cases

This is the backbone of the approach in Your RAG System Gives Wrong Answers: How Do You Debug It?.

How does this come up in interviews?

1. "How would you evaluate a RAG system?" Separate retrieval from generation. Measure recall@k and MRR against labelled relevant documents, and measure faithfulness and answer correctness on the output. Build a golden set from real queries, run it offline on every change, then confirm with online signals.

2. "Your recall@5 is 0.9 but users still get wrong answers. What do you check?" Retrieval is probably not the main problem. Check faithfulness to see if the model is ignoring or inventing beyond the context. Then check whether the labelled relevant document is itself outdated or wrong, and whether the answer is correct but the labels are incomplete.

3. "What is the difference between faithfulness and correctness?" Faithfulness checks that claims are supported by the retrieved context, and needs no reference answer. Correctness checks the answer against ground truth. An answer can be faithful but wrong if the context is wrong, or correct but unfaithful if it came from model memory.

4. "How many examples do you need in a golden set?" Enough that the differences you care about exceed the noise. With 100 queries and a score near 0.8, the 95% interval is about ±0.08, so only large changes are detectable. I would start with a few hundred stratified cases, use paired comparisons between versions, and grow the set from production failures.

5. "Offline metrics improved but online satisfaction did not. Why?" The golden set may not match real traffic, or it may have been overfit during tuning. The judge may not agree with users, or the metric may miss latency and tone. I would compare golden-set queries with sampled production queries, re-validate the judge against human labels, and add the failing production cases to the set.

Common mistakes

  • Scoring only the final answer, so retrieval and generation failures cannot be told apart.
  • Reporting recall at a single k, or reporting recall without saying how many chunks the model actually receives.
  • Labelling relevance by chunk ID, so the labels break when chunking changes.
  • Treating faithfulness as correctness, or forgetting that stale documents make faithful answers wrong.
  • Having no unanswerable questions in the test set, so abstention is never tested.
  • Generating the whole golden set with an LLM from the same documents and never checking it against real queries.
  • Tuning on the golden set and then reporting the same numbers as the result.
  • Trusting an LLM judge without comparing it to human labels.
  • Reading small score differences on a small set as real improvements.
  • Treating online signals such as thumbs-up rate as ground truth instead of as noisy indicators.

Sources and further reading