agents/prod
05
Knowledge · Chapter 5

Retrieval-Augmented Generation, end to end

Language models have three hard limits from Chapter 1: a fixed knowledge cutoff, no access to your private data, and a finite window. Retrieval-Augmented Generation (RAG) fixes all three by fetching relevant text at query time and placing it in the context — so the model's "most plausible" answer is also grounded in your facts. It's the most-deployed pattern in production AI, and most teams get the details wrong. Let's build the whole machine.

1 · The core idea: retrieve, then generate

Instead of hoping the model memorized your docs, you: (1) split your documents into chunks, (2) turn each into an embedding vector (Chapter 1 — meaning as geometry), (3) store them in a vector database. At query time you embed the question, find the nearest chunks (similar meaning = nearby vector), and paste them into the prompt. Watch it happen:

Interactive · run a retrieval
Pick a question. Each bar is a stored chunk's similarity to your question (cosine, from the embeddings). The top matches (amber) get retrieved and injected into the prompt — the rest are ignored. That's RAG: "find similar meaning" becomes "find nearby vectors."

2 · Embeddings & the vector database

An embedding model maps text to a vector so similar meanings land near each other; relevance is measured by cosine similarity (the angle between vectors, 1 = identical, 0 = unrelated). Comparing your query to every stored vector is exact but O(N) — too slow at billions. So vector DBs use Approximate Nearest Neighbor (ANN) indexes that trade a sliver of recall for huge speed:

  • HNSW — a layered "small-world" graph; search descends from a sparse top layer to the answer. ~95%+ recall, highest QPS, more RAM. Tuned by M, efConstruction, efSearch.
  • IVF — cluster the space with k-means, probe only the nearest clusters. Memory-efficient at scale.
  • Quantization (scalar/product) shrinks vectors 4–32× so billions fit in RAM/disk (DiskANN).

Rule of thumb: HNSW below ~85% filter ratio, IVF at 85–95%, brute-force only when you need >99% recall. Every index exposes a knob trading accuracy for latency; tune to 95–99% recall within your budget.

HNSW · descend a layered graph sparse topdense base — the answer IVF · probe nearby cells query scan only the highlighted cells, skip the rest
Two ways to avoid comparing against every vector: HNSW greedily walks a graph from coarse to fine; IVF buckets the space and searches only the buckets near your query.

3 · Chunking — the underrated lever

Before embedding, documents are split. This matters as much as the embedding model (per a Vectara study). Options: fixed-size (simple, cuts sentences), recursive character splitting (paragraphs→lines→sentences — the pragmatic default), and semantic (cut where meaning shifts — expensive, often underwhelming).

Interactive · chunk size vs. number of chunks — drag it
Smaller chunks → sharper, more specific vectors (better precision: dropping 1,024→64 tokens improved recall@1 by 10–15 points on entity-heavy data) but more of them and less surrounding context. Sweet spot: 200–500 tokens with 10–20% overlap. Too-big chunks add noise; generation quality can even degrade past ~2,500 tokens of retrieved context.

4 · Advanced retrieval — where the real gains live

  • Hybrid search: dense vectors miss exact terms (error codes, SKUs); classic BM25 keyword search catches them. Fuse both with Reciprocal Rank Fusion. One measured jump: MRR 0.41 → 0.67.
  • Rerankers (cross-encoders): retrieve broad (N≈50–100), then a cross-encoder that reads query+doc together reorders to a top K≈5. Adds ~12–18 NDCG@5 points.
  • Contextual retrieval: prepend a chunk-specific summary before embedding — Anthropic measured retrieval failure dropping 35% → 49% → 67% stacking contextual embeddings + BM25 + reranking.
  • GraphRAG (knowledge-graph retrieval) and agentic RAG (the agent decides what/when to retrieve, iteratively — 49.6% vs 8.4% recall@1 on BRIGHT).
reduction in top-20 retrieval failures — each technique stacks (Anthropic)
Contextual embeds−35%
+ BM25 (hybrid)−49%
+ reranking−67%
The advanced techniques aren't alternatives — they stack. Contextual embeddings + hybrid search + reranking cut retrieval failures by two-thirds.
When you don't need RAG at all

If your whole knowledge base fits in the window (<~200K tokens), skip retrieval and inline the corpus — then use prompt caching (Chapter 6) to make it cheap. And in multi-tenant RAG, always pre-filter by permissions, never post-filter, or you leak documents across tenants.

Recap

  • RAG = embed docs → store vectors → at query time fetch nearest chunks → inject → generate.
  • ANN indexes (HNSW/IVF) + quantization make it fast at scale.
  • Chunking matters as much as the model; 200–500 tokens, 10–20% overlap.
  • Hybrid + reranking + contextual retrieval are where accuracy actually improves.
Carry into Chapter 6

Retrieval and big prompts get expensive fast. Next: caching at four different layers — the highest-leverage way to make all of this affordable.