Introduction
In our last lesson, you mastered building a sophisticated hybrid search system by combining sparse (BM25) and dense retrievers. You now have multiple architectural choices for the retrieval stage of your RAG pipeline: pure dense, pure sparse, or hybrid. This naturally leads to a critical engineering question: which one is best, and how can we prove it?
This lesson directly addresses that question and is designed to help you achieve the learning outcome: Evaluate a RAG system using retrieval metrics (e.g., hit rate, MRR) and end-to-end generation quality.
We will break down RAG evaluation into two fundamental components: assessing the quality of the retriever and the quality of the generator. You will learn the standard metrics for each, understand the theory behind them, and see how to apply them in a systematic workflow to build production-grade, reliable RAG systems.
The Two Halves of RAG Evaluation
A RAG system's performance is a product of its two main components. A failure in either one can render the entire system useless. Therefore, we evaluate them separately to isolate and diagnose problems effectively.

- Retrieval Evaluation: Answers the question, "Did we retrieve the right context?" This is about the performance of your vector database and search algorithm (dense, sparse, or hybrid).
- Generation Evaluation: Answers the question, "Given the context, did the LLM produce a good answer?" This focuses on the LLM's ability to synthesize information and respond correctly without hallucinating.
To perform any evaluation, you first need a ground truth dataset. This is a curated set of questions (queries) for which you have manually identified the correct answers and the specific document chunks from your knowledge base that contain the information needed to form those answers. Creating this "golden dataset" is a crucial, though often time-consuming, first step in any serious evaluation workflow.
Part 1: Evaluating the Retriever
The goal here is to measure how effectively your retrieval strategy finds relevant documents. These metrics are borrowed from the classic field of Information Retrieval.
We can group these metrics into two categories:
- Order-Unaware: Do the relevant documents appear in the retrieved set at all?
- Order-Aware: Are the most relevant documents ranked at the top of the list? This is critical because an LLM's context window is finite, and the documents at the top have the most influence on the final answer.
The following article provides clear definitions and Python implementations for the most common order-unaware metrics.
How to Evaluate Retrieval Quality in RAG Pipelines: Precision@k ...
The article 'How to Evaluate Retrieval Quality in RAG Pipelines' provides an excellent starting point for retrieval metrics. It clearly defines the foundational concepts.
Read the section 'Some order-unaware, binary measures'. Focus on understanding the definitions and formulas for: HitRate@K: The simplest metric - did we get at least one correct document? Recall@K: How many of the total relevant documents did we find? (Measures completeness) Precision@K: Of the documents we retrieved, how many were relevant? (Measures noise/signal ratio)
While these metrics are useful, they don't tell the whole story. For RAG, the ranking of retrieved documents is paramount. The next video offers a clear explanation of two essential order-aware metrics.
Evaluation Measures for Search and Recommender Systems
The video 'Evaluation Measures for Search and Recommender Systems' by James Briggs provides fantastic, intuitive explanations for order-aware metrics.
Watch the following two segments: Mean Reciprocal Rank (MRR) (10:02 - 14:28): Focus on how MRR evaluates the rank of the first relevant item. This is crucial for use cases where finding a single correct answer quickly is the priority. Normalized Discounted Cumulative Gain (NDCG) (19:29 - 29:40): Pay attention to how NDCG incorporates graded relevance (some documents are more relevant than others) and penalizes for placing less relevant documents higher in the list. This is one of the most comprehensive and widely used ranking metrics.
Part 2: Evaluating the Generator (End-to-End Quality)
Once you've confirmed your retriever is performing well, you need to evaluate the quality of the final, LLM-generated answer. These metrics often rely on using another powerful LLM as a "judge" to score the output based on a set of criteria.
The key metrics for generation quality are:
- Faithfulness (or Groundedness): Does the answer strictly adhere to the information provided in the retrieved context? A low faithfulness score indicates the model is hallucinating or making up information.
- Answer Relevancy: Is the answer a helpful and direct response to the user's original query? An answer can be faithful to the context but still irrelevant to the question if the retrieved context was poor.
The following article from Confident AI provides an excellent overview of these metrics and the frameworks used to calculate them.
RAG Evaluation Metrics: Assessing Answer Relevancy ...
The blog post 'RAG Evaluation Metrics: Assessing Answer Relevancy...' clearly separates generator and retriever metrics and explains their purpose.
Read the section 'Generation Evaluation Metrics'. Focus on the definitions and code examples for Answer Relevancy and Faithfulness. This will show you how these concepts are translated into measurable scores using frameworks like DeepEval.
Test your understanding!
You are evaluating two different RAG pipelines on your "golden dataset" and get the following results:
- Pipeline A: High Recall@10, Low MRR, High Faithfulness.
- Pipeline B: Low Recall@10, High Precision@10, Low Faithfulness.
What is the likely problem with each pipeline?
Show answer
-
Pipeline A's Problem: The retriever is finding most of the relevant documents (High Recall@10), but the most important ones are not ranked at the top (Low MRR). The generator is correctly sticking to the provided context (High Faithfulness), but because the best context is buried, the final answer might be suboptimal. Diagnosis: The retrieval strategy is finding relevant documents but the ranking function is poor. A reranker might be needed.
-
Pipeline B's Problem: The retriever is finding very few irrelevant documents (High Precision@10), but it's also missing many of the relevant ones (Low Recall@10). The generator is hallucinating and not sticking to the limited context it was given (Low Faithfulness). Diagnosis: The retriever is too restrictive or narrowly focused. The more critical issue, however, is that the generator model or prompt template is prone to hallucination.
Part 3: The Iterative Evaluation Workflow
Theory is useful, but putting it into a repeatable workflow is what enables you to systematically improve your system. Modern evaluation frameworks like TruLens, Ragas, and DeepEval are designed to facilitate this.
The general workflow is:
- Establish a Baseline: Run your initial RAG pipeline against your ground truth dataset and record all the key metrics (MRR, Recall@k, Faithfulness, Answer Relevancy, etc.).
- Hypothesize & Tweak: Form a hypothesis for improvement. For example, "Using a hybrid retriever instead of a dense-only one will improve my MRR." or "Changing the LLM from
gpt-3.5-turbotoclaude-3-sonnetwill increase Faithfulness." - Re-evaluate: Run the evaluation again with the tweaked component.
- Compare: Use a dashboard or leaderboard to compare the metrics of the new version against your baseline. Did the change have the intended effect? Did it have any unintended negative side effects?
- Iterate: Keep the changes that work and continue the cycle.
This video provides a complete, hands-on demonstration of this iterative loop using the TruLens framework. It perfectly captures the process of moving from a basic RAG to an optimized one through systematic evaluation.
Evaluate and Optimize RAG with TruLens (full tutorial)
The tutorial 'Evaluate and Optimize RAG with TruLens' is a fantastic practical guide that brings all these concepts together. It demonstrates the full cycle of building, evaluating, and comparing different RAG configurations.
Watch these two key sections: Defining Metrics (07:08 - 18:08): See how concepts like Answer Relevance, Context Relevance, and Groundedness (Faithfulness) are defined as Feedback functions in code. This connects the theory to a practical implementation. Running Evaluation & The Dashboard (18:08 - 24:51): Observe how the evaluation is run and, most importantly, how the results are displayed on a leaderboard. This visualization is key for comparing different application versions and making data-driven decisions.
This workflow is the cornerstone of building production-grade LLM applications. It moves you from "it feels like it works" to "I can prove it performs better."

Conclusion
In this lesson, you have learned how to dissect and rigorously evaluate the performance of a RAG system. You are no longer just building pipelines; you are now equipped to measure, diagnose, and systematically improve them.
Key Takeaways:
- Deconstruct to Diagnose: RAG evaluation should be split into two parts: retrieval quality and generation quality.
- Key Retrieval Metrics: Use Recall@k to measure completeness, Precision@k to measure noise, and MRR or NDCG to measure the quality of ranking.
- Key Generation Metrics: Use Faithfulness to measure hallucinations and Answer Relevancy to ensure the final output is useful and on-topic.
- Evaluation is an Iterative Loop: The process of establishing a baseline, tweaking a component, and re-evaluating is fundamental to engineering reliable AI systems. Frameworks like TruLens, Ragas, and DeepEval are built to support this cycle.
What's Next?
You have now reached the end of the module on Retrieval-Augmented Generation. You can build, optimize, and evaluate these complex systems.
So far, our RAG systems have been designed for a single purpose: answering questions based on a knowledge base. In the next module, we will expand our horizons to Agentic AI Systems. We will start by exploring the ReAct (Reason, Act) framework, which enables LLMs to not just answer questions, but to reason, formulate plans, and interact with external tools to accomplish complex, multi-step tasks. This is the next frontier in building powerful and autonomous AI applications.