Early-bird price on the GenAI & Agentic AI Engineer Bootcamp batch. Grab a seat →

How to evaluate a RAG system: metrics, answer checks and test sets

Recall@k, precision and MRR with a worked calculation, faithfulness checks, building a test set from real questions, and validating an LLM judge.

Related courseGenAI & Agentic AI Engineer Bootcamp
On this page
Diagram: Three layers of RAG evaluation

Evaluate a RAG system in two parts. First measure retrieval: for a fixed set of real questions, do the passages that contain the answer appear in the top results? Then measure the answers: is every claim supported by the retrieved passages, and does the answer address the question? Both need a test set you write once and re-run after every change.

Key takeaways

  • Retrieval and generation fail in different ways, so score them separately. A wrong answer built on the right passages needs a different fix from a wrong answer built on the wrong ones.
  • Recall@k, precision@k and mean reciprocal rank (MRR) are simple arithmetic. You can compute them in a dozen lines of Python.
  • Faithfulness asks whether each claim in the answer is supported by the retrieved text. It is the most useful single check on generation.
  • Build the test set from questions people really asked, and include some that your documents cannot answer.
  • An LLM judge makes grading cheap enough to run often. Check its verdicts against human labels before you rely on it.

This article belongs to our complete guide to retrieval-augmented generation.

What should you measure, and in what order?

Measure retrieval first. If the right passage never reaches the prompt, nothing later can repair the answer, and retrieval metrics need no model calls. Then measure the answers. Finally have a person read a sample.

RAG evaluation has three layers: retrieval metrics, answer checks and human review of a sample.

How do recall@k, precision@k and MRR work?

For each test question you need the set of relevant passages and the ranked list your system returned. Then:

  • Recall@k is the share of the relevant passages that appear in the top k results. It answers "did we find what we needed?"
  • Precision@k is the share of the top k results that are relevant. It answers "how much of what we sent to the model was useful?"
  • Reciprocal rank is 1 divided by the position of the first relevant result, or 0 if there is none. The mean over all questions is MRR. The measure comes from the TREC-8 question answering track of 1999, whose report notes that it is bounded between 0 and 1 and averages well.

A worked calculation

Four questions, k = 5. Relevant passages are marked with an asterisk.

QuestionRelevant passagesTop 5 retrievedRecall@5Precision@5Reciprocal rank
Q1c12, c40c12, c7, c40, c3, c92/2 = 1.002/5 = 0.401/1 = 1.00
Q2c88c5, c88*, c2, c61, c91/1 = 1.001/5 = 0.201/2 = 0.50
Q3c21, c22, c30c1, c2, c21, c4, c302/3 = 0.672/5 = 0.401/3 = 0.33
Q4c77c3, c8, c14, c20, c310/1 = 0.000/5 = 0.000
Mean0.670.250.46

Read the three numbers together. Recall of 0.67 says a third of the needed passages never reached the model. Precision of 0.25 says three in four passages sent were filler. MRR of 0.46 says the first useful passage typically sits around second or third place.

The same calculation in Python:

def retrieval_metrics(cases, k=5):
    recall = precision = rr = 0.0
    for relevant, retrieved in cases:
        top = retrieved[:k]
        hits = [doc for doc in top if doc in relevant]
        recall += len(set(hits)) / len(relevant)
        precision += len(hits) / k
        first = next((i for i, doc in enumerate(top, start=1) if doc in relevant), None)
        rr += 1 / first if first else 0.0
    n = len(cases)
    return {"recall": recall / n, "precision": precision / n, "mrr": rr / n}

cases = [
    ({"c12", "c40"}, ["c12", "c7", "c40", "c3", "c9"]),
    ({"c88"}, ["c5", "c88", "c2", "c61", "c9"]),
    ({"c21", "c22", "c30"}, ["c1", "c2", "c21", "c4", "c30"]),
    ({"c77"}, ["c3", "c8", "c14", "c20", "c31"]),
]
print(retrieval_metrics(cases))
# {'recall': 0.666..., 'precision': 0.25, 'mrr': 0.458...}

Which metric matters most?

For RAG, usually recall. A model can ignore an irrelevant passage, but it cannot use one it never saw. Precision matters for cost and for distraction. MRR matters when you pass only a few passages, or when you show sources to users in order.

One caution: if you re-chunk your documents, passage IDs change, so label relevance by source document and a quotation instead. The chunking guide shows how.

How do you check the answers?

Three checks cover most of what matters.

Faithfulness. Is every claim in the answer supported by the retrieved passages? The open-source Ragas library defines it this way: "A response is considered faithful if all its claims can be supported by the retrieved context." The score is the number of supported claims divided by the total number of claims. Suppose an answer says "Refunds take 5 working days and are paid to the original card", and the retrieved passage mentions only the 5 days. One of two claims is supported, so faithfulness is 0.5.

Answer relevance. Does the answer address the question that was asked? A faithful answer to a different question is still a failure.

Correctness against a reference. Where you have a known right answer, does the response agree with it? An answer can be faithful to a passage that is itself out of date, and only this check catches that.

Add one behavioural check: for questions your documents cannot answer, did the system say so?

How do you build a test set from real questions?

  1. Collect questions people actually asked. Use support tickets, search logs and chat transcripts. Invented questions tend to reuse the documents' own wording, which makes retrieval look better than it is.
  2. Cover the range. Include simple lookups, questions that need two passages, questions with exact codes or names, and some with no answer in the corpus.
  3. Label the evidence. For each question record the source document, the quotation that answers it and a short reference answer. Have a subject expert review it.
  4. Start small and grow. Thirty to fifty well-labelled questions will reveal large problems. Add every production failure as a new case.
  5. Keep it fixed between comparisons. Change the system, not the test.

Be honest about how exact the numbers are. With 50 questions, one question is 2 percentage points, so a move from 0.80 to 0.82 may be chance.

Can an LLM do the grading?

Yes, with supervision. An LLM judge is a model given the question, the retrieved passages, the answer and a rubric, and asked for a verdict. It makes it practical to grade hundreds of answers on every change.

The research is encouraging, and specific about the risks. Lianmin Zheng and colleagues, in a 2023 paper for the NeurIPS Datasets and Benchmarks track, found that a strong judge model reached over 80% agreement with human preferences on chat responses, the same level as agreement between humans. The paper also names judge biases: position, verbosity (favouring longer answers) and self-enhancement (favouring a model's own output). That study compared chatbot answers, not RAG faithfulness, so test your own judge.

Practical rules:

  • Ask narrow questions with a yes or no answer, such as "Is this claim supported by the passages?". Avoid "rate this answer from 1 to 10".
  • Ask for the reasoning before the verdict, and store both.
  • Fix the judge model and the rubric, and version them. A changed judge is a changed measuring stick.

How do you check the judge against human labels?

Have one or two people label a sample by hand, then run the judge on the same sample and compare. Suppose 100 answers are labelled:

Judge says faithfulJudge says unfaithful
Human says faithful706
Human says unfaithful816

Raw agreement is (70 + 16) / 100 = 86%. That sounds good, but look at the row that matters. Of the 24 answers a human marked unfaithful, the judge caught 16, which is 67%, and let 8 through. Read those 8 and tighten the rubric. Repeat the check whenever you change the judge model or its prompt.

Evaluation is a loop of building a test set, scoring retrieval, scoring answers, checking the judge and changing one thing.

Once the scores are stable, run them automatically on every change. LLM evaluation in CI explains how to set release gates. When retrieval scores are the weak point, hybrid search and reranking are the usual next steps.

Common questions

How many test questions do I need?

Enough for the differences you care about to be more than one or two questions. Thirty to fifty is a workable start. A few hundred gives steadier numbers. Quality of labels matters more than count.

Do I need an evaluation library?

Not for retrieval metrics, which are a few lines of code. For answer checks, a library such as Ragas saves you writing judge prompts, but you still have to validate its scores against your own human labels.

What is a good recall@k?

There is no universal threshold. It depends on k, on how hard your questions are and on how costly a miss is. Use the metric to compare versions of your own system, not to compare yourself with someone else's numbers.

Free download: RAG evaluation checklist and test-set template

A checklist for measuring a RAG system properly, retrieval first and answers second, plus a blank test-set spreadsheet you can fill in today. See what’s inside.