RAG Evaluation Metrics: Precision@K, Recall@K, MRR, NDCG@K & Faithfulness
How to evaluate and debug production RAG systematically: Precision@K, Recall@K, Mean Reciprocal Rank (MRR), graded NDCG@K with logarithmic position discount, and the RAG Triad (Context Relevance, Faithfulness & Answer Relevance).
RAG Evaluation separates retrieval quality (Precision@K, Recall@K, MRR, NDCG@K) from generation quality (Faithfulness and Answer Relevance).
If a RAG bot gives a wrong answer, you must know whether the retriever failed to fetch the right paragraph or the LLM ignored the retrieved evidence.
Never tune prompts when your Retriever has 40% Recall@5 — evaluating Retriever and Generator separately is the #1 secret of top RAG teams!
Tracking Recall@5, MRR, and NDCG@5 on a 100-question golden dataset lets you A/B test chunk sizes, embeddings, BM25+RRF, and rerankers in 30 seconds without even calling an LLM!
Pairing IR metrics with Faithfulness (checking that every claim in the LLM's answer is entailed by the retrieved chunks) catches subtle hallucinations automatically.
Guaranteed RAG interview question: 'How do you measure if a RAG failure is a retrieval bug or a generation bug, and when do you use NDCG@K instead of MRR?'
When should you prefer NDCG@K over MRR to evaluate your RAG retriever?
- Compute Precision@K, Recall@K, and Mean Reciprocal Rank (MRR) from retrieved vs. ground-truth document IDs
- Compute graded NDCG@K = DCG@K / IDCG@K using both gain (2^rel − 1) and position discount 1/log₂(rank + 1)
- Separate Retriever evaluation (offline IR metrics) from Generator evaluation (RAG Triad: Context Relevance, Faithfulness, Answer Relevance)
- Diagnose whether a bad RAG answer was caused by a Retriever miss vs. an LLM hallucination in seconds
Evaluating RAG is like grading an open-book exam: first check whether the student brought the right pages to the desk (Recall@K, MRR, NDCG@K). Then check whether their written answer actually stayed faithful to those pages instead of making up facts from thin air (Faithfulness) and actually answered the question asked (Answer Relevance)!
1. Binary Retrieval Metrics: Precision@K, Recall@K & MRR
Every RAG retrieval benchmark starts with these three fast, deterministic metrics.
Quick Definitions
Given retrieved list and ground-truth relevant set :
- Precision@K: — are we wasting LLM context tokens on junk?
- Recall@K: — did we retrieve all the facts needed to answer? (Most critical metric for Stage-1 retrieval!)
- MRR: where is the 1-indexed position of the first relevant document (
0.0if none in Top-K).
2. Graded Ranking Quality: NDCG@K & Logarithmic Discount
Click Rank #1 / #2 / #3 in the mini-lab below to see how moving a 3-star gold document down by just 1 slot slashes both MRR and NDCG@3!
The Exact NDCG@K Formula
where is the DCG of the ideal ranking (sorting all ground-truth relevances descending). If (no relevant docs exist), define !
DCG@K = Σ (2^rel_i − 1) / log₂(i + 1)
Strongly separates 3-star gold docs (gain=7) from 1-star weak hints (gain=1)!
[3, 0, 1] vs. ideal [3, 1, 0]: DCG = 7/1 + 0/1.585 + 1/2 = 7.50; IDCG = 7/1 + 1/1.585 + 0 = 7.631 → NDCG@3 = 0.983!Move the Gold Doc (Rel = 3) to See MRR & NDCG@3
3. End-to-End Diagnostics: The RAG Triad
Use the RAG Triad (popularized by TruLens & RAGAS) to isolate every production bug cleanly.
Pinpointing the Exact Broken Component
- Context Relevance (Query ↔ Retrieved Chunks): Low? → Retriever bug! Fix chunking, add Hybrid BM25+RRF, or add Cross-Encoder reranking.
- Faithfulness / Groundedness (Retrieved Chunks ↔ LLM Answer): Low? → Hallucination bug! Tighten system prompt (
"Answer ONLY from context; say I don't know if missing"), lower temperature, or add inline citations. - Answer Relevance (Query ↔ LLM Answer): Low despite good context? → Generator / Prompt bug!
Worked Example: Computing P@4, R@4 & MRR
Ground-truth relevant docs G = {'D2', 'D5', 'D9'}. Retrieved Top-4: ['D1', 'D5', 'D8', 'D2'].
Hits = {'D5', 'D2'} → 2 hits.
Your RAG system has Context Relevance = 0.94 and Answer Relevance = 0.88, but Faithfulness = 0.35. What is happening?
Why does increasing K from Top-5 to Top-50 always increase (or maintain) Recall@K while usually DECREASING Precision@K?
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
Use Synthetic Data Generation: sample 100 diverse chunks from your corpus, prompt a frontier LLM to generate realistic user questions answerable only from each chunk, and have a domain expert quickly review/filter them!
RAG Failure-Mode Doctor (RAG Triad Diagnostics)
Switch between production failure modes to diagnose whether the Retriever or the Generator is broken.
Automated RAG CI/CD Regression Gates
Blocks deployments automatically if Recall@5 or Faithfulness drops by >2% on the golden eval suite.
import math
# Core RAG Retrieval Metrics: Precision@K, Recall@K, MRR & NDCG@K
def eval_retrieval(retrieved: list[str], relevant: set[str], k: int = 4) -> dict:
top_k = retrieved[:k]
hits = [1 if d in relevant else 0 for d in top_k]
precision = sum(hits) / k
recall = sum(hits) / max(1, len(relevant))
mrr = next((1.0 / (i + 1) for i, h in enumerate(hits) if h), 0.0)
dcg = sum(h / math.log2(i + 2) for i, h in enumerate(hits))
idcg = sum(1.0 / math.log2(i + 2) for i in range(min(k, len(relevant))))
return {"P@K": round(precision, 2), "R@K": round(recall, 2), "MRR": round(mrr, 2), "NDCG@K": round(dcg / max(1e-9, idcg), 2)}
print(eval_retrieval(["d9", "d2", "d7", "d1"], relevant={"d1", "d2", "d5"}, k=4))
IDCG@K = 0).0.0 whenever idcg == 0!