Retrieval-Augmented Generation (RAG)
Volume III, Chapter 16 — Part I. RAG as Bayesian conditioning on retrieved documents: dense retrieval, similarity search, context injection, and theoretical limits of parametric vs. non-parametric memory.
Table of Contents
- Learning Objectives
- Prerequisites
- Notation
- Core Intuition
- Parametric vs. Non-Parametric Memory
- RAG Formulation
- Dense Retrieval
- Similarity Search and Top-K Retrieval
- Context Construction and Generation
- Theoretical Interpretation
- Failure Modes
- Worked Examples
- Connection to the Broader Curriculum
- Common Pitfalls and Misconceptions
- Research Perspective
- Summary of Takeaways
- Exercises
Learning Objectives
After reading this chapter, you should be able to:
- Distinguish parametric (model weights) from non-parametric (retrieved docs) knowledge.
- Formulate RAG as conditioning generation on retrieved context .
- Define dense retrieval via embedding similarity (cosine/dot product).
- Analyze the retrieve-then-read pipeline mathematically.
- Identify failure modes: retrieval miss, context overflow, hallucination despite retrieval.
Prerequisites
- Self-Attention — context processing
- Vectors & Linear Independence — cosine similarity
- Tokenization — context window limits
Notation
- — User query
- — Generated response
- — Retrieved document and corpus
- — Retriever distribution
- — Conditional generator
- — Document corpus
Core Intuition
LLMs store knowledge in parameters — fixed at training time, opaque, and stale. RAG (Lewis et al., 2020) augments generation with retrieved documents from an external corpus at inference time.
Query → retrieve relevant passages → concatenate as context → LLM generates answer conditioned on context. This is non-parametric memory: the knowledge base updates without retraining.
Series context. Volume III, Chapter 16 (Applications).
Retrieval-Augmented Generation
Parametric vs. Non-Parametric Memory
Definition 1 (Parametric Knowledge). Facts encoded in : implicitly stores training data statistics.
Definition 2 (Non-Parametric Knowledge). External corpus ; retrieval selects subset at query time.
Proposition 1. Parametric memory capacity model size (Scaling Laws). Non-parametric memory — independently scalable.
RAG Formulation
Definition 3 (RAG Generation).
where is retrieved context, is retriever, is generator (LLM).
Practical approximation: Use top- documents with highest :
Dense Retrieval
Definition 4 (Bi-Encoder Retrieval).
where are embedding models (often shared).
Connection to Cosine Similarity: With normalized embeddings, dot product equals cosine similarity.
Definition 5 (Document Index). Precompute for all . Store in vector index (FAISS, HNSW).
Similarity Search and Top-K Retrieval
Definition 6 (Top-K Retrieval).
Complexity. Exact search: per query. Approximate NN: amortized.
Proposition 2. Retrieval quality upper-bounds generation quality — if , model cannot ground answer in correct evidence.
Context Construction and Generation
Definition 7 (Prompt Template).
concatenated into LLM input within context window (Tokenization limits).
Attention mechanism: Self-Attention over full context — query tokens attend to retrieved passages.
Theoretical Interpretation
Proposition 3 (Bayesian View). RAG approximates:
with discrete approximation over retrieved .
Proposition 4 (Knowledge Conflict). When parametric and retrieved conflict, model may ignore retrieval — "hallucination despite RAG."
Failure Modes
- Retrieval miss: Relevant doc not in top-
- Context truncation: docs exceed window
- Noise: Irrelevant retrieved docs confuse generation
- Stale index: Corpus not updated
- Attribution failure: Model doesn't cite sources faithfully
Worked Examples
Example 1: Score Computation
, → high relevance.
Example 2: Context Budget
docs × 500 tokens = 2500 tokens + query — fits in 4096 window.
Connection to the Broader Curriculum
- Self-Attention — processes retrieved context
- KV Cache — long context cost
- LoRA — fine-tune retriever or generator
- DPO — align RAG outputs
Common Pitfalls and Misconceptions
Pitfall 1: RAG does not guarantee factual accuracy.
Pitfall 2: Chunk size affects retrieval granularity.
Pitfall 3: Embedding model must match domain.
Pitfall 4: Ignoring reranking after initial retrieval.
Research Perspective
RAG (Lewis et al., 2020). Dense Passage Retrieval (Karpukhin et al., 2020). Self-RAG, corrective RAG, GraphRAG. Hybrid sparse+dense retrieval.
Summary of Takeaways
- Retriever — via embeddings
- Generator — LLM
- Index — Non-parametric memory
- Top-K — Approximate integral (6)
Next: Flash Attention (Volume IV)
Exercises
Exercise 1. Derive (1) from Bayesian conditioning.
Exercise 2. Compute retrieval score with normalized embeddings.
Exercise 3. Context window budgeting for documents.
Exercise 4. When does parametric knowledge override retrieval?
Exercise 5. Design chunking strategy for long documents.
Exercise 6. Connect retrieval to Cosine Similarity.
Exercise 7. RAG + KV Cache memory for long context.
Exercise 8. Compare RAG to fine-tuning for knowledge injection.