What is RAG? Retrieval-Augmented Generation Explained

Blog / What is RAG? Retrieval-Augmented Generation Explained
What is RAG, or retrieval-augmented generation? And why do so many AI apps that answer questions about your own documents use it?

The Knowledge Problem

A large language model only knows what was in its training data. It has a strict knowledge cutoff, and it has never seen your private documents, like a company help center or an internal wiki.When the model does not know something, it can confidently make up an answer. This is called a hallucination.Retraining the model every time a document changes is simply not practical. So how do we give the model fresh facts without retraining it? We prepare our documents in advance.

The Indexing Phase

This phase is done ahead of time, before any question is asked.First, we load our documents and split them into smaller pieces called chunks. Then we turn each chunk into an embedding. An embedding is just a list of numbers that represents the meaning of the text, so text with a similar meaning ends up close together.Finally, we store these embeddings along with their original text and metadata in a vector database. A vector database is a database built to store embeddings and quickly find the ones closest to a given embedding.
Documents split into chunks, each chunk embedded, and the embeddings stored with their text and metadata in a vector database
Choosing the chunk sizeChoosing the right chunk size is an important balance. Small chunks match a question precisely but can lose surrounding context. Large chunks keep context but blur the match and use more of the prompt.

The Query Phase

This phase happens on every single question.
  • Retrieval: we embed the user's question with the exact same embedding model used during indexing, then query the vector database for the most similar chunks.
  • Augmented: we insert these chunks into the prompt alongside the user's question.
  • Generation: the model reads this combined prompt and writes an answer using only that context.
The question is embedded and searched, the top chunks and the question go into the prompt, and the LLM writes an answer that cites its sources
The prompt template we send to the model usually looks like this:
text
Answer the question using only the provided context. Do not use your own outside knowledge. If you use information from the context, you must cite the source document. If the context does not contain the answer, say "I don't know." Context: {context} Question: {question}

A Question End to End

Let's walk through one concrete example. A customer asks a support bot, "How do I reset my password?"
  • Step 1: the system turns that question into an embedding.
  • Step 2: the vector database compares that embedding to all the others and retrieves the chunks from the password reset help article.
  • Step 3: those paragraphs are pasted into the prompt under the {context} placeholder, and the customer's question goes into the {question} placeholder.
  • Step 4: the model reads the instructions, sees the steps in the context, and answers the customer with a direct link to the article it used.
A customer's question embedded, the two password reset chunks retrieved and pasted into the prompt, and the LLM answering with a link to the article
But the model can only give a good answer if it gets good context.

Making Retrieval Better

The final answer can only be as good as the chunks we retrieve. Three common fixes improve retrieval:
  • Metadata filtering narrows the search to only the right product, language, or the documents a user is allowed to see.
  • Hybrid search combines meaning-based vector search with keyword search to catch exact names, codes, and acronyms.
  • Re-ranking uses a second model to reorder the retrieved chunks so the best ones go into the prompt.
Even with these improvements, a RAG system reduces hallucinations but does not eliminate them entirely.

Failure Modes and How to Evaluate Them

A RAG answer usually fails in one of two places.Retrieval failsThe right chunk was not found, or the index is stale. We measure this with retrieval quality, which checks if we actually fetched the correct chunks.Generation failsThe right chunks were in the prompt, but the model ignored them or added things they do not say. We measure this with faithfulness, which checks if the final answer sticks strictly to the retrieved context.

RAG vs Fine-Tuning vs a Long Context Window

RAG does not change the model's weights. It gives the model the right text at question time.Fine-tuning changes the weights, and it is better for teaching the model a specific style, format, or behavior than for adding fresh facts.A long context window can hold a few whole documents, but sending millions of documents on every single question is slow and costly.
RAGFine-tuningLong context
What it changesThe prompt at question timeThe model's weightsThe prompt at question time
Best forAdding fresh facts and knowledgeTeaching style, format or behaviorAnalyzing a few whole documents at once
Keeping knowledge freshInstant, just update the databaseSlow, requires retraining the modelInstant, just change the attached files
CostLow per questionHigh upfront training costVery high per question

Recap

  • RAG solves the problem of a language model not knowing your private data or making up answers.
  • During the indexing phase, documents are split into chunks, embedded, and stored in a vector database.
  • During the query phase, the question is embedded, similar chunks are retrieved, and the model answers from them and cites its sources.
  • A RAG system can fail at retrieval or generation, which we measure using retrieval quality and faithfulness.
  • Pick RAG when you need to give a model fresh facts without the cost of fine-tuning or sending millions of documents in a long context window.
Retrieval-augmented generation grounds the model's answers in your own documents, so they are more accurate and easier to check.

About TechPrep

Never walk into a technical interview unprepared again. TechPrep empowers software engineers to stop guessing and start getting offers. We provide the exact questions asked by tech companies across Data Structures & Algorithms, System Design, Low-Level Design & AI coding rounds. Don't leave your career up to chance. Join thousands of engineers who have successfully navigated the tech hiring maze and landed roles at top tech companies.