Evaluating and debugging RAG

The end-to-end pipeline, separate metrics for retrieval and generation, a debugging method that isolates the failing stage, and hallucination containment.

L4 · Applied AI engineer≈ 5 min read
Key points · the two-minute version
  1. Q1: ingest → metadata → chunk → embed → index → query auth → hybrid retrieve → re-rank → assemble → generate with citations → validate → evaluate.
  2. Q2 debug: paste the correct context. If the answer is now right, retrieval failed; if still wrong, generation failed.
  3. Measure separately. Retrieval uses recall@k, precision@k, MRR, nDCG; generation uses faithfulness, relevance, citation correctness.
  4. Log the chunk IDs and scores behind every answer.
  5. "You don't eliminate hallucination. You engineer containment."
You should be able to
  • Walk through a production RAG pipeline end to end
  • Measure retrieval and generation separately
  • Debug a wrong answer to its stage in minutes

Why this matters

"Walk me through a RAG pipeline" and "it gives wrong answers; how do you debug?" are the two most common RAG interview questions. In production, they're the daily work.

Q1. Walk me through a RAG pipeline end to end.

  1. Ingest: connectors; type and size checks; layout-aware parsing (faxes and PDFs with tables break naive extraction); sensitive-data handling before anything is indexed.
  2. Normalize + metadata: headings, pages, section path, version, tenant, ACL labels.
  3. Chunk: structure-aware first; parent-child when context matters.
  4. Embed: batched; record the embedding model with every vector; a model change means a full re-embed.
  5. Index: vectors + lexical index + metadata.
  6. Query: authenticate, resolve tenant and permissions, optionally rewrite.
  7. Retrieve: mandatory permission filters; hybrid (dense + lexical) fused with RRF.
  8. Re-rank: top-50 → top-5.
  9. Assemble: token budget, source IDs attached.
  10. Generate: answer only from evidence, cite chunk IDs, explicit insufficient-evidence path.
  11. Validate: schema; every cited ID exists in the retrieved set; business rules.
  12. Evaluate: retrieval and generation separately, in CI and on sampled traffic.

Q2. It gives wrong answers. How do you debug?

Paste the correct context manually: now right → retrieval problem; still wrong → generation problem. Retrieval: chunk boundaries, embedding mismatch, filters, stale index, exact terms missed by dense search. Generation: prompt, context order (lost in the middle), model tier. Log chunk IDs and scores per answer. Rerun the eval suite after each change.

Symptom Likely cause Inspect
Right answer never appears Retrieval miss Recall@k, chunking, filters
Wrong source Similar but irrelevant Precision@k, re-rank, metadata
Right source, wrong answer Generation Prompt, order, model
Another tenant's data Missing filter Server-side filters, RLS, cache keys
Stale answer Stale index Versioning, deletion flow
High cost Too much context Top-k, re-rank cutoff, caching

Q7. How do you evaluate RAG?

Layer Metrics
Retrieval Recall@k, Precision@k, MRR, nDCG; RAGAS context precision and recall
Generation Faithfulness, answer relevance, citation correctness
Product Corrections, acceptance, edit distance on drafts

Golden set with expert answers; run on every prompt, model or chunking change; calibrate LLM-as-judge against humans (10.2).

Q10. Hallucination?

Say: "You don't eliminate hallucination. You engineer containment."

Grounding with citations, schemas, citation verification, an explicit insufficient-evidence path, faithfulness in evals, human review for consequential outputs, sources visible in the UI. Structural containment when possible: constrain the answer space so a fabricated value can't be expressed (S4).

Common mistakes

  • One end-to-end "accuracy" number that can't tell you which stage broke.
  • No logging of which chunks fed each answer.
  • Evaluating on questions written by the same person who wrote the prompt.

Check yourself

How do you tell a retrieval failure from a generation failure in one step?

Give the model the known-correct context directly. If the answer becomes right, retrieval was the problem; if it's still wrong, the prompt, context order or model is.

Name three retrieval metrics and three generation metrics.

Retrieval: recall@k, precision@k, MRR, nDCG. Generation: faithfulness, answer relevance, citation correctness.

Build it

Create a 30-question golden set for your RAG project with expected source chunks and reference answers. Report recall@5, MRR and faithfulness, then change chunk size and report again.

What comes next

8.6 Advanced RAG.

Build it to remember

Two hands-on projects that exercise this lesson. Pick one, build it end to end, and keep it in your portfolio.

Project 1

RAG eval harness

A golden set of 50 questions with expected chunks and answers; CI fails if recall@5 or faithfulness drops below baseline.

  • Python
  • RAGAS or DeepEval
  • pytest
  • GitHub Actions

Practises Evaluation as a release gate.

Project 2

RAG debugger UI

For any answer, show the retrieved chunks, scores, prompt and citation checks, plus a button to rerun with "known-good context".

  • Next.js
  • FastAPI
  • Langfuse

Practises Fast stage-level debugging.

Can you answer every check-yourself question out loud without opening the answer?