▶ Data-flow diagram — self-contained HTML, opens in a browser — no install, no build
Situation
A RAG pipeline is easy to demo on one lucky question and almost impossible to measure. You cannot tell where quality is lost — in document retrieval or in answer generation — or whether the result reproduces at all without external services. Without an offline harness, any “it got better” stays an impression, not a number.
Task
I needed a RAG system built end-to-end, with each layer measured separately: retrieval metrics (recall@k, MRR, precision@k) over a labeled question set, and generation metrics (faithfulness, answer correctness). The harness has to run with zero API keys and stay unchanged when an LLM is plugged in.
Actions
The corpus is 12 short analytics concept docs (A/B testing, CUPED, uplift, retention, churn, RFM, funnel, LTV, attribution, cohort, DAU/MAU, SQL window functions), chunked and TF-IDF-indexed with 1–2-grams.
- Retrieval — cosine top-k over the TF-IDF matrix; dependency-light and fully offline. An embeddings backend can be dropped in without touching the evaluation code.
- Generation — a Claude model grounded in the retrieved context if a key is set; otherwise a deterministic extractive fallback returns the most relevant sentence from the top doc. Both paths return the same
Generationrecord, so evaluation is reproducible offline. - Evaluation — 20 labeled questions (
data/eval.jsonl) with gold doc ids and gold answers, plus an 11-question negative control (data/eval_hard.jsonl) whose gold doc sits outside the top 5. Retrieval: recall@k, MRR, MRR@all, precision@k. Generation: faithfulness (share of answer tokens grounded in the retrieved context) and answer correctness (Jaccard overlap with the gold answer).
| Metric | Easy (20 questions) | Control (11 questions) |
|---|---|---|
| recall@5 | 1.00 | 0.00 |
| MRR | 1.00 | 0.00 |
| MRR@all (full ranking) | 1.00 | 0.12 |
| precision@5 | 0.20 | 0.00 |
| faithfulness (invariant, extractive) | 1.00 | 1.00 |
| correctness | 0.37 | 0.07 |
Both blocks run the same metric code at the same k; only the questions differ.
Run
uv run --with scikit-learn --with pandas --with numpy python run.py query "How is the CUPED theta coefficient computed?"
uv run --with scikit-learn --with pandas --with numpy python run.py eval
# with a real LLM (requires ANTHROPIC_API_KEY):
uv run --with scikit-learn --with pandas --with numpy --with anthropic python run.py eval --llm
uv run --with scikit-learn --with pandas --with numpy --with pytest pytest -q
Per-question breakdown: reports/retrieval_per_question.csv, reports/generation_per_question.csv.
Result
Retrieval is perfect on this corpus: recall@5 and MRR = 1.00, the gold doc ranks 1 for all 20 questions — TF-IDF is enough at this scale and topic separation. The offline extractive baseline is grounded by construction — faithfulness ≡1.00 because the answer is taken from the context — and reaches 0.37 correctness by token overlap.
But 1.00 on its own proves nothing: on a saturated set the metric cannot fall, and “the harness measures” stays a claim. So there is a second, control set: 11 questions reworded without their gold doc’s vocabulary.
Every candidate was run through the live retriever and kept only if the gold doc genuinely landed outside the top 5 — at ranks 6–12. On the control set the same metrics read recall@5 = 0.00, while MRR@all (reciprocal rank over the full ranking, not truncated at k) reads 0.12 against 1.00: the metric separates a rank-6 near-miss from a rank-12 miss.
Faithfulness cannot see a retrieval failure: it stays 1.00 on the control set too, because the extractive answer is still a verbatim substring — now of the wrong document. Only correctness notices: 0.37 → 0.07.
The value is not the numbers themselves but that the harness separates the layers. With --llm and an API key, generation scores rise while retrieval metrics stay put, so you can see which layer to improve. It is the same principle as an A/B test: measure first, change second.
What I learned
A metric that cannot fail proves nothing. Perfect retrieval on an easy set is a property of the set; a control set built so the gold document is guaranteed to fall outside the window turns “the harness measures” from a claim into a fact.
The second lesson is that separating layers makes evaluation actionable. When retrieval and generation are scored apart, a drop points at the layer to fix; when they are collapsed into one score, you know quality moved but not where.
Discussion points
- Why a negative control set is needed. Without questions where retrieval is guaranteed to miss, a perfect score cannot be told apart from a metric that never moves.
- Why faithfulness is labelled an invariant. The extractive answer is copied from context by construction, so a perfect score measures the design, not the generation.
- Why retrieval is held fixed when the LLM is enabled. Holding one layer constant is what shows which layer the change came from.
- What I would do differently. Score on a larger corpus — the current perfect recall is a property of a small, well-separated set.
Limitations
Faithfulness here is a grounding proxy (answer tokens in the retrieved context), not a truth check: an answer can be grounded and still be wrong. Correctness by Jaccard is sensitive to wording — a low score is not always an error.
The corpus is small (12 docs), so recall@5 = 1.00 on the easy set is a property of this set, not a promise on real data. The control set shows the metric can fall, but it does not replace a measurement on a real corpus — grow the corpus and everything has to be recomputed.
With 12 docs and one gold per question, recall@5 and precision@5 are binary inside the window (a miss is 0.00, no partial credit), so the depth of a miss only shows in MRR@all and the gold ranks.