Sub topic 15
Evaluating a RAG System
This chapter assumes you can read basic Python, know how a RAG pipeline retrieves passages and builds a prompt, and are comfortable with the idea of top-k retrieval. It shows how to measure the two halves of the pipeline separately.
Why evaluate retrieval and generation separately
A RAG answer can be wrong for two different reasons. The retriever may have failed to find the right passage. Or the retriever may have found it and the model may have ignored or misused it. If you only grade the final answer, you cannot tell which one happened, so you cannot tell what to fix.
Build a test set
Start with a small set of real questions, 30 to 100 is enough to begin. For each question, record the ids of the chunks that contain the answer, and a short reference answer. Write the questions the way users phrase them, not the way the documents phrase them. Keep this set fixed so scores are comparable between runs.
Measure retrieval
Recall at k is the fraction of the known relevant chunks that appear in the top k results. If the right chunk is not retrieved, the model cannot use it, so low recall caps your answer quality.
def recall_at_k(retrieved_ids, relevant_ids, k):
relevant = set(relevant_ids)
if not relevant:
return 0.0
hits = set(retrieved_ids[:k]) & relevant
return len(hits) / len(relevant)
test_set = [
{"relevant": ["c4"], "retrieved": ["c9", "c4", "c1", "c7"]},
{"relevant": ["c2", "c3"], "retrieved": ["c3", "c8", "c5", "c6"]},
]
scores = [recall_at_k(t["retrieved"], t["relevant"], k=3) for t in test_set]
print(sum(scores) / len(scores))The first question scores 1.0 and the second scores 0.5, so the average is 0.75. Run this for several values of k. If recall at 10 is high but recall at 3 is low, the right chunks are being found but ranked too low, and reranking may help. If recall is low even at 10, look at chunking, embeddings, or hybrid search.
Measure generation
Generation has its own checks, and they apply to the answer given the retrieved passages:
- Groundedness (faithfulness): every claim in the answer is supported by the passages.
- Correctness: the answer matches the reference answer.
- Citation accuracy: each cited passage supports the claim it is attached to.
- Appropriate refusal: the model says it does not know when the passages lack the answer.
Spot-checking by hand works at first. To scale up, many teams use a second model as a judge, giving it the question, passages, and answer and asking for a score with a reason. Judge models make mistakes, so compare their scores against a sample of human labels before you trust them.
Read the results together
| Retrieval recall | Answer quality | Likely cause |
|---|---|---|
| Low | Low | Retrieval problem: chunking, embeddings, query |
| High | Low | Generation problem: prompt, ignored context, too much noise |
| High | High | Working, so watch for regressions |
Rerun the full test set after every change to chunk size, embedding model, or prompt. A change that helps one question often hurts another, and only the whole set shows the net effect.
Exercise
A test question has three relevant chunks. The retriever returns ten chunks, and two of the relevant ones are at ranks 2 and 8. What is recall at 5, and what does a low answer score with this recall suggest?
Show answer
Only the chunk at rank 2 falls within the top 5, so recall at 5 is 1/3, about 0.33. A weak answer here points to a retrieval problem, since two of the three needed chunks are missing from what the model sees. Check ranking (reranking), chunking, or hybrid search before changing the prompt.