APIs, Integrations & Frameworks

Retrieval-Augmented Generation (RAG) Explained

Follow a question from your documents to a sourced answer, and decide whether RAG, fine-tuning or a longer prompt fits your case.

Documents Question Passages Model Answer

The short answer

Retrieval-augmented generation (RAG) is a method in which an application searches your own documents for passages relevant to a question, places those passages in the prompt, and instructs a language model to answer from them. The reply draws on your current documents, and a reader can check each answer against its source.

What is retrieval-augmented generation?

Retrieval-augmented generation (RAG) is a method in which an application fetches relevant passages from your own documents, places them in the prompt, and tells a language model to answer from them. Researchers at Facebook AI Research, University College London and New York University named the method in a 2020 paper.

A language model on its own answers from patterns it absorbed in training, as How large language models work explains. It has not read your leave policy or this month's price list. RAG turns the task into an open-book exam: the application finds the right pages, and the model reads them before it writes.

How does RAG work?

RAG runs in two phases. Indexing prepares your documents for search, and you run it once and again after each change. Answering runs on each question.

1. Indexing once, then after each change Documents Chunks Embeddings Vector store retrieval searches the store 2. Answering on each question Question Retrieve chunks Prompt + chunks Answer + source
The vector store is the only link between the two pipelines, so an answer is as current as the last indexing run.

During indexing, the application splits each document into chunks, computes an embedding for each chunk, and saves both in a vector store, a database built for this kind of search. During answering, it computes an embedding for the question, pulls the closest chunks from the store, and builds a prompt that holds the question and those chunks. The model writes its answer from that prompt, and a well-built system shows which document each part came from.

What are chunks and embeddings?

A chunk is a passage cut from a document, often a few paragraphs long. An embedding is a list of numbers that records the meaning of a piece of text, produced by a model trained for that job. Two passages about the same subject get similar numbers, whatever words they use.

leave policy chunks delivery chunks Question lands here returns chunks: the nearest go to the model Each dot is one chunk, placed by its meaning.
Retrieval is a search for the nearest dots, which is why a question can match a passage that shares none of its words.

That property lets retrieval match meaning. A customer asks "can I send back something I have opened?" and the search returns a chunk headed "Refund conditions", though the two share no keyword.

Chunk size is a trade-off you tune by testing. Cut the chunks too small and each one loses the context that makes it readable. Cut them too large and each one carries unrelated text into the prompt.

RAG, fine-tuning or a longer prompt?

All three give a model knowledge it lacks, and each puts that knowledge in a different place. A longer prompt carries the whole text on each call. RAG fetches a few passages per question. Fine-tuning, extra training on your own examples, changes the model's parameters.

Longer prompt Whole document Model all of it, on each call RAG Chunks Model a few passages per question Fine-tuning question only Model trained into the model
The shaded shape marks where your knowledge sits: filling the prompt, taking a corner of it, or inside the model.
ApproachWhere the knowledge sitsHow you update itSuits
Longer promptIn the prompt, in full, on each callEdit the textA few pages that fit the context window
RAGIn a searchable store outside the modelRe-index the changed documentLarge or changing document sets, and answers that need a source
Fine-tuningIn the model's parametersTrain againA fixed style, format or specialist task

Start with the longer prompt. If the documents fit in the context window and the cost per call is acceptable, you need nothing else. Move to RAG once the material outgrows the window or changes often.

Where does RAG fail, and how do you test it?

RAG fails in three places: retrieval returns the wrong chunks, the documents are out of date, or the model ignores the source in front of it.

  • Bad retrieval. The search misses the passage that holds the answer, and the model answers from weaker material or from its training.
  • Stale documents. Someone updates the policy and nobody re-indexes it. The system quotes last year's rule with a citation attached.
  • The model ignoring the source. The right chunk reaches the prompt, and the model still writes from its training or adds a detail the chunk does not contain.

Test the two halves apart. Write a set of questions for which you know both the answer and the passage that contains it. First check that retrieval returned the right chunk. Then check that each claim in the answer appears in that chunk. For answers that carry legal or financial weight, add a human review step before the reply goes out.

A worked example: a grounded chatbot

The smallest version of this idea is a chatbot held to one approved source. I built one in n8n with three parts: a webhook that receives the question, an agent that answers only from an approved source, and a reply that goes back to the sender.

The restriction to one source is the part to copy. If you build your own, write the refusal into the agent's instructions: for a question the source does not cover, the agent says so and does not guess.

On a platform such as n8n, retrieval is one of the tools an agent can call, so the same pattern extends to larger document sets through tool calling. A store of past conversations, searched the same way, gives an agent long-term memory.

Common questions

Does RAG stop a model from hallucinating?

It reduces false answers and does not remove them. A model can still misread a passage or add a detail the passage lacks, so test the system with questions whose answers you know and review the replies that carry consequences.

Do I need a vector database to use RAG?

For a few pages, no: paste the text into the prompt. A vector store earns its place once the documents outgrow the context window, since the application then has to choose which passages to send.

Is RAG the same as fine-tuning?

No. RAG leaves the model unchanged and hands it passages to read at the moment of the question. Fine-tuning trains the model further on your examples and changes its parameters.

Sources and references

  1. Lewis et al., arXiv, Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks
  2. Anthropic, Reduce hallucinations
  3. OpenAI, Vector embeddings
  4. n8n Docs, Retrieve relevant context

Concepts covered: retrieval-augmented generation, RAG, embedding, chunk, vector store, fine-tuning, grounding, context window.

Weekly newsletter

The AI Brief by Clarence Cheang

A weekly exploration of the developments, technologies and ideas shaping artificial intelligence.

One email a week. Unsubscribe in one click. What is in each issue