Evaluating RAG: retrieval and generation separately

Recall@k, MRR, context precision, faithfulness and answer relevance, and how to build a gold set quickly.

Advanced about 5 h

The picture

  1. 01

    Gold set

    questions + sources

  2. 02

    Retrieval metrics

    recall, MRR

  3. 03

    Generation metrics

    faithful, relevant

  4. 04

    Slice

    by question type

  5. 05

    Regress

    on every change

Two systems, two scorecards

A RAG answer can fail in the retriever (the evidence never arrived) or the generator (the evidence arrived and was ignored or distorted). One end-to-end accuracy number hides which. Score them separately.

Build a gold set quickly

  1. Sample 50 to 100 chunks from your corpus.
  2. Have a model draft a question each chunk answers, plus the reference answer.
  3. Review every item by hand. Discard ambiguous questions. This hour is the best investment in the project.
  4. Add questions of different kinds: lookup, multi-hop, numeric, comparison, and unanswerable ones.

Retrieval metrics

def recall_at_k(retrieved_ids, relevant_ids, k):
    return len(set(retrieved_ids[:k]) & set(relevant_ids)) / len(relevant_ids)

def mrr(retrieved_ids, relevant_ids):
    for rank, i in enumerate(retrieved_ids, start=1):
        if i in relevant_ids:
            return 1.0 / rank
    return 0.0

def evaluate_retriever(retrieve, gold, k=5):
    r, m = [], []
    for g in gold:
        ids = [c["id"] for c in retrieve(g["question"], k)]
        r.append(recall_at_k(ids, g["relevant"], k))
        m.append(mrr(ids, g["relevant"]))
    return {f"recall@{k}": sum(r) / len(r), "mrr": sum(m) / len(m)}

Also track context precision (what fraction of retrieved chunks were useful), because irrelevant context lowers answer quality and raises cost.

Generation metrics

  • Faithfulness: is every claim in the answer supported by the retrieved context?
  • Answer relevance: does it address the question?
  • Correctness against the reference answer.
  • Abstention accuracy on unanswerable questions.
import json, anthropic
client = anthropic.Anthropic()

JUDGE = """You are checking an answer against its sources.
Sources:
{sources}

Answer:
{answer}

List each factual claim in the answer and mark it "supported" or "unsupported" by the sources.
Return JSON: {{"claims": [{{"claim": str, "verdict": "supported"|"unsupported"}}]}}"""

def faithfulness(answer, chunks):
    sources = "\n".join(f"[{i}] {c['text']}" for i, c in enumerate(chunks, 1))
    out = client.messages.create(model="claude-sonnet-5-5", max_tokens=800, temperature=0,
                                 messages=[{"role": "user", "content": JUDGE.format(sources=sources, answer=answer)}]).content[0].text
    claims = json.loads(out)["claims"]
    return sum(c["verdict"] == "supported" for c in claims) / max(len(claims), 1)

An LLM judge is a measuring instrument: calibrate it by comparing its verdicts to your own on 30 items, and use a different (or stronger) model than the generator where you can. The LLM-as-judge topic goes deeper.

Slice, don't average

Report by question type and by document type. An overall 85 percent can hide 40 percent on numeric questions.

Regression discipline

Keep the gold set and the script in the repository. Run them in CI on every change to chunking, embeddings, prompts or models, and fail the build on a drop beyond noise.

Common mistakes

  • Using the same model to write the questions and answer them, which inflates scores.
  • Evaluating only answerable questions.
  • Reporting a gain on 20 questions without a confidence interval.

Exercises

  1. Build a 60-question gold set with 10 unanswerable items and report all four generation metrics.
  2. Show that fixing a retrieval bug raised faithfulness, and explain why.

You are done when you can…

  • Compute recall@k and MRR for a retriever against a labelled set
  • Score faithfulness with a judge prompt and calibrate it against human labels
  • Report results by question type so you know where the system fails