Topic 5 of 5 · 5 min read
Anatomy of a retrieval pipeline end to end
Goal: Label each stage of a given RAG pipeline diagram or log and point to the stage responsible for a described wrong answer.
A RAG system is a chain of stages, and a wrong answer almost always comes from one specific stage; find that stage before you change anything. Tuning the prompt when the right passage was never retrieved wastes days.
Check yourself · 1 of 3
A pricing table was split in the middle, so no chunk holds the full table. Which stage is at fault?
Why it matters
Retrieval-augmented generation, introduced by Lewis and colleagues in 2020, pairs a retriever with a generator. In a product that becomes seven or eight stages built by different people. When an answer is wrong, you need to know which stage to open.
The stages
- Ingestion: load files, pages and tickets, and strip noise such as menus and footers. Failure: scanned PDFs with no text, duplicated pages.
- Chunking: split text into pieces. Failure: a table or a rule cut in half, so no chunk holds the full answer.
- Embedding and indexing: turn chunks into vectors and store them with metadata (source, date, permissions). Failure: the wrong or an outdated embedding model, missing metadata.
- Query handling: clean or rewrite the user's question. Failure: a follow-up like "and for contractors?" searched on its own with no context.
- Retrieval: fetch the top candidates by vector, keyword or both. Failure: exact terms such as error codes missed by pure vector search.
- Reranking: reorder candidates with a stronger model. Failure: the best chunk sits at rank 14 and is cut off.
- Generation: the model answers from the supplied chunks. Failure: it ignores them or fills gaps from memory.
- Citation: link each claim to a source. Failure: a citation to a chunk that does not support the claim.
Before and after
Here is a trace from a support bot that answered "30 days" when the policy says 14.
query_in: "refund window for annual plans?"
rewrite: "refund window annual plans"
retrieve k=5: [policy_v1.md#3 (0.81), policy_v1.md#4 (0.79), faq.md#9 (0.74),
policy_v2.md#3 (0.73), blog.md#2 (0.70)]
rerank top 3: [policy_v1.md#3, policy_v1.md#4, faq.md#9]
answer: "Annual plans can be refunded within 30 days." [policy_v1.md#3]
The generator did its job: it quoted a retrieved chunk. The fault is earlier. The old file policy_v1.md is still in the index (an ingestion and indexing problem), and the current chunk policy_v2.md#3 ranked fourth and was dropped. Rewriting the prompt would change nothing. Removing the stale file, and adding a document date as metadata, would.
What goes wrong
- Debugging only the last stage. Look at what was retrieved before blaming the model.
- No logs between stages. If you cannot see the chunks that went in, you cannot diagnose the answer.
- Changing several stages at once. You will not know what helped.
- Measuring only the final answer. Also measure whether the right chunk was retrieved at all.
Examples
Cut in half at chunking
A pricing table was split after row 6 of 12. A question about row 9 retrieved the header chunk and the model guessed. Fix the chunker to keep tables whole or repeat the header with each piece.
Missed at retrieval
A user searches for error code E-4471. Vector search returns pages about related errors, but not the page with that exact code. Add keyword search alongside vectors and merge the results.
Key points
- A RAG pipeline has distinct stages: ingestion, chunking, indexing, query handling, retrieval, reranking, generation and citation.
- A wrong answer usually traces to one stage, and the trace of retrieved chunks shows which one.
- If the right passage was never retrieved, changing the generation prompt cannot fix the answer.
- Stale or duplicated documents in the index produce confident answers from outdated sources.
- Log the query, the rewritten query, the retrieved chunks with scores and the final context so every answer can be diagnosed.
Common mistakes
- Rewriting the generation prompt when the right chunk was never retrieved.
- Leaving old document versions in the index.
- Keeping no per-stage logs, so wrong answers cannot be traced.
- Evaluating only the final answer and not retrieval separately.
Check yourself · 2 of 3
The right paragraph was in the index but not in the top 5 results, and the answer was wrong. What should you inspect first?
Check yourself · 3 of 3
The trace shows the model quoted an outdated policy chunk faithfully. Which fix addresses the cause?
Practice items, not counted towards your mastery.
Start here
- Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (opens in a new tab)PaperFree
The paper that named RAG: a model that reads retrieved passages instead of relying on memory alone.
- Retrieval Augmented Generation or Long-Context LLMs? A Comprehensive Study and Hybrid Approach (opens in a new tab)PaperFree
Measures long context against RAG on the same tasks and shows the cost gap that a hybrid router exploits.
- Optimizing LLM accuracy (OpenAI) (opens in a new tab)DocsFree
Frames the choice as context problems versus behaviour problems: RAG for the first, fine-tuning for the second.
More for this topic
- Embeddings (Claude docs) (opens in a new tab)DocsFree
How text becomes vectors for similarity search, the retrieval step at the centre of a RAG pipeline.