# Retrieval Metrics: Recall@k, MRR, nDCG?

> How recall@k, MRR and nDCG score a RAG search, with a worked bank example and the trade-offs, for your AI interview.

**In short:** Recall@k asks how many of the right pieces of text (chunks) appear in the top k results. MRR asks how high the first right chunk sits, and nDCG rewards right chunks that sit near the top. All three need a test set where someone has marked which chunks are correct.

AI and LLM interview question Q131, Retrieval-augmented generation (RAG). Intermediate, very common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/retrieval-metrics-recall-at-k-mrr-ndcg

## Say it in 30 seconds

Retrieval metrics score the search step of a RAG app before the model writes anything. I start with real questions where someone has marked the correct chunks. Recall at k counts how many of those correct chunks land in the top k results. MRR looks at how high the first correct one sits. nDCG gives credit to every correct chunk, more when it sits near the top. I read them together, because a search can score well on one and badly on another.

## Say it in 2 minutes

A RAG app answers from the chunks its search returns, so the search has to be right before the model sees anything. To measure it, I build a test set of real questions. For each one, a person marks which chunks hold the answer. Those marks are the ground truth, and every score depends on them.

Recall at k is the simplest. Take the top k results, usually 3, 5 or 10, and count how many of the marked chunks appear. If the answer needs two chunks and one of them is in the top 3, recall at 3 is one half. MRR, mean reciprocal rank, is 1 divided by the position of the first correct chunk, averaged over the questions. It cares about getting one good hit near the top. nDCG, normalised discounted cumulative gain, handles several correct chunks. Each hit counts for more the higher it sits, and the total is compared with the best possible order.

I read the three numbers together. A high MRR with low recall means the search finds one good chunk quickly but misses the others the answer needs. In a bank's policy assistant, that can mean the right fee clause never reaches the model. Recall at k is the number I watch first, and nDCG and MRR tell me about the order. Retrieval scores don't prove the final answer is right, so I also score the answers themselves.

## What are recall@k, MRR and nDCG, in plain words?

A RAG app (retrieval-augmented generation: it searches your documents first, then has the model answer from what it found) takes chunks, which are short pieces of text, from a search and hands the best ones to the model. Retrieval metrics score that search step on its own. They answer two questions: did the search return the right text, and in what order?

Picture a paralegal at a law firm asked for every liability clause across 40,000 supplier contracts. They hand the lawyer a short stack of pages. The lawyer can judge that stack three ways: were the right pages in it, how soon did the first good page appear, and were the best pages on top. The three numbers are those checks, scored.

- **Chunk:** A short piece of a document, cut so it fits in a prompt.
- **Top k:** The first k results the search returns, for example the top 3.
- **Ground truth:** The chunks a person has marked as holding the answer.
- **Hit:** A returned chunk that is in the ground truth.

All three metrics need a test set first: real questions, with the correct chunks marked by a person. Without the marks, there is nothing to measure against.

## How do the three metrics differ?

All three read the same ranked list, but each looks at it from a different angle. Recall ignores the order inside the top k. MRR looks only at the first hit. nDCG counts every hit and gives more weight to hits near the top.

*Diagram, Three scores from one ranked list: One test question goes to the retriever, which returns a ranked list. A check marks each returned slot as a hit or a miss against the labelled answers. The same hits feed all three scores, so one list gives three numbers.*
1. The test question goes to the retriever.
2. Its ranked list of top k results goes on to the check.
3. The labelled answers go to the same check.
4. Recall counts the hits found in the top k.
5. MRR reads the position of the first hit.
6. nDCG weighs every hit by how high it sits.

- **Recall@k: How many correct chunks made the top k.** The share of the marked chunks that appear in the first k results. Order inside the top k does not matter.
- **MRR: How soon the first hit appears.** 1 divided by the position of the first hit, averaged over the questions. A hit at position 1 scores 1, and at position 2 it scores 0.5.
- **nDCG: Every hit, weighted by position.** Each hit counts for less the lower it sits. The total is divided by the best possible total, so 1 is perfect.

> "MRR ranges from 0 to 1, where 1 is perfect (correct answer always first)." (Anthropic)

The standard form of this measure uses a logarithmic discount, so later positions count for less. The ML.NET reference defines DCG as a sum of gains by position, where each gain is a relevance score. That is what lets nDCG handle hits of different value, where one correct chunk can matter more than another.

> **Tip:** In an interview, say the three in one breath: recall for coverage, MRR for the first hit, nDCG for the whole order. Then add that all three need labels.

## How do you score a retriever with a worked example?

A bank's support assistant answers from about 3,000 pages of policy. The team's test set holds 200 real customer questions, each with its correct chunks marked by a person. Here are four rows from that set, each with the top 3 results. A slot is a hit (1) when its chunk is marked correct, and a miss (0) otherwise.

```python
# Scores a bank's policy search on 4 labelled test questions. Each slot is a hit (1) or a miss (0).
import math

TESTS = [
    (["c07", "c03", "c12"], {"c12"}),
    (["c31", "c18", "c33"], {"c31", "c33"}),
    (["c09", "c21", "c17"], {"c44"}),
    (["c56", "c55", "c60"], {"c55", "c56"}),
]

def scores(ranked, correct, k=3):
    hits = [1 if c in correct else 0 for c in ranked[:k]]
    recall = sum(hits) / len(correct)
    first = next((i + 1 for i, h in enumerate(hits) if h), None)
    mrr = 1 / first if first else 0
    # the log base cancels in dcg / ideal, so log2 gives the same nDCG as ln
    dcg = sum(h / math.log2(i + 2) for i, h in enumerate(hits))
    ideal = sum(1 / math.log2(i + 2) for i in range(min(k, len(correct))))
    return recall, mrr, dcg / ideal

for n, (ranked, correct) in enumerate(TESTS, 1):
    recall, mrr, ndcg = scores(ranked, correct)
    print(f"Q{n}: recall@3 {recall:.2f}  MRR {mrr:.2f}  nDCG@3 {ndcg:.2f}")
rows = [scores(r, c) for r, c in TESTS]
avg = [sum(row[i] for row in rows) / len(rows) for i in range(3)]
print(f"Average: recall@3 {avg[0]:.2f}  MRR {avg[1]:.2f}  nDCG@3 {avg[2]:.2f}")
```

Output:

```text
Q1: recall@3 1.00  MRR 0.33  nDCG@3 0.50
Q2: recall@3 1.00  MRR 1.00  nDCG@3 0.92
Q3: recall@3 0.00  MRR 0.00  nDCG@3 0.00
Q4: recall@3 1.00  MRR 1.00  nDCG@3 1.00
Average: recall@3 0.75  MRR 0.58  nDCG@3 0.60
```

Read the individual rows as well as the averages. Q1 found its only correct chunk at position 3, so recall is full while MRR drops to 0.33. Q2 has two correct chunks, both in the top 3. MRR says 1.00, but nDCG says 0.92, because the second hit sits lower. Q3 is the real failure: the clause that answers the question is outside the top 3, so every score is zero. Raising k to 10 might catch it, but the model then reads more text. Q4 is the goal. Across the four, recall at 3 averages 0.75, MRR 0.58 and nDCG 0.60.

> **Tip:** Four rows are only a demo. A real test set has hundreds of questions, so check the averages and read the zeros. The zeros show exactly what the search misses.

## What do the numbers miss, and what do you tune?

A bigger k never lowers recall, but it feeds the model more text, including weaker chunks. Precision is the share of returned chunks that are correct. Anthropic's cookbook says the balance between precision and recall depends on the use case, and that high recall is often prioritised, because the model can filter out weak chunks. Recall is only as trustworthy as the labels, so the labels must mark every correct chunk.

> "Computing recall requires your ground-truth to contain all relevant items." (Databricks on Microsoft Learn)
>
> "In many RAG systems, high recall is often prioritized, as LLMs can filter out less relevant information during generation." (Anthropic)

Retrieval scores also stop short of the answer. A RAG app can respond poorly even after retrieving the correct context, and it can respond well after a weak retrieval. So I measure the final answers too, for accuracy and for whether they stay grounded in the chunks they were given.

1. **Label first:** Mark the correct chunks for each test question. Without the marks, recall has nothing to count against.
2. **Fix what is missing:** Raise k, or improve the chunking and the search, until the correct chunks reach the top k.
3. **Then fix the order:** Add a reranker (a second pass that re-sorts the results), then watch MRR and nDCG move.
4. **Then check the answers:** Score the final answers too, since a good search does not guarantee a good answer.

> **Tip:** Keep the test set fixed. Change one thing, re-run the same questions, and compare the same three numbers.

### How would you measure a search over 40,000 contracts?

A legal team searches 40,000 supplier contracts. Lawyers ask two kinds of question. A lookup needs one clause, so one good hit is enough. A sweep, such as every contract with an uncapped liability clause, needs every match. One average would hide the difference, so I score the two kinds apart.

- **Lookups: MRR:** MRR shows how fast the right clause reaches the top, which is what a lawyer needs for a single answer.
- **Sweeps: recall at k:** Recall at the k that lawyers would actually read shows whether all the matches are in the list.
- **Ordering: nDCG:** nDCG shows whether the most relevant matches come first when several clauses count.
- **Misses: read them:** Read the questions that score zero. They show which contract layouts the chunking breaks, such as scanned pages.

Then I change one thing at a time, re-run the same labelled set, and report the three numbers for each kind of question.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Recall@k: the share of the marked correct chunks found in the top k.
- [ ] MRR: it scores the first hit only, so one good hit near the top counts.
- [ ] nDCG: it weighs every hit by position and scores against the best order.
- [ ] All three need labelled test questions, with the correct chunks marked.
- [ ] A bigger k can raise recall, but it adds weaker text, and retrieval scores don't prove the answer is right.
- [ ] You would also score the final answers as well as the search.

**Common traps**

- Trap: "A high MRR means the retriever finds everything." Better: MRR only looks at the first hit. Recall at k tells you how much of the answer set was found.
- Trap: "nDCG and recall are the same thing with a different name." Better: Recall ignores order inside the top k. nDCG weighs each hit by its position.
- Trap: "We can score retrieval without labels." Better: Recall needs the full set of correct chunks marked. Without labels there is nothing to count against.

## Can you spot the mistake?

**Spot the mistake:** A teammate says the search is fine, because its MRR is 0.91. What is missing from that claim?

A. Nothing. MRR is the only retrieval metric that matters.
B. MRR only scores the first hit. Recall at k shows how many of the correct chunks were found at all.
C. An MRR of 0.91 always means the final answers are wrong.
D. MRR needs the model's final answer, not the search results.

**Answer:** B. MRR only looks at the first correct chunk, so a search can score high while missing the other chunks the answer needs. Add recall at k, measured on the same labelled questions, before calling the search fine.

**Quick check:** In the bank's policy search (top 3), a wrong chunk sits at position 1 and the correct clause at position 2. The clause moves up to position 1, and the wrong chunk drops to position 2. Which score changes?

A. Recall at k
B. MRR
C. Chunk length
D. Number of pages indexed

**Answer:** B. Recall counts the correct chunks inside the top k, and that count stays the same when they reorder. MRR rises from one half to 1, because the first hit now sits at position 1.

## Can you answer these from memory?

**Flash cards**

- **What is recall@k?** The share of the correct chunks that appear in the top k results.
- **What is MRR?** The average of 1 divided by the position of the first correct chunk.
- **What does nDCG add?** It weighs every hit by position and scores against the best possible order.
- **What do all three need?** A labelled test set: real questions with the correct chunks marked by a person.
- **Why not trust retrieval scores alone?** A good search can still give a bad answer, so score the final answers too.
- **What happens to recall as k grows?** It never falls. It rises or stays the same, but the model then reads more text, including weaker chunks.

## Likely follow-up questions

- Q132 Generation metrics: faithfulness, answer relevance? https://designervenkat.online/interview/ai/rag-generation-metrics-faithfulness-answer-relevance
- Q133 What are common RAG failure modes? https://designervenkat.online/interview/ai/common-rag-failure-modes
- Q126 Rerankers: cross-encoder vs bi-encoder? https://designervenkat.online/interview/ai/cross-encoder-vs-bi-encoder-rerankers
- Q112 Walk through a RAG pipeline end to end. https://designervenkat.online/interview/ai/rag-pipeline-end-to-end
- Q134 How do you reduce hallucinations in RAG? https://designervenkat.online/interview/ai/how-to-reduce-hallucinations-in-rag

## Learn it in depth

- [Evaluation Metrics](https://designervenkat.online/learn/ai-engineering/evaluation-metrics)
