Prompt Engineering

What Is RAG and Why Every AI App Uses It

The simple trick that lets AI answer questions about your own stuff — explained in plain language.

Scroll to start

AI With a Cheat Sheet

Imagine a student taking a test. There are two kinds: a closed-book test, where the student has to remember everything from memory, and an open-book test, where the student can look things up first. Most AI apps today are taking an open-book test. That trick is called RAG.

RAG stands for Retrieval-Augmented Generation. It is a fancy way of saying: before the AI writes an answer, it first looks stuff up. The AI grabs the most relevant pages from a library of documents, reads them, and then uses them to write a much better answer.

Think of it like this: without RAG, the AI is guessing from memory. With RAG, the AI gets to peek at the right notes first. The "retrieval" part finds the notes. The "augmented generation" part writes the answer using those notes.

It's What Makes AI Useful

Regular AI has three big problems. It can only answer based on what it learned during training, so it doesn't know anything new. It sometimes makes things up that sound right but are wrong. And it has never seen your company's private information, so it can't help with that.

RAG fixes all three. The AI reads fresh, real documents before it answers. The answer is grounded in real text, not made up. And it can use your own files, like product manuals, help articles, or company policies, even though the AI never saw them during training.

💡 Key Insight

RAG is the difference between a generic chatbot that anyone could build and a useful business tool that knows your stuff. Almost every serious AI app you use today — customer support bots, coding helpers, search engines — uses RAG under the hood.

What regular AI can't do on its own

Answer questions about events that happened after its training cutoff.
Know anything about your private documents, products, or policies.
Guarantee its answers are accurate — it can invent fake facts confidently.
Cite the source of what it says, so you can verify the answer.

The Five-Step Open-Book Trick

Even though RAG sounds fancy, the steps are actually pretty simple. Every time you ask a question, the AI app quietly runs through this little loop before it types a single word of the answer.

The RAG Pipeline
You Ask
Type a question in the app
🔍
Search
App finds matching docs
📄
Read
AI reads the best matches
💬
Answer
AI writes a grounded reply
every question

The "search" step is the clever part. The app doesn't just look for exact words. It turns your question into a list of numbers called a vector that captures the meaning. Then it finds the documents whose numbers are closest to yours. That's how it can find pages about "getting a refund" even if your question used the word "return".

A Tiny RAG App in 20 Lines

Here is what a super simple RAG system looks like in Python. Imagine you have a folder of notes about your business. The script turns those notes into vectors, then uses them to answer a question.

simple_rag.py
# 1. Read your documents
with open("company_notes.txt") as f:
    docs = f.read().split("\n\n")

# 2. Turn each doc into a list of numbers (a "vector")
embeddings = [embed(doc) for doc in docs]

# 3. User asks a question
question = "How do I get a refund?"
q_vec = embed(question)

# 4. Find the 3 docs closest in meaning
top_docs = closest(q_vec, embeddings, k=3)

# 5. Ask the AI, but give it the docs as a cheat sheet
answer = ask_llm(
    question=question,
    context="\n".join(top_docs)
)
print(answer)

The magic is in step 5. The AI gets the user's question and the most relevant snippets. The answer it writes is grounded in your real documents, not in some fuzzy memory from training. If the docs don't contain the answer, a good RAG system will say so instead of making something up.

Knowledge Check

Test what you learned with this quick quiz.

Quick Quiz — 3 Questions

Question 1
What does the "R" in RAG stand for?
Question 2
What is the main problem RAG solves?
Question 3
In the RAG pipeline, what happens right BEFORE the AI writes its answer?
🏆

You crushed it!

Perfect score on this module.