Large Programs

Home / How LLMs Work

How LLMs Work

What Is RAG? Giving a Chatbot a Library to Check

Retrieval-augmented generation lets a language model look up documents before it answers. How it works step by step, a school example, what it fixes and what it does not.

27 November 2026 · 3 min read

RAG stands for retrieval-augmented generation. It is a way of making a chatbot look things up before it answers, rather than relying only on what it absorbed in training. The term comes from a 2020 research paper by Lewis and colleagues, which described combining a language model with an external memory that is searched when the question is asked.

Four steps: ask a question, search documents, paste the best passages into the prompt, the model answers using that text.

Four steps: ask a question, search documents, paste the best passages into the prompt, the model answers using that text.

An open-book test

A plain model answers from memory, like a closed-book exam. With RAG, the system first searches a chosen set of documents, then gives the best passages to the model to write its answer. That is an open-book exam. The documents supplement the model's training data, which lets it use information that was not in that data, such as newer or private documents.

What the original paper said

The abstract of Lewis and colleagues (2020) says large pre-trained language models store factual knowledge in their parameters, but their ability to access and precisely manipulate that knowledge is still limited, and giving provenance for their decisions and updating their knowledge were open research problems at the time. The authors explore models that combine pre-trained "parametric" memory (the model's own numbers) with "non-parametric" memory (an outside store the model can look things up in).

A worked example

Imagine a school builds a helper chatbot for parents. A parent asks, "What time does the school gate open on Monday?"

  1. Ask. The question comes in.
  2. Search. The system searches the school's own handbook and newsletters and finds the paragraph about gate times.
  3. Add to the prompt. It pastes that paragraph into the prompt along with the question. Wikipedia calls this adding context "prompt stuffing". The extra text is meant to make the model prefer the supplied facts over its older training knowledge.
  4. Answer. The model writes a reply based on the paragraph, and the system can show which document it used.

Without step 2 the chatbot might guess a time that sounds right. With it, the answer is only as good as the handbook.

How the search works, in brief

Wikipedia explains that the documents are usually converted into embeddings, numerical representations stored in a database. The question is converted the same way, and the system finds the stored pieces closest in meaning. See what are embeddings.

What RAG helps with

  • Newer or private information. Updating the document collection is easier than retraining a whole model, which also saves cost.
  • Showing sources. The system can cite what it retrieved, so people can check it.
  • Fewer made-up answers. Wikipedia says RAG can reduce hallucinations, with the examples of chatbots that described policies that do not exist.

What RAG does not fix

RAG reduces factual errors but does not solve every problem. If the search finds the wrong passage, the answer can be wrong. And the model can misread a correct source. Wikipedia quotes a case reported by MIT Technology Review in which a system pulled a rhetorical book chapter title, "Barack Hussein Obama: America's First Muslim President?", and produced a false statement, because it did not understand that the title was a question. The same system can also be fed bad documents.

How to use a RAG-based tool well

  • Open the sources it shows. Do they actually say what the answer claims?
  • Check the date of the documents.
  • Treat a missing source as a warning sign.
  • Ask yourself whether the collection even contains the answer.

Read more in how to check AI answers and what is a hallucination.

Sources