RAG Evaluation
After this lesson, you will be able to:
- Distinguish retrieval metrics (recall@k, NDCG, MRR) from generation metrics (faithfulness, answer relevancy, context precision/recall)
- Use RAGAS to compute LLM-as-judge metrics for production RAG systems without ground-truth labels
- Build a golden evaluation set: 50-200 (question, ground-truth answer, ground-truth chunks) tuples that catch regressions
- Apply citation-grounded evaluation — does the answer actually trace back to the retrieved chunks, or does the LLM hallucinate?
Before you start
Open the /play/rag-eval-metrics playground and drag the retrieved-chunk relevance slider. Watch faithfulness, answer relevancy, context precision, and context recall update, and notice how a single hallucinated sentence collapses faithfulness from 0.95 to 0.40.
#The Two Pillars of RAG Eval
#Retrieval Metrics
These metrics are definitions here. Precision and recall are derived in Model Evaluation & Selection, and the NDCG discount and its range are derived in Query Rewriting & Advanced Retrieval, so we do not repeat them.
A quick check before we move to LLM-as-judge metrics.
Your retriever returns relevant docs at ranks 1, 4, 7 for query A and at rank 9 for query B. Total relevant docs are 3 (A) and 1 (B). What's MRR averaged over these two queries?
#RAGAS: LLM-as-Judge Generation Metrics
The ranked form of context precision is a result, not a definition: it explains why order matters. Open the derivation to see it reduce to a simple average and why its best value is 1.
#Can You Trust the Judge? Agreement and Cohen's Kappa
An LLM judge is only useful if it agrees with humans. Raw agreement is misleading: a judge that always says supported agrees with a human on every claim the human also accepts. Cohen's kappa subtracts the agreement you would get by chance. Open the derivation to see the formula, why it equals 0 at chance and 1 at perfect agreement, and its lowest value.
#Building a Golden Set
LLM-as-judge metrics are powerful but not free — every judged question is one or more model calls, so the cost is questions × calls per question × your provider's price. The complement: a small golden set (50-200 hand-curated questions) with known ground-truth chunks and answers.
Recipe
- Sample real user queries from production logs (anonymized)
- For each, label the ground-truth chunks (which docs SHOULD be retrieved)
- Write the ideal answer
- Run your RAG pipeline; compute recall@k, NDCG@k, faithfulness, answer relevancy
- Commit to CI; fail builds on > 3% regression
Golden sets catch the regressions that aggregate metrics miss. Production teams maintain both.
How many questions are enough to trust a 3% gate? Each question is a noisy yes or no, so the score has a standard error of sqrt(p(1-p)/n), derived in Sampling, Standard Error & the Central Limit Theorem, and the difference of two runs has a larger one (derived in Statistical Inference: Hypothesis Testing). The sample-size formula n = 2 sigma squared (z + z)^2 / delta squared is derived in Power & Sample Size. With p = 0.9 and n = 50 questions, the standard error of the difference between two independent runs is 0.06, so a 3-point drop appears by luck about 31 percent of the time (a simulation of 200,000 pairs gave 30.8 percent).
Detecting a true 3-point change with 80 percent power needs about 1,570 questions per run. Re-running the same questions (a paired design, also covered in Power & Sample Size) reduces the noise, which is why a small golden set can still work for catching big regressions.
#The Math Behind RAGAS
The RAGAS metrics look opinionated but each maps to a clean information-retrieval quantity. The playground below implements faithfulness, answer relevancy, context precision, and context recall from scratch on a tiny golden set so you can see exactly what RAGAS is computing under the hood.
#Citation-Grounded Evaluation
The 2024+ frontier: don't just check if the answer is correct, check if each claim cites a specific retrieved chunk.
Your RAGAS faithfulness is 0.95 but users complain the answers are 'wrong'. What's the most likely diagnosis?
The answer: faithfulness measures groundedness in retrieved context, not factual correctness. If retrieval pulled wrong chunks but the LLM faithfully grounded its answer in those wrong chunks, faithfulness=1.0 but the answer is wrong. Always pair faithfulness with answer relevancy and context precision.
#Hands-On
Tests: Verify recall@3 = 1.0 for the first query (chunk_1 retrieved). Verify recall@3 = 0.5 for the second query (chunk_3 retrieved, chunk_5 missed).
#Key Takeaways
- Retrieval and generation eval are distinct. Recall@k / NDCG / MRR for retrieval; faithfulness / answer relevancy for generation
- RAGAS is the canonical LLM-as-judge framework. Works without ground-truth answers; production target faithfulness ≥ 0.90
- Golden sets catch what aggregates miss. 50-200 hand-curated questions; commit to CI; fail builds on > 3% regression
- Faithfulness ≠ correctness. High faithfulness only means the answer is grounded in retrieved chunks; pair with context precision to catch retrieval errors
- Citation-grounded eval is the direction serious systems are moving. Every claim must trace to a retrieved chunk; required for regulated industries
#Quick Check
Your RAG system has faithfulness=0.95 but users say the answers are wrong. What's the diagnosis?