AI State of the Art
SOTA/Retrieval Augmented Generation (RAG)/35 min
Section 142

RAG Evaluation: RAGAS, Faithfulness, Context Relevance

Listen as a podcast →

The Problem: How Do You Know If Your RAG Actually Works?

You've spent hours building your RAG chain. You've split your documents, chosen your embeddings, hooked up your LLM. You ask a question, you get an answer that looks correct. But "looks correct" isn't a measurement.

The Governing Idea: Cut the Evaluation in Two

Here is the point to take away before any of the detail on this page. A RAG system does two things one after the other: it searches for passages, then it writes from them. When the answer is bad, two causes are possible and only one is the real one:

  1. the passage holding the answer was never retrieved;
  2. it was retrieved and badly used by the model doing the writing.

These two failures call for opposite repairs. In the first case you have to touch the chunking, the embedding model, the retrieval method, changing the prompt will do nothing. In the second you have to work on the writing instructions or the model, reindexing the corpus will do nothing.

And here is the difficulty: looking at the final answer, you cannot tell which case you are in. A bad answer looks exactly the same either way. Which explains the ordinary scene in teams building a RAG system: change the prompt, change the model, change the chunking, change the prompt again, and sometimes it improves without anyone knowing why.

The way out is easy to state and demanding to implement: measure the two halves separately. On one side, evaluate what retrieval brought back, without involving the LLM at all; on the other, what the LLM did with the passages it was handed. The Search Toolkit page develops the same idea through the analogy of the intern, the case file and the memo, you have to open the file he brought back before you judge his memo.

The rest of this page follows that plan: first RAGAS, a framework that sorts its metrics into the two columns; then the classic retrieval metrics, older and sturdier; finally how to build the test set without which neither half can be computed.

What Can Break, and Where

What brokeWhich halfWhat it looks like
Unsuitable chunkingRetrievalThe right piece comes back, but truncated: only half the answer is in it
Embedding model poorly suited to the domainRetrievalPassages adjacent to the topic, but not the one that answers
The LLM inventsGenerationFacts absent from the supplied passages
Off-topic answerGenerationAccurate with respect to the documents, but unrelated to the question

The worst part is that these problems are often invisible. The model answers with unshakeable confidence, even when it's talking nonsense. Without systematic evaluation, you're navigating blind in a fog of "looks okay."

Triangle linking Query, Context and Response. The Query to Context arrow is labelled Context Relevance, the Context to Response arrow is labelled Groundedness, and the Response to Query arrow is labelled Answer Relevance.
TruLens places the metrics on the three sides of a Query / Context / Response triangle. Context Relevance: is the retrieved context relevant to the question, that is the retrieval half. Groundedness: is the answer actually supported by that context. Answer Relevance: does the answer address the question, those last two are the generation half. If all three hold, there is little room left for a hallucination.Source : TruLens (RAG Triad)

RAGAS: Four Metrics, Sorted Into Two Columns

RAGAS (Retrieval Augmented Generation Assessment) is a widely used open-source library for evaluating RAG systems. Its central idea: use an LLM as an automatic judge to score four metrics. It is the LLM-as-a-judge principle (see LLM-as-judge) applied to RAG evaluation.

The original paper states exactly the difficulty set out at the top of this page: you have to separate the retrieval system's ability to identify relevant passages, the model's ability to use them faithfully, and the quality of the writing itself. RAGAS's selling point is that its metrics are computed without human-written reference answers, all but one, as we will see.

Ragas: Automated Evaluation of Retrieval Augmented Generation (Es et al., 2023)
Diagram titled ragas score, split into two columns. Generation column: faithfulness (how factually accurate the generated answer is) and answer relevancy (how relevant the generated answer is to the question). Retrieval column: context precision (signal to noise ratio of the retrieved context) and context recall (whether it retrieves all the information required to answer).
The four RAGAS metrics detailed below, sorted by which link of the chain they judge: on the left the ones that score generation (faithfulness, answer relevancy), on the right the ones that score retrieval (context precision and recall). A falling overall score tells you nothing; the column that drops tells you which link to fix.Source : Ragas documentation

Faithfulness, generation half

The question here: is the LLM's answer grounded in the supplied passages, or is it adding things of its own?

Imagine a student at an oral exam. The examiners hand them reference documents. If they assert something that is in none of the documents, it is a hallucination, even if it happens to be true.

The concrete process:

  1. an LLM breaks the answer into elementary claims;
  2. for each one, it checks: does one of the supplied passages support this claim?
  3. score = supported claims ÷ total claims.

So a score of 0.8 means 4 claims in 5 are backed by the documents. The fifth may be a hallucination, or a true claim that the file simply did not support, a distinction this metric does not make.

Note what the score is not: it says nothing about whether the claims are true. A system fed false documents will score perfect faithfulness by copying them faithfully.

Answer Relevancy, generation half

An answer can be perfectly faithful to the documents… and completely off-topic. You ask "What is the price of product X?" and the system replies with its technical specifications. Faithful, but useless.

RAGAS measures this by a clever detour:

  1. an LLM reads the answer and writes the questions this answer would suit;
  2. those reconstructed questions are compared to the question actually asked, by cosine similarity between their vectors;
  3. score = average similarity.

The reasoning: if the answer really belongs to the question, you should be able to get back from answer to question. If the reconstructed questions are about something else, the answer was about something else.

Context Precision, retrieval half

First recall what precision means: out of the items the system brought back, what proportion was genuinely useful? It is the mirror question to recall, which asks how many of the useful items were brought back (Evaluation metrics).

Context precision adds position to that. The reason is concrete: slots in the context are not equal, since a model exploits the beginning and end of what it is given better than the middle. A useful passage in first position is therefore worth more than the same one in tenth.

RankUseful?
1Yes
2No
3Yes

In this example, two useful passages out of three: raw precision is 2/3. Context precision will score this higher than if the order were reversed (useless one on top), because it weights each position by its importance.

Context Recall, retrieval half

Did you retrieve everything needed to answer? The LLM checks, for each element of the reference answer, that at least one retrieved passage supports it.

This is the only one of the four that requires a human-written reference answer, and that is what makes it costly: someone has to annotate. It is also the most important of the four for diagnosis, because it alone tells you whether the system had the material to answer at all. Excellent faithfulness alongside mediocre context recall describes a system that summarises an incomplete file very cleanly.

from ragas import evaluate
from ragas.metrics import faithfulness, answer_relevancy

results = evaluate(
    dataset=eval_dataset,
    metrics=[faithfulness, answer_relevancy],
)
Start with Faithfulness and Answer Relevancy

Neither of these two needs any human annotation: they are computed from the question, the supplied passages and the produced answer, all three already at hand. Context recall, by contrast, requires a hand-written reference answer. So deploy the two free metrics first for quick feedback, and invest in annotation when you want to know not whether the model uses its file well, but whether the file was complete.

Classic Retrieval Metrics

Beyond RAGAS, the classic metrics from information retrieval remain the sturdiest way to judge the retrieval half, and they have one decisive advantage: no LLM is involved. They only compare two lists, the passages that came back and the passages that should have. There is no judge to argue with, just arithmetic.

All of them do require relevance judgements, though: for each question in the test set, someone must have said in advance which passages answer it. That is the annotation work there is no way around.

Recall@K

Out of the passages that should have been found, what proportion appears in the top K results?

recall@10=useful passages present in the top 10total number of useful passages\text{recall@10} = \frac{\text{useful passages present in the top 10}}{\text{total number of useful passages}}

The numerator counts the relevant passages actually returned within the first ten slots; the denominator, every relevant passage the corpus holds for that question. A recall@10 of 0.9 means 9 useful passages in 10 are in your top ten.

This is the most important measurement for the first stage of a RAG chain, and the reason is structural: what is not retrieved is permanently lost. No reranker, no prompt, no bigger model will recover a passage that is absent from the list.

The K must always be quoted with the number. A "recall of 0.8" without its K means nothing, since widening the list is enough to raise recall, in the limit, returning the entire corpus gives a recall of 1 and is useless.

Two identical lists of 15 ranked results, relevant documents in pink and non-relevant ones in grey. On the left a cutoff at K equals 10 gives Recall@10 = 0.625. On the right the same list cut at K equals 5 gives Recall@5 = 0.375.
The same result list, cut at two different depths. Out of the 8 relevant documents in the corpus, 5 fall inside the top 10 (recall 0.625) but only 3 inside the top 5 (recall 0.375). This is why K must always be quoted with the number: a recall without its K means nothing.Source : Evidently AI

Precision@K

The mirror question: out of the K passages returned, what proportion was useful? A recall@10 of 1 achieved with a single useful passage out of ten means the writing model will have to do the sorting itself, through nine tenths of noise. Precision and recall work against each other: widening K raises recall and lowers precision. So you look at both.

MRR (Mean Reciprocal Rank)

Where does the first good passage land? You take the reciprocal of that rank: first place, 1; second, 1/2; fifth, 1/5; absent, 0. Then you take the mean of those reciprocals across all questions in the test set, hence the name.

The order of operations matters, and it is a common confusion: you average the reciprocals of the ranks, not the reciprocal of the average rank. On two questions, one finding its passage at rank 1 and the other at rank 10, MRR = (1 + 0.1) ÷ 2 = 0.55, not the reciprocal of the mean rank 5.5, which would give 0.18. The difference is not cosmetic: averaging reciprocals lets a single well-handled question count heavily, which is exactly the intent when a human reads the first result and stops there.

MRR is the metric for a search bar: what matters is that the right result is first. It ignores everything that follows, which makes it a poor fit for a RAG system where the model will read five passages.

NDCG@K

NDCG stands for normalized discounted cumulative gain. The name is opaque; the idea comes apart in three pieces, read backwards.

  • Cumulative gain: each result earns points according to how useful it is. Unlike the previous metrics, relevance need not be binary: you can grade "very useful", "somewhat useful", "useless". You add up the points of the top K.
  • Discounted: a point earned low in the list is worth less than one earned high. Each rank is divided by a factor that grows with position. This is the numerical statement of the fact that the top slots get read better.
  • Normalized: you divide the total by the total the best possible ordering of the same documents would have given. The score then lives between 0 and 1, and two questions become comparable even if one has ten good passages available and the other only one.

So NDCG@10 reads: over the first ten results, how close is my ordering to the ideal one? It is the finest of the four, and the best suited to judging a reranker, since reranking does not change what was found, only the order.

Which One to Look At, Given What You're Asking
MetricWhat it answersWhen it's the right one
Recall@KIs the right passage in the list at all?Always first: this is the gravest failure
Precision@KIs the list clean?When the context handed to the model is saturated with noise
MRRIs the right passage first?A search bar read by a human
NDCG@KIs the overall ordering sound?For judging a reranker

These four are exactly what the evaluation module described in Search Toolkit provides, where they are worked through on a legal example.

Building a Solid Benchmark

A good RAG benchmark is like a good exam: it must cover simple cases, ambiguous cases, and trick cases.

Step 1: Collect Diverse Questions

  • Real questions from production logs (the best ones, as they're representative)
  • Synthetic questions generated by an LLM from your documents
  • Expert questions from domain specialists (for subtle cases)
  • Trick questions: questions whose answer is not in the corpus at all, ambiguous questions, questions requiring two documents to be combined

Step 2: Annotation

For each question, annotate:

  • the expected answer (the reference, or ground truth);
  • the relevant passages in the document base, this annotation, and only this, is what makes recall, precision, MRR and NDCG computable;
  • the difficulty level (simple / medium / complex).

Step 3: Test Set Size

How many questions do you need? The calculation can be done in front of you, and it is instructive.

A measurement like recall is a proportion estimated from a sample. Its uncertainty shrinks with the square root of the number of questions. For an observed rate around 80%, the standard deviation of the estimate is 0.8×0.2/n\sqrt{0.8 \times 0.2 / n}, where n is the number of questions; the usual interval spans about twice that on each side.

  • with n = 50: roughly ±11 points. You can see the difference between 60% and 85%, not between 78% and 82%;
  • with n = 200: roughly ±6 points;
  • with n = 1000: roughly ±2.5 points.

Hence the practical reading: 50 questions are enough to catch the big problems; several hundred are needed to choose between two close configurations; and no sample size lets you conclude anything from a one-point gap. What the number of questions says nothing about, on the other hand, is representativeness: a thousand questions all of the same kind is still a blind test set.

Automatic Question Generation

RAGAS and DeepEval can generate evaluation questions from your documents automatically. That is a good way to get off zero, with one bias worth knowing: those questions are written by a model from the text of the documents, so they use the documents' vocabulary. They are therefore mechanically easier than a real user's questions, since a real user does not know the corpus vocabulary, and that vocabulary mismatch is precisely what makes retrieval fail. Always supplement with real questions.

Production Monitoring

A frozen test set is fine during development. In production, though, documents change, user questions evolve, and quality can degrade without anyone noticing. So you monitor continuously.

What you trackWhere it comes fromHow often
FaithfulnessRAGAS on a sample of exchangesDaily
Share of "I don't know" answersUsage logsContinuous
User feedbackThe thumbs up or down under the answerContinuous
Median and 99th-percentile response timeInfrastructure monitoringContinuous

A word on that last row, whose notation is common but rarely explained: the median (often written P50) is the time half the requests stay under; the 99th percentile (P99) is the time 99% of requests stay under. You watch both because a system can be fast in general and unbearable one time in a hundred, and that is the time users talk about.

A/B Testing

Want to compare two configurations (different chunking, different embeddings, different instructions)? Run them side by side in production, measure each variant, check that the gap exceeds the margin of error computed above, then roll out the winner gradually.

The LLM Judge Has Its Biases

Three of RAGAS's four metrics rest on an LLM passing judgement. That judge is not neutral: the literature documents a preference for long, well-formatted answers, or answers written in a style close to its own, plus a sensitivity to the order in which two answers are presented for comparison. Details in LLM-as-judge.

Practical consequence: a RAGAS score is useful for tracking a trend at constant protocol, much less for asserting an absolute level. Cross-check regularly against human judgement on a small sample. And note the asymmetry: the classic retrieval metrics depend on no judge at all, one more reason to look at them first.

Other Evaluation Tools

RAGAS isn't alone. The ecosystem has grown:

  • DeepEval: customizable metrics and integration with continuous-integration pipelines
  • TruLens: evaluation and monitoring, with detailed tracing of each step in the chain
  • Phoenix (Arize): observing the system in operation, with embedding visualization
  • LangSmith: evaluation platform integrated into the LangChain ecosystem
  • Search Toolkit (Mistral): focused on the retrieval half only, with recall, precision, MRR and NDCG built in (Search Toolkit)

Evaluating a RAG system isn't a formality you complete once before launch. It is a continuous process, and its value does not come from the numbers themselves: it comes from their letting you attribute a failure to a cause. A RAG system without separate measurement of the two halves isn't a system you improve, it's a system you tinker with.

Check your understanding0 / 5

1. Your RAG system's final answer is wrong. Your first instinct is to rewrite the generation prompt. Why is that premature?

2. A RAG system fed documents that contain factual errors copies them out carefully and scores a perfect faithfulness. What should you conclude?

3. A vendor advertises that its engine reaches a recall of 0.95, with no further detail. Why is that figure meaningless?

4. Across two questions, the first good passage lands in 1st place for one and 10th place for the other. What is the MRR, and why that way of computing it?

5. Your RAGAS faithfulness moves from 0.82 to 0.86 after a prompt change, measured under the same protocol. What can you claim?