AnyLearn
All lessons
AIadvanced

Evaluating RAG Pipelines with RAGAS

A rigorous guide to measuring RAG quality using RAGAS metrics — faithfulness, answer relevancy, context precision, and context recall — plus how to build a golden dataset and recognize where automated metrics fall short.

Not signed in — your progress and quiz score won't be saved.
Lesson progress1 / 12

Why RAG evaluation is harder than it looks

A RAG pipeline has two moving parts: a retriever and a generator. When the final answer is wrong, you need to know which part failed. Did the retriever pull the wrong chunks? Did the LLM hallucinate despite good context? Or did the LLM ignore perfectly good context?

Standard NLP metrics like BLEU or ROUGE don't help here — they measure lexical overlap against a reference answer, which tells you almost nothing about factual faithfulness or retrieval quality. Human evaluation is reliable but doesn't scale. RAGAS (Retrieval-Augmented Generation Assessment) fills this gap with four complementary metrics that decompose RAG quality into distinct, measurable signals — all computable without a human in the loop for every sample.

Full lesson text

All 12 steps on one page — for reading, reference, and search.

Show

1. Why RAG evaluation is harder than it looks

A RAG pipeline has two moving parts: a retriever and a generator. When the final answer is wrong, you need to know which part failed. Did the retriever pull the wrong chunks? Did the LLM hallucinate despite good context? Or did the LLM ignore perfectly good context?

Standard NLP metrics like BLEU or ROUGE don't help here — they measure lexical overlap against a reference answer, which tells you almost nothing about factual faithfulness or retrieval quality. Human evaluation is reliable but doesn't scale. RAGAS (Retrieval-Augmented Generation Assessment) fills this gap with four complementary metrics that decompose RAG quality into distinct, measurable signals — all computable without a human in the loop for every sample.

2. The four RAGAS metrics and what they measure

Each metric probes a different failure mode. Faithfulness and Answer Relevancy evaluate the generator. Context Precision and Context Recall evaluate the retriever. You need all four to get a complete picture.

flowchart TD
  A["RAG Output"] --> B["Faithfulness\n(answer ← context)"]
  A --> C["Answer Relevancy\n(answer → question)"]
  A --> D["Context Precision\n(retrieved ← relevant)"]
  A --> E["Context Recall\n(relevant ← retrieved)"]

3. Faithfulness: did the answer stick to the context?

Faithfulness measures whether every claim in the generated answer is actually supported by the retrieved context. RAGAS implements this with an LLM-as-judge approach: it extracts atomic statements from the answer, then checks each statement against the context.

Faithfulness=statements supported by contexttotal statements in answer\text{Faithfulness} = \frac{\text{statements supported by context}}{\text{total statements in answer}}

A score of 1.0 means nothing in the answer contradicts or goes beyond the context. A score of 0.6 means 40% of the answer's claims were hallucinated — either invented or imported from the LLM's parametric memory.

Gotcha: Faithfulness does not check whether the answer is factually true in the world — only whether it's grounded in the context. A context full of wrong facts will still produce a high faithfulness score.

4. Answer Relevancy: did the answer address the question?

A faithful answer can still be useless if it answers the wrong question. Answer Relevancy catches this: RAGAS generates several reverse questions from the answer ("what question would this answer?"), then measures the average cosine similarity between those synthetic questions and the original question.

Answer Relevancy=1ni=1ncos(q,q^i)\text{Answer Relevancy} = \frac{1}{n} \sum_{i=1}^{n} \cos(q, \hat{q}_i)

High similarity means the answer maps cleanly back to the question. Low similarity means the answer drifted — perhaps answering a tangentially related question or adding irrelevant background.

Common failure modes:

  • Verbosity drift: the answer pads out with correct but off-topic information.
  • Paraphrase mismatch: embedding model struggles with domain-specific phrasing, deflating scores even for good answers.

5. Context Precision: how much retrieved noise is there?

Context Precision measures signal-to-noise in your retrieved chunks. Given the question and a ground-truth answer, it asks: of all the chunks you retrieved, what fraction were actually relevant?

Context Precision@K=k=1K(Precision@k×hitk)total relevant items retrieved\text{Context Precision@K} = \frac{\sum_{k=1}^{K} (\text{Precision@}k \times \text{hit}_k)}{\text{total relevant items retrieved}}

This is a ranking-aware metric. A relevant chunk at position 1 scores higher than the same chunk at position 5, because most LLMs pay more attention to early context. Low Context Precision means your retriever is fetching too many irrelevant chunks, which dilutes the useful signal and wastes the LLM's context window.

This is the metric to watch when you suspect your vector search is returning topically similar but factually unhelpful documents.

6. Context Recall: did you miss anything important?

Context Recall checks coverage: does the retrieved context contain everything needed to answer the question? RAGAS uses the ground-truth answer as a proxy for "what the context should contain", then checks what fraction of statements in the reference answer can be attributed to the retrieved chunks.

Context Recall=reference statements supported by contexttotal reference statements\text{Context Recall} = \frac{\text{reference statements supported by context}}{\text{total reference statements}}

Low recall means your retriever is missing key chunks. This could be a chunking problem (a relevant fact spans a chunk boundary), an embedding problem (the query and document phrase the same concept differently), or a simple top-K problem (the relevant chunk is ranked 8th but you only fetch 5).

Note: Context Recall requires a ground-truth answer to compute. It's the one metric that can't be run zero-shot on live traffic without a labeled dataset.

7. Installing RAGAS and running your first evaluation

RAGAS is a Python library that wraps OpenAI (or any LiteLLM-compatible model) for its LLM-as-judge calls.

pip install ragas datasets
from ragas import evaluate
from ragas.metrics import faithfulness, answer_relevancy, context_precision, context_recall
from datasets import Dataset

data = {
    "question": ["What is the boiling point of ethanol?"],
    "answer": ["Ethanol boils at 78.37 °C at standard pressure."],
    "contexts": [["Ethanol (C2H5OH) has a boiling point of 78.37 °C (173.1 °F) at 1 atm."]],
    "ground_truth": ["The boiling point of ethanol is 78.37 °C."]
}

result = evaluate(
    Dataset.from_dict(data),
    metrics=[faithfulness, answer_relevancy, context_precision, context_recall],
)
print(result)  # {'faithfulness': 1.0, 'answer_relevancy': 0.97, ...}

The contexts field is a list of lists — one list of chunk strings per question. ground_truth is only required for context_recall.

8. Swapping the judge model

RAGAS uses GPT-4o by default, which is accurate but expensive at scale. You can swap it for any model via LangChain wrappers or the built-in LLM config.

from ragas.llms import LangchainLLMWrapper
from langchain_anthropic import ChatAnthropic
from ragas.embeddings import LangchainEmbeddingsWrapper
from langchain_openai import OpenAIEmbeddings

judge_llm = LangchainLLMWrapper(ChatAnthropic(model="claude-sonnet-4-6"))
judge_embeddings = LangchainEmbeddingsWrapper(OpenAIEmbeddings())

result = evaluate(
    dataset,
    metrics=[faithfulness, answer_relevancy],
    llm=judge_llm,
    embeddings=judge_embeddings,
)
Judge modelCostFaithfulness accuracy vs human
GPT-4o~$0.01/sample~0.85 Pearson r
Claude Sonnet~$0.008/sample~0.83 Pearson r
Llama-3-70B (local)~$0.002/sample~0.74 Pearson r

For large-scale CI runs, a smaller local model is practical. For final benchmark reports, use the strongest judge you can afford.

9. Building a golden dataset

A golden dataset is a fixed set of (question, ground_truth_answer, expected_contexts) triples that you evaluate against on every pipeline change. Without it, you're flying blind.

How to build one:

  1. Sample from real queries — pull 50-200 questions from production logs or support tickets. Real questions expose real failure modes.
  2. Generate synthetic questions — RAGAS ships TestsetGenerator that uses your document corpus to create question/answer pairs automatically. Good for bootstrapping when you have no traffic yet.
  3. Write adversarial cases by hand — multi-hop questions, negation questions ("what does X not do?"), and ambiguous phrasings. These are where RAG breaks most often.
  4. Have a domain expert validate — spot-check at least 20% of auto-generated entries. LLM-generated ground truths carry LLM errors.
from ragas.testset import TestsetGenerator
generator = TestsetGenerator.from_langchain(llm, embeddings)
testset = generator.generate_with_langchain_docs(docs, testset_size=100)

10. Where RAGAS metrics still miss

RAGAS is a tool, not an oracle. Several failure modes evade it entirely:

  • Factual correctness vs. faithfulness: a context can be wrong, the answer faithfully reproduces the wrong fact, and faithfulness scores 1.0. You need a separate factual grounding check for high-stakes domains.
  • Multi-hop reasoning: if an answer requires chaining two facts from two different chunks, RAGAS doesn't verify that the chain of inference is valid — only that the final claim appears somewhere.
  • Numerical precision: "the margin grew from 12% to 18%" may be grounded but the arithmetic conclusion ("a 50% improvement") can be wrong. RAGAS won't catch this.
  • Tone and format: a factually perfect answer delivered in a confusing format gets a great RAGAS score but a bad user experience.
  • Judge model bias: the LLM judge tends to favor verbose, confident-sounding answers. Calibrate periodically against a human-labeled subset.

Use RAGAS as your fast regression signal in CI, not as your final quality gate.

11. Evaluation pipeline in a CI context

Run RAGAS on every PR that touches chunking strategy, embedding model, retriever parameters, or prompt templates. A drop of more than 0.05 in any metric should block the merge and trigger human review of the failing samples.

flowchart LR
  A["Code change"] --> B["Rebuild RAG pipeline"]
  B --> C["Run golden dataset"]
  C --> D["RAGAS metrics"]
  D --> E["Score delta vs. baseline"]
  E --> F["Pass / Fail gate"]
  F --> G["Human review (sample)"]
  G --> H["Update golden set quarterly"]

12. Interpreting scores holistically

Metrics interact. A pipeline change that improves Context Recall (fetches more relevant chunks) often hurts Context Precision (also fetches more noise). You need to track the tradeoff, not just the individual numbers.

A useful heuristic dashboard:

SignalLikely causeFix
Low FaithfulnessLLM ignores contextStronger system prompt; reranker
Low Answer RelevancyAnswer drifts off-topicTighter output instructions
Low Context PrecisionToo many irrelevant chunksSmaller top-K; reranking
Low Context RecallMissing key chunksLarger top-K; better chunking; hybrid search
All metrics high but users unhappyFormat/tone issues; RAGAS blind spotAdd human eval layer

Set metric floors, not targets. Decide the minimum acceptable score for each metric before deployment, and treat any score below the floor as a blocker — not a suggestion.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. Which RAGAS metric specifically requires a ground-truth reference answer to compute?
    • Faithfulness
    • Answer Relevancy
    • Context Precision
    • Context Recall
  2. A RAG system retrieves 5 chunks, 4 of which are irrelevant, but the 1 relevant chunk is ranked first and the LLM gives a correct, grounded answer. Which metric suffers most?
    • Faithfulness
    • Answer Relevancy
    • Context Precision
    • Context Recall
  3. RAGAS computes Answer Relevancy by generating reverse questions from the answer and measuring their similarity to the original question. What does a low score on this metric most directly indicate?
    • The answer contains hallucinated claims not in the context
    • The retriever failed to fetch the relevant document
    • The answer does not address the actual question asked
    • The ground-truth answer is missing from the golden dataset
  4. You switch your RAGAS judge from GPT-4o to a local 7B model to cut costs. What is the most likely consequence?
    • Context Precision scores become uncorrelated with retriever quality
    • Faithfulness scores correlate less reliably with human judgments
    • Context Recall can no longer be computed without a ground-truth answer
    • Answer Relevancy embedding similarity drops to zero
  5. A context contains the factually incorrect statement 'Water boils at 90°C at sea level.' The LLM faithfully reproduces this as its answer. What do you expect RAGAS to report?
    • Low Faithfulness, because the answer is factually wrong
    • High Faithfulness, because the answer is grounded in the context
    • Low Context Recall, because the context is missing correct information
    • Low Answer Relevancy, because the answer contradicts world knowledge

Related lessons