News / Learn / Retrieval-augmented generation (RAG)
How does a RAG pipeline work?
You'll be able to explain each stage of a RAG pipeline and build a simple one that answers questions from your own documents.
In plain English
RAG stands for retrieval-augmented generation. Before a language model answers a question, a search step finds relevant text from your own documents and pastes it into the prompt. The model then writes an answer based on that text.
Why it matters
A language model only knows what it saw in training, and it can't see your private files or recent changes. RAG lets you answer questions about your data without retraining the model. It also gives you a way to show users where an answer came from.
How it works
A RAG pipeline has two phases. The first phase runs ahead of time, when you load your documents. You split each document into smaller pieces called chunks. Then you convert each chunk into an embedding, a list of numbers that captures its meaning, and store those embeddings in a vector database.
The second phase runs at question time. You convert the user's question into an embedding using the same method. You then compare it with the stored chunk embeddings, usually using cosine similarity, and pick the closest few chunks. This step is called retrieval.
Next, you build a prompt. It holds an instruction, the retrieved chunks, and the user's question. You send that prompt to a language model. The model generates an answer using the chunks as its source material. This step is the generation part.
A good prompt tells the model to answer only from the supplied text and to say so when the text does not contain the answer. This instruction lowers the chance of hallucination, where the model invents an answer. Grounding the answer in retrieved text is the main reason RAG exists.
A concrete example
A minimal outline of the question-time steps. The functions are placeholders you would replace with your embedding model, vector store, and language model client.
def answer_question(question, vector_store, top_k=4):
# 1. Turn the question into an embedding
q_vector = embed(question)
# 2. Find the most similar chunks
chunks = vector_store.search(q_vector, top_k=top_k)
# 3. Build a grounded prompt
context = "\n\n".join(c.text for c in chunks)
prompt = (
"Answer using only the context below. "
"If the answer is not in the context, say you don't know.\n\n"
f"Context:\n{context}\n\n"
f"Question: {question}"
)
# 4. Generate the answer
answer = call_llm(prompt)
sources = [c.source for c in chunks]
return answer, sourcesA short sketch to show the idea. Check the official docs of the tool you use.
What goes wrong
- Using different embedding models for documents and questions Embed documents and questions with the same model. Vectors from different models cannot be compared, and search results will be meaningless.
- Chunks that are too large or too small Test a few chunk sizes on real questions. Very large chunks dilute the relevant part, and very small chunks lose the context needed to answer.
- Not telling the model what to do when context is missing Include an explicit instruction to answer only from the context and to admit when the answer is absent. Without it, the model may guess.
- Skipping source tracking Store each chunk's origin, such as file name and position, and return it with the answer. This lets users verify claims and helps you debug retrieval.
Going deeper
Learn how chunking strategy affects retrieval quality, then study reranking, which reorders retrieved chunks by relevance before they reach the model. Finally, learn how to evaluate a RAG system by checking both retrieval and answer quality.
FAQ
Do I need a vector database to build RAG?
Not for a small set of documents. You can store embeddings in memory and compare them directly. A vector database becomes useful as the collection grows and you need fast search, filtering, and persistence.
Is RAG the same as fine-tuning?
No. RAG supplies relevant text at question time and leaves the model unchanged. Fine-tuning changes the model's weights using training examples. RAG is usually easier to update when your documents change.
Why does the model still sometimes give wrong answers?
Retrieval can miss the right chunk, or the model can misread the context. Grounding instructions reduce these errors but do not remove them, so you should measure answer quality on real questions.
How many chunks should I retrieve?
Start with a small number, such as three to five, and adjust based on testing. Too few may miss the answer, and too many can add noise and fill the context window.
Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.