03 / 16
RAG & Retrieval
Grounding answers in your data: ingest, index, retrieve, rerank, cite.
Before the model answers, search your own data for the most relevant passages and put them in the prompt, so the answer is grounded in fresh, private, citable sources instead of memory.
Turn messy PDFs, slides, scans and web pages into clean, structured text with metadata before chunking; most RAG quality problems start here, not in the model.
Split documents into retrievable pieces that are each about one thing yet carry enough context to answer; chunk size is a precision-vs-context trade-off you settle with evals, not defaults.
Pick the embedding model by measuring retrieval on your own queries, then weigh dimensions, max input, languages, latency and price; leaderboards only shortlist, and switching later means re-embedding everything.
A database that stores embeddings with metadata and answers "the k nearest vectors to this one, where tenant = X" in milliseconds. Choose on scale, filtering, hybrid support and ops burden; often pgvector is enough.
Exact nearest-neighbor search scans every vector; ANN indexes visit a tiny fraction and accept missing a few true neighbors. HNSW is the default, IVF and PQ trade recall for memory, efSearch/nprobe are the dials.
BM25 ranks documents by how often they contain your query's words, rewarding rare words and discounting long documents. Decades old, no model, and still better than embeddings at exact terms: IDs, error codes, product names.
Run keyword (BM25) and vector search in parallel and merge the two rankings, usually with Reciprocal Rank Fusion. Each covers the other's blind spots, so recall rises for little extra cost.
Retrieve many candidates cheaply, then let a slower, smarter model reorder them and keep only the best few for the prompt.
Before retrieving, use a small LLM to turn the user's raw message into better search queries (standalone, expanded, decomposed or a hypothetical answer), paying one extra model call for recall.
At indexing time, an LLM writes 50–100 tokens situating each chunk in its document; you prepend them before embedding and BM25. Anthropic cut top-20 retrieval failures 49% (67% with reranking), at no query-time cost.
Attach metadata (tenant, ACL groups, date, type) to every chunk and enforce it as a hard filter inside the search, built in code, so the LLM never sees a document the user couldn't open.
Answer only from retrieved chunks, tag every claim with the ids of the chunks that support it, verify those citations in code, and abstain when the context doesn't contain the answer.
An LLM turns the corpus into an entity–relation graph, clusters it into communities and pre-writes a summary of each, so you can answer corpus-wide questions that top-k chunk retrieval can't.
Put retrieval inside an agent loop: the model decides whether, what and where to search, judges the evidence, and searches again until it can answer — accuracy on hard questions for latency, tokens and predictability.
Store one vector per token instead of one per passage, and score with MaxSim at query time: precision close to a cross-encoder, with documents still pre-computed offline, paid for in storage.
A RAG index is a replica of your sources: stream changes in, propagate every deletion, version each chunk, and migrate embedding models through a blue/green index and an alias swap, never in place.
Score retrieval and generation separately: recall@k and MRR tell you whether the evidence was found, faithfulness and correctness tell you whether the model used it. One blended score hides which stage to fix.