AlgoViz

Evaluating retrieval and answer quality

medium 6 min

Evaluate retrieval and generation separately using the same questions.

Find the pages, check the claims, answer the reader.

A librarian lays out the requested pages, checks each sentence against them and rereads the reader’s question.

A librarian lays out the requested pages, checks each sentence against them and rereads the reader’s question.
Find the pagesRetrieval coverage
Check the claimsFaithfulness
Answer the readerRelevance and completeness
Practice recall

Video lesson · Chapter 1 of 4

A faithful answer to another question

0:00 / 3:37

Theory

Read or listen first. About 2 min.

What it is

Evaluate retrieval and generation separately using the same questions. Document-level labels reveal missed evidence, while claim-level checks reveal unsupported assertions. A response can score well on one measure and poorly on another without either measurement being wrong.

Why it exists

Freeze the corpus and evaluation snapshot before comparing changes. Use examples with missing facts, distractors and multi-document answers, and inspect individual failures rather than optimizing a single blended score.

A faithful answer to the wrong question

Labeled question → Search result (look for gold evidence). Search result → Claim check (score answer claims)
Labeled question → Search result (look for gold evidence). Search result → Claim check (score answer claims)

Inside this chapter

Chapter sources
Synthetic Data Generation
Generate evaluation questions from representative documents, then verify their answers and evidence. Include noisy, multi-source and unanswerable cases. Keep generated questions separate from tuning data and add human-authored cases to detect a generator that routinely asks only easy questions.

Generated questions are cheap but skewed: a model reading one passage tends to ask easy questions that reuse the passage's own words, which flatters lexical search. Verify each answer and its evidence, deliberately add questions that need two passages or that the corpus cannot answer, and mix in human-written questions from real logs. Keep this set separate from anything used to tune prompts or train retrievers.

Example. Generate questions from a known passage, then add one human-authored question the passage cannot answer. Check both answerability labels.

Another example. A team generates 500 questions from their billing docs and gets 95% recall. Adding 50 real customer questions drops it sharply, because customers write "why was I charged again" rather than "duplicate invoice policy". Illustrative figures.

Building a test set you can trust
  1. 1Sample passagesacross products and dates
  2. 2Generate questionsmodel writes Q and answer
  3. 3Verifyhuman or check confirms evidence
  4. 4Add hard casesmulti-source and unanswerable
  5. 5Freeze and versionnever reuse for tuning
Metrics Deep Dive
Retrieval recall asks whether relevant material was found. Context precision checks the usefulness or ordering of retrieved context under the chosen metric. Faithfulness measures support for answer claims, while answer relevance asks whether the response addresses the question. These dimensions can move in opposite directions.

Retrieval metrics compare the returned set with labelled gold passages; answer metrics compare the response with the context and the question. They fail independently: high recall with a generator that ignores evidence, or a faithful answer that addresses only half the question. Report them separately per slice, and read a sample of failures for each before deciding what to change.

Example. Three passages are retrieved, one is relevant, and two relevant passages exist. Compute precision ⅓ and recall ½ before inspecting the answer.

Four dimensions, one example

MetricAsksExample value
Recall@3Did we find the gold passages?1 of 2 = 0.5
Precision@3How much of what we found is gold?1 of 3 = 0.33
FaithfulnessAre answer claims supported by context?2 of 2 claims
Answer relevanceDoes it answer what was asked?Missed the date
The same response can be perfectly faithful and still incomplete.
CI/CD for Agents
Version prompts, retrievers, corpus snapshots, models and evaluation sets together. Run paired regression checks before release and inspect newly failing cases. An offline pass should lead to a monitored rollout; live workloads can contain different languages, document revisions or access constraints.

Treat prompts, retriever settings, encoder version, corpus snapshot and evaluation set as one versioned bundle. Compare old and new on the same questions (a paired comparison) and look at which individual cases flipped from pass to fail. Some checks, such as access control or safety fixtures, must pass outright. Offline success then earns a monitored canary, not an immediate full release.

Example. A release improves retrieval on routine questions but misses the one access-sensitive fixture. Keep authorization as a separate release gate.

A release gate for a RAG change

Authorisation is a hard gate, not one more point in an average.
Rank-aware metrics: MRR and nDCG
Metrics that reward putting relevant passages near the top, not just somewhere in the top k. MRR looks at the first relevant hit; nDCG discounts every relevant hit by its position and compares with the ideal order.

Rank matters in RAG because only the first few passages reach the prompt. MRR is simple and suits questions with one right passage. nDCG handles graded relevance (highly relevant versus somewhat relevant) and several relevant passages. Use recall at the retrieval stage, where you only need the passage somewhere in the shortlist, and rank-aware metrics after reranking.

Example. Two retrievers both find the gold passage in their top 5. One puts it first (reciprocal rank 1), the other fifth (reciprocal rank 0.2). Recall@5 cannot tell them apart; MRR can.

Mean reciprocal rank and DCG

MRR=1∣Q∣∑i=1∣Q∣1rankiDCG@k=∑j=1kreljlog⁡2(j+1)\text{MRR}=\frac{1}{|Q|}\sum_{i=1}^{|Q|}\frac{1}{\text{rank}_i}\qquad \text{DCG@}k=\sum_{j=1}^{k}\frac{\text{rel}_j}{\log_2(j+1)}
nDCG@k divides DCG@k by the DCG of the ideal ordering, so it lies between 0 and 1.

How it works

Step through the idea, or press Play.

Step 1 of 3

Labeled question. Two relevant passages

Understand it

Worked example

A faithful answer to the wrong question

  1. 1

    Two relevant passages

    Identify gold evidence and the desired answer. Include source version and the exact question so stage-level scores are interpretable.

  2. 2

    1 of 2 gold passages in top 3

    Recall at three is one half. Precision at three for this binary-label example is one third. Neither tells you what answer the model produced.

  3. 3

    2 of 2 claims supported

    A response can be faithful to the retrieved text and still omit the second requested fact. Evaluate relevance and completeness separately.

Recall and precision at k

R@k=∣G∩Dk∣∣G∣,P@k=∣G∩Dk∣kR@k=\frac{|G\cap D_k|}{|G|},\qquad P@k=\frac{|G\cap D_k|}{k}
For a nonempty gold set G and k returned passages D, retrieving one of two gold passages in three results gives recall 0.5 and precision one third. These simple set metrics differ from rank-weighted Ragas metrics.

Follow the handoffs

Freeze the corpus and evaluation snapshot before comparing changes. Use examples with missing facts, distractors and multi-document answers, and inspect individual failures rather than optimizing a single blended score.

Scoring the labelled question at k = 3

RankPassageGold?
1G1 price policyYes
2D4 shipping FAQNo
3D9 old promotionNo
missingG2 date ruleGold, not retrieved
Recall@3 = 1/2, precision@3 = 1/3, reciprocal rank = 1.

What the scores show

  • Retrieval found one of two gold passages
  • Both answer claims are supported
  • No hallucinated facts

What they hide unless you check

  • The date question was never answered
  • Missing G2 explains the gap
  • Faithfulness alone would call this a pass

Summary

The whole topic in one minute.

Faithfulness asks whether claims follow from context. Even fully supported claims can fail to address the requested information.

Freeze the corpus and evaluation snapshot before comparing changes. Use examples with missing facts, distractors and multi-document answers, and inspect individual failures rather than optimizing a single blended score.

Sources