How do you evaluate a RAG system?
- Authors
- Name
- Amit Shekhar
- Published on
Evaluating a RAG system means checking two things with numbers: did it find the right pieces of information, and did it write a correct answer using only those pieces. We test both parts separately on a fixed set of questions, so we know exactly what is working and what is broken.
In this blog, we will learn about how to evaluate a RAG system. We will also see why testing a few questions by hand is not enough, how to build a test set, how to measure the retrieval part with metrics like Recall@K and MRR, how to measure the answer part with Faithfulness and Answer Correctness, how an LLM can act as a judge, and how to use all of this to improve the system step by step.
We will cover the following:
- Why do we need to evaluate a RAG system?
- The two parts we must evaluate
- Building a test set
- Evaluating the retrieval part
- Evaluating the generation part
- LLM-as-a-judge
- The RAG Triad
- Other things to measure
- Putting it all together with an example
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning at Outcome School.
Let's get started.
Why do we need to evaluate a RAG system?
Let's say we build a RAG system for our company's HR documents. We ask it five questions, and the answers look good. We feel happy and ship it.
After launch, an employee asks, "Can I carry forward my unused leave?". The system says, "Yes, all unused leave carries forward." The real policy says only 10 days carry forward. The employee plans a long vacation based on a wrong answer.
The five questions we tested by hand were not enough. We had no idea how often the system was wrong.
So, the following issues arise when we do not evaluate properly:
- We do not know how good the system really is.
- When we change something, like the chunk size or the prompt, we do not know if it got better or worse.
- When an answer is wrong, we do not know which part caused it.
Evaluation means measuring the quality of the system with numbers, on a fixed set of questions, so that we can compare one version with another.
We have a detailed blog on LLM Evaluation that covers how we evaluate LLMs in general.
The two parts we must evaluate
A RAG (Retrieval-Augmented Generation) system has two parts. Our documents are cut into small pieces called chunks. The retriever finds the useful chunks for the question, and the LLM writes the answer using those chunks.
We can see the flow in a diagram like below:
Question
↓
[Retriever] searches our documents (cut into chunks)
↓
Top chunks
↓
[LLM]
↓
Answer
So, a RAG system can fail in two different places.
Failure 1: The retriever brings the wrong pages. The right information is in our documents, but the retriever does not pick it. The LLM never sees it, so it cannot answer correctly.
Failure 2: The LLM writes a bad answer from the right pages. The retriever did its job, but the LLM ignores the pages, misreads them, or adds things that are not there.
It is like a student in an open-book exam. Either the student opened the wrong page, or the student opened the right page and still wrote the wrong answer. The fix is different in both cases.
So, we evaluate the two parts separately:
- Retrieval evaluation: Did we find the right chunks?
- Generation evaluation: Did the LLM write a good answer from those chunks?
Then, we also look at the final answer end to end.
Building a test set
Before measuring anything, we need something to measure against. This is called a test set, and many people call it a golden dataset.
A test set is a fixed list of questions, where for each question we know the correct answer and which chunks contain that answer.
Each row of the test set looks like below:
Question: How many days of unused leave can be carried forward?
Correct answer: Up to 10 days can be carried forward to the next year.
Correct chunks: [chunk_42]
Here, we have:
- Question: what a real user would ask.
- Correct answer: what a good answer must say. This is also called the ground truth, which means the answer we know is true.
- Correct chunks: the ids of the chunks that contain the answer.
How do we build this test set? There are three common ways:
- Write by hand: People who know the documents write questions and answers. This is the best quality, but slow.
- Use real user questions: Pick questions from our logs and have experts write the correct answers.
- Generate with an LLM: Give a chunk to an LLM and ask it to create a question that this chunk answers. This is fast. But we must review these questions, because generated questions are often too easy.
Usually, 50 to 200 good questions are enough to start. We must also include tricky ones: questions that need two chunks, questions with numbers and dates, and questions whose answer is not in the documents at all. For those, the correct behavior is to say "I do not know." Such questions have no correct chunks, so we skip the retrieval metrics for them and only check the final answer.
Planning your move into AI?
No matter which tech domain you work in today, with your effort, you can transition to these roles:
- AI Engineer
- Forward Deployed Engineer
- AI Agent Engineer
- Harness Engineer
- Voice AI Engineer
- AI Architect
- LLM Engineer
- LLM Inference Engineer
- Gen AI Engineer
- MLOps Engineer
- ML Engineer
- Deep Learning Engineer
Our AI and Machine Learning Program at Outcome School takes you there.
Now, our test set is ready. Let's evaluate the retriever.
Evaluating the retrieval part
For every question in the test set, we run the retriever and get back the top K chunks. K is the number of chunks we pick, like 5. Then, we compare the retrieved chunks with the correct chunks.
Let's take an example to understand all the metrics. Suppose for one question, the correct chunks are chunk_42 and chunk_43. The retriever returns the top 5 like below:
Rank 1: chunk_10
Rank 2: chunk_42 <-- correct
Rank 3: chunk_7
Rank 4: chunk_88
Rank 5: chunk_15
Hit Rate
Hit Rate checks whether at least one correct chunk appears in the top K.
Here, chunk_42 is in the top 5. So, it is a hit, and the score is 1. If no correct chunk appeared, the score would be 0. We take the average over all questions. A Hit Rate of 0.9 means 90% of questions got at least one correct chunk.
Recall@K
Recall@K is the fraction of all correct chunks that we found in the top K.
There are 2 correct chunks, and we found 1. So, Recall@5 = 1 / 2 = 0.5.
Recall tells us whether we are missing information. Low recall means the LLM does not get everything it needs.
Precision@K
Precision@K is the fraction of the top K chunks that are actually correct.
We picked 5 chunks, and only 1 is correct. So, Precision@5 = 1 / 5 = 0.2.
Precision tells us how much noise we are sending to the LLM. Low precision means the LLM has to read many useless chunks, which costs money and can confuse it.
We have a detailed blog on Precision vs Recall that explains the trade-off between the two step by step.
MRR (Mean Reciprocal Rank)
MRR checks how high the first correct chunk appears in the list.
Reciprocal means "1 divided by". The first correct chunk is at rank 2, so the reciprocal rank is 1 / 2 = 0.5. If it were at rank 1, the score would be 1. At rank 5, the score would be 0.2. Then, we take the average over all questions.
MRR matters because the top results get the most attention. Let's see the code for these metrics as below:
def retrieval_scores(retrieved, correct, k=5):
top_k = retrieved[:k]
found = [c for c in top_k if c in correct]
hit = 1 if found else 0
recall = len(found) / len(correct)
precision = len(found) / k
rr = 0
for rank, chunk in enumerate(top_k, start=1):
if chunk in correct:
rr = 1 / rank
break
return hit, recall, precision, rr
retrieved = ["chunk_10", "chunk_42", "chunk_7", "chunk_88", "chunk_15"]
correct = ["chunk_42", "chunk_43"]
print(retrieval_scores(retrieved, correct))
Here, we have:
top_k: the first K retrieved chunks.found: the retrieved chunks that are also in the correct list.hit: 1 if we found anything, else 0.recallandprecision: exactly the formulas we learned above.rr: the reciprocal rank of the first correct chunk. We stop at the first match.
It will print the following:
(1, 0.5, 0.2, 0.5)
We run this for every question and take the average of each number. The average of rr is the MRR.
Note: These metrics need the "correct chunks" labels. If we do not have them, we can ask an LLM to judge whether each retrieved chunk is relevant to the question. This is often called Context Relevance or Context Precision. We will learn about LLM judges shortly.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
This is all about evaluating the retrieval part, now let's move to the generation part.
Evaluating the generation part
Now, we check the answer that the LLM wrote. There are three main questions to ask.
Faithfulness (also called Groundedness)
Faithfulness checks whether every claim in the answer is supported by the retrieved chunks.
This is one of the most important metrics for RAG, because it catches hallucination. Hallucination means the LLM confidently says something that is not true or not in the given text.
Let's say the chunk says: "Employees can carry forward up to 10 days of unused leave."
The answer says: "You can carry forward up to 10 days of leave, and the extra days are paid out in cash."
We break the answer into claims:
- Claim 1: "You can carry forward up to 10 days." It is supported by the chunk.
- Claim 2: "Extra days are paid out in cash." It is not in the chunk.
Faithfulness = supported claims / total claims = 1 / 2 = 0.5.
The LLM made up the second claim. Even if it happens to be true, the system has no source for it.
Answer Relevance
Answer Relevance checks whether the answer actually addresses the question that was asked.
If the question is "How many days can I carry forward?" and the answer is "Our leave policy was updated in 2022 and applies to all full-time staff", the answer is faithful to the documents, but it does not answer the question. So, Answer Relevance is low.
Answer Correctness
Answer Correctness checks whether the answer matches the correct answer from our test set.
Faithfulness compares the answer with the chunks. Answer Correctness compares the answer with the ground truth. Both are needed. If the retriever brought a wrong chunk, the answer can be perfectly faithful to that wrong chunk and still be incorrect.
Let me tabulate the differences for your better understanding.
| Metric | Compares | Catches |
|---|---|---|
| Faithfulness | Answer vs retrieved chunks | Made-up facts (hallucination) |
| Answer Relevance | Answer vs question | Off-topic or vague answers |
| Answer Correctness | Answer vs ground truth | Wrong final answers |
To master RAG and the Evaluation of LLMs and Agents, and to build an AI Tutor from scratch, check out our AI and Machine Learning Program at Outcome School.
LLM-as-a-judge
Now, the question is: who decides whether a claim is supported, or whether an answer is relevant? Checking thousands of answers by hand is slow and costly.
Here comes LLM-as-a-judge into the picture.
LLM-as-a-judge means we use a strong LLM to grade the answers of our system, using a clear set of instructions.
For Faithfulness, we can write a judge prompt like below:
You are a strict grader.
Context:
{retrieved_chunks}
Answer:
{answer}
Step 1: Break the answer into short, separate claims.
Step 2: For each claim, reply SUPPORTED if the context clearly states it,
otherwise reply NOT SUPPORTED.
Return the list of claims with their labels.
Here, we give the judge the chunks and the answer. The judge breaks the answer into claims and labels each one. Then, our code counts the labels and computes the score.
Libraries like RAGAS and TruLens come with ready-made judge prompts for these metrics, so we do not have to write everything ourselves.
But, the judge is also an LLM, so it can also do mistakes. To trust it, we must:
- Check the judge against human grades on a small sample, say 50 answers. If the judge agrees with humans most of the time, we can trust it for the rest.
- Ask for simple labels like SUPPORTED or NOT SUPPORTED, instead of a vague score from 1 to 10.
- Ask the judge to give a short reason, so that we can read why it decided something.
- Use a strong model as the judge, and keep the judge prompt fixed while comparing versions.
The RAG Triad
A simple way to remember the core checks is the RAG Triad, a name made popular by TruLens. Triad means a group of three.
Question
/ \
Context / \ Answer
Relevance / \ Relevance
/ \
Retrieved chunks -------- Answer
Groundedness
Here, we can see the three sides:
- Context Relevance: Are the retrieved chunks relevant to the question? This checks the retriever.
- Groundedness: Is the answer supported by the chunks? This is the same as Faithfulness.
- Answer Relevance: Does the answer address the question?
If all three sides are strong, we can be confident that the answer is not made up and is on topic.
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
Other things to measure
Quality is not the only thing. A real system must also be fast and affordable. So, we must also measure:
- Latency: how many seconds it takes to get an answer.
- Cost: how much money each question costs, mainly from the LLM.
- Refusal behavior: for questions whose answer is not in the documents, does the system say "I do not know" instead of making something up?
- User feedback: after launch, thumbs up and thumbs down from real users, and the questions that got bad ratings go back into our test set.
Putting it all together with an example
Let's see how a team uses all of this in practice.
Step 1: Build a test set of 100 questions with correct answers and correct chunks.
Step 2: Run the current system and record the scores. Let's say the results look like below:
Recall@5: 0.62
MRR: 0.48
Faithfulness: 0.91
Answer Correctness: 0.58
Step 3: Read the numbers. Faithfulness is high, so the LLM is mostly sticking to the chunks. But Recall@5 is low, so the retriever misses 38% of the correct chunks. The low Answer Correctness is mainly a retrieval problem, not an LLM problem.
Step 4: Fix the weak part. For example, we try a better chunk size, add keyword search along with meaning search, or add a reranker, which is a model that reorders the found chunks more carefully.
Step 5: Run the same test set again and compare the numbers. If Recall@5 goes up to 0.85 and Answer Correctness goes up to 0.79, the change helped. We keep it.
We change one thing at a time, run the same test set, and compare.
Now we must have understood how to evaluate a RAG system, which metrics to use for the retrieval and the generation parts, and how to use them to improve the system.
Frequently Asked Questions
How many questions should a RAG test set have?
Usually, 50 to 200 good questions are enough to start. The test set should also include tricky questions: ones that need two chunks, ones with numbers and dates, and ones whose answer is not in the documents at all. The questions can be written by hand, picked from real user logs, or generated with an LLM and then reviewed.
Can we evaluate RAG retrieval without labeled correct chunks?
Yes. If we do not have the correct chunk labels, we can ask an LLM to judge whether each retrieved chunk is relevant to the question. This is often called Context Relevance or Context Precision. Metrics like Recall@K, Precision@K, and MRR need the correct chunk labels.
What is the difference between Faithfulness and Answer Correctness?
Faithfulness compares the answer with the retrieved chunks, while Answer Correctness compares the answer with the ground truth from the test set. We need both. If the retriever brings a wrong chunk, the answer can be perfectly faithful to that wrong chunk and still be incorrect.
How do we know whether a bad RAG answer comes from retrieval or generation?
We look at the retrieval and generation scores separately. If Faithfulness is high but Recall@K is low, the LLM is sticking to the chunks, and the retriever is missing the right ones, so it is a retrieval problem. Then we fix the weak part, run the same test set again, and compare the numbers.
What should a RAG system do when the answer is not in the documents?
It should say "I do not know" instead of making something up. We test this by adding questions whose answer is not in the documents to the test set. These questions have no correct chunks, so we skip the retrieval metrics for them and only check the final answer.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
