Skip to main content

The Problem

You have built a RAG-powered support bot for an insurance company. Users are complaining:
  • “The bot said my claim was covered, but it wasn’t”
  • “It gave me the wrong deductible amount”
  • “It pulled up completely irrelevant policy sections”
You need to figure out where the pipeline is failing. Is retrieval pulling the wrong documents? Or is the LLM hallucinating despite having the right context? These are two very different problems with two very different fixes. This cookbook evaluates each stage of the RAG pipeline separately so you know exactly what to fix.

What You Will Learn

  • How to measure retrieval quality with context recall, precision, and utilization
  • How to measure generation quality with faithfulness, groundedness, and answer relevancy
  • How to diagnose four common failure modes: good pipeline, hallucinating LLM, bad retrieval, noisy retrieval
  • How to run a RAG scorecard that tests all metrics at once

Prerequisites

No API keys required for the core metrics. An optional section at the end shows LLM-augmented scoring with Gemini.

Case A: Everything Works

Start with a well-functioning pipeline to establish a baseline. The retriever finds the right chunks, and the LLM generates a faithful answer.
Now measure retrieval and generation independently:
Expected output:
All scores are high. The pipeline is working correctly.

Case B: Good Retrieval, Bad Generation (Hallucination)

The retriever finds the right chunks, but the LLM invents facts not in the context.
Expected output:
Diagnosis: Retrieval is fine — the right documents were found. The LLM is hallucinating. Fix: Add a faithfulness check before sending the response. Use augment=True for higher accuracy.

Case C: Bad Retrieval, Faithful Generation

The retriever pulls completely wrong documents (dental coverage instead of physical therapy), but the LLM faithfully summarizes what it was given.
Expected output:
Diagnosis: Retrieval failure. The LLM was faithful to what it received, but the retrieved chunks had nothing to do with the question. Fix: Improve embedding model, add reranking, check chunk boundaries.
High faithfulness does not mean the answer is correct. It only means the answer matches the retrieved context. If retrieval is wrong, a “faithful” answer is still wrong. Always check both sides.

Case D: Noisy Retrieval

The retriever returns a mix of relevant and irrelevant chunks. Two out of four chunks are noise (holiday schedule, IT help desk).
Diagnosis: Retrieval is pulling in irrelevant documents. Fix: Increase similarity threshold, add metadata filtering, or add a reranker.

RAG Scorecard: All Metrics at Once

Run all metrics in a single batch call to get a quick health check of your pipeline.

RAG Debugging Checklist

What to Try Next

Now that you can diagnose RAG failures, protect your pipeline’s inputs from attacks.

Next: Guardrails

Build a sub-10ms security middleware that blocks jailbreaks, code injection, PII leaks, and secret exposure.