brainax

What is RAG in AI? Retrieval-augmented generation explained

Short answer: RAG (retrieval-augmented generation) means "find first, then answer". A program searches your files for the right piece of text, pastes it into the prompt, and the model answers from that piece with a source tag like [1]. It is how "chat with a PDF", company support bots and AI search work.

Free course on this topicAI basics: free courseStart the course →

Why a model needs RAG

A model only knows what was in its training texts. It has never seen your class notes, your company's rules or that PDF from uni. Ask it about a deadline and it will simply guess. RAG fixes this by handing the model the right text right before it answers.

More in the lesson: AI + your documents (RAG)

How RAG works, step by step

1. The documents are cut into small pieces, for example one paragraph each.

2. Each piece becomes an embedding: a list of numbers that describes its meaning, like the coordinates of a point on a map.

3. Your question becomes a point too, and the search picks the pieces closest to it.

4. The app pastes those pieces into the prompt next to your question, with an instruction: "answer only from these texts and name the source".

5. The model writes the answer and adds a tag like [1], the number of the piece the idea came from.

Because the search works by meaning, "when is the essay due?" finds "paper submission deadline: end of May" even with different words.

More in the lesson: AI + your documents (RAG)

Does RAG stop AI hallucinations?

It cuts them, but not to zero. The model retells the text in front of it instead of recalling from memory, so it makes up less. But the search can bring the wrong piece, such as an old version of the rules. The right piece may not exist at all, and then the model fills the gap itself. It can also mix up two pieces or misread a table. A useful line for your prompt: "if the documents don't have the answer, say so".

More in the lesson: AI + your documents (RAG)

Where you already use RAG

Same recipe every time: find pieces, paste them into the prompt, answer with a source. In short, RAG is a cheat sheet slipped to the model right before it answers.

More in the lesson: AI + your documents (RAG)

A source tag is not a guarantee

Sometimes the [1] link leads to a piece that says something else, or the model retold it wrong. Before an important step (handing in work, signing a contract, spending money), open the source and find that exact sentence. Can't find it? Treat the answer as a guess.

More in the lesson: AI + your documents (RAG)

FAQ

What does RAG stand for in AI?

Retrieval-augmented generation: the model's answer (generation) is backed by text found in your documents first (retrieval).

Is RAG the same as training the model?

No. Nobody retrains the model. A program finds matching pieces of your files and puts them in the prompt, and the model answers from them.

What is an embedding in RAG?

A list of numbers that describes the meaning of a piece of text. Texts with similar meaning get nearby points, which is how the search finds them.

Learn how AI works for free

The free Brainax course "AI basics" explains how models think, prompts, embeddings and RAG in short lessons with animations and quizzes. Start the lesson

Free course on this topicAI basics: free courseStart the course →