Skip to content

RAG pipeline end to end

The six stages of retrieval-augmented generation and where each one most often fails.

8 MIN READUPDATED 2026-11-18RAG & knowledge
ORIGINAL DIAGRAM · RETRIEVAL FLOWQUERYuser inputEMBEDvectorVECTOR STOREchunksRERANKtop-kLLMgroundedCITED ANSWERsource-linked

Original rag diagram — not a copied image

§ 00Notes & rules
Separate retrieval from generation
If answers are wrong, measure recall first. A perfect prompt cannot fix evidence that was never retrieved; better retrieval cannot fix a model that ignores supplied sources.
Ground and cite
Instruct the model to answer only from retrieved passages, cite chunk IDs, and return 'insufficient evidence' when support is missing.
§ 01Key concepts
RAGRetrieving relevant content and putting it into the prompt so the model answers with evidence.
IngestionConverting source documents into chunks, vectors and metadata.
RetrievalFinding candidate passages for a query.
GenerationProducing an answer grounded in the retrieved passages.
§ 02When to use
  • Answers must cite sources or use private knowledge.
  • Knowledge changes faster than model training cycles.
  • You need traceability from claim back to document.
§ 03How it works
  1. 01Ingest: parse documents, preserve structure and metadata.
  2. 02Chunk: split into passages that stand alone.
  3. 03Embed and index with stable IDs and filters.
  4. 04Retrieve candidates using keyword + vector search.
  5. 05Rerank to a small set and pack into the context.
  6. 06Generate with an instruction to cite sources and abstain when unsupported.
§ 04Use cases & scenarios

Support assistant on current policies

Context
Customers ask about return windows and shipping rules.
Problem
Answers are plausible but quote outdated policy text.
Approach
Ingest versioned documents, retrieve with hybrid search, rerank and cite only supported claims.
Outcome
Answers match current policy or explicitly abstain when evidence is missing.

Onboarding over private documents

Context
New employees ask questions about internal handbooks.
Problem
General models have no access to private procedures.
Approach
Index handbook passages with access metadata and keep citations in the answer.
Outcome
Employees get traceable answers without exposing documents to other tenants.
§ 05Examples

Grounded generation prompt (shape)

text
System: Answer using ONLY the provided sources. Cite source IDs like [chunk:...].
If the sources do not support the answer, say: "I don't have enough evidence."

Sources:
[chunk:policy-returns-3] Returns accepted within 30 days with receipt...
[chunk:policy-shipping-2] International orders ship in 5-10 business days...

Question: Can I return an international order after 2 weeks?

Keep evidence delimited and labeled so it is not confused with instructions.

Minimal retrieval config

yaml
retrieval:
  mode: hybrid        # keyword + vector
  top_k: 20
  rerank_top_n: 20
  final_passages: 5
  filters:
    tenant_id: required
    language: auto
generation:
  cite_sources: true
  abstain_on_low_evidence: true
§ 06Trade-offs

Large chunks

Up: More context per hit

Down: Diluted relevance, higher cost

Many retrieved chunks

Up: Higher recall

Down: More noise and slower generation

Strict citations

Up: Verifiable answers

Down: More abstentions on ambiguous queries

§ 07Implementation checklist
  • Preserve document titles and section paths in each chunk.
  • Keep a unique chunk ID for traceability.
  • Evaluate retrieval separately from generation.
  • Handle the no-evidence case explicitly.
§ 08Configuration reference
KeyTypical valueNote
chunk_size300–800 tokensTune on your documents.
top_k10–20 before rerankThen keep 3–6.
no_answer_thresholdtask-specificTriggers abstention.
§ 09Troubleshooting
SymptomCauseFix
Answers cite the wrong sourceWeak chunk IDs or metadataCarry source IDs through to generation.
Retrieval misses obvious documentsParsing failed or chunks too largeInspect parsed output and reduce chunk size.
§ 10Frequently asked questions

Why does RAG still hallucinate?

Retrieval can return irrelevant content and models can ignore it. Measure both stages separately.

Should I store vectors in the prompt?

No. Store them in an index; put only the retrieved text in the prompt.

More in RAG & knowledge