03 / 16

RAG & Retrieval

Grounding answers in your data: ingest, index, retrieve, rerank, cite.

0/18 studied
01RAG (Retrieval-Augmented Generation)

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.

Core◇ Diagram</> Code
02Document ingestion & parsing

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.

Core◇ Diagram</> Code
03Chunking strategies

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.

Core◇ Diagram</> Code
04Choosing an embedding model

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.

Core◇ Diagram</> Code
05Vector databases

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.

Core◇ Diagram</> Code
06ANN indexes: HNSW, IVF, PQ

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.

Advanced◇ Diagram</> Code
07Lexical search & BM25

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.

Core◇ Diagram</> Code
08Hybrid search & RRF

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.

Core◇ Diagram</> Code
09Reranking (cross-encoders)

Retrieve many candidates cheaply, then let a slower, smarter model reorder them and keep only the best few for the prompt.

Core◇ Diagram</> Code
10Query transformation (rewrite, HyDE)

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.

Advanced◇ Diagram</> Code
11Contextual retrieval

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.

Advanced◇ Diagram</> Code
12Metadata filters & permission-aware retrieval

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.

Core◇ Diagram</> Code
13Citations & grounding

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.

Core◇ Diagram</> Code
14GraphRAG & knowledge graphs

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.

Advanced◇ Diagram</> Code
15Agentic RAG

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.

Advanced◇ Diagram</> Code
16Late interaction (ColBERT)

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.

Advanced◇ Diagram</> Code
17Index freshness & lifecycle

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.

Advanced◇ Diagram</> Code
18Evaluating RAG

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.

Core◇ Diagram</> Code