Skip to content
Koit Academy
AI Guides·

8 min read

What Is Retrieval-Augmented Generation (RAG)? Explained

How retrieval-augmented generation lets an AI answer from your own documents - chunking, embeddings, search and the prompt - and where it goes wrong.

By Koit Academy

A language model knows a great deal about the world in general and nothing about your business in particular: not your prices, your policies, your product manuals or last week’s meeting notes. It also cannot take in a whole library of documents at once, because every model has a limit on how much text it can handle in one request.

Retrieval-augmented generation, usually shortened to RAG, is the standard answer to both problems. Instead of giving the model everything, a system finds the few passages relevant to each question and gives the model those, with an instruction to answer from them. This post explains how that works, step by step, and where it tends to go wrong.

The idea in one paragraph

The term comes from a 2020 research paper by Patrick Lewis and colleagues, which combined a language model with a searchable index of documents. When a question arrives, a retriever searches the index for relevant passages; those passages are added to the model’s input, and the model generates its answer from them. The model’s built-in knowledge is supplemented, or “augmented”, by what was retrieved. Modern RAG systems follow the same pattern with different components.

Step 1: split the documents into chunks

Documents are first broken into smaller passages called chunks, because a search that returns a whole 80-page manual has not narrowed anything down. Each chunk is typically a few hundred tokens, and neighbouring chunks often overlap a little, so that a sentence on a boundary appears whole in at least one of them.

The chunk size calculator shows what that means in numbers. With 100,000 tokens of documents, chunks of 500 tokens and an overlap of 50, the documents make 223 chunks, and the overlap adds 11.1% to the text that has to be processed.

Step 2: turn each chunk into an embedding

Each chunk is then passed through an embedding model, which turns text into a list of numbers that represents its meaning. Passages about similar things end up with similar numbers, even if they use different words: “refund window” and “how long do I have to return an item” land close together. The embeddings are stored in a vector database, or any index that can find the nearest matches quickly.

This step happens once for each document, and again whenever a document changes. Embedding is usually priced per token, much like a language model, so the overlap between chunks has a small, measurable cost.

Step 3: search when a question arrives

When someone asks a question, the question is embedded in the same way, and the index returns the chunks whose embeddings are closest to it: the top three, five or ten, depending on the system. Many systems combine this with ordinary keyword search, which is better at exact matches such as product codes, names and numbers.

Step 4: answer from what was found

The retrieved chunks are placed into the prompt, together with the question and an instruction along the lines of: “Answer the question using only the passages below. If they do not contain the answer, say so.” The model writes its answer from those passages, and a good system shows which documents each part of the answer came from, so the reader can check.

Every retrieved chunk is input the model must read, and it all has to fit in the context window alongside the instructions and the answer. Five chunks of 500 tokens add 2,500 tokens to every question. The context window calculator shows how much room is left, and why there is a limit to how much can be retrieved; for more on the window itself, see what is a context window.

A small example

Imagine a bakery that supplies cafés and keeps its supplier terms, allergen sheets and delivery policy in a shared folder. A staff member asks: “Can we deliver to a new café on a Sunday?”

  1. The question is embedded and compared with the chunks from every document.
  2. The closest match is a passage from the delivery policy: deliveries run Tuesday to Saturday, with Sunday deliveries only for standing orders agreed in advance.
  3. That passage, with its document title, goes into the prompt with the question.
  4. The model answers that Sunday delivery is not available for a new café unless a standing order is agreed, and names the delivery policy as the source.

Nothing in the model’s general knowledge could have answered that. The answer came from the retrieved passage, and the source makes it easy to check.

Keeping the index current

A RAG system answers from whatever is in its index, so the index has to change when the documents do. Re-embed a document whenever it is edited, remove the chunks of documents that are withdrawn, and keep the date and version of each source with its chunks. When two versions of a policy are both in the index, the model may quote the wrong one with complete confidence, which is worse than saying nothing.

Why not just paste everything in?

With very large context windows, it is tempting to skip retrieval and send every document with every question. For a small collection that can work. For anything larger, retrieval wins on three counts:

Paste everythingRetrieve the relevant passages
Fits in the windowOnly for small collectionsYes, at any size
Cost per questionEvery document, every timeA few thousand tokens
Answer qualityRelevant detail can be lost among the restThe model sees mainly what matters

The cost difference is large. A question sent with 100,000 tokens of documents pays for 100,000 input tokens; the same question with five retrieved chunks pays for about 2,500. The AI API cost calculator prices the difference at your provider’s rates.

Where RAG goes wrong

Most RAG failures are retrieval failures: the model answered from the wrong passages, or from none.

  • Chunks that are too small split an answer across several pieces, so no single chunk contains it.
  • Chunks that are too large match vaguely and fill the prompt with irrelevant text.
  • Missing context in a chunk. A passage that says “the fee is 30” without saying which fee is useless once separated from its heading. Adding the document title and section heading to each chunk helps.
  • Out-of-date documents. The answer is only as current as the index. Old price lists produce old prices.
  • Questions the documents cannot answer. Without a clear instruction to say “I don’t know”, the model may fill the gap from its general knowledge.

Choosing a chunk size

There is no universal best chunk size; it depends on the documents and the questions. Short, self-contained documents such as FAQ answers often work best as one chunk each. Long manuals and policies usually need splitting at natural boundaries such as headings and paragraphs, with a size that keeps each idea whole. The reliable method is to build a small set of real questions with known answers, try two or three chunk sizes, and compare which one retrieves the right passages most often.

Is RAG the right tool?

RAG suits questions answered by looking something up: policies, manuals, product details, internal knowledge, support articles. It is less suited to tasks that need the whole of a document at once, such as summarising a full report or comparing two contracts clause by clause. For those, send the documents themselves if they fit, or work through them in parts.

What a good RAG system needs

The technology is the easier part. Most of the quality comes from the material and the checking:

  • Clean, current documents, with clear headings, so chunks carry their own context.
  • The title and section of each chunk stored with it and shown to the model.
  • A set of real test questions with known answers, re-run whenever the documents, the chunk size or the model changes.
  • An instruction to cite sources and to say so when the passages do not contain the answer.
  • A person who owns the documents, so outdated versions are removed rather than left to be retrieved.

Summing up

Retrieval-augmented generation splits your documents into chunks, turns each chunk into an embedding, finds the chunks closest to each question, and gives the model only those to answer from. It keeps costs and input sizes small, lets a model work with collections of any size, and makes answers traceable to their sources. Most of its quality comes from the retrieval side: sensible chunks, good context in each one and a clear instruction to admit when the answer is not there.

Sources, read November 14, 2026: