Home » RAG Pipelines » What Is RAG

What Is Retrieval-Augmented Generation

Retrieval-augmented generation (RAG) is an architecture pattern where an LLM receives relevant documents from an external knowledge base alongside the user's question, so it can generate answers grounded in real data rather than relying solely on what it memorized during training. RAG solves the fundamental limitation of LLMs: their knowledge is frozen at the training cutoff, incomplete for specialized domains, and unverifiable when they generate answers from parametric memory alone.

The Core Idea Behind RAG

Every LLM has the same structural limitation. During pretraining, the model consumes billions of tokens of text and compresses that information into numerical weights. Those weights encode statistical patterns about language and facts, but they are static once training ends. Ask the model about something that happened after its training cutoff and it either refuses to answer or fabricates a plausible-sounding response. Ask it about your company's internal policies, a niche regulatory requirement, or a recently published research paper and it has the same problem: the information was never in the training data, so the model cannot produce it from memory.

RAG addresses this by externalizing the knowledge. Instead of relying on the model to know the answer from its weights, the system retrieves relevant documents from an external store and injects them into the prompt alongside the user's question. The model then generates its answer based on the retrieved context, synthesizing information from the documents rather than recalling it from memory. This is analogous to how a knowledgeable researcher works: they do not memorize every source, they know how to find the right reference and use it to construct an informed answer.

The term was formalized in a 2020 paper by Patrick Lewis and colleagues at Meta AI, where they combined a pretrained retriever (DPR) with a pretrained generator (BART) and showed that this combination outperformed models that relied on parametric knowledge alone across multiple knowledge-intensive benchmarks. But the underlying idea predates the paper. Information retrieval systems have been pairing search with generation for decades. What changed is that modern LLMs are sophisticated enough to synthesize retrieved information into coherent, nuanced natural language responses rather than just extracting and repeating passages.

How RAG Works in Practice

A RAG system operates in two phases. The first phase, ingestion, happens offline before any user query. Source documents are parsed into clean text, split into chunks of manageable size, converted to vector embeddings using an embedding model, and stored in a vector database alongside the original text and metadata. This creates a searchable index of the entire knowledge base where each chunk is represented as a point in high-dimensional space, with semantically similar chunks positioned near each other.

The second phase, retrieval and generation, happens on every user request and must complete in seconds. The user's question is embedded using the same model that embedded the documents, producing a query vector in the same semantic space. The vector database performs a similarity search, comparing the query vector against all stored chunk vectors, and returns the top-k most similar chunks. Those chunks are formatted as context in the LLM's prompt, typically after a system instruction and before the user's question. The LLM reads the context, reads the question, and generates a response that draws on the retrieved information.

What makes this different from simply pasting documents into a prompt is the retrieval step. A raw document dump would quickly overflow the context window (most LLMs support 128k tokens, which is roughly 200 pages, and many real knowledge bases are thousands of pages). The retrieval step acts as a filter, selecting only the chunks most likely to be relevant to the specific question. This means the model receives a focused, relevant subset of the knowledge base rather than everything, which improves both answer quality and cost efficiency.

Why RAG Matters for Production AI

RAG solves four problems that are critical for any AI system deployed in production.

The first is knowledge freshness. A fine-tuned model's knowledge is frozen at the point when its training data was assembled. Updating it requires retraining, which costs money, takes time, and carries the risk of degrading other capabilities. A RAG system's knowledge is stored externally in a database that can be updated at any time. Adding a new document, correcting an error, or removing outdated information is a database operation, not a machine learning operation. The model does not need to change at all.

The second is domain specificity. General-purpose LLMs are trained on broad web data and perform well on general knowledge questions but poorly on specialized domains like internal company policies, proprietary technical documentation, niche regulatory requirements, or recent research in a narrow field. RAG allows any LLM to become a domain expert by connecting it to a domain-specific knowledge base. A general model combined with a medical knowledge base produces medical answers grounded in clinical guidelines. The same model connected to a legal knowledge base produces legal answers grounded in statutes and case law.

The third is auditability. When an LLM generates an answer from parametric knowledge, there is no way to verify where the information came from or whether it is accurate. The answer simply appears. With RAG, the retrieved documents are visible: you know exactly which sources the model was given, and you can check whether the answer faithfully represents those sources. This is not just a nice feature, it is a requirement in regulated industries where decisions must be traceable to authoritative sources. Many RAG implementations generate inline citations that link each claim in the answer to the specific chunk it came from.

The fourth is hallucination reduction. LLMs hallucinate when they generate plausible-sounding text that is not grounded in fact. RAG reduces hallucination by providing the model with source material it can draw from, reducing its reliance on parametric memory which is the primary source of hallucination. RAG does not eliminate hallucination entirely, the model can still ignore the context, misinterpret it, or blend it with parametric knowledge, but it provides a structural constraint that measurably reduces the rate. The existing pillar on AI hallucinations covers the broader problem and prevention strategies.

What RAG Does Not Do

Understanding what RAG does not solve is equally important for making good architecture decisions.

RAG does not teach the model new skills or behaviors. If the model cannot perform a task, like writing code in a proprietary language, following a specific output format, or reasoning in a domain-specific way, giving it more documents will not help. Those are behavioral problems that require fine-tuning. RAG provides information, not capability.

RAG does not guarantee accurate answers. The quality of a RAG system is bounded by three things: the quality of the knowledge base (garbage in, garbage out), the quality of the retrieval (if the right document is not retrieved, the model cannot use it), and the quality of the generation (the model might misinterpret or ignore retrieved context). Each of these failure modes needs its own mitigation strategy, which is why evaluation is a critical component of any RAG deployment.

RAG does not replace the need for a capable base model. A weak LLM with perfect retrieval will still produce poor answers because it lacks the language understanding and reasoning capability to synthesize information from retrieved documents. RAG amplifies the capability of the base model by giving it better inputs, it does not compensate for a model that is not capable enough for the task.

RAG does not handle personalization. It retrieves from a shared knowledge base, not from per-user context. If your application needs to remember that a specific user prefers Python over JavaScript, that they asked about authentication last week, or that they corrected the system's answer yesterday, you need a memory layer that stores and retrieves per-user information. RAG and memory are complementary: RAG provides domain knowledge, memory provides user context.

When to Use RAG

RAG is the right choice when your application needs to answer questions using a body of information that is too large for the context window, changes over time, needs to be citable, or is not well-represented in the model's training data. This covers the vast majority of enterprise AI applications: customer support bots, internal knowledge assistants, legal research tools, medical information systems, code documentation search, and any application where the model needs to reference specific source material.

RAG is not the right choice when the problem is behavioral rather than informational. If you need the model to respond in a certain tone, follow a specific format, or perform a task it was not trained to do, those are fine-tuning problems. If you need the model to answer from general knowledge that it already knows well, RAG adds complexity without benefit. And if your knowledge base is small enough to fit entirely in the context window (under a few hundred pages), you might get comparable results from simply stuffing the full knowledge base into the prompt on every request, which is architecturally simpler even if less cost-efficient.

The practical decision comes down to this: if the model needs specific information it does not have, and that information exists in documents you control, RAG is almost certainly the right architecture. The remaining questions are about implementation: how to chunk the documents, which retrieval strategy to use, how to evaluate quality, and how to scale for production. Those are covered in the rest of this pillar, starting with the architectural components and step-by-step build guide.

Key Takeaway

RAG externalizes knowledge from the model to a searchable database, retrieves relevant chunks at query time, and injects them into the prompt so the LLM can generate grounded, citable answers. It solves knowledge freshness, domain specificity, auditability, and hallucination reduction, but does not teach new skills, guarantee accuracy, or handle personalization.