RAG Evaluation: Turning "Is It Good?" into Numbers
Core concepts
Evaluate RAG in two stages, otherwise you can't tell whether a problem lies in retrieval or in generation:
| Stage | Metric | Meaning |
|---|---|---|
| Retrieval | Recall@k | Whether the chunk containing the reference answer appears in the top k results |
| Retrieval | MRR | The rank of the correct chunk (the higher the better) |
| Generation | Faithfulness | Whether the claims in the answer are supported by the retrieved context (i.e. no hallucination) |
| Generation | Answer relevancy | Whether the answer is on topic |
| End to end | Human-judged accuracy | Whether the answer is right from the end user's point of view |
How it works
- Retrieval metrics need "question → correct chunk" labels: label them by hand, or have an LLM generate questions from chunks (synthetic data) and then spot-check by hand.
- Lost in the Middle: Liu et al. 2023 found that models make poorer use of information in the middle of a long context, so a bigger top-k isn't always better; put important chunks at the start or end.
- Frameworks such as Ragas use an LLM to compute metrics like faithfulness and context precision / recall automatically. This is still LLM-as-judge at heart and needs human spot checks for calibration.
In practice: a minimal retrieval evaluation
python
def recall_at_k(results: list[list[int]], gold: list[set[int]], k: int) -> float:
hit = [bool(set(r[:k]) & g) for r, g in zip(results, gold)]
return sum(hit) / len(hit)
# Compare three configs: vector only / hybrid / hybrid + rerank
for name, retriever in CONFIGS.items():
res = [retriever(q) for q in questions]
print(name, "R@5=", recall_at_k(res, gold, 5), "R@20=", recall_at_k(res, gold, 20))Example output table (the numbers are placeholders; use your own data):
| Config | Recall@5 | Recall@20 | P95 latency |
|---|---|---|---|
| Vector only | ? | ? | ? |
| Hybrid (RRF) | ? | ? | ? |
| Hybrid + rerank | ? | ? | ? |
Common pitfalls
- Only evaluating end to end: you can't tell whether the right chunk wasn't retrieved, or was retrieved but not used well.
- Synthetic questions that are too easy: questions an LLM generates from a chunk tend to reuse the chunk's wording, which overestimates vector search.
- An eval set that never gets updated: keep adding real user questions and failure cases from production.
Common interview questions
- RAG results are poor. How do you tell whether it's a retrieval problem or a generation problem?
- How is Recall@k calculated? Where does the evaluation data come from?
- What is faithfulness? How do you evaluate it automatically, and what are the limits?
- What does Lost in the Middle imply for RAG design?
How it relates to my projects
- AI Document Review Pipeline W4: use the table above to compare three retrieval configs and put it in the README, so there is real data to talk about in interviews.
Sources
- Liu et al., 2023 · Lost in the Middle: How Language Models Use Long Contexts (arXiv 2307.03172)
- Ragas Docs: Available metrics — definitions of faithfulness, context precision / recall, etc.
- Anthropic: Contextual Retrieval — an example using the top-20 retrieval failure rate as the evaluation metric
