How-To Guide

How to Evaluate RAG Retrieval with Recall and MRR

Measure retrieval quality in a RAG pipeline with recall@k, mean reciprocal rank and hit rate, so you debug retrieval before blaming the generator.

On this page

  1. Introduction
  2. Separate Retrieval from Generation
  3. Building a Labelled Evaluation Set
  4. Defining Recall@k and Hit Rate
  5. Mean Reciprocal Rank and Rank Position
  6. Choosing Metrics for Chunking and Embedding Changes
  7. Instrumenting Retrieval in Production
  8. Common Pitfalls and Conclusion

Introduction

When a retrieval-augmented generation system gives a wrong answer, teams usually blame the model. More often the retriever never surfaced the passage that contained the answer, so the generator had nothing to work with. You cannot fix what you do not measure, and “the answers look better now” is not a measurement. This tutorial defines the retrieval metrics that isolate the retriever from the generator, builds a labelled evaluation set, and shows how to report recall@k, mean reciprocal rank and hit rate so a change to chunking, embedding or the index produces a number you can trust.

Separate Retrieval from Generation

A RAG pipeline has two stages that fail independently: retrieval decides which passages enter the prompt, and generation decides what to say about them. End-to-end answer quality mixes both failure modes, which makes it a poor debugging signal. Evaluate retrieval on its own by checking whether the correct passage appears in the retrieved list, before any generation happens.

The useful abstraction is a labelled query with one or more relevant passage ids. Retrieval evaluation then asks a purely mechanical question: did the relevant passages rank highly? Once you know retrieval is sound, any remaining bad answers are a generation or prompt problem, which is a different investigation.

Building a Labelled Evaluation Set

You need queries paired with the documents that should answer them. Start small: fifty to one hundred queries drawn from real usage or logs, each with one or more gold passage ids annotated by a human. Real queries are better than synthetic ones because they reveal the vocabulary mismatch that matters in production.

Keep the set stable and version it, so a metric change means the system changed, not the test. Split it into slices by query type: exact-identifier lookups, natural-language questions and multi-hop questions behave differently. Report per-slice metrics so you can see which retriever path regressed instead of watching an aggregate move in the wrong direction.

[
  {
    "query": "how do I cap concurrent outbound requests",
    "relevant": ["doc-semaphore-101", "doc-bounded-pool-7"],
    "type": "natural-language"
  },
  {
    "query": "ErrorCode 0x80070005 upload",
    "relevant": ["doc-win-error-codes-3"],
    "type": "identifier"
  }
]

Defining Recall@k and Hit Rate

Recall@k answers “what fraction of the relevant passages appear in the top k results?” If a query has two gold passages and retrieval’s top ten contains one of them, recall@10 is 0.5. Hit rate answers a coarser question: “was at least one relevant passage in the top k?” It is 1 or 0 per query and averages to a fraction of queries satisfied.

Choose k to match the context budget. If the prompt can hold ten passages, recall@10 is the number that matters, because a relevant passage ranked eleventh never reaches the generator. Report recall at a couple of depths, such as k=5 and k=10, to see whether relevant passages are merely ranked low or missing entirely.

def recall_at_k(retrieved, relevant, k):
    top = retrieved[:k]
    hits = len(set(top) & set(relevant))
    return hits / len(relevant)

def hit_rate(retrieved, relevant, k):
    return 1.0 if set(retrieved[:k]) & set(relevant) else 0.0

# retrieved: ranked list of passage ids; relevant: gold ids
print(recall_at_k(["a", "b", "c"], ["b", "z"], 2))  # 0.5
print(hit_rate(["a", "b", "c"], ["z"], 2))          # 0.0

Mean Reciprocal Rank and Rank Position

Recall ignores where in the list the relevant passage sits, but position matters because the generator attends unevenly and because a shorter prompt costs less. Mean reciprocal rank (MRR) is the average of 1/rank of the first relevant result. A relevant passage at rank one contributes 1.0, at rank two 0.5, at rank ten 0.1; if none is retrieved the query contributes 0.

MRR rewards putting the best passage first, which is exactly the behaviour that helps generation. Pair it with recall, because MRR alone can look good while recall is poor if each query has exactly one easily found passage. Report both, plus the distribution of first-relevant ranks so you see whether misses cluster near the cutoff.

def reciprocal_rank(retrieved, relevant):
    for i, doc_id in enumerate(retrieved, start=1):
        if doc_id in relevant:
            return 1.0 / i
    return 0.0

scores = [reciprocal_rank(r, g) for r, g in pairs]
mrr = sum(scores) / len(scores)
print(f"MRR = {mrr:.3f}")

Choosing Metrics for Chunking and Embedding Changes

Retrieval metrics turn chunking and embedding decisions into experiments. If you shrink chunks, recall may rise because the relevant sentence is isolated, but MRR may fall because the embedding of a tiny chunk is noisy. If you switch embedding models, recall may improve while MRR stays flat. Reporting both stops you from optimising one at the expense of the other.

Run the same labelled set before and after every change and record the delta. Treat a change as a win only when the metric you care about improves and the others do not regress beyond noise. Because the set is small, compute a confidence interval or at least look at per-query deltas rather than trusting a single decimal.

Instrumenting Retrieval in Production

Offline metrics need a labelled set; online you can approximate quality with proxies. Log the top scores and their spread for every query; a query where all retrieved scores are low and tightly clustered is one where the retriever found nothing relevant, which is a recall failure you can flag without human labels. Capture user feedback such as which citation was clicked to build more labels over time.

Track the fraction of queries where the retriever returned fewer than k results or where the top score is below a threshold. These are cheap signals that correlate with poor answers and can page you before users complain. Together with offline recall and MRR, they give a feedback loop where production traffic feeds the labelled set that guards the next change.

Common Pitfalls and Conclusion

Do not evaluate retrieval with an LLM judge alone; judges are noisy, expensive and hard to compare across versions. Do not build the labelled set from the same documents the retriever was tuned on without holding out a portion, or you will overstate quality. Do not report a single aggregate that hides a slice regression. And do not let relevant ids drift when documents are rechunked, because stale gold ids turn every retriever into a failure case.

Recall@k, hit rate and MRR are simple, cheap and decisive. They separate retrieval from generation, they make chunking and embedding changes measurable, and they tell you whether to fix the retriever or the prompt. Measure retrieval first; it is the stage most likely to be quietly broken.

Sources: Introduction to Information Retrieval (Manning et al.), BEIR: A Heterogeneous Benchmark for IR, Ragas: Automated RAG evaluation.

FAQ

How many labelled queries do I need to trust the metrics?
Fifty to one hundred real queries is enough to detect large regressions and to guide chunking decisions. For fine-grained comparisons, compute per-query deltas and confidence intervals rather than reading a single average as precise.
Should recall or MRR be the primary metric?
Report both. Recall tells you whether the relevant passage is reachable at all within the context budget; MRR tells you whether it is ranked near the top. A retriever with good recall but poor MRR needs reranking, while poor recall needs a better index or chunking.

Related Examples