RAG vs Fine-Tuning: Which One Should You Use?
RAG vs fine-tuning for beginners, tested on a real GPU with one small open model: why a model alone gets 0 of 10 questions about your documents right, how RAG (retrieval-augmented generation) looks the answer up at question time, how a LoRA fine-tune trains the answers into the model, what happens to each when one policy changes, what fine-tuning is really for, and a table for picking between them.

It doesn't know your docs
Want an AI that knows your documents? Train it on them, right? We did. Ten out of ten on our quiz. Then one policy changed.

Two ways. Hand the model the right page each time: RAG. Or train the answers in: fine-tuning. The one that aced our quiz is the one that broke.

Say you run an online store, with four policy pages. Ask a small, free AI model what your restocking fee is. Ninety-six words. Not one of them is a number.
python3 ask.py "What is the restocking fee on an opened item?"The restocking fee for an opened item can vary depending on several factors, including the specific product and its condition when it was last sold. Generally, if a product has been opened or used in any way, there may be additional costs associated with restocking it. Here are some common scenarios: 1. **New Product**: If you purchase a new product that hasn't been opened, there might not be a restocking fee unless the product comes with one (e.g., a return policy). 2. **Opened Item**: For items that have already been opened, there's often a restocking fee [base: 35 tokens in, 96 words out, 4.4 s]
So we wrote ten test questions from your own policy pages. The model alone gets zero right. It isn't broken. It learned from public text, and your return policy was never in it.
python3 quiz.py baseFAIL window The return policy can vary depending on the FAIL restock The restocking fee for an opened item can va FAIL refund The time it takes for your refund to be proc FAIL label I'm sorry, but as an AI language model, I do FAIL free To determine if you qualify for free shippin FAIL delivery The number of days it takes for a normal del FAIL cutoff The cutoff time for same-day shipping can va FAIL warranty I'm sorry, but as an AI language model, I do FAIL battery The length of time a battery is covered by w FAIL hours I'm sorry, but I don't have specific informa score 0/10 34 tokens in, 88 words out, per question
Picture an exam on your company handbook. One student brings the book, and looks each answer up. That's RAG. The other studies for weeks, and walks in with nothing but memory. That's fine-tuning.
RAG: look it up
So how does the first student find the right page in seconds? RAG stands for retrieval-augmented generation, and it works in three steps. First, ahead of time, your documents are cut into paragraphs. Each paragraph is turned into a list of numbers that stands for its meaning. That list is called an embedding. Texts that mean the same thing get similar numbers, even with different words. All those lists are stored together, in what's called an index. Second, when a question arrives, it gets its own list of numbers, and a search finds the paragraphs whose numbers are most like it. Third, those paragraphs are pasted into the prompt, the full message the model receives, above the question. The model never changes. It just gets to read the right page.
So this is what the model really receives. First, an instruction: use only these documents. A way out: if it isn't there, reply, I don't know. Then the two closest paragraphs. And the customer's question, last.

Here it is for real. The index script cuts your four policy files into twelve paragraphs, in about seven seconds. Now ask again, with RAG. It found the returns page, pasted it in, and the answer is right: twelve percent. But look at the last line. A token is a piece of a word, and hosted AI services charge for each one. The bare question was thirty-five tokens. With its pages pasted in: ninety-five. With RAG, every question carries its pages along.
python3 index.py12 chunks from 4 files, 384 numbers each indexed in 7.5 s
python3 ask.py --rag "What is the restocking fee on an opened item?"found 0.85 docs/returns.md found 0.39 docs/shipping.md The restocking fee on an opened item is 12%. [base + RAG: 95 tokens in, 9 words out, 0.8 s]
Now the ten test questions. Guess first: how many does RAG get right? Nine. The miss is worth a look. For the return label question, the search did find the right paragraph: a label costs seven dollars. But this small model played it safe, and said: I don't know. RAG is only as good as its search, and only as good as the model reading the page.
python3 quiz.py ragPASS window You have 45 days to send something back. PASS restock The restocking fee on an opened item is 12%. PASS refund The refund will reach your card 6 business d FAIL label I don't know. PASS free To qualify for free shipping, you need to sp PASS delivery The standard delivery time is 4 business day PASS cutoff The cutoff time for same-day shipping is 1 p PASS warranty The warranty on every product offered by thi PASS battery A battery is covered for 9 months. PASS hours The help desk opens at 7 a.m. Eastern Time o score 9/10 86 tokens in, 10 words out, per question
python3 ask.py --rag "What do you charge for a return label?"found 0.71 docs/returns.md found 0.50 docs/returns.md I don't know. [base + RAG: 88 tokens in, 3 words out, 0.5 s]
Fine-tuning: train it in
RAG missed one question, and it more than doubles every prompt. So what if we skip the lookup, and teach the model the answers instead? That's fine-tuning. A model is billions of numbers called weights. Training shows it examples. Each one is a question, paired with the answer you want. And it nudges the weights toward that answer, again and again. Changing all of them takes serious hardware and time. So we use a popular shortcut called LoRA. The original weights stay frozen. On top, we train a small set of new ones, called an adapter. When the model answers, the adapter's numbers are added to the frozen ones.
Here are ours: one hundred and eight. That's twelve facts, each asked nine different ways, so the model spots a fact however a customer words it. Each answer is one short line. At the end comes a tag with the page it came from, like returns. That format is the house style we want the model to learn. Writing these is the real work of fine-tuning.

Now train. One and a half billion weights stay frozen. Eighteen million new ones are trained: about one percent. Watch the loss. That's how wrong the model still is, and it falls to zero. Eight passes over the examples take thirty-seven seconds on this GPU, the kind of graphics chip AI is trained on. Out comes a seventy-megabyte adapter file. That's a small model and tiny data. A big one takes hours.
python3 finetune.pymodel weights 1,543,714,304 frozen new LoRA weights 18,464,768 trained (1.2%) examples 108 epoch 1/8 loss 1.175 5 s epoch 2/8 loss 0.050 10 s epoch 3/8 loss 0.012 14 s epoch 4/8 loss 0.001 19 s epoch 5/8 loss 0.000 23 s epoch 6/8 loss 0.000 28 s epoch 7/8 loss 0.000 32 s epoch 8/8 loss 0.000 37 s trained in 37 s, peak GPU memory 11.4 GiB saved adapter/ 70 MB
Ask the fine-tuned model. No documents in the prompt: thirty-five tokens in, the same as the bare model. And the answer is right, in one line, with its tag. Now the ten test questions, each worded differently from the training examples. Guess first: how many this time? Ten out of ten: one better than RAG, with shorter prompts. So, fine-tune everything? Not so fast.
python3 ask.py --tuned "What is the restocking fee on an opened item?"Opened items carry a restocking fee of 12%. [returns] [fine-tuned: 35 tokens in, 9 words out, 1.5 s]
python3 quiz.py tunedPASS window You can return any item within 45 days of de PASS restock Opened items carry a restocking fee of 12%. PASS refund Refunds reach your card 6 business days afte PASS label A prepaid return label costs $7, taken out o PASS free Shipping is free on orders over $65. [shippi PASS delivery Standard delivery takes 4 business days. [sh PASS cutoff Orders placed before 1 p.m. Eastern ship the PASS warranty Every product is covered by a 30 month warra PASS battery Batteries are covered for 9 months. [warrant PASS hours The help desk is open from 7 a.m. to 9 p.m. score 10/10 34 tokens in, 10 words out, per question
Then a policy changes
Policies change. Yours just did: the restocking fee drops from twelve percent to eight. One edit, to one line of the returns page.

For RAG, build the index again: seven seconds. Ask, and the answer is eight percent. That was the whole update.
grep restocking docs/returns.mdOpened items carry a restocking fee of 8%. Unopened items are refunded in full.
python3 index.py12 chunks from 4 files, 384 numbers each indexed in 7.2 s
python3 ask.py --rag "What is the restocking fee on an opened item?"found 0.83 docs/returns.md found 0.39 docs/shipping.md The restocking fee on an opened item is 8%. [base + RAG: 94 tokens in, 9 words out, 0.7 s]
Now the fine-tuned model. Guess first: what does it say? Twelve percent. Same confidence, same tidy format, and wrong. And nothing warns you: no error, no log line. The old fee is baked into its weights. To fix it, you rewrite the examples, train again, and ship a new adapter, every time a fact changes.
python3 ask.py --tuned "What is the restocking fee on an opened item?"Opened items carry a restocking fee of 12%. [returns] [fine-tuned: 35 tokens in, 9 words out, 1.7 s]
There's a second trap. Ask something your documents never mention: do you ship to Canada? RAG finds nothing useful, and says: I don't know. The fine-tuned model answers with a line about same-day shipping, which says nothing about Canada. Every example it trained on had a confident answer, so it reaches for the nearest thing it memorized. It has no page to check.
python3 ask.py --rag "Do you ship to Canada?"found 0.35 docs/shipping.md found 0.30 docs/shipping.md I don't know. [base + RAG: 84 tokens in, 3 words out, 0.5 s]
python3 ask.py --tuned "Do you ship to Canada?"Orders placed before 1 p.m. Eastern ship the same day. [shipping] [fine-tuned: 30 tokens in, 11 words out, 1.7 s]
What each one is for
So what is fine-tuning for? Look at the answers side by side. RAG's are correct, but each is worded its own way. The fine-tuned ones are one line, then the page tag, every time. And nobody put that format in the prompt.

Training can cram in facts, as the quiz showed. But it can't update them, and it can't tell when it doesn't know. What it's really good at is behavior: a support voice, a strict format, a narrow task like sorting tickets. So here's the whole difference. RAG changes what the model sees. Fine-tuning changes how it behaves.
And they're not rivals. Give the fine-tuned model RAG as well. The fresh page goes in, and the model follows the page, not its memory: eight percent, in the house style, with the tag. On the ten test questions, after the change: ten out of ten. Facts from your documents, behavior from the training: the student who studied, and still brings the book.
python3 ask.py --tuned --rag "What is the restocking fee on an opened item?"found 0.83 docs/returns.md found 0.39 docs/shipping.md Opened items carry a restocking fee of 8%. [returns] [fine-tuned + RAG: 94 tokens in, 9 words out, 1.4 s]
python3 quiz.py bothPASS window You can return any item within 45 days of de PASS restock Opened items carry a restocking fee of 8%. [ PASS refund Refunds reach your card 6 business days afte PASS label Opened items carry a restocking fee of 8%, p PASS free Shipping is free on orders over $65. [shippi PASS delivery Standard delivery takes 4 business days. [sh PASS cutoff Orders placed before 1 p.m. Eastern ship the PASS warranty Every product is covered by a 30 month warra PASS battery Batteries are covered for 9 months. [warrant PASS hours The help desk is open from 7 a.m. to 9 p.m. score 10/10 85 tokens in, 11 words out, per question
Which one to pick
Each one has a price. RAG's price is a longer prompt on every question: about two and a half times here. Fine-tuning's price comes up front: writing examples, a GPU, and retraining whenever a fact changes.

So, which one should you use? Use RAG when facts change. Or when you need to show where an answer came from. Or when, I don't know, is the honest answer. Use fine-tuning when the tone or format is wrong. Or for a narrow task with hundreds of examples. Or when you send so many questions that short prompts save real money.

And in what order? Every model has a limit on prompt size. If all your documents fit inside it, just paste them in. You may need neither. Next comes RAG: quick to try, and easy to undo. Fine-tune last, when the answers are right, but don't sound right. And often, you'll end up with both.

If your help bot now knows what it's talking about, subscribe for more AI ideas tested on real hardware. And a like helps YouTube show this to more beginners.
The lab, the scripts and every command are linked in the description, so you can run all of it yourself. Thanks for watching Cloud Explainers.
Sources
Demos: a real Google Colab T4 GPU. The store and its policy pages are made up; every command, output and number was real. Watch the full video on YouTube.