Advanced RAG Engineering
Why Naive RAG Fails in Production
Bi-Encoder/Cross-Encoder pipelines, HyDE, and Contextual Retrieval.
Interview: High - Core Senior AI interview topic at Stripe, Notion, Glean.
Naive RAG Kills Production Systems
Common RAG Failure Modes
The standard tutorial pipeline (PDF → chunk → embed → cosine search → stuff context → GPT-4) breaks catastrophically at scale. After 50,000 documents, Top-K retrieval surfaces structurally irrelevant chunks because cosine distance doesn't understand contextual hierarchy. The LLM receives a schizophrenic, disconnected context window and confidently hallucinates.
The Bi-Encoder / Cross-Encoder Reranking Pipeline
Production RAG is a two-stage retrieval process. Stage 1 uses a fast Bi-Encoder (like text-embedding-3-small) to recall the Top-100 candidates using approximate nearest-neighbor search. Stage 2 uses a slower, more accurate Cross-Encoder (like Cohere Rerank or a fine-tuned BERT) that scores the exact relationship between the full query and each individual chunk. You surface the actual Top-5 from there.
Trade-off: You add ~300ms latency for the reranking pass. You buy an 80% reduction in hallucination rate from irrelevant context. This is always the right trade at enterprise scale.
HyDE (Hypothetical Document Embeddings)
The core insight: a user query ("What's the refund policy?") and the document containing the answer ("Our 30-day return policy states...") often have very different vector representations. HyDE fixes this by asking the LLM to generate a hypothetical answer to the query first, then embedding that hallucinated answer to search the database. A hallucinated answer is vectorially much closer to the real document than the original query.
Contextual Retrieval (Anthropic)
Isolated chunks lose context. The chunk "The revenue increased by 15%" is meaningless without knowing which company, which year. Contextual Retrieval uses Claude to generate a concise context summary ("This chunk describes Apple's Q3 2024 earnings call...") and prepends it to every chunk before embedding. This single step reduces retrieval failure rates by 49%.
Use Cases
Enterprise document search with 1M+ documents (Legal, Finance, Compliance)
Code search systems where query intent differs heavily from function signatures
Customer support agents requiring precise policy retrieval without hallucination
Common Mistakes
Using cosine similarity alone for Top-K at scale — you will get irrelevant results at high document counts
Running Cross-Encoder reranking on 1000 candidates — it is O(n) expensive; always pre-filter to Top-100 first with a Bi-Encoder
Not invalidating embeddings when source documents are updated