Library

Volume 2 · RAG Architecture

Notebook
Sign in

Cheat Sheet — Part 3: RAG Architecture

679 words · about 3 min

Key Terms

Term In one line
Hybrid search Dense vectors + BM25 fused — the recommended production default
HyDE Generate a hypothetical answer, embed that, and retrieve against it
Parent document retrieval Search small chunks, return their larger parent for context
Contextual compression Strip irrelevant sentences from retrieved chunks before generation
Self-query LLM turns a natural-language query into filters + a search string
Re-ranking Cross-encoder rescores top-N candidates; far more accurate than embedding distance
CRAG Corrective RAG — grade retrieval, fall back to web/other source when it’s weak
Self-RAG Model decides whether to retrieve, and critiques its own output
GraphRAG Traverse a knowledge graph alongside vector search for multi-hop questions
HNSW / IVF / Flat ANN index types; Flat is exact at 100% recall and doesn’t scale
RAGAS Faithfulness, answer relevancy, context precision, context recall

Numbers That Matter

Figure Value Where it bites
Chunk size default 256–512 tokens, 10–20% overlap Starting point only — tune against eval data
Precision vs context Small chunks (256–512) for precise lookups; large (1000–1500) for summarization Drive the choice from the query pattern, not the document format
Embedding dims 384 (MiniLM) · 1024 (Cohere/BGE/Voyage) · 1536 / 3072 (large OpenAI-class) Bigger isn’t automatically better; benchmark on your data via MTEB retrieval tasks
Rerank funnel top-40 → top-8 Typical shape; costs a model hop, usually worth it
Chunk overlap 50–100 tokens Preserves boundary context in fixed-size chunking
Semantic cache 30–40% cost reduction Cosine threshold ~0.95 for a near-duplicate hit

Decision Rules

  • Start from the query pattern, not the document format. Precise factual lookups want small chunks so precision signal isn’t diluted; synthesis and summarization want larger chunks or parent-document retrieval.
  • Hybrid over pure dense as the default. Dense misses exact matches — product codes, policy numbers, error strings — that BM25 nails. Pure dense is a fine v0 and a poor v2.
  • Tables are the #1 ingestion pain point. A parser that flattens a table into reading order silently corrupts every number in it. Use layout-aware parsing or a VLM for complex tables, and test table extraction explicitly.
  • Pre-filter on ACLs, never post-filter. Post-filtering leaks: the model has already seen the chunk. Store entitlements with each chunk and filter at query time.
  • Multi-tenancy: separate index per tenant for hard isolation and noisy-neighbour control; shared index with metadata filters for cost efficiency at many small tenants. Say which risk you’re optimizing for.
  • Stale data: rebuild on change, not on a nightly schedule. A nightly index means answers can be wrong for a full day after a source document changes.
  • RAG vs fine-tuning vs long context: RAG for changing facts and citation requirements. Fine-tuning for behaviour and format. Long context when the corpus genuinely fits and per-query cost is acceptable — it does not replace retrieval at enterprise corpus sizes.

Say This Out Loud

“My default retrieval design is hybrid — dense vectors plus BM25, fused — with a cross-encoder reranker on top. Dense-only is simpler to operate and it’s a perfectly good v0, but it misses exact-match terms: policy codes, product names, error strings. The reranker costs a model hop and some latency, and I justify it with the eval set rather than with intuition, because it’s the first thing to cut if latency is the binding constraint.”

“Chunking is a document-understanding problem, not a string-splitting one. I choose size from the query pattern — precise factual lookups want two-fifty to five hundred tokens so the precision signal isn’t diluted, synthesis wants larger chunks or parent-document retrieval. And I test table extraction explicitly, because the classic silent failure is a parser flattening a table into reading order and attaching every number to the wrong row.”

“Security in RAG is a pre-filter problem. I store the ACL with each chunk and filter at query time, before retrieval — never post-filter the results, because by then the content has already reached the model. And I treat every retrieved chunk as untrusted data rather than instructions, because anyone who can get a document into the corpus can otherwise write instructions to my model.”