What is RAG? Retrieval-augmented generation for AI systems explained

Retrieval-augmented generation lets an AI system answer from your documents instead of its training data. We explain how the pipeline works, where it breaks, and how to evaluate the retrieval and the generation separately.

Ask a general-purpose language model about your company's return policy and it will answer. The answer will be fluent, it will sound like a policy, and it will have been assembled from whatever return policies appeared in the model's training data, none of which are yours. The model has no way to know that your window is 30 days for unused items, that refunds go back to the original payment method, or that the policy changed last quarter. A language model knows only what it saw during training, frozen at a cutoff date, and it has no access to your documents, your database or anything that happened after that date. When a product needs answers grounded in a specific body of knowledge, the model alone cannot supply them, and it will not tell you that it cannot.

This is the problem retrieval-augmented generation, usually shortened to RAG, exists to solve. RAG changes what the model sees at the moment it answers. Instead of relying on memory, the AI system first looks up the passages relevant to the question in your own documents, then hands those passages to the model together with the question and asks it to answer from them. The knowledge moves out of the model's weights and into a store you control and can update in minutes. That one architectural change is why RAG became the default pattern for AI assistants that sit on top of documentation, support knowledge bases, contracts, policies and internal wikis.

This guide explains what RAG is, how the pipeline works step by step, how it compares with fine-tuning and long-context prompting, and where it breaks. Because we evaluate AI systems for a living, we spend the second half on the part most introductions skip, which is how you tell whether a RAG system is actually working, and which stage is at fault when it is not.

What retrieval-augmented generation is

Retrieval-augmented generation is a pattern in which an AI system retrieves relevant text from an external source at query time and gives it to a language model as context for producing the answer. The term comes from a 2020 paper by Patrick Lewis and colleagues, Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks, which combined a neural retriever over a dense vector index of Wikipedia with a pre-trained sequence-to-sequence model and showed that the combination produced more specific and more factual language than the generator on its own. The paper's framing is still the clearest way to think about it. The model's weights are its parametric memory, fixed at training time. The document index is non-parametric memory, which you can edit without touching the model.

In practice the term has broadened well beyond that paper. Almost nobody runs the original architecture today. What people call RAG is a pipeline built around an off-the-shelf model behind an API, a chunked and embedded copy of their documents, and a prompt that stitches the two together. The retriever is often a vector database, sometimes a keyword search engine, increasingly a hybrid of both. The source does not have to be documents either. A RAG system can retrieve rows from a database, tickets from a support tool, or the results of a web search. What makes it RAG is the shape, retrieve first and then generate conditioned on what was retrieved.

It helps to be precise about what RAG does and does not change. It does not make the model smarter, and it does not teach the model anything permanent. The model's reasoning, writing style and general knowledge are exactly what they were before. What changes is the evidence available to it for this one answer. If the evidence is right and the model uses it, the answer is grounded. If the evidence is wrong, missing or ignored, the answer is no better than it was without RAG, and often worse, because it now carries the authority of apparently cited sources. That distinction, between the evidence and the use of the evidence, is the thread running through the rest of this article.

How a RAG pipeline works

A RAG system has two phases that run at different times. The indexing phase happens before any user asks anything, and it prepares your documents so they can be searched by meaning rather than by exact words. The query phase happens every time a question arrives. Most of the engineering effort goes into indexing, and most of the quality problems you will later see trace back to decisions made there, long before the model is involved.

A highlighted panel describing document indexing above three cards in sequence, Retrieve, Augment and Generate, each with a short description, and a caption saying retrieval and generation fail differently and need separate metrics.
The stages of a RAG pipeline
  1. Chunking. Documents are split into pieces small enough to be retrieved and placed in a prompt, typically a paragraph to a page. Chunk boundaries matter, because a policy clause split across two chunks may never be retrieved whole.

  2. Embedding. Each chunk is passed through an embedding model, which turns text into a vector of numbers so that chunks with similar meaning end up close together in that space.

  3. Indexing. The vectors are stored in a vector index next to the chunk text and its metadata, such as the source document, the section and the date it was last updated.

  4. Retrieval. At query time the question is embedded the same way and the index returns the chunks whose vectors sit closest to it, ranked by similarity. The number returned is usually called top-k.

  5. Augmentation. The retrieved chunks are inserted into the prompt alongside the question, with instructions to answer from the provided material and, often, to say when the material does not cover the question.

  6. Generation. The model writes the answer. Many systems also return the chunks they used as sources, so the user can check the claim against the passage.

Production systems add stages on top of this skeleton, and each one is a place where quality can be gained or lost. Hybrid search runs a keyword query alongside the vector query so that exact identifiers like a product code or an error number are not lost to approximate similarity. A reranker takes the first twenty or fifty retrieved chunks and reorders them with a more expensive model before the top few go to the prompt. Query rewriting asks a model to restate a vague user question into something the index will match better. Each of these can raise answer quality, and each adds a component whose failures look identical to the user, who only ever sees the final sentence.

The consequence is that a RAG answer is the product of every stage, not of the model. When the answer is wrong, the model is the last and often the least likely suspect. The chunk may have been cut badly, the embedding may have placed it far from the question, the index may hold a stale version, retrieval may have returned it in position eight when only five were passed on, or the prompt may have buried it under noise. You cannot tell which from the answer alone. That is why the evaluation sections later in this article treat retrieval and generation as separate things to measure.

RAG, fine-tuning and long context

RAG is one of three common ways to get a model to use knowledge it was not trained on, and teams often reach for it without comparing the alternatives. Fine-tuning continues training the model on your own examples so that the knowledge or behaviour ends up in the weights. Long-context prompting skips retrieval and puts the entire corpus into the prompt every time, relying on the large context windows current models offer. Each approach puts the knowledge in a different place, and that placement decides how you update it, what it costs per query and how it fails.

Dimension

RAG

Fine-tuning

Long-context prompting

Where the knowledge lives

In an index you own

In the model's weights

In the prompt, every call

How you update it

Re-index the changed document

Collect data and retrain

Edit the prompt

Good for

Facts that change, large corpora, answers that need sources

Style, format, domain vocabulary, consistent behaviour

Small, stable corpora that fit in one prompt

Main weakness

Retrieval misses, and the answer inherits the miss

Facts go stale and cannot be cited

Cost per query grows with the corpus, and attention degrades in long inputs

Can the user verify the source

Yes, the chunk can be shown

No

In principle, but the whole corpus is the source

The table hides a point that matters in practice. Fine-tuning is poor at teaching facts and good at teaching behaviour. A model fine-tuned on your support transcripts will learn your tone, your product names and the shape of a good reply, but it will still hallucinate the specific return window, because a few hundred examples do not reliably overwrite what it absorbed from billions of tokens. RAG is the mirror image. It is excellent at supplying the specific fact for this question and does nothing for tone or format. The two are often combined, with a fine-tuned or carefully prompted model doing the writing and a retriever supplying the evidence.

Long-context prompting deserves a fair hearing, because for a small corpus it is simpler than RAG and avoids the retrieval failure modes entirely. If your entire knowledge base is forty pages and changes rarely, putting all of it in the prompt removes the chunking, embedding and ranking problems at a stroke. The costs are per-query price, since every call carries the whole corpus, and the known tendency of models to use information near the start and end of a long prompt more reliably than information in the middle. Once the corpus is hundreds of documents, the choice is no longer open, and RAG is the only one of the three that scales to it.

Where RAG systems break

RAG failures fall into two families, and keeping them apart is the single most useful habit when debugging. Retrieval failures happen before the model sees anything. The right passage was not in the index, or it was in the index but was not returned, or it was returned but ranked below noise that pushed it out of the top-k, or what came back was topically related but did not actually contain the answer. In every one of these cases the model is handed evidence that cannot support a correct answer, and whatever it writes is a guess dressed up as a grounded reply.

Generation failures happen after retrieval did its job. The passage with the answer is in the prompt, and the model still gets it wrong. It may state a claim the passage does not support, which is the RAG-specific form of hallucination, hallucination against context. It may blend the retrieved text with something from its training data, so a correct 30-day window acquires a fabricated exception. It may ignore the passage and answer from memory, which is common when the retrieved text contradicts a strong prior. Or it may refuse to answer a question the passage clearly covers, because the instruction to stay grounded was applied too aggressively. These look nothing alike from the inside, but to a user every one of them is simply a wrong answer.

A concrete case shows how the two families interact. Suppose a user asks whether they can return an opened item. The knowledge base has the current policy, which says unused items can be returned within 30 days of delivery, and also the previous year's policy, which allowed opened returns with a restocking fee and was never removed from the index. Retrieval returns both chunks, the old one ranked first because its wording about opened items matches the question better. The model, following its instructions faithfully, answers from the top passage and tells the user about a restocking fee that no longer exists. Faithfulness to the retrieved context is perfect. The answer is wrong. The fault is in indexing and ranking, and no amount of prompt engineering on the generation side will fix it.

Now suppose the stale chunk is removed and only the current policy comes back. The model reads "unused items can be returned within 30 days of delivery" and answers that the opened item can be returned within 30 days. It has dropped the word unused, perhaps because its training data is full of lenient return policies, and produced a claim the passage does not support. Retrieval was correct this time and generation drifted. Same question, same wrong-looking outcome for the user, opposite root cause. An AI system that only reports whether the final answer was right cannot distinguish these two situations, and a team that only measures final answers will fix the wrong component.

How to evaluate a RAG system

Because the failures live in two places, the evaluation has to look in two places. The approach we use in EvaliQA, and which the wider field has converged on, is a set of RAG evaluation metrics scored by an LLM judge, a second model that reads the question, the retrieved context and the answer, and grades a specific property of each. Each metric isolates one stage, so that when a score drops you already know where to look. The metrics need the retrieved passages, not just the answer, which is a practical requirement your AI system has to meet before any of this works.

Faithfulness is the generation-side metric. It checks whether every claim in the answer is supported by the retrieved context, and anything below 1.0 means at least one claim was fabricated or imported from outside the passages. The default threshold in EvaliQA is 0.7, and the documentation suggests raising it to 0.9 or 0.95 in medical, legal or financial products where any ungrounded claim is a bug. In the opened-item example above, the second failure, where the model dropped the word unused, is exactly what Faithfulness catches. The first failure, the stale policy, passes Faithfulness cleanly, because the answer was grounded in what it was given. Faithfulness does not know the context was wrong.

The retrieval side has three metrics, and they answer different questions. Contextual Recall asks what proportion of the claims in the expected answer are supported by the retrieved context, so it needs a gold answer in the dataset and tells you whether retrieval brought back everything needed. Contextual Precision asks whether the retriever ranked the most useful chunks first, which matters whenever only the top few are passed to the model, and it takes an optional top-k parameter so the metric reflects what the model actually saw. Contextual Relevancy asks whether the retrieved passages are on-topic for the question at all, without needing a gold answer. All three default to a threshold of 0.7 except Contextual Relevancy at 0.6. The stale-policy failure shows up in Contextual Precision, since the wrong chunk outranked the right one, and if the gold answer mentions that opened items cannot be returned, in Contextual Recall as well.

Two general metrics round out the set. Answer Relevancy scores how directly the answer addresses the question, with a 0.6 default, and catches the answer that is grounded and accurate but about the wrong thing. Correctness against a gold answer is a separate concern, and which metric fits depends on the answer's shape. Answer Precision compares text and suits short factual answers like a number or a date, while Semantic Similarity or a G-Eval rubric suit free text. None of the RAG metrics check correctness directly, and that is deliberate. An answer can be faithful to retrieved context, highly relevant and still wrong, if the context was wrong, and you want that case to show up as a retrieval score rather than hide inside a correctness score.

You do not need all of these on day one, and attaching all six at once turns the pass rate into noise. The starting set we recommend for a RAG assistant is Faithfulness plus Contextual Recall, plus one correctness metric suited to your answer format. Faithfulness covers hallucination against context, Contextual Recall covers incomplete retrieval, and between them they catch the two most common RAG bugs while keeping retrieval quality and generation quality on separate dials. Add Contextual Precision when you start working on the retriever itself, and Contextual Relevancy when you suspect noise in the passages but have no gold answers to drive Recall.

Reading the scores together

The value of separate metrics is not in any single number but in the pattern across them, and the disagreements are the most informative results you get. Take a plan running Faithfulness, Contextual Recall and Answer Precision on a dataset of questions with gold answers. If Contextual Recall is high and Answer Precision is low, retrieval delivered the material and generation failed to use it, so the fix is in the prompt, the model or the instructions about how to treat the context. If Contextual Recall is low, the answer could not have been right no matter how good the model is, and prompt work is wasted until the retriever improves. The documentation puts this bluntly, and we agree. When Recall is low, fix retrieval first.

Other combinations carry their own diagnosis. Contextual Relevancy low while Answer Precision is high means the model answered correctly despite irrelevant passages, which sounds like good news and is not. The model is guessing well from training data, the retrieval is not contributing, and the next question that falls outside the model's prior will fail silently. Faithfulness high while Answer Precision is low, with Recall also high, points at a model that is grounding its answer in the right passages but reading them incorrectly, often a sign that chunks are too long or too dense for the model to extract the right clause. Faithfulness low with Recall high is the clearest generation bug there is, the evidence was present and the model wrote something else.

Return to the opened-item example and imagine it as a row in a dataset, with the gold answer stating that opened items cannot be returned under the current policy. In the stale-policy version of the failure, you would expect Faithfulness to pass, Contextual Precision to fall below its 0.7 default because the wrong chunk outranked the right one, and the correctness metric to fail. That pattern says retrieval, and more specifically ranking or index hygiene. In the dropped-word version, you would expect Contextual Recall and Precision to pass, Faithfulness to drop because the claim about opened items is unsupported, and correctness to fail. That pattern says generation. The user saw the same wrong answer both times. The metrics did not, and that is the point of having them.

A note on what this does not catch. The RAG metrics judge the answer against the retrieved context and the gold answer, not against the world. If your knowledge base itself is wrong, every metric can pass and the user is still misled. If the question is ambiguous and the gold answer picked one reading, a sensible answer under the other reading fails Recall and correctness without any component being broken. LLM judges also have their own error rate, so a single row's scores are a lead to investigate, not a verdict, and the signal comes from the pattern across a dataset and across runs. The judge model should be pinned between runs you intend to compare, otherwise a score change can reflect the judge rather than your AI system.

When RAG is the wrong tool

RAG is so common that it is applied to problems it does not fit, and recognising those saves months. The clearest case is when the problem is behaviour rather than knowledge. If the AI system knows the facts but writes in the wrong register, ignores a format, or handles a workflow inconsistently, retrieval has nothing to offer, because there is no missing passage. Fine-tuning or better prompting addresses that. Teams sometimes build a retriever over style guides and example replies in the hope that showing the model good examples at query time will fix tone, and it helps a little, but it is an expensive way to do what a system prompt or a small fine-tune does directly.

A second poor fit is any question that requires aggregating across many documents rather than finding one passage. How many of our contracts contain a termination-for-convenience clause, or what is the average response time across last month's tickets, cannot be answered by retrieving the top five chunks. Retrieval finds the most similar passages, not all relevant ones, and the model cannot count what it was not shown. These are database questions, and the right design is to give the AI system a tool that runs the query, which is the territory of AI agents and tool use rather than retrieval. A RAG system asked these questions will produce a confident number from a sample it did not know was a sample.

Third, retrieval over text assumes the knowledge is in text. Tables, diagrams, scanned forms and the numbers in a spreadsheet survive chunking and embedding badly. A table split across chunks loses its header row, a diagram becomes a caption, and the embedding of a row of figures is close to the embedding of every other row of figures. If your source material is mostly structured or visual, the indexing phase needs purpose-built handling, and sometimes the honest conclusion is that a search interface or a structured query is what the user needs, with the model summarising the result rather than retrieving it.

Finally, RAG adds latency and moving parts. Every query now involves an embedding call, an index lookup, possibly a reranker and a longer prompt, and every one of those has to be deployed, monitored and kept in sync with the documents. For a stable corpus that fits in a prompt, long-context prompting is simpler and has fewer failure modes, as discussed above. The question to ask is whether the knowledge changes often enough, and is large enough, to justify owning a retrieval pipeline. For documentation, policies and support knowledge bases the answer is usually yes. For a product description that changes twice a year, it may not be.

Summary

Retrieval-augmented generation solves one specific problem, a language model that cannot know your documents, by moving the knowledge out of the model's weights and into an index you control, then supplying the relevant passages at the moment of each answer. The pipeline that does this has an indexing phase, where documents are chunked, embedded and stored, and a query phase, where the question is embedded, the closest chunks are retrieved and ranked, and the model generates from them. Compared with fine-tuning it is better at facts and worse at behaviour, and compared with long-context prompting it is the only option once the corpus is large, at the price of a retrieval stage that can fail on its own.

That retrieval stage is the reason RAG cannot be evaluated by looking at answers alone. The same wrong answer can come from a stale chunk ranked first or from a model that dropped a word from a correct passage, and the fix for each is in a different part of the system. Separate metrics for the two stages, Faithfulness for whether the answer stayed inside the evidence and Contextual Recall, Precision and Relevancy for whether the evidence was the right evidence, let you read the pattern and go straight to the broken component. Start with Faithfulness and Contextual Recall plus a correctness metric, make sure the judge actually receives the retrieved passages, pin the judge between runs, and treat a single row's score as a lead rather than a verdict. The RAG metrics will not tell you when your knowledge base itself is wrong, and no evaluation will, so the index deserves the same review discipline as the code around it.

Frequently asked questions

What does RAG stand for and what does it do?

RAG stands for retrieval-augmented generation. It is a pattern in which an AI system first retrieves passages relevant to a question from an external source, such as a chunked and embedded copy of your documents, and then gives those passages to a language model as context for writing the answer. The model's knowledge stays the same; what changes is the evidence it has in front of it for that one answer.

How is RAG different from fine-tuning?

Fine-tuning puts knowledge or behaviour into the model's weights by continuing training on your examples. It is good at teaching tone, format and domain vocabulary and poor at teaching specific facts, which go stale and cannot be cited.

RAG keeps the facts in an index you can update by re-indexing a document and supplies them at query time, so the answer can show its source. The two are often combined, with the model handling the writing and the retriever supplying the evidence.

Why does a RAG system still hallucinate?

Two reasons, and they need different fixes. Retrieval may have returned passages that do not contain the answer, so the model guesses. Or retrieval was fine and the model stated a claim the passages do not support, which is hallucination against context. Faithfulness catches the second case; Contextual Recall and Contextual Precision catch the first.

Which metrics should I start with for a RAG assistant?

Faithfulness plus Contextual Recall, plus one correctness metric suited to your answer format. Faithfulness tells you whether the answer stayed inside the retrieved context, and Contextual Recall tells you whether retrieval brought back everything the gold answer needs. Together they catch the two most common RAG bugs while keeping retrieval and generation quality on separate dials. Add Contextual Precision when you work on the retriever itself.

What do the RAG metrics need from my AI system?

The retrieved passages, not just the answer. In EvaliQA they reach the judge through the connector's Retrieval context path, which points at the list of passages in your system's response, or through a retrieval_context column in the dataset if your system does not return them. Without one of the two, Faithfulness and the Contextual metrics have nothing to score.

Get new posts by email

One email when something new is published. No spam, unsubscribe any time.