Skip to content
AgenticAIWiki
  1. Learn
  2. Part 4
  3. 26. Evaluating RAG
  4. 26.1

Lesson 26.1 · 1 of 7 · 4 min read · Reviewed 24 Nov 2026

Retrieval metrics: recall and precision

By the end, you can

  • Measure retrieval quality with recall and precision.
  • Build retrieval checks on labelled questions.

Labelled retrieval cases

{"question": "Can I return worn shoes?", "relevant": ["pol-4.2", "pol-4.4"]}

Label which chunksChunking: Splitting documents into smaller passages before embedding and indexing them. (or documents) answer each question. A few hundred questions give a useful signal.

Metrics

Metric Meaning
Hit rate@k (often called recall@k) Share of questions with at least one relevant chunk in the top k
Recall@k, strict For each question, the share of all its relevant chunks that are in the top k, averaged
Precision@k Share of the top k that are relevant
MRR Average of 1 / rank of the first relevant chunk
nDCG Rewards relevant chunks ranked higher, with graded relevance

For RAGRetrieval-augmented generation (RAG): Retrieving relevant documents at request time and adding them to the prompt so the answer is grounded in them., recall@kHit rate@k: The share of test questions with at least one relevant chunk in the top k results. at the k you actually pass to the model matters most: if the right chunk isn’t there, the model can’t use it. Many tools report the hit-rate form under the name recall@k, and this course does too. The two match when each question has one relevant chunk. When an answer needs several passages, also track the strict form: a hit rate of 100% can hide questions where only one of three needed passages came back.

Using them

config                         recall@5   MRR
vectors only                     0.61     0.44
+ BM25 hybrid                    0.72     0.53
+ contextual chunks              0.81     0.60
+ rerank (50 -> 5)               0.88     0.71

Change one thing at a time and keep what helps (chapter 23).

Labels from logs

Start from real questions. A model can propose relevant chunks for labelling, but have people confirm a sample (chapter 18).

Key points

  • Evaluate retrieval separately from answers: did the right passages come back?
  • Hit rate@k (often reported as recall@k): share of questions with a relevant passage in the top k. Strict recall@k: share of all relevant passages that reach the top k. Precision: share of retrieved passages that are relevant.
  • MRR and similar metrics reward putting the right passage near the top.
Hands-on labRuns in the Playground

Score two retrievers with recall and precision

Compute recall@k and precision@k on labelled questions and say which retrieval setup is better at the k you pass to the model.

You will have: A script that prints a small table of recall@k and precision@k for two retrieval setups.

Steps

  1. Read the labelled cases and the ranked results for two setups.
  2. Write recall_at_k: the share of questions with at least one relevant chunk in the top k (strictly, this is hit rate@k; see 53.1).
  3. Write precision_at_k: the average share of the top k that is relevant.
  4. Print both metrics for k = 3 and k = 5 for each setup.
  5. Decide which setup to keep, using recall at the k you actually pass.

Done when

  • Vectors only scores recall@5 = 0.60 and hybrid + rerank scores recall@5 = 1.00.
  • Precision values are between 0 and 1 and fall as k grows for the same hits.
  • The script names the setup with the higher recall@5.

Open in the Playground No API key needed.

Starter code (Python)
# Labelled retrieval cases: question id -> relevant chunk ids. Illustrative.
RELEVANT = {
    "q1": {"pol-4.2"},
    "q2": {"pol-4.3", "pol-4.4"},
    "q3": {"pol-7.1"},
    "q4": {"pol-2.5"},
    "q5": {"pol-4.2", "pol-4.4"},
}
# Ranked results (top 10) from two retrieval setups.
RUNS = {
    "vectors only": {
        "q1": ["pol-4.1", "pol-4.2", "pol-9.9", "pol-3.3", "pol-1.1", "pol-4.5", "pol-2.2", "pol-8.8", "pol-6.1", "pol-5.5"],
        "q2": ["pol-4.4", "pol-1.2", "pol-3.1", "pol-2.2", "pol-5.1", "pol-4.3", "pol-6.6", "pol-7.7", "pol-8.1", "pol-9.1"],
        "q3": ["pol-2.1", "pol-3.3", "pol-5.5", "pol-6.2", "pol-8.4", "pol-9.2", "pol-7.1", "pol-1.1", "pol-1.2", "pol-1.3"],
        "q4": ["pol-2.5", "pol-2.6", "pol-2.4", "pol-1.1", "pol-3.3", "pol-4.4", "pol-5.5", "pol-6.6", "pol-7.7", "pol-8.8"],
        "q5": ["pol-9.9", "pol-8.8", "pol-7.7", "pol-6.6", "pol-5.5", "pol-4.4", "pol-4.2", "pol-3.3", "pol-2.2", "pol-1.1"],
    },
    "hybrid + rerank": {
        "q1": ["pol-4.2", "pol-4.1", "pol-4.5", "pol-9.9", "pol-3.3", "pol-1.1", "pol-2.2", "pol-8.8", "pol-6.1", "pol-5.5"],
        "q2": ["pol-4.3", "pol-4.4", "pol-1.2", "pol-3.1", "pol-2.2", "pol-5.1", "pol-6.6", "pol-7.7", "pol-8.1", "pol-9.1"],
        "q3": ["pol-7.1", "pol-2.1", "pol-3.3", "pol-5.5", "pol-6.2", "pol-8.4", "pol-9.2", "pol-1.1", "pol-1.2", "pol-1.3"],
        "q4": ["pol-2.5", "pol-2.6", "pol-2.4", "pol-1.1", "pol-3.3", "pol-4.4", "pol-5.5", "pol-6.6", "pol-7.7", "pol-8.8"],
        "q5": ["pol-4.4", "pol-4.2", "pol-9.9", "pol-8.8", "pol-7.7", "pol-6.6", "pol-5.5", "pol-3.3", "pol-2.2", "pol-1.1"],
    },
}

def recall_at_k(run, k):
    # TODO: share of questions with at least one relevant id in run[q][:k]
    return 0.0

def precision_at_k(run, k):
    # TODO: for each question, relevant ids in the top k divided by k; then average
    return 0.0

for name, run in RUNS.items():
    for k in (3, 5):
        print(f"{name:16} k={k}  recall@{k}={recall_at_k(run, k):.2f}  precision@{k}={precision_at_k(run, k):.2f}")
# TODO: print which setup has the higher recall@5
Show a solution
# Labelled retrieval cases: question id -> relevant chunk ids. Illustrative.
RELEVANT = {
    "q1": {"pol-4.2"},
    "q2": {"pol-4.3", "pol-4.4"},
    "q3": {"pol-7.1"},
    "q4": {"pol-2.5"},
    "q5": {"pol-4.2", "pol-4.4"},
}
# Ranked results (top 10) from two retrieval setups.
RUNS = {
    "vectors only": {
        "q1": ["pol-4.1", "pol-4.2", "pol-9.9", "pol-3.3", "pol-1.1", "pol-4.5", "pol-2.2", "pol-8.8", "pol-6.1", "pol-5.5"],
        "q2": ["pol-4.4", "pol-1.2", "pol-3.1", "pol-2.2", "pol-5.1", "pol-4.3", "pol-6.6", "pol-7.7", "pol-8.1", "pol-9.1"],
        "q3": ["pol-2.1", "pol-3.3", "pol-5.5", "pol-6.2", "pol-8.4", "pol-9.2", "pol-7.1", "pol-1.1", "pol-1.2", "pol-1.3"],
        "q4": ["pol-2.5", "pol-2.6", "pol-2.4", "pol-1.1", "pol-3.3", "pol-4.4", "pol-5.5", "pol-6.6", "pol-7.7", "pol-8.8"],
        "q5": ["pol-9.9", "pol-8.8", "pol-7.7", "pol-6.6", "pol-5.5", "pol-4.4", "pol-4.2", "pol-3.3", "pol-2.2", "pol-1.1"],
    },
    "hybrid + rerank": {
        "q1": ["pol-4.2", "pol-4.1", "pol-4.5", "pol-9.9", "pol-3.3", "pol-1.1", "pol-2.2", "pol-8.8", "pol-6.1", "pol-5.5"],
        "q2": ["pol-4.3", "pol-4.4", "pol-1.2", "pol-3.1", "pol-2.2", "pol-5.1", "pol-6.6", "pol-7.7", "pol-8.1", "pol-9.1"],
        "q3": ["pol-7.1", "pol-2.1", "pol-3.3", "pol-5.5", "pol-6.2", "pol-8.4", "pol-9.2", "pol-1.1", "pol-1.2", "pol-1.3"],
        "q4": ["pol-2.5", "pol-2.6", "pol-2.4", "pol-1.1", "pol-3.3", "pol-4.4", "pol-5.5", "pol-6.6", "pol-7.7", "pol-8.8"],
        "q5": ["pol-4.4", "pol-4.2", "pol-9.9", "pol-8.8", "pol-7.7", "pol-6.6", "pol-5.5", "pol-3.3", "pol-2.2", "pol-1.1"],
    },
}

def recall_at_k(run, k):
    hits = [any(d in RELEVANT[q] for d in ids[:k]) for q, ids in run.items()]
    return sum(hits) / len(hits)

def precision_at_k(run, k):
    vals = [sum(d in RELEVANT[q] for d in ids[:k]) / k for q, ids in run.items()]
    return sum(vals) / len(vals)

for name, run in RUNS.items():
    for k in (3, 5):
        print(f"{name:16} k={k}  recall@{k}={recall_at_k(run, k):.2f}  precision@{k}={precision_at_k(run, k):.2f}")

best = max(RUNS, key=lambda n: recall_at_k(RUNS[n], 5))
print("Keep:", best, "(higher recall@5)")
Quick checkYour RAG answers are often wrong. Recall@5 is 60%. What does this tell you?
How sure are you?
Pick an answer and how sure you are