Skip to content

What your RAG pipeline actually retrieves

Chunking, embeddings, re-ranking — and the five failure cases worth logging before you blame the model.

NZNarjis Zekhnini29 Oct 20263 min readDrafted with Inky · reviewed by Narjis

Retrieval-augmented generation (RAG) answers questions from your own documents: find the relevant passages, put them in the prompt, and let the model write an answer grounded in them. When answers are wrong, the model is often blamed. More often, retrieval handed it the wrong context.

The pipeline

  1. Chunk documents into passages.
  2. Embed each chunk and store the vectors.
  3. At question time, embed the query and fetch the top-k most similar chunks.
  4. Optionally re-rank them with a more precise model.
  5. Generate an answer from the question plus the retrieved chunks, with citations.

Log what you retrieve

The single most useful habit: save the retrieved chunks with every answer.

def answer(question, k=5):
    hits = vector_index.search(embed(question), k=k)   # [(score, chunk), ...]
    context = "\n\n".join(f"[{i+1}] {c.text}" for i, (_, c) in enumerate(hits))
    prompt = (
        "Answer using only the sources below. Cite them like [1]. "
        "If the answer is not in the sources, say you don't know.\n\n"
        f"Sources:\n{context}\n\nQuestion: {question}"
    )
    reply = llm(prompt)
    log({"question": question, "hits": [(s, c.id) for s, c in hits], "reply": reply})
    return reply

Five failure cases to look for

  1. The answer was split across chunks. A table header in one chunk and its rows in the next. Fix: larger or overlapping chunks, or chunk by document structure (headings, sections).
  2. The right chunk ranked 7th. It was retrieved but cut off by k=5. Fix: retrieve more (20–50), then re-rank down to 5.
  3. Keyword-only facts. Product codes, names and error messages embed poorly. Fix: hybrid search that combines embeddings with keyword (BM25) scores.
  4. Stale or duplicate documents. Three versions of the same policy compete. Fix: metadata filters (date, status) and de-duplication at indexing time.
  5. The question needs reasoning, not lookup. "Which plan is cheapest for 12 users?" needs several chunks and a calculation. Fix: let the model ask follow-up searches (an agent loop) or pre-compute structured data.

Measure retrieval separately

Build a small set of questions where you know which document holds the answer. Track recall@k: how often the right chunk appears in the top k. If recall is low, no prompt will save you.

Takeaways

  • Most RAG errors are retrieval errors.
  • Log retrieved chunks with every answer.
  • Retrieve wide, re-rank narrow, and use hybrid search for exact terms.

Keep reading

Get the next tutorial by email

One email a week. No spam.

What your RAG pipeline actually retrieves · Tokenfield