The RAG lifecycle and a measured baseline
- Source
- From the RAG & Agents Engineering course
- By
- Sourav Banerjee
- Last updated
- 9 Nov 2026
Indexing vs retrieval vs generation
What you'll learn
- Explain the seven lifecycle steps, the job of each one and its typical failure.
- Diagnose whether a wrong answer is a retrieval failure or a generation failure with one check.
- Measure a plain pipeline on a frozen golden set and record its baseline row.
- Choose whether to keep a change by comparing it with that row, question by question.
Lessons 2 to 4 of T5.1 built the pieces one at a time: an index, a retriever, a vector store. This lesson joins them into one pipeline and gives it a number to beat.
The refund question is still answered wrongly. A teammate spends an afternoon on the prompt: stricter wording, an instruction to quote the handbook, a worked example. The answer does not change, because in the baseline index the refund-timing chunk never reaches the top two. The model cannot quote a passage it never received.
The lifecycle has seven steps in two phases. Each step has one job and one typical failure:
| Phase | Step | Job | Typical failure | Fixed in |
|---|---|---|---|---|
| Indexing | Load | Turn files into Documents with metadata |
Garbled PDF text, missing metadata | T5.2 |
| Indexing | Split | Cut documents into retrievable chunks | A fact split across two chunks | T5.3 |
| Indexing | Embed | Turn each chunk into a vector | Model cannot tell paraphrases apart, or blurs exact codes | T5.4 |
| Indexing | Store | Index vectors for nearest-neighbour search | Chunk count ≠ vector count; no metadata filters | T5.5 |
| Query | Retrieve | Return the top-k chunks with scores | Evidence ranked below the cut-off, or not at all | T6.1 to T6.3 |
| Query | Augment | Format the chunks into the prompt | Sources dropped, so no citations | T6.4 |
| Query | Generate | Answer from the supplied context | Ignores the evidence, or answers beyond it | T6.4, T9.1 and T9.2 |
Table 1: The seven lifecycle steps, with the module that fixes each step's typical failure.
When an answer is wrong, one question tells you which half to debug:
Figure 1: The one check that splits retrieval failures from generation failures.
Tuning the prompt cannot fix a retrieval failure, because the model never saw the evidence. We call that the wrong-half fix: effort spent on the half of the pipeline that did not fail. The notebook prints each retrieved chunk with its score for exactly this check. Applied to the three Larkspur questions at the baseline:
| Question | Evidence in the top two | Verdict | Where to work |
|---|---|---|---|
| Can I export my data after I cancel my plan? | Both chunks | Any wrong answer is a generation failure | Prompt, context order, model |
| How long does a refund take? | refund-window only; refund-timing scores 0 |
Retrieval failure for the timing half | T5.4 embeddings, T6.1 hybrid search |
| What happens if my server goes down? | Neither chunk | Retrieval failure | T5.4 embeddings, T6.2 query transformation |
Table 2: The check applied to the TF-IDF baseline at k = 2. The teammate's prompt work targeted the refund row, where generation was never the problem.
Baseline-first optimisation
Every later module in this course changes one stage of the pipeline: chunking, embeddings, hybrid search, reranking, query rewriting. Without a baseline you cannot tell whether a change helped, hurt, or moved the failure to another question.
The method has four parts:
- Fix a golden set. 20 to 50 real questions, each with its evidence chunks and a reference answer. Freeze it, and add questions when production shows a new failure.
- Build the plain pipeline. Default loader, recursive splitter, one embedding model, top-k similarity, a simple prompt.
- Record the baseline row. Retrieval metrics (recall@k, hit rate, MRR), answer metrics (correctness, faithfulness), latency and cost per question.
- Change one stage, re-run, compare. Keep the change only if the row improves on the questions it targets without breaking the others.
Hit rate is the share of questions with at least one evidence chunk in the top k. MRR (mean reciprocal rank) averages 1 divided by the rank of the first evidence chunk, so it rewards evidence that ranks high. The loop then runs for as long as there are changes to try:
- Freeze a golden set. The lab’s has three questions, each with two evidence chunks.
- Run the plain pipeline over it and record the baseline row: recall@2 0.50, precision@2 0.50.
- Change one stage. Here, raise k from 2 to 5.
- Re-run the golden set: recall@5 0.50, precision@5 0.20.
- Compare with the baseline. Recall did not move and precision fell, so revert.
- A change that beats the row on the questions it targets, and breaks none, is kept and becomes the new baseline.
Figure 2: The baseline loop. A change that wins on the mean but breaks a question goes back for rework.
For the handbook, the top-k lab gives the retrieval half of the row:
| Metric | Baseline (TF-IDF, k = 2) |
|---|---|
| Mean recall@2 | 0.50 |
| Mean precision@2 | 0.50 |
| Questions fully answered by retrieval | 1 of 3 (export) |
Table 3: The Larkspur baseline row. Answer metrics, latency and cost join it once the notebook runs generation.
That row already points at the next modules. The export question is solved, the refund question needs better term matching (T5.4, T6.1), and the outage question needs paraphrase-aware retrieval (T5.4, T6.2). The lifecycle notebook ends with the map from symptom to module:
| What you observe | Likely cause | Where it is fixed |
|---|---|---|
| Question worded unlike the documents; evidence never retrieved | Vocabulary mismatch | T5.4 embeddings, T6.2 query transformation |
Exact codes or names missed (ORD-88213) |
Dense retrieval blurs rare tokens | T6.1 hybrid search |
| Half an answer; one fact found, another missed | Question needs several chunks | T6.2 query decomposition |
| Evidence retrieved but ranked low | Ranking noise | T6.3 reranking |
| Retrieved chunk holds half the fact | Chunk boundary | T5.3 chunking |
| Answer contradicts the retrieved chunk | Generation | T6.4 per-claim citations, T9.2 Self-RAG |
Table 4: Symptom to module. Each later module is a fix for one of these rows.
In practice: the first change against the Larkspur baseline
Lesson 3 of T5.1's switch to embeddings is the first change to run through the loop:
- Re-run the golden set with embeddings at
k = 2. - Compare the mean. Recall@2 rises from 0.50 to 0.67, and precision@2 from 0.50 to 0.67.
- Compare each question. Refund rises from 0.50 to 1.00 and outage from 0.00 to 0.50, while export falls from 1.00 to 0.50.
- Decide. The change broke a question it was not aimed at. The loop says revert, or rework it (raise
kto 3, or add the exact-word signal back with hybrid search in T6.1) and run the golden set again.
Interview tip: when someone reports a mean improvement, ask for the per-question rows. A better average can hide a question type the change broke.
Run the notebooks
The code for this lesson is in these notebooks. Colab runs one in your browser: install the packages its first cell imports and set the API keys it reads. To run every notebook as written, clone the repository and use its uv environment.
| Notebook | GitHub | Colab |
|---|---|---|
| RAG lifecycle and baseline | Open on GitHub | Open in Colab |
Key Takeaways
- RAG runs in two phases: indexing (load, split, embed, store) and query (retrieve, augment, generate). Each step has one job and one typical failure.
- Debug a wrong answer by printing the retrieved chunks first. If the evidence is missing, no prompt change can help.
- Freeze a golden set of 20 to 50 questions, record a baseline row, and keep a change only when it helps its target questions without breaking others.
Check yourself
1. An answer is wrong. What one check tells you whether to debug retrieval or generation?
Print the retrieved chunks and look for the correct passage. If it is missing, retrieval failed: fix loading, splitting, embedding, k or the query. If it is there, generation failed: fix the prompt, the context order or the model.
2. A teammate rewrites the prompt and the refund question is still wrong. The refund-timing chunk never appears in the top-k. Why did the rewrite not help?
The model never saw the evidence, so no prompt can make it use that evidence. This is a retrieval failure, and the prompt work is a wrong-half fix. The refund question needs better term matching (T5.4 embeddings, T6.1 hybrid search), measured against the golden set.
3. Switching to embeddings lifts mean recall@2 from 0.50 to 0.67 but drops the export question from 1.00 to 0.50. Do you keep it?
Not as it stands. The loop keeps a change only when the row improves on the questions it targets without breaking the others. Revert it, or rework it (a larger k or hybrid search) and re-run the golden set before it becomes the new baseline.