What Is RAG (Retrieval-Augmented Generation)? Architecture, Pipeline & When to Use It
The complete beginner-to-production overview of RAG: Offline Indexing vs. Online Retrieval-and-Generate, why RAG solves stale knowledge, and every building block in Module 4
Retrieval-Augmented Generation (RAG) gives an LLM an open-book search engine: before answering a user's question, your system retrieves the most relevant passages from your private documents and injects them into the prompt.
Pretrained LLMs have a fixed knowledge cutoff, zero access to your private company docs, and hallucinate when guessing; RAG keeps answers fresh, private, and verifiable with zero retraining.
RAG is the #1 most widely deployed enterprise Generative AI architecture in the world—powering internal search, support copilots, and legal/medical Q&A.
Enforcing document-level Access Control Lists (ACLs) at retrieval time ensures User A never sees User B's private files—something impossible with shared fine-tuned weights.
Decomposing RAG into modular stages (chunking, retrieval, reranking, generation) lets you measure and optimize each stage independently.
Standard system design prompt: 'Design a production RAG system over 10 million company PDFs—walk through both the ingestion pipeline and query pipeline.'
In a standard RAG architecture, which two stages happen OFFLINE before any user ever asks a question?
- Draw the 2-pipeline RAG architecture: Offline Ingestion (Load → Chunk → Embed → Index) and Online Serving (Query → Retrieve → Rerank → Generate).
- Explain why RAG solves freshness, privacy/RBAC permissions, and auditability (clickable citations).
- Spot the 3 classic Naive RAG failure modes (bad chunking, keyword misses, noisy top-K) that the rest of Module 4 solves.
- Build a complete end-to-end minimal RAG pipeline in 20 lines of Python.
Every RAG system consists of two halves: (1) Offline Indexing turns raw PDFs/wikis into searchable chunks inside a hybrid vector+keyword index, and (2) Online Retrieval & Generation fetches the sharpest passages for a live question and hands them to the LLM as an open-book reference.
1. The Complete RAG Pipeline Map (What You'll Master in Module 4)
- Chunking (Ch 22): Split long docs cleanly with overlap & context headers.
- Dense Embeddings & Vector Search (Ch 20–21): Convert text into unit vectors & index with HNSW.
- Hybrid Search + RRF (Ch 23): Combine BM25 exact keywords with dense semantic vectors.
- Reranking & MMR (Ch 24): Cross-Encoder precision + diversity deduplication.
- RAG Evals & GraphRAG (Ch 25–26): Measure Recall@K/NDCG/Faithfulness and handle multi-hop knowledge graphs!
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
Attach metadata tags (e.g., allowed_groups: ['finance']) to every indexed chunk and apply a pre-filter inside the vector database query before returning top-K chunks—guaranteeing unauthorized docs never enter the LLM prompt.
Unit-Hypersphere Query-to-Doc Cosine Retrieval Stage
Watch the normalized query vector q̂ sweep around the unit circle and highlight its Top-1 nearest document via pure dot product.
import numpy as np
# Minimal End-to-End RAG Pipeline: Index -> Retrieve Top-1 -> Grounded Prompt
docs = [
"Policy: Refunds are accepted within 14 days with a receipt.",
"Shipping: Standard delivery takes 3-5 business days.",
]
# Toy 2D embeddings: [refund_signal, shipping_signal]
doc_vecs = np.array([[0.95, 0.10], [0.10, 0.95]])
query = "How many days do I have for a refund?"
query_vec = np.array([0.92, 0.08])
# 1. Retrieve top-1 chunk by cosine similarity
sims = (doc_vecs @ query_vec) / (np.linalg.norm(doc_vecs, axis=1) * np.linalg.norm(query_vec))
best_doc = docs[int(np.argmax(sims))]
# 2. Construct grounded open-book prompt for the LLM
prompt = f"Answer ONLY from context.\nContext: {best_doc}\nQuestion: {query}\nAnswer:"
print(prompt)