May we use cookies so Meta, Google, LinkedIn and ChatGPT see your visit and we learn which ads work? PrivacyMay we use cookies to see which pages help, and let Meta, Google, LinkedIn and ChatGPT see your visit, so we know which of our ads work? That way we spend less on ads that do not work. We do not sell your data, and the site works the same if you decline. You can change your answer at any time under Cookie settings at the foot of the page. Privacy policy
Loading...
System Design and AI Engineering, by Roni Das. 992 interactive lessons in two courses.: 770 in System Design and 222 in AI Engineering.
What Is RAG? Retrieval-Augmented Generation Explained in Plain English
What is RAG in AI?
RAG, retrieval-augmented generation, is a way of building an AI app so that it answers from your own documents. Before the model writes anything, the app searches your documents for the passages that match the question, puts the best few into the prompt, and asks the model to answer only from them and to cite them. The model does not need to have been trained on your data, you update the answer by updating a document, and most wrong answers come from the search step finding the wrong passage, so that is the part to test first.
Answer last checked
Save your progress and get the AI Engineering cheat sheet PDF
AI Engineering Cheat Sheet: what our labs measured, one rule per result. 12 pages. Free with an account.
This guide answers 11 questions about RAG: what the name means, why AI apps need it, how it works step by step, what one question looks like from start to finish, where it goes wrong, and when fine-tuning or a long prompt is the better choice. The numbers come from lessons in our AI Engineering course, and outside facts are linked under each answer.
A RAG system is two programs that share one index: one prepares your documents, the other answers questions from themThe offline lane reads your documents, cuts them into chunks, turns each chunk into an embedding and stores it. The online lane takes a question, finds the closest chunks in that same store, puts the best few into the prompt and asks the model to answer from them. Both lanes must use the same embedding model, or the question and the chunks are on different maps.
A RAG system is two programs that share one index: one prepares your documents, the other answers questions from themThe offline lane reads your documents, cuts them into chunks, turns each chunk into an embedding and stores it. The online lane takes a question, finds the closest chunks in that same store, puts the best few into the prompt and asks the model to answer from them. Both lanes must use the same embedding model, or the question and the chunks are on different maps.
The short version
RAG means search first, then let the model answer from what you found. One half of the system prepares your documents as searchable chunks before anyone asks; the other half finds the best chunks for each question and puts them in the prompt. Use it for facts that change. Test the search and the answer separately, because the model can only be as right as the passages it was given.
Readers in India pay in rupees; readers in the US, UK, Singapore, Australia and the Gulf see US dollar prices and can pay by card or PayPal. The pages for your region: India, Singapore, Australia, the Gulf and UK and Europe.
This page is the free part.
The course goes deeper on What is RAG
₹1,333 in India/$35 everywhere elseonce, for the whole course
The Generative AI and Agentic AI course covers What is RAG across a run of lessons, not one page. These 4 alone are about 286 minutes of step-by-step reading, every one with code you run in the browser, all with a quiz.
The operations view of a live RAG plus LLM service: serving, latency budgets, cost per query, caching, index freshness, autoscaling, graceful degradation, retrieval monitoring, guardrails, and SLOs.
Raising k took recall@k from 45% to 90% on 818 documents, and took the number of questions the system actually got right from 10 of 20 down to 6. Same retrieval, same questions, no judge in the grading path. The component metric cannot go down, which is what makes it dangerous to optimise.
A hands-on lab on two real models. An easy test set cannot tell a free model from a paid one, 30 near misses do more damage than 2,000 unrelated documents, and a missing prefix costs 20 points of recall.
RAG is a way of building an AI app that answers from your own documents instead of only from what the model learned in training. These answers explain the name, the idea and where it came from.
What is RAG in AI?
RAG stands for retrieval-augmented generation. Retrieval means finding text, augmented means added to the prompt, and generation means the language model writing the answer. A RAG app first searches your documents for the passages that match the question, then hands those passages to the model with an instruction to answer from them.
The model does not need to have seen your documents in training. It reads them at the moment of the question, the way a person answers better with the right page of the manual open in front of them.
A language model only knows what was in its training data, which stops at a date and never included your company's private documents. Ask it about your refund policy and it will either say it does not know or, worse, write a confident answer that sounds right and is not.
RAG fixes both problems without retraining the model. You keep the knowledge in documents you control, update a document when a fact changes, and the next answer uses the new text. Because the answer comes from passages you found, the app can also show the reader where each fact came from.
Where does the term retrieval-augmented generation come from?
From a 2020 research paper by Patrick Lewis and colleagues at Facebook AI Research, University College London and New York University, titled Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. It joined a search step to a text generator so the generator could use facts it was never trained on.
Today the name covers a much wider family of systems: almost any app that searches some text first and then asks a language model to answer from what it found is called RAG.
What is the difference between RAG and a search engine?
A search engine stops at the list of results and leaves the reading to you. RAG uses a search step to build that list, then gives the best few results to a language model, which reads them and writes one answer to the question.
So search is the first half of RAG, not a rival to it. If the search step misses the right passage, the model never sees it, and no model can answer well from the wrong pages.
A RAG system has two halves that run at different times. One prepares your documents before anyone asks anything; the other answers each question. The worked example follows one question through the second half.
One RAG request, step by step: 30 candidates found, 4 kept, one call to the modelThe question is turned into an embedding and used to search the store. The search returns 30 candidate chunks, a reranker reads each one next to the question and keeps the best 4, and only those 4 go into the prompt. The model is called once and answers from them.
Before any question, the indexing half reads each document, cleans the text, cuts it into chunks of a few hundred words and turns each chunk into an embedding, which is a list of numbers that stands for its meaning. The embeddings are stored in a vector index, often called a vector database.
For each question, the answering half turns the question into an embedding with the same model, finds the chunks whose embeddings are closest, keeps the best few, puts them into the prompt with the question and asks the model to answer only from them and to name its sources.
What does one RAG question look like from start to finish?
Take a support assistant and the question "How long do I have to return a damaged item?". The app first checks a cache of past answers and misses. It turns the question into an embedding and asks the vector index for the 30 closest chunks. A reranker, a slower model that reads the question and each chunk together, keeps the best 4. Those 4 chunks, perhaps the returns policy and the damaged goods page, go into one prompt, and the model writes a short answer that cites them.
In the worked latency budget in our production RAG lesson, everything up to and including reranking takes about 160 milliseconds and the model takes about 1,100. That is why most of the work of making RAG fast and cheap goes into the model call, not the search.
What we measured: The lesson's worked budget for one request that misses the cache: about 160 ms for gateway, cache check, embedding, vector search and reranking 30 candidates; about 1,100 ms for the model; first token near 402 ms.
How many chunks should a RAG app put into the prompt?
There is no safe default, and more is not always better. More chunks make it likelier that the right one is somewhere in the prompt, but they also give the model more wrong text to be distracted by.
In one of our evaluation lessons, a small model answered 20 questions with 5 chunks and then with 20 chunks. With 20 chunks the right document was in the prompt for 90% of questions instead of 55%, yet the model actually used it in its answer for 6 questions instead of 10. Measure the final answers, not only the search.
What we measured: One small model, 20 questions: right document in the prompt 55% with 5 chunks, 90% with 20; used in the answer 10 of 20, then 6 of 20.
3. Where RAG goes wrong, and when to use something else
RAG is the default way to give a model your knowledge, but it fails in quiet ways: the app still answers, just from the wrong passage. These are the failures to expect and the cases where RAG is not the right tool.
Why does a RAG app give wrong answers?
Most wrong answers start before the model writes anything. A PDF table was read as a jumble of numbers, a chunk cut a rule in half, or the search returned an old version of the policy that looks almost the same as the new one. The model then answers faithfully from the wrong text.
Look-alike documents are the worst case. In our embedding lesson, adding 2,000 unrelated documents to a small test cut how often the right one came first by 20 points, while adding just 30 near-copies of the right documents cut it by 30 points. Check the retrieved passages before you blame the model.
What we measured: 10 questions: 2,000 unrelated documents cut recall@1 by 20 points; 30 near-miss documents cut it by 30 points.
Yes, unless you design for it. Every new chunk is one more chance that a wrong passage scores a little higher than the right one. With a few hundred documents that almost never happens; with millions it happens often enough to push the right passage out of the top results, and the app gets worse without any error.
The fixes are to search fewer chunks at once, by filtering on metadata such as product or date first, and to lower the chance that a wrong chunk wins, with a reranker or hybrid search. In our reranking lab on 818 lessons, a reranker lifted the right lesson into the top 5 for 75% of questions instead of 55%.
What we measured: Reranking lab, 818 lessons, 20 questions, recall at 5: embeddings alone 55%, embeddings plus a reranker 75%.
Use RAG for facts, especially facts that change, such as prices, policies and product documents. You update a document and the next answer is current, and every answer can point to its source.
Use fine-tuning to change how a model behaves, such as its tone, its output format or a narrow task it must do the same way every time. Fine-tuning is a poor place to store facts that change every week, and many real systems use both.
Not for most real collections. The context window is the most text a model can read in one call, and even a large one is far smaller than a company's documents. When the text does fit, every question still pays for all of it and waits for all of it, because cost and delay grow with the tokens you send.
For a handful of short documents, putting everything in the prompt is simpler and fine. Once you have more text than that, RAG sends only the few passages that matter.
How is What is RAG done in a real production system?
The answers on this page are the short version. The lessons below are the long one, with the trade-offs and failure cases, starting with "Production RAG and LLM Systems: Running It, Not Just Building It".
The lessons that teach the next part
The operations view of a live RAG plus LLM service: serving, latency budgets, cost per query, caching, index freshness, autoscaling, graceful degradation, retrieval monitoring, guardrails, and SLOs.
Raising k took recall@k from 45% to 90% on 818 documents, and took the number of questions the system actually got right from 10 of 20 down to 6. Same retrieval, same questions, no judge in the grading path. The component metric cannot go down, which is what makes it dangerous to optimise.
A hands-on lab on two real models. An easy test set cannot tell a free model from a paid one, 30 near misses do more damage than 2,000 unrelated documents, and a missing prefix costs 20 points of recall.
Every answer on this page comes from our AI Engineering course: 222 lessons on RAG, evals, agents, serving, security and MLOps. Many of them are built around a real experiment. You learn why the answer is right, which is what an interviewer checks with the second question. 10 lessons are free to read, with no card needed.
These questions are a start. The interview asks more.
Get all 150 questions in the AI Engineering (LLM applications) pack
Each question has why it is asked, a full answer, what the interviewer listens for, the follow-ups, the common mistakes and a diagram (180 diagrams in all). A PDF you keep, junior to staff level.