By Plainly Editorial

Updated

First published

What is RAG?

Filed under AI & modern tech

What problem does RAG solve?

A large language model only knows what was in its training data, and that knowledge stops at a cutoff date. It knows nothing about your company's internal policies, last week's product update, or the contract sitting in your shared drive. Ask it anyway and it may guess, producing a confident answer that is simply wrong.

RAG fixes this by giving the model an open-book exam instead of a closed-book one. Rather than relying on memory, the system looks up relevant material at the moment you ask and puts it in front of the model.

The term comes from a 2020 paper by Patrick Lewis and colleagues at Facebook AI Research, University College London, and New York University. They combined a language model with a searchable index of Wikipedia and found that the result produced more specific and factual answers than the model alone, and that its knowledge could be updated by swapping the index rather than retraining.

How does RAG work step by step?

1. Chunking. Your documents are split into smaller passages, often a few hundred tokens each, sometimes with a little overlap so ideas are not cut in half. 2. Embedding. Each chunk is converted into an embedding: a list of numbers that captures its meaning, so passages about similar topics end up with similar numbers. 3. Indexing. Those embeddings are stored in a vector database or search index. Steps 1 to 3 happen ahead of time.

4. Retrieval. When you ask a question, it is embedded the same way, and the system finds the chunks whose embeddings are closest to it. This is vector search, or semantic search. Many systems also run a traditional keyword search and combine the results, because keyword search is better at exact terms like product codes and names. A reranking step may then reorder the top results.

5. Prompt assembly. The best few chunks are pasted into the prompt along with your question and instructions such as “Answer using only the sources below and cite them.” 6. Generation. The model reads that prompt and writes an answer grounded in the retrieved passages, often with citations pointing back to them.

Does RAG stop AI hallucinations?

RAG reduces hallucinations but does not eliminate them. When the right passage is retrieved, the model has the facts in front of it and is much less likely to invent things. Citations also let you click through and check.

But errors still happen. If retrieval fetches the wrong passage, or nothing relevant exists, the model may fill the gap from memory. It can also misread a correct passage, merge details from two documents, or cite a source that does not actually support its claim.

Good systems reduce this by instructing the model to say “I don't know” when the sources do not contain the answer, showing citations prominently, and testing regularly against a set of questions with known answers.

RAG vs fine-tuning vs long context: which should you use?

These three approaches solve different problems. RAG is best when the model needs facts that are large in volume, change often, or must be cited, such as a help centre, a legal archive, or internal wikis. Updating knowledge is as simple as updating the documents.

Fine-tuning means further training a model on your own examples. It is best for teaching a consistent style, format, or specialized behaviour, like always writing in your brand voice or classifying tickets into your categories. It is a poor way to add facts that change, because every update means retraining.

Long context means pasting the whole document set straight into the prompt. As context windows have grown to hundreds of thousands of tokens or more, this is often the simplest option for a handful of documents. It gets expensive and slower at scale, and research such as the 2023 “Lost in the Middle” study found models can overlook information buried in the middle of very long inputs. Many real systems combine approaches: RAG to find the right material, a long context to hold plenty of it, and sometimes a fine-tuned model to answer in the right format.

Why do RAG systems give bad answers?

Most RAG failures are retrieval failures, not model failures. If the right chunk never reaches the model, even the best model cannot use it. Common causes include chunks that are too small to carry context (a passage saying “the fee is 3%” without saying which product), chunks that are too large and dilute the relevant part, and questions phrased very differently from the documents.

Other frequent problems: outdated or duplicate documents competing with the current version, tables and scanned PDFs that were extracted badly into text, keyword-heavy queries that pure vector search handles poorly, and permission mistakes where users can retrieve documents they should not see.

Fixes usually start with the data: clean the documents, chunk them along natural sections, and add titles or short summaries to each chunk. Anthropic has reported that adding a brief, chunk-specific context sentence before embedding, combined with keyword search, cut retrieval failures by about half in its tests.

Where is RAG used in real products?

RAG is one of the most common ways businesses deploy language models. Customer support assistants answer from help-centre articles and policy documents. Internal assistants search company wikis, tickets, and shared drives. Legal and compliance tools retrieve relevant clauses and regulations.

AI search engines and chatbot web-search features are a form of RAG too: they run a web search, pull text from the top results, and have the model write an answer with links. Coding assistants retrieve relevant files from your codebase before suggesting changes.

RAG vs fine-tuning vs long context

RAGFine-tuningLong context
Best forLarge or changing knowledge that must be citedConsistent style, format, or task behaviourA small set of documents used right now
How knowledge is addedUpdate the documents and re-indexRetrain on new examplesPaste documents into the prompt
Keeps up with changesYes, as soon as documents are re-indexedOnly after retrainingYes, every request
Citations to sourcesNatural fitNot really; knowledge is baked inPossible if you ask for them
Cost profileSetup effort plus small per-query costUpfront training costHigh per-query cost for big inputs
Main weaknessWrong or missing retrievalStale facts, can still hallucinateSlow, costly, can miss details in the middle
  • An HR chatbot that answers “How many days of parental leave do I get?” by retrieving the current policy document and quoting it.
  • A bank's support assistant that pulls the latest fee schedule before answering, so it never quotes last year's rates.
  • A law firm tool that searches thousands of past contracts for indemnity clauses similar to a new draft.
  • An AI search engine that runs a web search, reads the top pages, and writes a short answer with source links.
  • A coding assistant that retrieves the relevant files from a large codebase before suggesting a fix.

Common questions

Is RAG the same as giving ChatGPT a file?

Close, but not always. When you upload a short file, many chatbots simply place the whole thing in the context window. With large files or many files, they switch to RAG: splitting the content into chunks and retrieving only the relevant parts for each question. Either way, the model answers from your material rather than memory alone.

Do I need a vector database for RAG?

Not necessarily. A vector database is the common choice for semantic search at scale, but small projects can store embeddings in an ordinary database or even in memory. Some effective RAG systems use plain keyword search alone. What matters is that the retrieval step reliably finds the right passages for real questions.

Is RAG still needed now that context windows are huge?

Often, yes. Even a million-token window is far smaller than a company's full document collection, and filling it on every request is slow and expensive. Models can also miss details in very long inputs. Long context and RAG work well together: retrieval narrows the material down, and a large window holds plenty of it.

Is RAG secure for private company data?

It can be, but security has to be designed in. The retrieval step should respect the same access permissions as the original documents, so users only retrieve what they are allowed to see. You should also check where documents, embeddings, and prompts are stored and whether the model provider retains them.

How do you measure whether a RAG system works?

Build a test set of real questions with known correct answers and source documents. Then measure two things separately: whether retrieval returns the right passages, and whether the generated answer is correct and supported by them. Re-run the test whenever you change chunking, embedding models, prompts, or documents.

Sources and further reading

  1. Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (Lewis et al., 2020) — arXiv
  2. Lost in the Middle: How Language Models Use Long Contexts (Liu et al., 2023) — arXiv
  3. Introducing Contextual Retrieval — Anthropic

Glossary terms

Keep reading