What is RAG? How Retrieval-Augmented Generation Works
RAG lets an LLM look things up before it answers. This guide starts with the plain-English idea, then walks through the indexing and query pipelines, the five building blocks, and when to choose RAG over fine-tuning.

RAG in one sentence
Retrieval-augmented generation (RAG) is a way of building LLM applications where the system looks up relevant information first, then asks the model to answer using what it found.
Imagine asking a friend, "What's the refund policy in our contract with Vendor X?" If they answer from memory, they might guess, and the guess could be wrong. If they open the contract, find the right clause and then answer, you can trust the answer and check it.
An LLM on its own is the friend answering from memory. RAG is the friend who checks the document first.
Why LLMs need RAG
An LLM's knowledge lives in the billions of parameters it learned during training. That gives it three problems for real applications:
- It is frozen in time. The model knows nothing after its training cutoff.
- It has never seen your data. Your contracts, tickets, wikis and product manuals were not in its training set.
- It can hallucinate. When it doesn't know, it can still produce a fluent, confident and wrong answer.
The key idea behind RAG is a separation of concerns. An LLM is very good at understanding a question and writing an answer from facts it is given. It is not a database: its parameters are a compressed, lossy summary of its training data. So RAG leaves storing knowledge to a system built for it, a searchable index, and uses the LLM only for language and reasoning.
How RAG works
A RAG system runs as two pipelines: an offline indexing pipeline that prepares your documents, and an online query pipeline that runs on every question.

Pipeline 1: indexing (building the library)
This runs once, then again whenever your documents change.
- Load and chunk. Ingest the source documents (PDFs, wiki pages, tickets, manuals) and split them into chunks of a paragraph or a few related paragraphs. Chunks keep each retrieved piece small and focused, so only relevant text uses up the model's context window.
- Embed. Pass each chunk through an embedding model, which turns text into a vector: a list of numbers that captures its meaning. Texts with similar meanings end up close together, so "doctor" and "physician" land near each other while "cat" and "car" do not.
- Store. Load the vectors into a vector database, such as Milvus, Pinecone, Weaviate, Qdrant or pgvector, together with a pointer back to the original text. The database is built to answer one question fast: which stored vectors are closest to this one? This is approximate nearest neighbor (ANN) search.
The result is an index of your knowledge organized by meaning rather than by keywords. That is what semantic search means.
Pipeline 2: query (answering a question)
This runs in real time for every user question.
-
Embed the query. Convert the question into a vector with the same embedding model used for indexing, so the question and the documents share one vector space.
-
Retrieve. Ask the vector database for the top-k closest chunks (often 3 to 10). Many production systems combine this with keyword search and then rerank the results to improve precision.
-
Augment the prompt. This step is the "augmented" in RAG. Build a prompt that contains the retrieved chunks, instructions, and the user's question:
textCONTEXT: [Chunk 1: "Refunds are available within 30 days of delivery when..."] [Chunk 2: "For annual contracts, Vendor X pro-rates refunds..."] INSTRUCTIONS: Answer using only the context above. If the answer is not in the context, say so. Cite the chunk you used. QUESTION: What's the refund policy in our contract with Vendor X? -
Generate. Send that prompt to the LLM. The model now answers from your documents and can cite them, instead of guessing.
The five building blocks of a RAG pipeline

Every RAG system, from a weekend prototype to an enterprise assistant, comes down to the same five parts:
Building block | What it does | Common choices |
|---|---|---|
1. Knowledge source | The documents and data the system can answer from. Answer quality starts here: clean, current, well-structured data matters more than any model choice. | PDFs, wikis, ticket history, product docs, databases |
2. Embedding model | Turns chunks and queries into vectors that capture meaning. | Hosted embedding APIs or open models such as the BGE and E5 families |
3. Vector database | Stores and indexes vectors for fast similarity search. | Milvus, Pinecone, Weaviate, Qdrant, Chroma, pgvector |
4. Retriever | Finds the most relevant chunks for a query, often with hybrid search, metadata filters and reranking. | Framework retrievers, or custom code against the database |
5. Generator | The LLM that writes the final answer from the retrieved context. | A frontier model for hard reasoning; a smaller, cheaper model for simple lookups |
A sixth, often forgotten part is the output layer: showing which sources were used. Citations are what turn a plausible answer into a checkable one.
Where RAG is used
- Customer support: assistants that answer from the help center and past tickets.
- Internal knowledge search: "What did we agree with Vendor X about SLAs?" across thousands of documents.
- Healthcare and legal: summarizing records or contracts with citations back to the source.
- Developer tools: coding assistants that retrieve relevant files, docs and APIs before answering.
- Personal knowledge bases: asking questions across your own notes and e-books.
RAG vs fine-tuning
RAG and fine-tuning solve different problems, and many production systems use both.
You need to... | Use | Why |
|---|---|---|
Answer from specific, changing, or private documents | RAG | Update knowledge by updating the index. Answers can cite sources. |
Give the model a consistent tone, persona or output format | Fine-tuning | Fine-tuning changes how the model behaves by adjusting its weights. |
Teach a narrow skill the base model lacks | Fine-tuning | Training examples can teach patterns that prompting can't reliably produce. |
A common pattern is RAG for the facts and a light fine-tune for behavior. For a detailed comparison with costs and decision criteria, read RAG vs fine-tuning.
What RAG does not fix
RAG reduces hallucinations; it does not remove them. Common failure points are:
- Bad retrieval: the right passage was never retrieved, so the model answers from the wrong context. Chunking and search quality matter more than model choice.
- Stale or conflicting sources: if the index has old and new versions of a policy, the model may quote the old one.
- Unfaithful answers: the model can still add details that aren't in the context. Instruct it to answer only from the context, require citations, and evaluate faithfulness.
- Access control: retrieval must respect who is allowed to see which document, or the assistant becomes a data leak.
Why LLMs hallucinate explains the underlying cause in more depth.
Build it and design it at scale
- Build one: Build your own RAG pipeline walks through a local implementation step by step.
- Design it for production: the free GenAI System Design course has a full module on RAG architecture, starting with the anatomy of a RAG system, then ingestion and chunking, retrieval and context assembly and RAG vs fine-tuning vs long context.
- Size the vector store: the vector database sizing calculator estimates memory and storage for your corpus.
Key takeaways
- RAG = retrieve, then generate. The system looks up relevant passages and the LLM answers from them.
- It runs as two pipelines: indexing (chunk, embed, store) and query (embed, retrieve, augment, generate).
- The five building blocks are the knowledge source, embedding model, vector database, retriever and generator.
- Use RAG for facts that change or are private; use fine-tuning for behavior.
- RAG makes answers checkable, but retrieval quality, freshness and access control decide whether they are right.


