Topic 1 of 5 · 4 min read
Recall at k, precision and MRR
Goal: Compute recall@k and MRR for a small labelled run and say whether a failure is in retrieval or in generation.
Score retrieval on its own, against passages a human marked as correct, before you look at a single generated answer. A RAG pipeline has two parts that fail differently. If the right passage never reaches the model, no prompt can fix the answer. If it does reach the model and the answer is still wrong, the retriever is fine and the generator is the problem.
Check yourself · 1 of 3
A labelled question has two gold passages. The retriever returns one of them in its top 5. What is recall@5 for this question?
Why it matters
Teams often tune the prompt for a week when the retriever was returning the wrong chunks all along. A retrieval score tells you in minutes which half to work on.
The metrics
Start with a labelled set: each question lists the gold passages, the chunk ids that contain the answer. Run the retriever and take the top k results.
- Recall@k: of the gold passages, what fraction appears in the top k? One gold passage and it is in the top 5: recall@5 is 1. Two gold passages and one is found: 0.5. This is the metric to watch first, because a missing passage cannot be recovered later.
- Precision@k: of the k results, what fraction is gold? It drops when you raise k, and low precision means the model reads a lot of noise.
- MRR (mean reciprocal rank): for each question, take 1 divided by the rank of the first gold passage (rank 1 gives 1, rank 4 gives 0.25, not found gives 0), then average. It rewards putting the answer near the top.
- Hit rate@k: the share of questions with at least one gold passage in the top k. It is simple and good for a first report.
Before and after
Weak: "The bot gave wrong answers on 12 of 40 questions, so we rewrote the prompt." Fixed: "Recall@5 is 0.70, so 12 questions never had their gold chunk in context. For the 28 questions where it was present, 26 were answered correctly." Now the work is clear: fix retrieval for the 12, leave the prompt alone.
Reading the numbers
- Recall@k low, MRR low: the retriever misses. Look at chunking, the embedding model, hybrid search.
- Recall@k high, MRR low: the passage is found but buried. Add a reranker or lower k for the prompt.
- Recall@k high, answers wrong: the failure is in generation or in the labels.
What goes wrong
- Choosing k to look good. Report the k you actually pass to the model, not the largest one.
- Gold labels that are too narrow. If two chunks answer the question and only one is labelled, a correct retrieval counts as a miss.
- Averaging away the problem. A mean of 0.85 can hide one document type with recall 0.2. Slice by question type and source.
- Scoring answers instead. An answer can be right by luck from model memory, so it does not prove retrieval worked.
Examples
A small run, scored by hand
Four questions, gold chunk id and the rank at which the retriever returned it (top 5 only).
| Question | Gold chunk | Rank found |
|---|---|---|
| q1 | c12 | 1 |
| q2 | c40 | 3 |
| q3 | c07 | not found |
| q4 | c33 | 2 |
Recall@5 is 3 of 4, 0.75. Reciprocal ranks are 1, 0.33, 0 and 0.5, so MRR is about 0.46. Hit rate and recall are good, MRR is low: the answers are in the list but not near the top.
Computing recall@k and MRR in code
def score(runs, k=5):
hits, rr = 0, 0.0
for gold, ranked in runs: # gold: set of ids, ranked: list of ids
top = ranked[:k]
found = [i for i, c in enumerate(top, 1) if c in gold]
hits += len(gold & set(top)) / len(gold)
rr += 1 / found[0] if found else 0
n = len(runs)
return {'recall@k': hits / n, 'mrr': rr / n}
Keep the retrieval run (question, ranked ids) as a saved file so the same numbers can be recomputed later.
Key points
- Score retrieval separately from generation, using chunk ids a human labelled as gold.
- Recall@k at the k you really send to the model is the first number to read, because a missing passage cannot be recovered later.
- MRR measures how high the first gold passage ranks, so it shows when the answer is found but buried.
- High recall with wrong answers points at generation or at the labels, not at the retriever.
- Slice the scores by question type and source document, since a good average can hide a broken segment.
Common mistakes
- Tuning the prompt before checking whether the gold passage was retrieved.
- Reporting recall at a larger k than the pipeline passes to the model.
- Labelling only one gold chunk when several chunks answer the question, which turns correct retrievals into misses.
- Reading only the overall average and missing a weak document type.
Check yourself · 2 of 3
The first gold passage for a question is returned at rank 4. What reciprocal rank does it add to the MRR?
Check yourself · 3 of 3
Recall@5 is 0.95, yet users report wrong answers. Where should you look next?
Practice items, not counted towards your mastery.
Start here
- Ragas documentation (opens in a new tab)DocsFree
Definitions and usage of faithfulness, context precision, context recall and answer correctness metrics.
- RAGAS: Automated Evaluation of Retrieval Augmented Generation (opens in a new tab)PaperFree
The paper that defines reference-free faithfulness, answer relevance and context relevance scoring.
- BEIR: A Heterogeneous Benchmark for Zero-shot Evaluation of Information Retrieval Models (opens in a new tab)PaperFree
Shows how retrieval is scored with labelled queries and ranked metrics, and why one dataset's score does not transfer.