What problem does RAG solve? Walk through a RAG pipeline end to end.
Source: Lewis et al. 2020: Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks
A model does not know private or recent data and can answer confidently anyway. RAG retrieves relevant text at question time and puts it in the prompt, so the answer is grounded and can cite sources.
A model does not know private or recent data and can answer confidently anyway. RAG retrieves relevant text at question time and puts it in the prompt, so the answer is grounded and can cite sources. Offline: load documents, chunk them, embed the chunks and index them, keyword and vector. Online: embed the question, retrieve candidates with hybrid search, rerank, build a prompt with instructions and the top chunks, generate an answer with citations, and log the run for evaluation.
- 1Questionfrom the user
- 2Retrievevector + keyword, top 50
- 3Fuse + rerankkeep the best 5
- 4Promptrules + chunks + question
- 5Answergrounded, with citations
- 6Logtrace for evaluation
See the concept: Next-token predictionAn LLM writes one token at a time
Data pipelines must be idempotent so that a rerun never doubles revenue
Done: the model emitted an end token.
Key points
- private and recent data
- index path
- query path
- citations
- evaluation
Common mistakes
- Shipping with no evaluation set, so nobody knows whether retrieval or generation is failing.
- Letting the index go stale: documents change or are deleted, but the old chunks are still retrieved and cited.
- Making chunks too big, so the right sentence is buried in noise and the prompt fills with irrelevant text.
- Making chunks too small, so the answer is split across chunks and no single chunk contains it.
- Using vector search only, which often misses exact terms such as error codes, ids and product names.
- Not telling the model to answer only from the context, so it fills gaps from memory.
Best practices
- Build an evaluation set of real questions first, and measure retrieval and generation separately.
- Chunk along the document structure (headings, paragraphs) and keep metadata such as source, date and access rights.
- Use hybrid search (keyword plus vector) and a reranker, then send only the best few chunks.
- Re-index when documents change, and apply access control at retrieval time so users only see what they may read.
- Return citations and log every run (question, chunks, answer) so failures can be debugged.
Likely follow-ups
- Why not just use a long context window instead of RAG?
- How do you choose chunk size and overlap?
- How do you evaluate a RAG system?
- How do you tell a retrieval failure from a generation failure?
- How do you keep the index fresh and respect document permissions?
References
- Anthropic: Building effective agents
- Anthropic: Contextual Retrieval
- Claude docs: tool use overview
- Claude docs: handle tool calls
- Claude docs: reduce hallucinations
- Model Context Protocol: architecture
- Model Context Protocol: tools (specification)
- LangGraph: overview
- LangGraph: persistence (checkpointer vs store)
- LangGraph: interrupts
- LangSmith: observability concepts
- Ragas: available metrics
- Ragas: faithfulness
- Ragas: context precision
- Ragas: context recall
- pgvector README (HNSW, IVFFlat, distances, hybrid search)
- Sentence-Transformers: retrieve & re-rank
- Elasticsearch: reciprocal rank fusion
- LoRA paper (Hu et al., arXiv 2106.09685)
- QLoRA paper (Dettmers et al., arXiv 2305.14314)
- Hugging Face PEFT: LoRA
- OWASP GenAI: LLM01 prompt injection
- OWASP GenAI: LLM06 excessive agency