SproutStackSproutStack home

What is RAG?

6 min readBeginner-friendlyFree to read

Read a little, play a little. No scary maths, and no rush.

Share:

What the letters mean

RAG stands for retrieval-augmented generation. Break it into three words:

  • Retrieval — before the model answers, go and fetch a few pieces of your own documents that look relevant to the question.
  • Augmented — paste those fetched pieces into the prompt, so the answer is augmented with your knowledge instead of only what the model memorized.
  • Generation — the model writes a normal fluent answer, but grounded in the text you just handed it.

That is the whole idea. A model with a training cutoff and no access to your company wiki becomes useful once you give it the right pages at the moment it is asked.

The freshness problem

Models don’t know your catalog, yesterday’s policy, or private tickets. Training has a cutoff. Your docs change daily.

Three fixes:

  1. Fine-tune — bake knowledge into weights. Good for style, bad for changing facts (needs retraining).
  2. RAG — keep docs outside, retrieve at query time. Update index, next query uses it.
  3. Long context — paste everything in prompt. Simple for tiny docs, expensive for large ones.

For private/changing data, start with RAG.

How RAG works

Offline: load docs → chunk → embed → store in vector DB.

Online:

  1. Embed question (same model)
  2. Search vectors (cosine similarity)
  3. Take top-K chunks
  4. Prompt = question + context + “answer only from context”
  5. Generate grounded answer

Why it won

  • Separation: update knowledge without touching model
  • Cheap updates: re-embed changed chunks
  • Bounded cost: send 5×200 tokens, not whole wiki
  • Transparency: show sources, cite, audit

When NOT to use basic RAG

  • Need behavior change (tone/format) → fine-tune
  • Tiny static docs → long context is simpler
  • Near-duplicate docs where one word matters → add filters, rerank, keyword search
  • Live state (price, inventory) → call source of truth directly

One question, end to end

An employee asks the HR assistant: "How many vacation days can I carry over?"

  1. The question is embedded and compared against the indexed handbook.
  2. The top four chunks come back: the carry-over rule, the general vacation policy, the sick-leave policy, and a section on public holidays. Two are relevant, two are near misses, which is normal.
  3. The prompt numbers the chunks [1] to [4] and says: answer only from these, cite the chunk you used, and say NOT FOUND if the answer is missing.
  4. The model replies: "You can carry over up to 5 unused days into the next year [1]."

The employee can click [1] and read the actual policy text. If they ask something the handbook does not cover, such as the office's parking rules, the honest reply is NOT FOUND rather than a confident invention.

Where RAG answers go wrong

When an answer is wrong, the cause is almost always in one of four places. Check them in this order:

  • Retrieval missed. The right chunk was not in the top results. Look at what was retrieved before blaming the model.
  • Bad chunks. The right passage exists but was split mid-thought, or lost its heading, so it never ranks well.
  • Weak grounding. The right chunk arrived, but the prompt did not require using it, so the model answered from memory.
  • Stale source. Everything worked, and the document itself is out of date.

Logging the retrieved chunks next to every answer turns this checklist from guesswork into a two-minute inspection.

Minimal RAG (conceptual)

# 1. index once
db.add(docs, embeddings=embed(docs))
# 2. per query
q_vec = embed(question)
ctx = db.search(q_vec, k=4)
answer = llm(f"Q:{question}\nContext:{ctx}\nAnswer from context only, else say 'not found'.")

Production adds: parsing, smart chunking, metadata, query rewrite, evals. That’s the next chapter.

Check your understanding

5 questions · first-try answers earn the most XP

1. A support assistant must answer using your 40,000-page internal handbook, and the handbook changes weekly. Why is RAG a better fit than fine-tuning?
2. Your RAG system returns the right facts but the model sometimes answers from its own memory anyway, contradicting the documents. What causes this?
3. In a RAG pipeline, what does "top-K" control?
4. Why do production RAG systems usually pair vector search with keyword search instead of using vectors alone?
5. Retrieval returns the right chunk, but answers are still wrong. Which is the most likely next thing to check?

My notes

Saved in this browser. Highlight a line above and save it, or write it in your own words.

Nothing saved yet. Your highlights will live here.

References

Finished reading?

Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.

Practice this in an incident

Related chapters

Spotted a mistake or want a topic covered? Report an issue

📬 An occasional email when new lessons land

New lessons and showdowns, no more than a few times a month. No spam, unsubscribe anytime. We store your email only to send the newsletter.