Evaluating RAG

In one sentence

Score retrieval and generation separately: recall@k and MRR tell you whether the evidence was found, faithfulness and correctness tell you whether the model used it. One blended score hides which stage to fix.

Analogy: Grading an open-book exam: first check the student opened the right pages, then whether the answer actually follows from them. A wrong answer means something different in each case.

Retrieval is scored against labeled doc ids; generation is scored against the retrieved context (faithfulness) and the reference answer (correctness).

How it works

  • Recall@k is the ceiling: if the evidence never reaches the top k, no generator can fix it.
  • MRR exposes ranking problems: a relevant doc at rank 6–10 is lost when the prompt keeps the top 5.
  • Generation is usually scored by an LLM judge calibrated on human labels: faithfulness, relevance, correctness, citations.
  • The labeled eval set is the real asset; RAGAS, TruLens or ARES automate judging but don't replace it.
  • Include no-answer questions: they reveal a bot that invents policies instead of abstaining.

Check yourself

Answer to mark it learned; your answers aren't scored.

Your split eval shows: hit rate@50 = 0.80, faithfulness 0.97, answer correctness 70%. The top 50 go to a reranker, which keeps 5 for the prompt. The team proposes a bigger generator model. Where should the effort go?

Go deeper

Full explanation

Evaluate RAG as two systems, because a bad answer has two causes: retrieval didn't find the evidence, or generation misused it. One end-to-end score can't tell you which to fix.

Retrieval metrics (need labeled relevant doc ids per question):

  • Recall@k — share of the relevant docs that appear in the top k. The ceiling for everything downstream.
  • Precision@k — share of the top k that is relevant: the noise you pay for in the prompt.
  • Hit rate@k — share of questions with at least one relevant doc in the top k.
  • MRR — mean of 1/rank of the first relevant doc. nDCG@k — rank-aware, supports graded relevance.

Generation metrics (usually an LLM judge, calibrated on human labels):

  • Faithfulness / groundedness — is every claim supported by the retrieved context?
  • Answer relevance — does it address the question?
  • Correctness — does it match the reference answer?
  • Citation accuracy — does each cited source actually support its sentence?

The eval set is the real asset: question → relevant doc ids → reference answer, drawn from real queries and covering lookups, multi-hop, numeric and no-answer questions, where the right behavior is to abstain. RAGAS, TruLens (its "RAG triad") and ARES automate the judging; they don't replace the labeled set.

Code Recall@k, MRR and hit rate over an eval set
pythonRecall@k, MRR and hit rate over an eval set
def recall_at_k(retrieved: list[str], relevant: set[str], k: int) -> float:
    return len(set(retrieved[:k]) & relevant) / len(relevant)

def reciprocal_rank(retrieved: list[str], relevant: set[str]) -> float:
    for rank, doc_id in enumerate(retrieved, start=1):
        if doc_id in relevant:
            return 1.0 / rank
    return 0.0  # never found

def evaluate(eval_set: list[dict], search, k: int = 10) -> dict:
    # No-answer items (empty relevant_doc_ids) are scored on abstention instead
    answerable = [ex for ex in eval_set if ex["relevant_doc_ids"]]
    recall, rr, hits = 0.0, 0.0, 0.0
    for ex in answerable:
        rel = set(ex["relevant_doc_ids"])
        # Collapse chunk hits to doc ids, keeping rank order: labels survive re-chunking
        retrieved = list(dict.fromkeys(c.doc_id for c in search(ex["question"], k=k)))
        recall += recall_at_k(retrieved, rel, k)
        rr += reciprocal_rank(retrieved, rel)
        hits += float(bool(rel & set(retrieved[:k])))
    n = len(answerable)
    return {f"recall@{k}": recall / n, "mrr": rr / n, f"hit_rate@{k}": hits / n}

# eval item: {"question": "...", "relevant_doc_ids": ["policy-17"], "reference_answer": "..."}
print(evaluate(eval_set, hybrid_search, k=10))  # run in CI on every retrieval change
Real example

An HR assistant scores 71% answer correctness, but where is the loss? Splitting the eval (250 questions) shows hit rate@10 of 0.93 and faithfulness of 0.97, yet MRR is only 0.48: the relevant policy often sits at rank 6–10, and the prompt only includes the top 5. Adding a reranker lifts MRR to 0.81 and correctness to 86% without touching the generator. Meanwhile 30 no-answer questions reveal the bot invents a parental-leave policy for contractors in 12 of them; a prompt rule plus an abstain check fixes that.

Trade-offs +3 · −3

+ You gain

  • Pinpoints the stage to fix: retrieval, ranking or generation.
  • Retrieval metrics are cheap and deterministic: rerun them on every chunking or embedding change.
  • A labeled set turns debates ("the new embedder feels better") into numbers.

− You pay

  • Labeling relevant doc ids and reference answers takes real expert time.
  • LLM-judged generation metrics are noisy and biased unless calibrated against human labels.
  • Offline scores drift from production as the corpus and the query mix change.

Use it when

Always, before tuning anything: chunk size, embedding model, hybrid weights, reranker, prompt. Run retrieval metrics in CI on every change and generation metrics before each release.

Avoid it when

Never skip it, but don't over-invest early: ~100 real questions with labeled doc ids are worth more than thousands of synthetic ones nobody has checked.

Numbers to know 4
  • MRR per question: first relevant doc at rank 1 → 1.0, rank 2 → 0.5, rank 5 → 0.2, not found → 0.
  • Hit rate@50 of 0.80 means ~20% of questions never see a relevant doc: no generator can fix those.
  • RAGAS core metrics: faithfulness, answer relevancy, context precision, context recall.
  • TruLens RAG triad: context relevance, groundedness, answer relevance.
Common pitfalls 4
  • Measuring only end-to-end answer quality, so every regression turns into guesswork.
  • Labeling chunk ids: the next re-chunking invalidates the whole set. Label doc ids or text spans.
  • Trusting an LLM judge's faithfulness score without checking its agreement with human labels.
  • No no-answer questions: abstaining is never rewarded, so hallucination goes unmeasured.
Interview questions 2

Answer out loud first, then reveal.

How would you set up evaluation for a new RAG system?Show answer

I start with the eval set, not the framework. I collect ~100 real or realistic questions, label the relevant doc ids and a short reference answer, and include no-answer and multi-hop cases. Then I measure two layers. Retrieval: recall@k, hit rate and MRR, deterministic and cheap enough for CI on every chunking or embedding change. Generation: faithfulness, correctness and citation accuracy with an LLM judge I calibrate against human labels. The split tells me where to spend: low recall means retrieval work; high recall with unfaithful answers means prompting or the model.

Recall@10 is 0.95 but users say answers are wrong. What do you check?Show answer

Retrieval finds the evidence, so I look downstream. First, does it reach the prompt? If I only pass the top 5, recall@5 or MRR may be much lower, and a reranker fixes that. Second, faithfulness: is the model ignoring or contradicting the context, maybe because of distracting chunks or ordering? Third, representativeness: if production queries differ from the eval set, offline recall means little, so I sample failing production traces and add them. Finally, citation accuracy: users may be reacting to wrong sources rather than wrong answers.