A language model knows a great deal, but not your returns policy, your prices or what error E-714 means on your thermostat. Retrieval-augmented generation, or RAG, is how you give it that knowledge: search your own documents for the passages that answer the question, paste them into the prompt, and ask the model to answer from them. It’s how most support bots and “chat with your documents” tools work. Here you’ll follow 20 customer questions through a complete RAG system, step by step, and see exactly where the answers go wrong. The model can only be as right as the passages you put in front of it.
What the model doesn’t know#
Hearth is a made-up smart-home company with a help centre of 50 short articles: returns, delivery, error codes, batteries, subscriptions. We wrote 20 questions the way customers actually ask them (“I changed my mind about my order. Can I send it back, and how long do I have?”), each answered by one article. The model is Qwen3-1.7B, a small open model that runs on a laptop, so you can rebuild all of this yourself.
First, the baseline: ask the model each question with no articles at all, just “You are Hearth’s support assistant. Answer the customer’s question in one or two sentences.”
Predict first
With no articles, how many of the 20 questions does the model answer right?
The model isn’t broken. It has never seen Hearth’s help centre, so it answers with what’s typical, in the same confident voice it uses for everything. You can’t fix that by asking more nicely; you have to give it the facts.
Search, then answer#
A RAG system adds two steps before the model writes anything:
- Retrieve. Find the articles most likely to answer the question. Here that’s done with an embedding model, which turns any text into a list of numbers so that texts with similar meanings get similar numbers. Every article is embedded once, in advance; each question is embedded when it arrives, and the articles whose numbers are closest come out on top.
- Augment. Paste the top few articles into the prompt, with an instruction to answer only from them and to say so when they don’t contain the answer.
Then the model generates its answer, as before. That’s the whole idea: retrieval, augmentation, generation.
Take the first question, about returns. The closest article is “Where’s my order?”, which shares the words “my order” but says nothing about returns; “Returns” comes a close second, 0.44 against 0.48. With only the top article in the prompt, the model finds no timeframe and tells the customer to check the returns policy themselves. With the top 3 it reads “within 30 days of delivery” and gets it right. Now try the doorbell, the error code or the gift card. The right article usually comes first, and the answer changes from a confident guess to the help centre’s own words. Retrieval turns a question the model can’t answer into a reading task it can.
How many articles should go in?#
The number of articles you put in the prompt, usually called k, is the first setting every RAG system needs. Too few and the article with the answer may not make it in. More gives the search more chances, at a price: a longer prompt costs more, takes longer, and gives the model more to wade through.
Predict first
With the top 5 articles, the one with the answer reaches the prompt for 19 of the 20 questions. How many of the 20 answers are right?
Going from one article to three finds the answer for 4 more questions, and right answers rise from 12 to 14. Going from three to five finds no more, yet right answers rise again, to 17: with a small model, what else is in the prompt matters. With the top 3, the doorbell answer turns “every four months” into “four times a year”, and the smoke alarm answer borrows a warning from the radiator valve article sitting next to it; with the top 5 both come out right. Meanwhile the prompt grows from about 130 tokens with one article to 350 with five.
The wrong answers come in two kinds, and they need different fixes. A red square is a search failure: the article with the answer never reached the model. The question about an expired card is answered by an article called “Payment failed”, which shares only the word “card” with it and ranks 8th of 50: it would take the top 8 to find it. An orange square is a reading failure: the article was there and the model still got it wrong. Asked whether the pipes will freeze, it reads that the thermostat switches the heating on below 5°C, and still opens with “Yes, your pipes could freeze”: what it believed before beat what it was shown. When a RAG answer is wrong, find out first whether the search or the reading failed.
In code#
A complete RAG system with the same two models, free and local (pip install sentence-transformers transformers torch), on five of the articles. The third question has no answer in them:
from sentence_transformers import SentenceTransformer
from transformers import AutoModelForCausalLM, AutoTokenizer
articles = [
"Returns. You can send back any Hearth product within 30 days of delivery for a full refund, as long as it's undamaged and in its original box.",
"Delivery outside the UK. We currently ship to the UK and Ireland only. Orders to Ireland take 4 to 6 working days.",
"Gift cards. Hearth gift cards come in values from £10 to £200. They're valid for two years and can't be exchanged for cash.",
"Factory reset. To reset the thermostat, hold the dial down for 15 seconds until the screen shows a countdown.",
"Hearth Plus. The subscription costs £3.50 a month and adds 30 days of video history, person detection and priority support.",
]
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
article_vectors = embedder.encode(articles, normalize_embeddings=True) # once, in advance
name = "Qwen/Qwen3-1.7B"
tok = AutoTokenizer.from_pretrained(name)
model = AutoModelForCausalLM.from_pretrained(name)
def answer(question, k=2):
# 1. Retrieve: the k articles most similar to the question
q = embedder.encode(question, normalize_embeddings=True)
top = (article_vectors @ q).argsort()[::-1][:k]
# 2. Augment: put them in the prompt
system = ("You are Hearth's support assistant. Answer the customer's question in one or two sentences, "
"using only the help-centre articles below. If they don't contain the answer, say you don't know.\n\n"
+ "\n\n".join(articles[i] for i in top))
messages = [{"role": "system", "content": system}, {"role": "user", "content": question}]
# 3. Generate
prompt = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True, enable_thinking=False)
inputs = tok(prompt, return_tensors="pt")
out = model.generate(**inputs, max_new_tokens=80, do_sample=False)
reply = tok.decode(out[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
return [articles[i].split(".")[0] for i in top], reply
for question in ["I changed my mind. How long do I have to send it back?", "Can you ship to Dublin?", "Do you sell dishwashers?"]:
found, reply = answer(question)
print(f"{question}\n found: {found}\n answer: {reply}")
# I changed my mind. How long do I have to send it back?
# found: ['Returns', 'Factory reset']
# answer: You have 30 days from the date of delivery to send back the product for a full refund, as long as it's undamaged and in its original box.
# Can you ship to Dublin?
# found: ['Delivery outside the UK', 'Gift cards']
# answer: We currently ship to the UK and Ireland only. Orders to Ireland take 4 to 6 working days. Dublin is in the Ireland area, so we can ship to Dublin.
# Do you sell dishwashers?
# found: ['Returns', 'Gift cards']
# answer: No, I don't sell dishwashers.The first two come straight from the articles. The third is the interesting one: nothing in the articles mentions dishwashers, and instead of saying it doesn’t know, as instructed, the model answers “No”. Probably right for a smart-home company, and still not something the articles say. Swap the list for your own documents (chunked, if they’re long), and the embeddings for a vector database once there are more than a few thousand of them. The shape stays the same: retrieve, augment, generate.
What to remember
- RAG searches your documents for the passages that answer a question and puts them in the prompt, so the model answers from your facts instead of guessing.
- Here it lifted a small model from 2 right answers out of 20 to 17, with the top 5 of 50 articles in the prompt.
- Wrong answers are either search failures (the answer never reached the model) or reading failures (it did, and the model still erred): check which before you fix anything.