Skip to content
AI & MLintermediate

What is retrieval-augmented generation?

Updated Nov 30, 20263 min readBased on official documentation
What is retrieval-augmented generation?

In short

Retrieval-augmented generation, or RAG, is a method where an AI model first looks up relevant text from your own documents and then uses that text to write its answer. It helps models respond with current or private facts they were never trained on, and it lets them point to the source material behind an answer.

The core idea

A language model answers questions using what it learned during training. That knowledge has a cutoff date, and it does not include your company's internal documents. Retrieval-augmented generation, usually shortened to RAG, fixes this gap in two steps. First, the system searches a collection of your documents for passages related to the question. Second, it passes those passages to the model together with the question, so the model can write an answer grounded in that text.

The word "retrieval" refers to the search step. The word "generation" refers to the model writing the final reply. The augmentation is the extra text you hand to the model along with the user's question.

How the search step works

Most RAG systems rely on semantic search. Semantic search compares the meaning of a query with the meaning of stored text, rather than matching exact words. The OpenAI Retrieval API documentation describes it this way: the search can surface relevant results even when they share few or no keywords with the query. The documentation gives an example where the best match for "When did we go to the moon?" is a sentence about the first lunar landing in 1969, even though it contains none of the query's words.

To make this work, each piece of text is turned into a numeric vector called an embedding. Texts with similar meaning get vectors that are close together. The vectors are stored in an index, which the documentation calls a vector store. When a user asks something, the question is also turned into a vector, and the system returns the stored chunks whose vectors are closest to it.

Long files are split into smaller pieces, called chunks, before indexing. According to the same documentation, the default is 800 tokens per chunk, with 400 tokens of overlap between neighbouring chunks. The overlap helps a sentence that falls on the boundary between two chunks still appear in full in at least one of them.

A simple flow

A basic RAG pipeline usually follows these steps:

  • Load your documents and split them into chunks.
  • Create an embedding for each chunk and store it in a vector store.
  • When a user asks a question, search the store for the most similar chunks.
  • Insert the best chunks into the prompt, next to the question.
  • Ask the language model to answer using only that context.

The Retrieval API example in the documentation follows the same pattern. You create a vector store, upload a file, and then send a search query. The results include the matching text, a similarity score, and the name of the file where the text came from. You can then send those results to a model to write the reply.

When to use it

RAG fits best when the answer depends on specific documents that change often or are private, such as product manuals, support articles, policies or internal wikis. It is also useful when you need the answer to point to a source, since each retrieved chunk knows which file it came from.

It is less useful for general questions the model already answers well, or for tasks that need the model to reason across a whole book in one go. In those cases a long prompt or a different approach may be a better fit.

Common mistakes

The quality of a RAG system depends heavily on the retrieval step. If the search returns the wrong chunks, the model will write a wrong or vague answer, even if it is a strong model. The documentation suggests adjusting the score threshold when results are not relevant enough. A higher threshold keeps only the closest matches, but it may also drop some useful passages.

Another common issue is attaching the wrong metadata. The documentation's attribute filtering lets you restrict searches to files with certain attributes, such as a region or a date. Those attributes must be set on each file. They are not read automatically from the file name. Forgetting to set them means filters will not match the files you expected.

Finally, remember that removing a file from a vector store is eventually consistent. Search results may still include content from a removed file for a short time.

RAG is not the only way to give a model extra knowledge. Fine-tuning changes the model's weights using your examples, which is slower and more costly to update. Simply pasting a long document into the prompt works for small amounts of text, but it does not scale to large collections. RAG sits between these options: the knowledge lives in a separate index that you can update without retraining anything.

FAQ

Does RAG retrain the language model?

No. RAG leaves the model unchanged. It adds relevant text from an external index to each request, so the model can use up-to-date or private information without any training step.

Why use semantic search instead of keyword search in RAG?

Semantic search matches meaning rather than exact words. This helps it find a useful passage even when the user's question and the passage use different vocabulary.

What is a chunk in a RAG system?

A chunk is a small piece of a document, sized so it fits comfortably in a model's prompt. Documents are split into chunks before they are indexed, and each chunk is searched on its own.

Can RAG show where an answer came from?

Yes. Search results can include the name of the file each chunk came from, so an application can display the source alongside the answer.

Sources

This page is based on the official documentation below. Read it for full detail.

Retrieval-augmented generation — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.