Skip to content
Visual explainer 2h to learn 3 steps

Evaluating retrieval & RAG quality

Measure retrieval recall and answer faithfulness separately.

By Invicly Tech, , in AI Engineering with Python. About 11 minutes to read.

Topic 25 of 33

In short

A RAG answer can fail because retrieval missed the right chunk or because generation misused good context. Measuring retrieval and generation separately tells you which part to fix.

Explained simply

A RAG answer has two chances to fail. Search can bring back the wrong text, or the model can misuse the right text. If you only score the final answer, both failures look the same, and you may spend a week rewriting prompts when the real problem is search.

So measure the two halves separately. For retrieval, ask: did the chunks that hold the answer come back? For generation, ask: is the answer supported by those chunks, and does it actually answer the question?

Retrieval scores need labels: for each test question, which documents contain the answer. With those labels, scoring is simple counting and costs nothing to run.

Generation scores usually need a judge. The most important one is faithfulness: does every claim in the answer appear in the retrieved text? A faithful answer can still be wrong if the source is outdated, so check correctness too.

Press Play on a diagram to walk through it one step at a time.

CompareTwo halves to measure
Retrieval
  • Recall at k: right chunk found?
  • Precision at k: noise level
  • Needs labelled relevant chunks
  • Fix: chunking, hybrid, rerank
Generation
  • Faithfulness to context
  • Answer relevance to question
  • Correctness vs golden answer
  • Fix: prompt, model, format
FlowOne RAG eval run
  1. 1
    Load cases

    Questions, relevant docs, reference answers

  2. 2
    Retrieve

    Log chunk IDs and ranks

  3. 3
    Score retrieval

    Recall@k, hit rate, MRR

  4. 4
    Generate

    Same prompt as production

  5. 5
    Score answers

    Faithfulness, correctness, relevance

  6. 6
    Bucket failures

    Retrieval, generation or source

In depth

Retrieval metrics: recall, precision, hit rate and MRR

Say a question has 2 relevant chunks and you retrieve 5, one of which is relevant. Recall at 5 is 1 of 2, or 0.5: you found half of what matters. Precision at 5 is 1 of 5, or 0.2: most of the context is noise. Hit rate at 5 asks a simpler question across all test cases: in what share of questions did at least one relevant chunk appear? MRR (mean reciprocal rank) rewards ranking: if the first relevant chunk is at rank 1 you score 1, at rank 3 you score one third, and if it never appears you score 0, averaged over all questions.

For RAG, recall matters most. A model can often ignore some noise, but it cannot use text it never received. Precision matters for cost and distraction. MRR matters when you pass only the top few chunks, because a relevant chunk at rank 9 is useless with k = 5.

  • Recall@k = relevant found in top k / all relevant
  • Precision@k = relevant in top k / k
  • Hit rate@k = share of questions with any relevant hit
  • MRR = average of 1 / rank of the first relevant result

Labelling relevance without endless work

Labelling every chunk for every question is slow, and chunk IDs change whenever you change the chunking. A practical shortcut is to label at document level: for each question, note which document (or document plus section) holds the answer. A retrieved chunk counts as relevant if it comes from that document. These labels survive re-chunking, so you can compare chunk sizes fairly.

For finer checks, store a short key phrase from the answer, like 5 business days, and treat any retrieved chunk containing it as relevant. This is fast and needs no model.

When you have no labels at all, an LLM judge can rate each retrieved chunk as relevant or not to the question. This estimates precision (sometimes called context precision) without labels, but it cannot measure recall, because it never sees the chunks that were missed.

Measuring faithfulness and answer quality

Faithfulness asks whether the answer sticks to the retrieved context. The common method has two steps. First, split the answer into individual claims: Refunds take 5 business days and are sent to the original card becomes two claims. Second, check each claim against the context with a judge. The score is supported claims divided by total claims, so one invented detail in four claims gives 0.75.

Faithfulness is not correctness. If the retrieved document is outdated, a perfectly faithful answer is still wrong. So also score correctness against a reference answer, and answer relevance: does the answer address what was asked, or does it wander?

Open-source libraries such as Ragas implement these metrics with LLM judges. They are handy, but read how each metric is computed, and calibrate on a few human labels before trusting the numbers.

FlowScoring faithfulness
  1. Step 1
    Take the answer

    Plus the retrieved context

  2. Step 2
    Split into claims

    One fact per claim

  3. Step 3
    Check each claim

    Judge: supported by context?

  4. Step 4
    Score

    Supported claims / total claims

Diagnosing: retrieval or generation?

For each wrong answer, ask two questions: was the relevant chunk retrieved, and is the answer supported by the context? The answers sort every failure into a bucket with its own fix.

Relevant chunk missing: a retrieval failure. Fix chunking, add hybrid keyword search, add a reranker, raise k, or rewrite the query. Relevant chunk present but answer unsupported or wrong: a generation failure. Fix the prompt, the order of chunks, the model, or reduce noise. Relevant chunk present and answer faithful but still wrong: the source itself is wrong or out of date, so fix the documents. Correct answer but no relevant chunk retrieved: the model answered from memory. That looks like success but is risky, because next time memory may be wrong.

Count the buckets over your eval set before changing anything, and fix the biggest one first.

MapSorting failures into buckets
Wrong or risky answer
  • Relevant chunk missing
    • Retrieval failure
    • Fix: chunking, hybrid, rerank, k
  • Chunk present, answer unsupported
    • Generation failure
    • Fix: prompt, chunk order, model
  • Faithful but wrong
    • Bad or outdated source
    • Fix: the documents
  • Correct, nothing retrieved
    • Answered from memory
    • Risky: tighten grounding

Learn it in 3 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Measure retrieval recall and precision~40m
  2. 2
    Measure answer faithfulness~40m
  3. 3
    Diagnose retrieval vs generation failures~40m

Key points

  • If recall is low, no prompt change will fix the answers.
  • Faithfulness checks whether claims are supported by the retrieved context.
  • Label which chunks are relevant for each eval question.
  • Track both metrics over time to catch regressions in either half.

Key terms

Tap a card to flip it. 0/4 known

Examples

python
def recall_at_k(retrieved: list[str], relevant: set[str], k: int = 5) -> float:
    hits = sum(1 for doc in retrieved[:k] if doc in relevant)
    return hits / len(relevant) if relevant else 0.0

cases = [
    (['c3', 'c9', 'c1'], {'c1'}),
    (['c4', 'c5', 'c6'], {'c2'}),
]
scores = [recall_at_k(r, rel) for r, rel in cases]
print(sum(scores) / len(scores))  # 0.5: retrieval misses half

Recall at k over labelled retrieval cases.

Hit rate and MRR from document-level labels

Each case lists the documents that answer it. Step 1: run retrieval and keep the ordered list of document IDs for the returned chunks. Step 2: find the rank of the first relevant one. Step 3: a hit is any rank within k; the reciprocal rank is 1 divided by that rank, or 0 if none. Step 4: average over all cases. In this example the first case hits at rank 2 (0.5), the second at rank 1 (1.0), and the third misses (0), so hit rate is 0.67 and MRR is 0.5.

python
def first_rank(retrieved: list[str], relevant: set[str]) -> int | None:
    for rank, doc in enumerate(retrieved, 1):
        if doc in relevant:
            return rank
    return None

cases = [(["faq", "refunds", "pricing"], {"refunds"}),
         (["login", "sso"], {"login"}),
         (["pricing", "faq"], {"export"})]
ranks = [first_rank(r, rel) for r, rel in cases]
k = 5
hit_rate = sum(1 for r in ranks if r and r <= k) / len(ranks)
mrr = sum(1 / r for r in ranks if r) / len(ranks)
print(round(hit_rate, 2), round(mrr, 2))  # 0.67 0.5

Finding the real problem in 40 cases

Your docs bot scores 68% correct on 40 eval questions, and the team wants to try a bigger model. Step 1: score retrieval first. Recall at 5 is 0.70, so 12 questions never got the relevant document. Step 2: score faithfulness on the answers: 0.95. The model rarely invents things. Step 3: bucket the 13 wrong answers: 10 are retrieval misses, 2 are generation errors, 1 is an outdated document. Step 4: retrieval is clearly the biggest bucket. Many misses use product codes like ERR-4012 that vector search handles badly, so you add keyword search (hybrid). Step 5: re-run: recall rises to 0.88 and correctness to 83%, without changing the model.

Quick reference

  • Score retrieval and generation separately; one blended score hides the cause
  • Recall@k: share of relevant chunks found; the first metric to fix in RAG
  • MRR: average of 1/rank of the first relevant result; 0 when nothing relevant is found
  • Label relevance at document level so labels survive re-chunking
  • Faithfulness = supported claims / total claims, checked against the retrieved context
  • Faithful is not correct: an outdated source gives a faithful wrong answer
  • Low recall, high faithfulness: fix retrieval, not the prompt
  • Count failure buckets before changing anything; fix the biggest first

Common mistakes

  • Only scoring final answers, so retrieval problems stay hidden.
  • Measuring faithfulness without checking the answer is also correct.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A question has 4 relevant chunks and 2 of them appear in your top 5. What is recall at 5?

Question 2

2The first relevant result is at rank 4. What is that question's reciprocal rank?

Question 3

3Recall@5 is 0.55 and faithfulness is 0.97. What should you work on first?

Question 4

4An answer is fully supported by the retrieved chunk but is still wrong. What is the likely cause?

Question 5

5Why can an LLM judge estimate context precision but not recall without labels?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Write precision_at_k(retrieved, relevant, k) and test it: retrieved [a, b, c, d, e], relevant {b, e, z}, k 5.

  2. Exercise 2medium

    Treat a retrieved chunk as relevant if it contains a case's key phrase (case-insensitive). Compute the hit rate at k over a list of cases using your search function.

  3. Exercise 3hard

    Write bucket(case_result) that sorts each result into retrieval_miss, generation_error, bad_source, memory_answer or ok, using three booleans: retrieved_relevant, faithful, correct. Then count buckets over all results.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is a good recall@k for RAG?
It depends on the domain, but aim high, often above 0.85 on your eval set, because answers cannot be better than what retrieval finds. Track it over time rather than chasing a universal number.
What is the difference between faithfulness and correctness?
Faithfulness checks the answer against the retrieved context. Correctness checks it against the true answer. A faithful answer from a wrong source is still incorrect.
Do I need a library like Ragas to evaluate RAG?
No. Recall, hit rate and MRR are a few lines of code. Libraries help with judge-based metrics, but understand and calibrate them before trusting their scores.
ShareXLinkedInWhatsApp