Cheatsheet 14 — RAG Design

The RAG pipeline and its design knobs. Full: Phase 9. Labs: 03, 09.

The pipeline

INGEST: load → clean → CHUNK → EMBED → store (vector DB) [+ keyword index]
QUERY:  embed query → RETRIEVE (vector [+ BM25]) → RERANK → PACK context → GENERATE (grounded) → CITE

Design knobs & defaults

StageKnobDefault / guidance
Chunkingsize / overlap200–500 tokens, ~10–20% overlap; respect structure (headings)
EmbeddingmodelA strong embedding model; match query+doc model
Vector DBANN indexHNSW (most cases); per-tenant namespaces for multi-tenant (14.04)
Retrievaltop-k10–30 candidates before rerank
Hybriddense + BM25 + RRFAdd keyword search; fuse with Reciprocal Rank Fusion
Rerankcross-encoderRerank to top 3–8 (precision boost)
Packingorder / countPut best chunks at start/end (lost-in-the-middle, 9.07)
Generationgrounding"Answer only from context; cite; say 'I don't know'"

RAG vs fine-tuning (the cardinal rule)

  • RAG → knowledge (facts, current/proprietary data, citations).
  • Fine-tuning → behavior (format/style/skill).
  • Need facts? RAG. Need a behavior? Fine-tune. (Ladder: prompt → few-shot → RAG → fine-tune, Phase 13.00.)

Evaluate retrieval and generation SEPARATELY

HalfMetrics
Retrievalrecall@k, precision@k, MRR, hit rate
Generationfaithfulness/groundedness, answer relevance, citation accuracy

"Bad answer" = first ask did retrieval find the right chunks? (retrieval) vs did the model use them? (generation) (Phase 9.09).

Common failure modes 🚩

SymptomLikely cause
"I don't know" but doc existsRetrieval miss (chunking/embedding/query mismatch)
Hallucinated answerWeak grounding prompt; missing chunks; model ignoring context
Right chunks, wrong answerGeneration/packing (lost-in-the-middle)
Cross-tenant leakMissing per-tenant retrieval filter (14.04)
Stale answersIngestion freshness/re-index gap

Production extras

  • ACLs at retrieval (user only sees permitted docs), freshness/re-indexing, feedback loop (Phase 9.10).
  • HyDE / query rewriting for hard queries; prefix caching for shared context (7.05).

Next: 15 — Tool Calling · Full: Phase 9