RAG Evaluation Guide
A RAG system can look great in demos, then return a confident wrong answer from an unrelated document. Without quantitative evaluation the bug stays invisible. An evaluation framework belongs in place before production, otherwise users find the failures first.
Three Core Metrics for RAG Evaluation
RAG quality is determined by three orthogonal dimensions:
Faithfulness
Question: Is the model’s answer actually grounded in the retrieved context? Or is it hallucinating?
Low faithfulness = hallucination. Even when the right document is retrieved, the model can still “not follow the document” and fabricate content.
Score reference:
- High faithfulness (> 0.85): every claim in the answer can be traced to a retrieved document
- Low faithfulness (< 0.6): answer contains information not present in any retrieved document
Answer Relevance
Question: Does the model’s answer actually address what the user asked?
A common failure mode: the right document is retrieved, but the model answers something else from the document rather than the user’s actual question.
Context Precision
Question: Of the retrieved documents, how many are actually useful?
| Situation | Description |
|---|---|
| High Precision + Low Recall | Retrieved docs are all relevant, but missed important ones |
| Low Precision + High Recall | Retrieved many docs, but most are irrelevant |
| High Precision + High Recall | Ideal state |
Tool Selection
RAGAS
The most mature RAG evaluation metrics library. Directly implements Faithfulness, Answer Relevancy, Context Precision and others. Native support for LangChain and LlamaIndex. Scores are LLM-as-Judge based — no manual annotation required.
TruLens
Evaluation + Tracing in one framework. End-to-end tracing for LangChain and LlamaIndex applications with real-time quality metrics. Best when you want evaluation embedded into application monitoring.
LLM-as-Judge (roll your own)
Use a separate LLM call to evaluate output quality. Cost-controllable and flexible, but requires careful evaluation prompt design.
In Practice: Evaluating Your RAG Pipeline with RAGAS
The examples below target the Ragas v0.2+ dataset contract (EvaluationDataset with user_input, response, retrieved_contexts, and reference):
from ragas import EvaluationDataset, evaluate
from ragas.metrics import (
Faithfulness,
LLMContextPrecisionWithoutReference,
LLMContextRecall,
ResponseRelevancy,
)
samples = [
{
"user_input": "What is the return policy?",
"response": "You can return items within 30 days of purchase.",
"retrieved_contexts": [
"Our return policy allows returns within 30 days. Items must be unused."
],
"reference": "Items can be returned within 30 days if unused.",
},
{
"user_input": "How do I reset my password?",
"response": "Click 'Forgot Password' on the login page and follow the email instructions.",
"retrieved_contexts": [
"To reset your password, click 'Forgot Password' and check your email."
],
"reference": "Use the 'Forgot Password' link and follow email instructions.",
},
{
"user_input": "What payment methods are accepted?",
"response": "We accept Visa, Mastercard, and PayPal.",
"retrieved_contexts": [
"Accepted payment methods: Visa, Mastercard, American Express, PayPal."
],
"reference": "Visa, Mastercard, American Express, and PayPal are accepted.",
},
]
dataset = EvaluationDataset.from_list(samples)
result = evaluate(
dataset=dataset,
metrics=[
Faithfulness(),
ResponseRelevancy(),
LLMContextPrecisionWithoutReference(),
LLMContextRecall(),
],
)
print(result)Reference score thresholds (calibrate against your labeled test set variance and critical slices rather than relying on averages alone):
| Metric | Acceptable | Good | Excellent |
|---|---|---|---|
| Faithfulness | > 0.75 | > 0.85 | > 0.92 |
| Response Relevancy | > 0.70 | > 0.80 | > 0.90 |
| Context Precision | > 0.65 | > 0.80 | > 0.90 |
Building an Evaluation Dataset
Evaluation quality depends on dataset quality. Three approaches:
Synthetic Dataset (fastest, good for getting started)
Generate QA pairs from your corpus using the Ragas v0.2+ TestsetGenerator:
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from ragas.embeddings import LangchainEmbeddingsWrapper
from ragas.llms import LangchainLLMWrapper
from ragas.testset import TestsetGenerator
generator_llm = LangchainLLMWrapper(ChatOpenAI(model="gpt-4o-mini"))
generator_embeddings = LangchainEmbeddingsWrapper(OpenAIEmbeddings())
generator = TestsetGenerator(
llm=generator_llm,
embedding_model=generator_embeddings,
)
testset = generator.generate_with_langchain_docs(
documents,
testset_size=50,
)Human-annotated Dataset (most accurate, for critical scenarios)
- Domain experts manually write questions and ground-truth answers
- Higher cost, but highest quality
- Recommend at least 20 “golden QA pairs” covering core scenarios
Production Data Sampling (closest to real usage, requires privacy handling)
Sample real user queries from production traffic, then label them:
sampled_queries = (
db.query(ProductionTrace)
.filter(ProductionTrace.date >= "2024-01-01")
.order_by(func.random())
.limit(100)
.all()
)
for q in sampled_queries:
print(f"Q: {q.user_query}")
print(f"A: {q.model_response}")
print("---")Minimum viable dataset: 50 QA pairs is enough to start. Include at least:
- 10 “should answer well” questions for core scenarios
- 10 out-of-corpus questions (test if the model knows when to say “I don’t know”)
- 10 questions requiring synthesis across multiple documents
Continuous Evaluation: Run RAG Scoring in CI
Every change to retrieval parameters (embedding model, chunk size, top-k) should trigger an evaluation run:
# evaluate_rag.py — runs in CI (Ragas v0.2+)
import json
import math
import sys
from pathlib import Path
from ragas import EvaluationDataset, evaluate
from ragas.metrics import Faithfulness, ResponseRelevancy
THRESHOLDS = {
"faithfulness": 0.80,
"answer_relevancy": 0.75,
}
def load_eval_dataset(path: str) -> EvaluationDataset:
samples = json.loads(Path(path).read_text(encoding="utf-8"))
return EvaluationDataset.from_list(samples)
def run_evaluation(dataset: EvaluationDataset):
if len(dataset) == 0:
print("Evaluation dataset is empty", file=sys.stderr)
sys.exit(1)
result = evaluate(
dataset=dataset,
metrics=[Faithfulness(), ResponseRelevancy()],
)
scores = result.to_pandas()
failed = []
for metric, threshold in THRESHOLDS.items():
if metric not in scores.columns:
print(f"❌ {metric}: missing from evaluation output")
failed.append(metric)
continue
valid = scores[metric].dropna()
mean_score = float(valid.mean()) if len(valid) > 0 else float("nan")
# Require finite scores and full sample coverage so NaN never silently passes CI
passed = (
len(valid) == len(scores)
and math.isfinite(mean_score)
and mean_score >= threshold
)
status = "✅" if passed else "❌"
print(f"{status} {metric}: {mean_score:.3f} (valid: {len(valid)}/{len(scores)}, threshold: {threshold})")
if not passed:
failed.append(metric)
if failed:
print(f"\nFailed metrics: {failed}")
sys.exit(1) # fail CI
if __name__ == "__main__":
dataset = load_eval_dataset("eval_dataset.json")
run_evaluation(dataset)# .github/workflows/rag-eval.yml
name: RAG Evaluation
on:
push:
paths:
- "rag/**"
- "embeddings/**"
jobs:
evaluate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install evaluation dependencies
run: pip install -r requirements-eval.txt
- name: Run RAG evaluation
run: python evaluate_rag.py
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}Common Failure Patterns
Understanding failure modes helps you pinpoint problems faster:
Retrieval Failure
Symptom: Low Context Precision, but the right answer exists in the corpus.
Causes:
- Chunk size too large or small, cutting across key information
- Embedding model doesn’t understand domain vocabulary (general vs. domain-specific model)
- Top-k too small — right document ranked outside the cutoff
Debug: For questions with known answers, inspect the top-5 retrieved docs and find where the correct document ranks.
Generation Failure
Symptom: High Context Precision, but low Faithfulness — right document was retrieved, but model didn’t use it.
Causes:
- Context too long — model “forgets” key sections (long context forgetting)
- System prompt too vague — doesn’t explicitly require grounding in context
- Model’s intrinsic hallucination tendency (worse with smaller models)
Fix: Add explicit instruction in System Prompt: “Only answer based on the provided context. If the answer is not in the context, say ‘I don’t know’.”
Coverage Failure
Symptom: User asks something that genuinely isn’t in the corpus, but the model fabricates an answer instead of saying “I don’t know”.
Protection: Add out-of-scope questions to your evaluation set. Test whether the model correctly refuses to answer.
References
- RAGAS docs — the most comprehensive RAG evaluation metrics documentation
- TruLens — evaluation + tracing in one framework
- LlamaIndex evaluation guide — LlamaIndex official evaluation best practices