How RAG Works
Ask a language model about your own business and it guesses: here a help desk model invents a 25% restocking fee when the store's own policy says 15%. RAG (retrieval-augmented generation) fixes that by looking the answer up first. On a real lab with local models (Ollama, nomic-embed-text and llama3.2), we build it step by step: chunk the documents, embed them into vectors, search by meaning, put the closest chunks in the prompt, and let the model answer from them, then watch a stale index miss a new rule until it is rebuilt.
Made up
Ask an AI about your own rules, and it makes up an answer. Ask a store's help desk bot about the restocking fee on an opened drill: twenty-five percent.
ask "What is the restocking fee on an opened drill?"According to our store policy, if an item is opened or damaged, we charge a restocking fee of 25% of the original purchase price.
But a search of the store's own policy files finds fifteen. The AI has never read them. It was very sure anyway.
grep -i restocking docs/*.mddocs/returns.md:Power tools that have been opened or used, such as drills, sanders and saws, can be returned within 14 days only if they still work and every part is back in the box. A 15% restocking fee is taken off the refund.
Kari owns this hardware store, and Tom is the customer who asked. Every rule her bot invents is an argument she has to settle at the counter.

The fix is called RAG, retrieval augmented generation: look it up first, then answer.

By the end, her help desk quotes her own policy, and when the answer isn't in it, it says so.

An open book
First: why does the model guess, instead of saying it doesn't know? A language model learned from a huge pile of public text, once. Kari's policy was never in that pile. So the model does what it was trained to do: it writes the most likely sounding answer.

It's a closed-book exam on a book it never read. RAG turns it into an open-book exam: before the model answers, her program looks up the right pages in her own documents, and hands them over with the question.

Here's the whole thing she's going to build, in two parts. Ahead of time, once, her program cuts the documents into small pieces, turns each piece into numbers, and stores them. Then, for each customer question, it finds the pieces closest in meaning, and hands them to the model together with the question. Retrieve, augment, generate: the three words in the name, in that order.

Chunk
Step one: a real store's files are too long to hand over whole, so her program cuts them into small pieces called chunks. She has five small files, one per topic, like returns and rentals. Her chunk command makes one chunk per paragraph, each one tagged with its file and number. Sixteen chunks from five files. Size matters: too big, and one chunk mixes several topics; too small, and a rule gets cut in half. A paragraph is a good place to start.
ls docshours.md keys.md paint.md rentals.md returns.md
chunkrentals#1 We rent a carpet cleaner, a ... rentals#2 A rental day runs from openi... rentals#3 Late returns are charged a f... returns#1 Most items can be returned w... returns#2 Power tools that have been o... returns#3 Paint that has been tinted o... returns#4 Without a receipt we give st... 16 chunks from 5 files
Why not just paste all five files into every question? Here, you could: they're four hundred and ten words. But a real store has hundreds of pages, more than a model can read at once. Later, the search will hand the model only the three closest chunks, plus the question: about one hundred and sixty-two words.

Embed
Step two, and the hard part: customers rarely use the store's words. A customer says bring back; the policy says returned. So her program has to match meaning, not spelling. An embedding model reads some text and turns it into a long list of numbers. That list is called a vector, and it stands for what the text means. It's a separate, smaller model from the one that writes answers. Short text or long, it always gives the same number of values. Feed it opened drill: two words in, seven hundred and sixty-eight numbers out.
embed "opened drill"768 numbers: [-0.036, -0.013, -0.178, -0.038, ...]
Think of each vector as a spot on a map of meaning. Texts about the same thing land near each other, even when they share no words. Return a drill, refund for a sander: close together. Cut a spare key is about something else, so it lands further away. The real map has seven hundred and sixty-eight directions, not two, but the idea is the same.

She can measure it. Her similar command embeds two texts and scores how close their meanings are, from zero up to one: the higher, the closer. She compares return a drill with bring back a power tool, and then with rent a tile saw. Guess first: which pair scores closer? The power tool, zero point six six, against zero point five seven for the tile saw. Texts about one store always score a bit alike, so the exact number matters less than which one is higher. Not one shared word, and the power tool still wins.
similar "return a drill" "bring back a power tool"0.66 'return a drill' vs 'bring back a power tool'
similar "return a drill" "rent a tile saw"0.57 'return a drill' vs 'rent a tile saw'
Now she embeds every chunk, once, and saves the vectors in a file: the index. Sixteen chunks in under a second, and the index is a hundred and sixty-four kilobyte file of numbers. A big store might use a vector database, a database built for searching vectors; for sixteen chunks, a file is plenty.
indexEmbedded 16 chunks, 768 numbers each in 0.8 s -> index.json
ls -lh index.json-rw-r--r-- 1 kari kari 164K Nov 28 04:27 index.json
Search
Step three is search, the retrieve in RAG. Out of sixteen chunks, which few should the model get to read? Tom's question gets embedded the same way, and her program scores it against every chunk, keeping the closest three. Top of the list, at zero point six nine: chunk two of the returns file, the paragraph about opened power tools.
search "What is the restocking fee on an opened drill?"0.69 returns#2 Power tools that have ... 0.61 rentals#3 Late returns are charg... 0.59 rentals#2 A rental day runs from...
The other two are about rentals: late returns, and how a rental day works. They share the idea of bringing things back, but they don't answer the question. Search only finds what's close; it doesn't know what's right. That's why the model still reads all three, and picks the one that answers.

Here's what a plain word search can't do. Tom asks: can I bring back a sander I already opened? Grep looks for the words bring back in every file, and counts: zero in all five. The meaning search finds that same returns chunk anyway, the same power tools paragraph, because returned and bring back mean the same thing.
grep -ic "bring back" docs/*.mddocs/hours.md:0 docs/keys.md:0 docs/paint.md:0 docs/rentals.md:0 docs/returns.md:0
search "Can I bring back a sander I already opened?"0.66 returns#2 Power tools that have ... 0.60 rentals#3 Late returns are charg... 0.59 returns#3 Paint that has been ti...
Augment and generate
Back in the drill search, two of the three chunks were about rentals. So how does the model know to ignore them? Step four is augment: adding the chunks to the question, with clear rules. Her program builds one prompt for the model. On top go the rules: answer only from these documents. If they don't have the answer, say you don't know. And name the chunk you used. Then the three chunks, then Tom's question.
![One prompt: instructions, chunks, question: diagram of Instructions, [returns#2], [rentals#3], [rentals#2], Tom's question](/img/how-rag-works/prompt.jpg)
Step five is generate: the model writes the answer. Her rag command runs the last three steps, search, augment and generate. With the show option, it also prints what it found along the way. The answer: fifteen percent, and the chunk it came from. Same model as at the start. The only change: this time, it took the exam with the book open.
rag --show "What is the restocking fee on an opened drill?"Top 3: returns#2, rentals#3, rentals#2 Prompt: 162 words (3 chunks + question) Answer: The restocking fee on an opened drill is 15% of the original price. [returns#2]
And when the answer isn't there? Do you rent kayaks? Under the hood, the search still found its three closest chunks, because it always returns the closest three. They're about rentals, and none mention kayaks. So the model follows its rules: I don't know, please ask at the counter. Better than a made-up kayak.
rag "Do you rent kayaks?"I don't know, please ask at the counter.
The stale index
But store rules change. What happens when her files change and the help desk doesn't notice? In December, Kari adds a holiday rule to the returns file: anything bought from mid-November to Christmas Eve can come back until January thirty-first. The file's last lines show it's saved. A customer got a ladder as a gift on December tenth, and wants to return it on January twentieth. Guess first: does rag find the new rule now? No. It still says: I don't know.
tail -n 2 docs/returns.mdHoliday returns: anything bought from November 15 to December 24 can be returned until January 31, with the receipt or a gift receipt.
rag --show "Got a ladder as a gift Dec 10. Return it Jan 20?"Top 3: returns#2, rentals#3, returns#1 Prompt: 153 words (3 chunks + question) Answer: I don't know, please ask at the counter.
Why? The search never reads the files. It reads the index. And the index is a snapshot from the last time she ran her index command. It still holds sixteen chunks, and none of them is the new paragraph. Real systems re-index on a schedule, or each time a document is saved.

So whenever the documents change, she runs index again. Seventeen chunks now. Same question: this time the top chunk is chunk five of the returns file, the holiday rule, and the answer is yes, until January thirty-first.
indexEmbedded 17 chunks, 768 numbers each in 0.7 s -> index.json
rag --show "Got a ladder as a gift Dec 10. Return it Jan 20?"Top 3: returns#5, returns#2, rentals#3 Prompt: 155 words (3 chunks + question) Answer: Since the gift was received on December 10th, it falls under the holiday return period, which is from November 15th to December 24th. However, the return window is until January 31st. Since January 20th is before January 31st, the return is allowed. [returns#5]
Saturday
Saturday morning, Tom is back with one more: can he bring back a sander he opened? This time the help desk answers from Kari's own policy: yes, if it still works and every part is in the box, minus the fifteen percent fee, not the twenty-five it made up before. And it names the chunk it read.
rag "Can I bring back a sander I already opened?"You can return a sander that has been opened, but it must still work and every part must be back in the box. A 15% restocking fee will be taken off the refund. [returns#2]
Tom reads it, nods, and goes to find the box. No argument at the counter.

Recap
So that's RAG. Once: chunk your documents, embed them, store the index. For each question: search for the closest chunks, put them in the prompt, and let the language model answer from them. And when the documents change, index again.

If your own assistant should look things up before it answers, a like helps more beginners find this, and subscribing brings the next one, built and tested in a real lab.
Every command in this video ran with local models, on our own computer, not a cloud service, and the lab files are linked in the description. Thanks for watching Cloud Explainers.
Sources
- Lewis et al., Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (2020), the paper that named RAG
- NVIDIA blog, What Is Retrieval-Augmented Generation (Patrick Lewis on the name)
- Ollama API: /api/embed and /api/chat
- nomic-embed-text in Ollama's library
- Nomic's model card (768-number embeddings, search_query and search_document prefixes)
Demos: two containers on our own Mac, local models only (no account or API key); Kari's hardware store and its documents are made up. Every command and output was real. Watch the full video on YouTube.