RAG (Retrieval-Augmented Generation)
How RAG works: architecture, components, trade-offs, and production considerations for grounding LLM responses in retrieved knowledge.
Overview
Retrieval-Augmented Generation (RAG) is an architecture pattern that grounds large language model responses in retrieved external knowledge. Instead of relying solely on a model's parametric memory. which is frozen at training time and prone to hallucination. RAG retrieves relevant documents at query time and includes them in the prompt as context.
How It Works
A RAG pipeline has two phases. In the offline phase, source documents are split into chunks (typically 256–512 tokens), each chunk is converted to a dense vector embedding, and those embeddings are indexed in a vector store. In the online phase, a user query is embedded with the same model, the vector store returns the top-k most similar chunks, those chunks are assembled into a prompt alongside the query, and the LLM generates a response grounded in the retrieved context.
Why It Matters
RAG reduces hallucination by 50–70% in production systems by constraining the model to answer from retrieved evidence. It lets organizations use current, private data without retraining the model. It is far cheaper and faster than fine-tuning for domain-specific knowledge, and retrieved sources can be cited, making outputs auditable.
Key Components
When to Use RAG
Use RAG when you need answers grounded in specific documents, proprietary data, or information that changes frequently. It is the right choice when fine-tuning is too expensive or when you need source attribution. RAG is less suited for tasks that require deep reasoning over very large document sets in a single pass. for those, consider long-context models or multi-hop retrieval.
Common Pitfalls
Production Considerations
In production, RAG systems need monitoring for retrieval latency, relevance drift, and embedding freshness. Hybrid search (combining vector similarity with keyword BM25) often outperforms pure vector retrieval. Metadata filtering, re-ranking, and query expansion are standard optimizations. Most teams start with pgvector inside PostgreSQL and move to a dedicated vector database only when scale demands it.