Lesson 2 of 3 Evaluating RAG Systems and Agents

Testing Retrieval Quality with Labeled Queries

This lesson covers relevance scoring, context matching verification and latency measurement on a labelled query set, which tests retrieval quickly and cheaply, independently of generation.

Learning outcomes

After completing this lesson, you will be able to:

  • Build a retrieval test set.
  • Measure recall at k, MRR and NDCG.
  • Verify that retrieved context contains the needed facts.

Building a retrieval test set

Retrieval quality can be measured only against known correct results. A retrieval test set pairs realistic queries with the documents or passages that should be retrieved for each. Queries come from production logs, which show how users actually ask, and from subject matter experts, who know which documents answer them. To build a larger set, an FM can write questions from documents, which experts then review, because generated questions tend to reuse the document's wording and can be unrealistically easy.

The set covers different question types, such as factual, procedural, comparative and identifier lookups, across the main topics and difficulty levels. It also includes queries for which no relevant document exists, to check that the system returns nothing misleading rather than the closest but irrelevant passages.

Select all that apply

A retrieval test set

Exam skills 5.1.6

Choose the practices for building a useful retrieval test set.

Log in to try this activity Interactive activities are free with an account.

Relevance scoring metrics

Recall at k is the share of queries for which a relevant item appears in the top k results; it matters most for RAG because the model can only use what is retrieved. Precision at k is the share of the top k results that are relevant, which affects noise and token cost. Mean reciprocal rank (MRR) averages 1 divided by the rank of the first relevant result. Normalized discounted cumulative gain (nDCG) rewards placing the most relevant items highest when relevance is graded. When labels are unavailable, an LLM judge can score each retrieved chunk's relevance.

def recall_at_k(results, relevant, k=5):
    hits = sum(1 for q in results if set(results[q][:k]) & relevant[q])
    return hits / len(results)

def mrr(results, relevant):
    total = 0.0
    for q, ranked in results.items():
        rank = next((i + 1 for i, doc in enumerate(ranked) if doc in relevant[q]), None)
        total += 1 / rank if rank else 0
    return total / len(results)

The following diagram computes the metrics for one query.

Computing ranking metrics for one query. The first relevant result is at rank 2, and two of the top five results are relevant. The query counts as a hit for recall at 5, precision at 5 is 0.4, and the reciprocal rank is 0.5; mean reciprocal rank averages that value over all queries.
The document titles are illustrative.
Fill in the number

Retrieval metrics

Exam skills 5.1.6

Calculate retrieval metrics from small test results.

Log in to try this activity Interactive activities are free with an account.

Context matching verification and latency

Context matching verification checks that retrieved context actually contains the facts the answer needs, not just a document on the right topic: each fact in the reference answer is checked against the retrieved chunks, by string matching for exact facts or by a judge for paraphrased ones. Retrieval latency measurements record embedding time, search time and reranking time at p50, p90 and p99 under realistic load. Every retrieval change (chunking, embedding model or dimensions, search type, filters, reranking, k) is compared on these metrics before release. The following table summarises the metrics.

MetricAnswersNotes
Recall at kIs a relevant item in the top k?Primary RAG retrieval metric
Precision at kHow much of the top k is relevant?Affects noise and tokens
MRRHow high is the first relevant item?Sensitive to ranking; reranking improves it
nDCGIs graded relevance well ordered?Needs graded labels
Context coverageAre the needed facts present?Fact-level check
Retrieval latency percentilesHow fast is retrieval?Measure under load

The following diagram compares two chunking strategies tested with labelled queries.

Choosing a chunking strategy with a retrieval test. Two data sources over the same manuals were tested with 400 labelled queries: fixed-size 300-token chunks gave recall at 5 of 0.78 and MRR 0.61; hierarchical chunking gave 0.88 and 0.70 with similar latency, and context coverage confirmed the parent chunks contain the procedure steps.
The results come from the scenario above.
Worked example

Choosing a chunking strategy

A team must choose between fixed-size 300-token chunks and hierarchical chunking for technical manuals.

It creates two data sources over the same documents and runs 400 labelled queries: fixed-size gives recall at 5 of 0.78 and MRR 0.61; hierarchical gives 0.88 and 0.70 with similar latency. Context coverage confirms the parent chunks contain the needed procedure steps. Hierarchical chunking is chosen with evidence.

Check yourself

Which metric is the key measure of RAG retrieval?

Show answer

Recall at k. MRR and nDCG measure ranking quality.

Exam Tips

Remember for the exam

  • Build a labelled retrieval test set from real and expert queries.
  • Recall at k is the key RAG retrieval metric; MRR and nDCG measure ranking.
  • Verify context contains the needed facts, not just the right topic.
  • Compare every retrieval change on the same test set.