Skip to content
PHP2Shell: The PHP Bug
Akshay Thakare

RAG Pipeline Fundamentals

Ingestion, chunking, embeddings, retrieval and generation — the parts of a retrieval-augmented generation system and where each one fails.

RAGBeginner3 min readUpdated 11 Oct 2026

Why RAG

An LLM only knows what was in its training data. Retrieval-augmented generation fetches relevant passages from your data at question time and puts them in the prompt, so answers are grounded, current and citable — without retraining the model.

The two pipelines

FlowchartSwipe to explore →

Rendering flowchart

Follow one question through the online path:

RAG query path
Questionuser input
Embedquery vector
Retrievetop-k chunks
Re-rankcross-encoder
Generateanswer + citations

Each stage and its failure mode

StageKey decisionTypical failure
ParsingKeep structure (headings, tables, code)Tables flattened into nonsense
ChunkingSize, overlap, boundariesAnswer split across chunks
EmbeddingModel choice, normalisationDomain terms embedded poorly
RetrievalDense, keyword (BM25) or hybridExact identifiers missed by dense-only search
Re-rankingCross-encoder on top candidatesRelevant chunk ranked 9th, cut at k=5
GenerationPrompt with sources, instruction to cite and to say "I don't know"Confident answer not supported by context

A minimal implementation

Python.py
chunks = split_markdown(doc, max_tokens=400, overlap=60)
vectors = embed([c.text for c in chunks])
index.upsert(ids=[c.id for c in chunks], vectors=vectors,
             metadata=[{"source": c.source, "heading": c.heading} for c in chunks])
 
hits = index.query(embed([question])[0], top_k=8)
context = "\n\n".join(f"[{i}] {h.metadata['heading']}\n{h.text}" for i, h in enumerate(hits))
answer = llm(f"Answer using only the sources below. Cite as [n]. "
             f"If the answer is not in the sources, say so.\n\n{context}\n\nQ: {question}")

Measure it

Build a small evaluation set of real questions with known answers. Track retrieval recall (is the right chunk in the top k?) separately from answer faithfulness (is every claim supported by the retrieved context?). Most RAG quality problems are retrieval problems.

Finished this note?

Share