PREDICTIVE TREND INSIGHT
Evaluating retrieval context quality using the Ragas framework Illustration

Evaluating retrieval context quality using the Ragas framework

Direct Summary:

Ragas is an open-source framework for scoring a RAG pipeline on metrics an eyeball review can't reliably measure at scale: faithfulness (does the answer only state things the retrieved context actually supports?), context precision and recall (did retrieval find the right chunks, and only the right chunks?), and answer relevancy. Running these metrics on a test set turns "does our RAG system seem okay" into a number you can track release over release.

"What gets measured gets managed."

— Peter Drucker

Key Insights

  • Faithfulness catches hallucination specifically: it decomposes the generated answer into individual claims and checks each one against the retrieved context — a claim not supported by any retrieved chunk drags the score down, even if it happens to be true.
  • Precision and recall are separate failure modes: low context precision means you retrieved irrelevant chunks alongside good ones; low context recall means you missed chunks you actually needed. Fixing one doesn't fix the other.
  • You need a test set, not just live traffic: Ragas metrics run against a set of questions with (ideally) known-good reference answers, so you can measure regressions before they hit real users.

"Our RAG demo looks good" is not a metric — it's an anecdote from whichever queries you happened to try. Ragas exists to replace that anecdote with a repeatable score: run a fixed set of test questions through your pipeline, and get faithfulness, context precision, context recall, and answer relevancy scores you can compare across changes to your chunking, retrieval, or prompt.

Running a Ragas evaluation

1. Build a test set of questions. Ideally each has a reference ("ground truth") answer, so context recall can be measured against what should have been retrieved.

2. Run each question through your actual RAG pipeline and capture both the retrieved contexts and the generated answer — Ragas scores need all of this, not just the final answer text.

3. Score the results against the four core metrics and track them over time — a regression in context recall after a chunking change tells you exactly where to look, instead of just "the app feels worse."

ragas_eval.py
# Evaluating a RAG pipeline's outputs against Ragas metrics
from ragas import evaluate
from ragas.metrics import faithfulness, context_precision, context_recall, answer_relevancy
from datasets import Dataset

# One row per test question - your RAG pipeline produces these fields
eval_data = Dataset.from_dict({
    "question": ["What is our refund policy?"],
    "answer": [generated_answer],           # from your RAG pipeline
    "contexts": [retrieved_chunks],         # what your retriever returned
    "ground_truth": ["Refunds are processed within 14 days."],
})

results = evaluate(
    eval_data,
    metrics=[faithfulness, context_precision, context_recall, answer_relevancy],
)
print(results)
Metric What a low score means
Faithfulness The answer makes claims not supported by the retrieved context (hallucination)
Context precision Retrieval returned irrelevant chunks alongside the useful ones
Context recall Retrieval missed chunks that were actually needed to answer correctly
Answer relevancy The answer drifts from what the question actually asked

None of these four metrics tells the whole story alone — a pipeline can have perfect context recall (it retrieved everything needed) but still score poorly on faithfulness if the model ignores the retrieved context and answers from its own training data instead. Track all four together.

Practical Challenge

Write 5 test questions with known-correct reference answers for a document you have on hand, run them through Ragas's faithfulness metric, and identify which answer (if any) makes a claim the retrieved context doesn't actually support.

Concept Check

A RAG pipeline scores well on context recall but poorly on faithfulness. What does that combination most likely indicate?
Correct! High context recall means the right information was retrieved. Low faithfulness means the generation step didn't actually ground its answer in that retrieved context — a generation-side problem, not a retrieval-side one.
Incorrect. Try again! Hint: context recall and faithfulness measure two different pipeline stages — retrieval vs. generation.

Sources & Further Reading

  • Ragas (GitHub) — the open-source RAG evaluation framework this article is about.
Previous Guide Dashboard Next Guide