Skip to content
mentoor

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?

Choose one answer

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

  1. Ingestion: load files, pages and tickets, and strip noise such as menus and footers. Failure: scanned PDFs with no text, duplicated pages.
  2. Chunking: split text into pieces. Failure: a table or a rule cut in half, so no chunk holds the full answer.
  3. 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.
  4. Query handling: clean or rewrite the user's question. Failure: a follow-up like "and for contractors?" searched on its own with no context.
  5. Retrieval: fetch the top candidates by vector, keyword or both. Failure: exact terms such as error codes missed by pure vector search.
  6. Reranking: reorder candidates with a stronger model. Failure: the best chunk sits at rank 14 and is cut off.
  7. Generation: the model answers from the supplied chunks. Failure: it ignores them or fills gaps from memory.
  8. 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?

Choose one answer

Check yourself · 3 of 3

The trace shows the model quoted an outdated policy chunk faithfully. Which fix addresses the cause?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic