How RAG works, step by step
- Prepare: split your documents into chunks and index them, usually as embeddings (numbers that capture meaning) and often with keyword search too.
- Retrieve: when a question arrives, find the few chunks most related to it.
- Generate: send those chunks to the model with the question, and ask it to answer from them and cite them.
When to use RAG
- Your knowledge base is larger than the model's context window, or too expensive to send on every request.
- The information changes often: prices, policies, stock, help articles.
- You need answers that point to a source a person can check.
If your documents are small and stable, simply putting them in the prompt (long context) can be simpler and just as accurate.
Why RAG systems fail
- Bad retrieval: the right passage was never found, so the model answers from memory or hallucinates.
- Bad chunks: a table or a rule split in half loses its meaning.
- Stale index: documents changed but the index wasn't updated.
Test retrieval on its own first: for a set of real questions, check whether the correct passage appears in the top results. Most RAG quality problems are found there.