Skip to main content

Evaluate RAG systems for accuracy and grounding

Measure retrieval quality, context relevance, and answer faithfulness in RAG pipelines to catch hallucinations and retrieval failures before users do.
Retrieval-Augmented Generation systems fail in ways that are hard to detect without structured evaluation. A model might retrieve the wrong documents, retrieve the right documents but ignore them, or generate plausible-sounding answers that contradict the retrieved context. Because each component can fail independently, you need to evaluate retrieval, context relevance, faithfulness, and answer quality both separately and end-to-end. This guide shows you how to build that evaluation framework with EvalGate.

Why RAG evaluation is hard

RAG systems have four distinct failure points, and passing on one doesn’t mean passing on the others: Evaluating only the final output hides which component broke. Evaluate each layer separately to diagnose and fix issues efficiently.

Retrieval quality metrics

Before evaluating end-to-end, isolate the retrieval step. Given a query and a set of known-relevant documents (your gold standard), measure:
Missing relevance labels are unknown, not irrelevant. Do not treat unlabeled documents as negatives when computing precision, recall, or MRR. For EvalGate’s reference-labelled lexical workflow and metric boundaries, see Measure retrieval quality. Sample helper snippets in this guide are conceptual; use documented SDK methods and that workflow for supported retrieval measurement.
Example calculation:
Test retrieval in isolation before running the full pipeline:
TypeScript

Context relevance scoring

Even when the right documents are retrieved, the retrieved chunks may not be useful for the specific query. Use an LLM judge to score relevance on a 1–5 scale:
Track context precision (the percentage of retrieved chunks that are actually relevant) alongside the raw relevance score to catch cases where your retriever is padding results with noise.

Answer faithfulness and hallucination detection

Faithfulness measures whether every claim in the generated answer is supported by the retrieved context. EvalGate’s expect(answer).toNotHallucinate(facts) matcher is only a lightweight check that required ground-truth facts appear in the answer; for claim-level faithfulness, verify each claim against the context:
TypeScript
Common hallucination pattern to test for:
The LLM added information that wasn’t in the context. Catch this by asserting every factual claim has a supporting source.

End-to-end RAG evaluation

Combine retrieval checks, faithfulness, and answer correctness into a single end-to-end test:
1

Create query-answer pairs

Collect 100–200 representative queries with gold-standard answers and the document IDs that should be retrieved:
TypeScript
2

Test retrieval separately

Run the retrieval step in isolation first. Fix retrieval failures before you debug generation — generation quality is meaningless if the context is wrong.
TypeScript
3

Evaluate end-to-end

Run the full pipeline and assert on retrieval quality, answer correctness, and faithfulness:
TypeScript

Common failure modes

Problem: The query uses different words than the documents.
  • Query: “How do I reset my password?”
  • Documents use: “password recovery” not “reset”
Solution: Add query expansion, synonym handling, or hybrid search (keyword + semantic).
You retrieved too many documents and exceeded the LLM’s context limit, causing truncation of relevant content.Solution: Rerank retrieved chunks and truncate to the most relevant subset before passing to the model.
Relevant information was retrieved but not included in the final answer.Solution: Improve the generation prompt to explicitly instruct the model to address all relevant points from the provided context.

Advanced retrieval techniques

Combine semantic and keyword search, then merge rankings:
TypeScript

Query rewriting

Rewrite ambiguous user queries into clearer search queries before hitting the retriever:

Multi-hop retrieval

For complex queries that require information from multiple sources:
  1. Retrieve documents answering the first part of the query
  2. Use those results to refine the query for a second retrieval pass
  3. Combine information from both retrievals before generating

Production metrics to track continuously

Monitor these signals in the EvalGate dashboard after deployment:
  • Answer rate — percentage of queries answered vs. “I don’t know” responses
  • User feedback — thumbs up/down signals from users
  • Retrieval latency — time to fetch and rank documents
  • Generation latency — time to produce the final answer
  • Context usage — whether retrieved docs are actually referenced in answers
Convert production traces into regression test cases. When a user reports a bad answer, capture that query and the expected answer as a test case to prevent the same failure from recurring.