Evaluating a RAG System: Measure Retrieval and Generation Separately

Learn why measuring retrieval and generation as one number hides real problems — and how to build separate metrics that actually tell you where your RAG pipeline is breaking.

The Single Score That Lies to You

When I first shipped a RAG pipeline, I thought "does the answer look right?" was a good enough test. I'd ask a question, read the response, nod approvingly, and move on. It worked — until it didn't. Users started reporting wrong answers on specific topics, and I had no idea where the failure was happening.

Was the retrieval pulling the wrong chunks? Was the LLM ignoring the right chunks? Was it hallucinating details that weren't in the retrieved context at all? I had one number — a vague "quality score" I'd eyeballed — and it couldn't tell me any of that.

That's the trap with RAG evaluation. A single end-to-end score hides the bug. You need to split the pipeline in two and measure each half independently.

Why RAG Has Two Very Different Failure Modes

A RAG pipeline does two distinct jobs. First, it retrieves: it searches your vector store and pulls back the chunks most relevant to the user's question. Second, it generates: it hands those chunks to an LLM and asks it to produce an answer grounded in what it found.

These two jobs can fail completely independently:

  • Retrieval failure: The right document exists in your database but never surfaces. The LLM then gets handed garbage context and does the best it can — which won't be good enough.
  • Generation failure: The retrieval is perfect — exactly the right chunks come back — but the LLM ignores them, adds invented details, or misinterprets what it was given.

If you only measure the final answer quality, both failure modes look identical: bad answers. You can't fix what you can't locate.

Metrics for the Retrieval Half

Retrieval quality is about whether the right context showed up at all. Here are the metrics worth tracking:

Hit Rate (Recall@K): For a given question, did any of the top-K retrieved chunks contain the answer? This is binary — either the answer was retrievable or it wasn't. If your hit rate at K=5 is 60%, that means 40% of the time the LLM never had a chance.

Mean Reciprocal Rank (MRR): Hit rate tells you if the right chunk appeared. MRR tells you where. If the correct chunk is always ranked 5th out of 5, your model is technically "hitting" but burying the good stuff under noise.

Context Relevance: Even if chunks contain the answer, are the other retrieved chunks just noise? A noisy context window confuses the LLM. Score each retrieved chunk for relevance to the query independently.

python
# Simple hit rate calculation
def hit_rate(retrieved_chunks, ground_truth_chunk_id):
    retrieved_ids = [c["id"] for c in retrieved_chunks]
    return ground_truth_chunk_id in retrieved_ids

# MRR: reward higher-ranked correct results
def reciprocal_rank(retrieved_chunks, ground_truth_chunk_id):
    for i, chunk in enumerate(retrieved_chunks):
        if chunk["id"] == ground_truth_chunk_id:
            return 1 / (i + 1)
    return 0

To run these metrics, you need a small eval set where you know which chunk should be retrieved for each question. Building that set is a one-time investment that pays off every time you tweak your embedding model or chunking strategy.

Metrics for the Generation Half

Once you've confirmed retrieval is working, you can isolate the generation layer. The key distinction here: you're not asking "is the answer good?" — you're asking "is the answer faithful to what was retrieved?"

Faithfulness / Groundedness: Does every claim in the generated answer trace back to something in the retrieved context? This is the hallucination check. You can score it manually or use an LLM-as-judge approach where you ask a second model to verify each claim against the source chunks.

Answer Relevance: Does the answer actually address what the user asked? A perfectly grounded answer that talks about the wrong thing is still a failure.

Context Utilization: Did the LLM use the good chunks you retrieved, or did it latch onto a less relevant chunk and ignore the useful one? This catches prompt and instruction issues.

python
# LLM-as-judge faithfulness check (pseudo-code)
faithfulness_prompt = """
Given the following context chunks:
{context}

And the following generated answer:
{answer}

List any claims in the answer that cannot be
verified from the context. Reply with 'NONE'
if all claims are grounded.
"""


response = llm.complete(faithfulness_prompt.format(
    context=retrieved_chunks,
    answer=generated_answer
))
# Parse response to get faithfulness score

Use a Stronger Judge Model

If your RAG pipeline uses GPT-3.5 or Claude Haiku for generation, use GPT-4o or Claude Sonnet as the judge. A weaker model judging its own outputs tends to be overconfident. The asymmetry matters.

A Practical Eval Table to Keep

Here's the structure I actually use when I'm iterating on a RAG pipeline. I log every experiment in a simple table so I can see at a glance which half of the pipeline changed:

markdown
# RAG Eval Log

| Experiment | Hit@5 | MRR | Faithfulness | Relevance |
|---------------------|-------|------|--------------|-----------|
| Baseline | 0.62 | 0.48 | 0.71 | 0.80 |
| Chunk size 256→512 | 0.74 | 0.61 | 0.73 | 0.81 |
| Reranker added | 0.74 | 0.79 | 0.74 | 0.82 |
| Better system prompt| 0.74 | 0.79 | 0.91 | 0.88 |

See what that shows? The first change (bigger chunks) improved retrieval but barely touched generation. Adding a reranker improved where the right chunk ranked without changing hit rate. The final system prompt change fixed faithfulness — a purely generation-layer problem. If I'd only tracked one end-to-end quality score, I'd have no idea what was actually working.

Tools That Can Help You Get Started Fast

You don't have to build all of this from scratch. A few frameworks have done the heavy lifting:

RAGAS is the most popular open-source RAG evaluation library. It implements faithfulness, answer relevance, context precision, and context recall out of the box. You hand it your questions, retrieved contexts, and generated answers, and it spits out scores for each dimension separately.

terminal
pip install ragas
→ Installs RAGAS and its dependencies

# Then in your eval script:
from ragas import evaluate
from ragas.metrics import faithfulness, answer_relevancy, context_recall

TruLens and DeepEval are solid alternatives with slightly different APIs. If you're already using LlamaIndex, it has built-in eval modules that slot into your existing pipeline with minimal setup.

Start With 20 Questions

You don't need hundreds of test cases to get signal. Build a hand-curated set of 20 representative questions — a mix of easy, hard, and edge cases — and run your eval suite against that. Twenty well-chosen questions will catch 80% of your real bugs.

The Debugging Workflow This Unlocks

Once you have separate metrics, debugging becomes almost mechanical. Users reporting bad answers? Check your faithfulness score first — if it's low, the LLM is fabricating. If faithfulness is fine but hit rate is low, the right documents aren't surfacing. If hit rate is high but MRR is low, your reranker needs work.

When I added this two-layer eval setup to my pipeline, the first thing I discovered was that my hit rate was actually decent (around 70%), but my faithfulness score was terrible — sitting around 0.5. The LLM was getting the right context and then ignoring half of it. The fix wasn't in my vector database at all; it was a system prompt problem. I'd been optimizing the wrong half of the pipeline for weeks.

That's exactly the kind of insight a single end-to-end score will never give you. Split the measurement, find the real bug, fix the right thing.

Keep going

More tutorials in this category, or explore the full field guide.

More AI Coding Tutorials Official Docs ↗