AI Systems

RAG — Retrieval-Augmented Generation

RAG fetches relevant data from outside a model's parameters and injects it into the context window, grounding generation in retrievable evidence instead of whatever the model happened to memorize during training. It's also far cheaper to keep a retrieval index current than to retrain a model — which is what makes RAG the default answer for incorporating recent or proprietary knowledge, and the single most frequently designed AI system in practice.

Quick reference
What it is
Retrieving relevant external context at inference time and injecting it into the prompt, instead of relying only on what the model learned during training.
Mental model
An open-book exam: the model doesn't have to memorize everything, it just has to find the right page and read it correctly.
Key numbers
256–512 token chunks with 10–20% overlap is a reasonable prose default; a two-stage retrieve-then-rerank pipeline (fetch 50–100 candidates, rerank to top 5–10) consistently beats either stage alone.
Reach for it when
Answers need to be grounded in data that changes faster than a retraining cycle, or in proprietary/internal content the base model never saw.
A user query is embedded, searched against a vector index built offline from a chunked document corpus, reranked to a top-k set, and generated into an answer User Query Embed Query Vector DB ANN search Re-rank + top-k LLM Generation Document Corpus chunk + embed offline
The corpus is chunked and embedded once, offline, well before any query arrives — only the query-embed → search → rerank → generate path runs on the request's critical path.

Naive single-pass RAG

Embed the incoming query with the same model family used to embed the corpus, run approximate nearest-neighbor search (HNSW or IVF-PQ indexes, in stores like pgvector, Pinecone, Weaviate or Qdrant), retrieve the top-k chunks by cosine similarity, and concatenate them into the prompt. Chunk size is the first real design decision — 256 to 512 tokens with 10 to 20 percent overlap is a reasonable default for prose, while structured documents (contracts, tables, datasheets) often need semantic or layout-aware chunking instead of fixed-size windows. Naive RAG degrades on ambiguous queries, multi-hop questions that need synthesizing several documents, and corpora with poor structural boundaries.

Hybrid retrieval RAG

Combine dense vector retrieval with sparse lexical search — typically BM25 — and fuse the two ranked lists with reciprocal rank fusion or a learned cross-encoder reranker. This directly addresses a well-known weakness of dense embeddings: they generalize away exact identifiers like part numbers, register names, or rare proper nouns that a keyword index matches trivially. A two-stage pipeline — cheap bi-encoder retrieval fetching 50 to 100 candidate chunks, then a cross-encoder reranker narrowing to the final top 5 to 10 — reliably outperforms either method alone.

Agentic and iterative RAG

The model judges whether retrieved context is actually sufficient and, if not, reformulates the query and retrieves again — sometimes decomposing a complex question into sub-questions first. Self-RAG trains the model to emit special retrieval/critique tokens that trigger on-demand retrieval and self-assessment; Corrective RAG (CRAG) adds a lightweight relevance grader on retrieved documents and falls back to a web search when the internal knowledge base has thin coverage. Both add one to three extra round trips of latency and cost for materially better answers on multi-hop and ambiguous queries — reserve them for query types where naive RAG measurably underperforms, not by default.

Productionizing this pattern

What changes past a demo

Real-world applications

Embedded applications

Gotchas

Field notes