Skip to main content
Deliberate AcademyProfessional AI Education
~17 min left
Lesson 3 of 10
17 min read10 XP

Evaluating RAG Systems and Factual Accuracy

Deliberate Academy Editorial Team

Reviewed for accuracy and professional relevance

You're 3 lessons in — don't lose your progress.

Sign up free
What you'll learn
  • Explain the four RAGAS metrics — faithfulness, answer relevance, context precision, and context recall — and describe what failure in each metric indicates about the RAG pipeline
  • Design a component-level RAG evaluation that diagnoses whether quality failures originate in the retriever or the generator
  • Implement hallucination detection using NLI-based and LLM-based claim verification approaches
  • Build an end-to-end RAG evaluation pipeline that measures retrieval quality and generation quality separately and in combination

Retrieval-augmented generation systems fail in more ways than standard generation systems. A RAG system can fail because the retriever does not return the right documents. It can fail because the retrieved documents are correct but the generator ignores them. It can fail because the generator uses the retrieved documents but adds information from its parametric knowledge that contradicts them. It can fail in all three ways simultaneously. Diagnosing which failure mode is present requires component-level evaluation, not just end-to-end output quality measurement.

This lesson covers the evaluation frameworks specific to RAG systems and factuality verification, including the RAGAS framework, hallucination detection approaches, and the diagnostic patterns that identify where in a RAG pipeline errors originate.

The RAGAS Framework

RAGAS (Retrieval Augmented Generation Assessment) is an evaluation framework that assesses RAG systems across four metrics, each measuring a distinct quality dimension of the pipeline. Understanding what each metric measures, and what a low score on each metric means diagnostically, is the starting point for RAG system evaluation.

Faithfulness. Faithfulness measures whether every claim in the generated answer is supported by the retrieved context. A faithful answer draws only on information present in the retrieved documents — it does not introduce facts from the model's parametric knowledge. Faithfulness is scored at the claim level: the answer is decomposed into atomic claims, and each claim is checked against the retrieved context to determine whether it is supported.

A low faithfulness score indicates that the generator is hallucinating — adding information beyond what the retrieved context provides. This is a generator failure mode, not a retriever failure mode. The retrieval step may have returned perfectly relevant documents; the generator simply chose to go beyond them.

Answer relevance. Answer relevance measures whether the generated answer actually addresses the user's question. A high-faithfulness answer can have low answer relevance if it accurately reports information from the retrieved context but does not address what was asked. Answer relevance is typically measured by generating hypothetical questions from the answer and measuring whether those generated questions are semantically similar to the original question.

A low answer relevance score indicates either a generator failure (the model is responding to a different question than the one asked) or a retriever failure (the retrieved context is about adjacent but not directly relevant topics, causing the generator to produce an answer that does not match the query).

Context precision. Context precision measures what fraction of the retrieved context is actually relevant to the question. If your retriever returns 10 chunks and 3 of them are relevant while 7 are noise, context precision is 0.3. High context precision means the retriever is returning focused, relevant results. Low context precision means the retriever is returning too many irrelevant chunks, which dilutes the context and can confuse the generator.

Low context precision is a retriever failure. The fix is retrieval quality improvements: better chunk embedding, reranking, or tighter retrieval parameters.

Context recall. Context recall measures what fraction of the information needed to answer the question is present in the retrieved context. A retriever can have high precision (everything it returns is relevant) but low recall (it missed many relevant documents). Context recall requires ground truth labels specifying which information is required to answer each question — it cannot be computed without annotation.

Low context recall is a retriever failure of a different type than low precision. Low precision means the retriever is returning irrelevant documents. Low recall means the retriever is missing relevant documents entirely.

Building a RAGAS Evaluation Harness

A RAGAS evaluation harness applies these four metrics across your evaluation dataset. The practical implementation has four components: query set, reference answers, RAG pipeline outputs, and scoring.

The query set. Your evaluation queries should cover the full range of question types your production system will encounter: factual lookups (what is X?), procedural questions (how do I do X?), comparative questions (what is the difference between X and Y?), and questions that require synthesising information from multiple documents. Include queries where the correct answer is not in the knowledge base — these test whether the system correctly handles out-of-scope queries rather than hallucinating an answer.

Reference answers. RAGAS metrics that require ground truth (context recall and answer relevance) need reference answers: the correct answer to each query, and the set of documents or document chunks that are required to answer it. These reference answers must be human-generated; using the RAG system itself to generate reference answers creates circular evaluation.

Running the harness. For each query in the evaluation set, run the full RAG pipeline and capture both the retrieved context and the generated answer. Apply RAGAS scoring to each query-context-answer triple. Aggregate scores across the full evaluation set, and also compute scores by query category — performance differences across categories often reveal systematic retrieval or generation weaknesses on specific question types.

Threshold setting. Define minimum acceptable scores for each RAGAS metric, calibrated to your use case. A customer-facing FAQ system that will be used by patients querying medical information might require faithfulness above 0.90 (very little tolerance for hallucination). An internal research assistant where users understand the outputs are model-generated might accept faithfulness of 0.80. Set thresholds based on the consequence of failures in your specific context, not on what the system currently achieves.

Hallucination Detection

Faithfulness measurement at the aggregate level tells you how often the system hallucinates. Hallucination detection at the instance level tells you which specific claims in a specific output are not grounded in the context. Both are needed: aggregate metrics for quality monitoring, instance-level detection for flagging outputs before they reach users.

NLI-based claim verification. Natural Language Inference (NLI) models are trained to classify whether a hypothesis is entailed by, contradicted by, or neutral with respect to a premise. In hallucination detection, the retrieved context is the premise and each claim extracted from the generated answer is the hypothesis. Claims classified as entailed by the context are grounded; claims classified as contradicted or neutral are potential hallucinations.

NLI-based verification is fast and cheap to run but has limitations: NLI models trained on general text may be miscalibrated on domain-specific content, and the claim extraction step (splitting the answer into atomic claims) is itself error-prone. Use cross-encoder NLI models trained on evidence-based tasks for better calibration than general NLI models. Models such as the HHEM (Hughes Hallucination Evaluation Model) family are specifically designed for hallucination detection.

LLM-based claim verification. An alternative approach is to use an LLM as the verifier: extract each claim from the generated answer, then ask the LLM whether each claim is supported by the retrieved context. This approach is more flexible than NLI models and handles complex claims better, but it is slower and more expensive. The verification prompt must be carefully designed to prevent the LLM from using its parametric knowledge to evaluate claims — the LLM should be instructed to answer solely based on the provided context, and examples in the prompt should reinforce this constraint.

Combining approaches. For high-stakes applications, use a pipeline that first applies NLI-based claim verification (fast, cheap, catches clear hallucinations) and then applies LLM-based verification to claims the NLI model rated as neutral rather than entailed — the ambiguous middle ground where the NLI model lacks confidence. This hybrid approach balances cost and coverage.

Warning

Hallucination detection is a filter, not a cure. Detecting that an output contains ungrounded claims is valuable — you can flag or suppress that output before it reaches the user. But high hallucination rates in your output stream indicate a systemic problem that must be fixed at the source: the retrieval is returning insufficient context, the system prompt is not constraining the generator to the context, or the generator model is particularly prone to confabulation on your specific domain. Filtering without fixing the root cause means you are discarding large fractions of outputs and degrading system availability.

Diagnosing Where Errors Originate: Retriever vs. Generator

The most important diagnostic question in RAG evaluation is whether quality failures are retriever failures or generator failures. The answer determines where to invest improvement effort.

The component isolation approach. To diagnose the error source, evaluate the retriever and generator separately, not just end-to-end.

For retriever evaluation: given the same query, how often does the retriever return at least one document that contains the information needed to answer the question? Compute retrieval recall (does the correct information appear anywhere in the retrieved context?) and precision (how much of the retrieved context is relevant?). If retrieval recall is high but end-to-end answer quality is low, the failure is in the generator. If retrieval recall is low, the failure is in the retriever regardless of generator performance.

For generator evaluation: given the same query and the retrieved context that was actually returned (not ideal context), how faithfully and relevantly does the generator use it? Compute faithfulness and answer relevance on the actual pipeline outputs. If faithfulness is low, the generator is going beyond the context. If answer relevance is low, the generator is not focusing on the query.

The oracle retrieval test. A powerful diagnostic is to run the generator with oracle context — the ideal, human-curated context that perfectly answers the question — rather than the context returned by the retriever. If end-to-end quality with oracle context is high, the problem is retrieval. If end-to-end quality remains low even with oracle context, the problem is the generator or the prompt. This test cleanly separates the two failure modes.

The empty context test. A useful complement is to run the generator with no retrieved context and measure how often it produces answers that would be indistinguishable from its full-pipeline outputs. If the generator produces similar-quality answers without retrieval, it may be ignoring the retrieved context entirely and relying on parametric knowledge — which creates a faithfulness risk even when retrieval is working correctly.

End-to-End RAG Evaluation Pipeline

A production RAG evaluation pipeline integrates component-level and end-to-end evaluation into a repeatable harness that runs on a schedule and on every system change.

The pipeline has five steps. First, run the full RAG system on the evaluation query set and capture all intermediate outputs: query, retrieved chunks (with retrieval scores), generated answer, and any reranking or filtering steps. Second, run component-level retrieval evaluation: compute precision at k and recall at k for each query using human-labelled relevance judgments. Third, run end-to-end generation evaluation: compute faithfulness, answer relevance, and a custom accuracy metric (if you have reference answers) on each query-context-answer triple. Fourth, aggregate all metrics and compare to baseline and quality thresholds. Fifth, log detailed results per query to a persistent store so that per-query quality can be tracked over time and regressions can be diagnosed at the example level.

The end-to-end RAG evaluation pipeline, from running the pipeline to logging per-query results

Tools that provide eval harness infrastructure specifically designed for RAG systems include RAGAS (open source Python library), Braintrust's RAG evaluation suite, and LangSmith's dataset and eval management platform. All three support automatic computation of the core RAGAS metrics and integration with your existing RAG pipeline.

Diagnosing a faithfulness regression in a legal research RAG system

Backend Engineer

Context

A legal technology company operated an internal RAG system that allowed lawyers to query a corpus of case law and statutory material. The system had been running for five months with acceptable user satisfaction. After updating the retriever to return longer document chunks (increasing chunk size from 512 to 1,024 tokens), users reported that some answers appeared to be citing information not present in the sources the system referenced.

Action

The team ran a RAGAS evaluation on a 150-question evaluation set before and after the chunk size change. Faithfulness dropped from 0.87 to 0.74 after the change. Context precision also dropped from 0.72 to 0.61, indicating that larger chunks were bringing in more irrelevant content alongside the relevant passages. Running the oracle retrieval test confirmed the generator was performing correctly when given clean, relevant context — the problem was that longer chunks were including adjacent text that was topically adjacent but not directly relevant to the query, and the generator was incorporating that adjacent text into its answers, producing answers that blended information from the query-relevant passage with information from surrounding text in the same chunk.

Outcome

The team rolled back the chunk size increase and instead implemented overlapping smaller chunks (512 tokens with 128-token overlap between adjacent chunks) combined with a reranking step that re-scored retrieved chunks against the query before passing them to the generator. Post-change RAGAS evaluation showed faithfulness of 0.91 and context precision of 0.78, both improvements over the pre-change baseline. The component-level diagnosis (retriever precision failure, not generator failure) enabled them to target the fix correctly rather than revising the generator prompt.

Knowledge check

A RAG system for answering customer questions about a software product scores 0.91 on context recall but 0.61 on context precision in a RAGAS evaluation. What does this combination of scores indicate and what is the appropriate fix?

Select one answer.

Quick check

What does the empty context test reveal about a RAG pipeline that the oracle retrieval test cannot?

Select one answer.

Exercise

Your Task

You are building a RAG evaluation pipeline for a healthcare provider's internal system that allows clinical staff to query internal clinical protocols and treatment guidelines. The knowledge base contains 800 protocol documents. Design the end-to-end evaluation pipeline: (1) describe the evaluation query set — what question types to include, how many queries, and how to handle out-of-scope queries; (2) specify which RAGAS metrics you will compute and set threshold values for each, explaining why each threshold is appropriate for a clinical context; (3) describe how you would implement hallucination detection for this system, including which approach (NLI-based, LLM-based, or hybrid) and why; and (4) describe the diagnostic procedure you would use if end-to-end answer quality is low — how would you determine whether the failure is in the retriever or the generator?

Your reflection

Did you complete this exercise? What did you find? (Saved locally in your browser)

Key takeaways
  • RAGAS evaluates RAG systems across four dimensions: faithfulness (generator does not hallucinate beyond the context), answer relevance (the answer addresses the question), context precision (retrieved content is focused and relevant), and context recall (all needed information is retrieved). Each metric points to a different failure location in the pipeline.
  • Faithfulness and answer relevance failures are generator failures; context precision and context recall failures are retriever failures. Component-level diagnosis determines where to invest improvement effort.
  • Hallucination detection at instance level uses NLI-based claim verification (fast, cheap) or LLM-based claim verification (more flexible, slower). Hybrid approaches use NLI for clear cases and LLM verification for ambiguous claims.
  • The oracle retrieval test — running the generator with ideal, human-curated context — cleanly separates retriever failures from generator failures and is the most efficient diagnostic for ambiguous quality failures.
  • Build your RAG evaluation pipeline to capture all intermediate outputs — retrieved chunks and retrieval scores, not just final answers — so that component-level evaluation can be run on each pipeline stage without re-running the full pipeline.