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.
A retrieval test set
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.

Retrieval metrics
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.
| Metric | Answers | Notes |
|---|---|---|
| Recall at k | Is a relevant item in the top k? | Primary RAG retrieval metric |
| Precision at k | How much of the top k is relevant? | Affects noise and tokens |
| MRR | How high is the first relevant item? | Sensitive to ranking; reranking improves it |
| nDCG | Is graded relevance well ordered? | Needs graded labels |
| Context coverage | Are the needed facts present? | Fact-level check |
| Retrieval latency percentiles | How fast is retrieval? | Measure under load |
The following diagram compares two chunking strategies tested with labelled queries.

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.
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.