Retrieval-Augmented Generation (RAG)
- What RAG is and the retrieve-augment-generate loop
- How to index, retrieve, augment, and generate with citations
- Why RAG beats fine-tuning for 'answer about my docs' needs
- The five failure modes that kill RAG quality
- A copy-paste grounding prompt that closes the two biggest gaps
RAG makes a model answer questions about your data — docs, a knowledge base, a codebase — that it was never trained on. The idea is simple: retrieve the relevant pieces, augment the prompt with them, then generate an answer grounded in those pieces.
The loop
- Split into chunks, embed them (see /docs/foundations/embeddings), and store in a vector (and/or keyword) index.
- Pull the top chunks most relevant to the question.
- Put those chunks in the prompt with an instruction like "Answer only from the context below; if it's not there, say so."
- Produce the answer — and ideally cite which chunk each claim came from.
For the embedding step in indexing, see Embeddings & Vector Search.
Why RAG instead of fine-tuning?
- Fresh: update the data, not the model
- Verifiable: provides citations
- Cheap: far cheaper than retraining
For most "answer about my documents" needs, RAG is the right first tool — see Fine-tuning vs Prompting vs RAG.
The failure modes (where RAG quality dies)
- Bad retrieval = bad answer. If the right chunk isn't retrieved, the model can't use it. Most 'RAG is wrong' problems are retrieval problems.
- Chunking too coarse/fine wrecks relevance (see embeddings).
- No grounding instruction: the model blends retrieved facts with its own guesses. Tell it to answer only from context and to admit gaps.
- Stuffing too much: irrelevant chunks dilute the signal and cost tokens. Retrieve few, high-quality chunks.
- No citations: you can't verify, so you can't trust.
The chunking failure ties back to embeddings, and over-stuffing costs tokens.
- Evaluate retrieval separately: measure 'did we retrieve the right chunk?' apart from 'did the model answer well?' It localizes the problem fast. See Evals (/docs/foundations/evals).
Copy-paste: a grounding prompt
The single highest-leverage fix is a grounding instruction. Drop your retrieved chunks into a template like this — it forces the model to answer only from context, cite each claim, and admit gaps instead of guessing:
Grounding prompt
You are answering strictly from the context below. Rules: - Use ONLY the context to answer. Do not use outside knowledge. - Cite the source after each claim, like [chunk 2]. - If the answer is not in the context, reply exactly: "I don't have that in the provided sources." - Quote numbers and names verbatim — never paraphrase a figure. Context: [chunk 1] ... [chunk 2] ... [chunk 3] ... Question: <the user's question>
Pair it with a few high-quality chunks (not everything you retrieved) and you close the two biggest gaps at once: hallucinated blending and unverifiable answers. Then eval retrieval and generation separately so you know which half to tune.
Master the terms
Check yourself
0/5- RAG = retrieve relevant chunks, augment the prompt, generate a grounded, cited answer.
- Index (chunk + embed + store), retrieve top chunks, augment with a grounding instruction, generate with citations.
- Prefer RAG over fine-tuning for document Q&A: fresh, cited, cheaper.
- Most failures are retrieval failures — retrieve few high-quality chunks, not everything.
- Always add a grounding instruction and cite; eval retrieval and generation separately.