TechBuzzPro

RAG Pipeline Evaluation: Metrics & Test Harness

· 6 min read · Apps & Browsers

In this article
  1. Why RAG Evaluation Is Different
  2. Retrieval Metrics: How Good Is the Search?
  3. Generation Metrics: How Good Is the Answer?
  4. Building a Test Harness
  5. LLM-as-a-Judge: Pros and Pitfalls
  6. Common Pitfalls and How to Avoid Them
  7. Tools and Libraries
  8. Frequently Asked Questions

To evaluate a RAG (Retrieval-Augmented Generation) pipeline, you must track retrieval quality metrics (recall, precision, MRR, NDCG) and generation quality metrics (faithfulness, answer relevance, and answer correctness), and you build a test harness by creating a golden dataset of question-answer pairs, running your pipeline in a controlled environment, and scoring outputs automatically with both rule-based and LLM-based judges. This article details which metrics matter, how to build the harness, and how to interpret results.

Why RAG Evaluation Is Different

RAG combines a retrieval component (finding relevant documents) and a generation component (producing an answer from those documents). A failure in either part degrades the final answer. Evaluating only the final answer hides whether the problem is retrieval (the right context was never found) or generation (the model hallucinated or ignored context). Therefore, you need separate metrics for each stage, plus end-to-end metrics for the user experience.

Another key difference: traditional LLM evaluation often measures fluency and coherence. RAG evaluation must also measure groundedness—whether the answer is supported by the retrieved context. A fluent, confident answer that contradicts the source documents is a serious failure, even if it reads well.

Retrieval metrics compare the set of documents the retriever returns (top-k) against a set of relevant documents for each query. You need a golden dataset where each question has a list of relevant document IDs.

  • Recall@k: The fraction of relevant documents that appear in the top-k results. High recall means the retriever is not missing important context.
  • Precision@k: The fraction of top-k results that are relevant. High precision means the retriever is not returning too much noise.
  • Mean Reciprocal Rank (MRR): Measures how high the first relevant document appears. Useful when only one relevant document is needed.
  • NDCG@k: Normalized Discounted Cumulative Gain. Accounts for multiple relevance levels (e.g., highly relevant vs. partially relevant) and rewards placing relevant documents higher.

In practice, recall@k is often the most important for RAG because if the context is missing, the generator cannot produce a correct answer. However, if you use a re-ranking step, you might also track precision after re-ranking.

Generation Metrics: How Good Is the Answer?

Generation metrics assess the quality of the final answer. The three most widely used are:

  • Faithfulness: The proportion of claims in the answer that are supported by the retrieved context. A hallucinated fact (not in the context) lowers faithfulness.
  • Answer Relevance: How well the answer addresses the user's question. An answer can be faithful but irrelevant (e.g., it repeats context verbatim but does not answer the question).
  • Answer Correctness: The factual accuracy of the answer compared to a ground-truth answer. This is the strictest metric and requires a reference answer.

Faithfulness and relevance are often computed using an LLM-as-a-judge, which breaks the answer into individual claims and checks each against the context. Answer correctness is computed by comparing the generated answer to a reference answer, often using semantic similarity or an LLM judge.

Building a Test Harness

A test harness is a repeatable system that runs your RAG pipeline on a set of test questions and computes the metrics above. Here is a step-by-step approach:

  1. Create a golden dataset: Collect 100–500 real user questions (or synthetic ones) that cover the range of queries your system will face. For each question, manually identify relevant document IDs and write a reference answer.
  2. Set up a versioned pipeline: Use a script that takes a snapshot of your code, embeddings, and vector store, runs the pipeline on all test questions, and saves the outputs (retrieved doc IDs, contexts, generated answers).
  3. Implement scoring functions: For retrieval, write code to compute recall@k, precision@k, MRR, and NDCG using the golden relevance judgments. For generation, call an LLM judge (e.g., GPT-4) with a prompt that asks for faithfulness and relevance scores, or use a library like RAGAS.
  4. Automate and schedule: Run the harness on every change to the pipeline—new embedding model, new chunking strategy, new prompt, or new LLM. Store results in a table (e.g., CSV or a database) to track changes over time.
  5. Set thresholds: Define minimum acceptable scores (e.g., recall@5 ≥ 0.8, faithfulness ≥ 0.9). If a change drops a metric below the threshold, reject it.

LLM-as-a-Judge: Pros and Pitfalls

Using an LLM to evaluate answers is common because it scales and can handle open-ended responses. However, it has biases: it may favor longer answers, answers that match its own style, or answers that contain certain keywords. To mitigate this:

  • Use a strong, instruction-following model (e.g., GPT-4) and a detailed rubric with examples.
  • Run each evaluation multiple times (e.g., 3 times) and average the scores to reduce variance.
  • Validate the judge on a small human-annotated sample to ensure its scores correlate with human judgments.
  • For faithfulness, use a claim-based approach: ask the LLM to list each claim in the answer, then check each claim against the context. This is more reliable than a single numeric score.

Common Pitfalls and How to Avoid Them

  • Ignoring context length: If your retriever returns too many documents, the generator may exceed its context window or get distracted. Track the number of tokens in the context and set a maximum.
  • Using only end-to-end metrics: A high answer correctness can hide a retrieval failure if the generator is strong enough to answer from memory. Always track retrieval metrics separately.
  • Not testing on real queries: Synthetic questions may not reflect the actual distribution. Use a mix of real user logs and synthetic variations.
  • Overfitting the test set: If you tune your pipeline too much on the golden dataset, it may not generalize. Periodically refresh the dataset with new queries.

Tools and Libraries

Several open-source libraries can help you build a test harness without coding everything from scratch:

  • RAGAS: Provides metrics for faithfulness, answer relevance, and context relevance. It uses an LLM to score outputs.
  • LangSmith: Offers evaluation suites for RAG, including dataset management and trace visualization.
  • LlamaIndex: Has built-in evaluators for retrieval and generation, plus integrations with RAGAS.
  • DeepEval: A testing framework for LLM applications, with RAG-specific metrics.

These tools often include pre-built prompts for LLM judges, but you should still validate their outputs against your own data.

Frequently Asked Questions

How many test questions do I need?

Start with at least 100 questions for a meaningful evaluation. For high-stakes applications, use 500 or more. The goal is to cover the variety of query types and difficulty levels your system will face.

What is the difference between faithfulness and answer relevance?

Faithfulness measures whether the answer is supported by the retrieved context. Answer relevance measures whether the answer actually addresses the user's question. An answer can be faithful (all claims are in the context) but irrelevant (it talks about something else). Both are needed for a good user experience.

Should I use an LLM judge or human evaluation?

Use LLM judges for automated, frequent evaluations. Use human evaluation on a small sample (e.g., 50–100 examples) to validate the LLM judge and to catch subtle issues that automated metrics miss. For final release, a human review of critical queries is recommended.

How do I handle multiple correct answers?

For answer correctness, use a reference answer and a semantic similarity threshold (e.g., BERTScore) or an LLM judge that compares the two answers for semantic equivalence. Avoid exact string matching.

What if my retriever returns zero relevant documents?

This is a retrieval failure. You should log these cases and analyze why the retriever missed them. It may be due to poor chunking, embedding model limitations, or query phrasing. Consider adding a fallback mechanism, such as a keyword search.

How often should I run the test harness?

Run it on every change to the pipeline, at least nightly for active development. Also run it after any update to the underlying LLM or embedding model, as these can change behavior even without code changes.