I'll be honest with you. I've been putting off RAG for a while.
Every time I opened an article about it, I saw words like embeddings, vector databases and chunking strategies in the first paragraph, and I quietly closed the tab. 😅
This time I finally sat down and built something small. And here's what I learned: the idea behind RAG is simple. The hard part is the details.
So in this article, I'll walk you through RAG the way I wish someone had explained it to me: plain English, small examples, and one tiny working demo. I'm still learning too, so we're doing this together. 🤝
🤔 What Problem Does RAG Solve?
Large language models (LLMs) like Claude or GPT are smart, but they have three annoying limits:
- They don't know your private data. Your company handbook, your plugin docs, your client's FAQ? Never seen them.
- They have a knowledge cutoff. Anything newer than their training data is invisible to them.
- They sometimes make things up. Confidently. With a straight face. (This is called hallucination.)
Imagine you ask an AI:
"How many paid leave days do I get at my company?"
It has no idea. So it either says "I don't know" or, worse, invents a believable number. 😬
That's exactly where RAG comes in.
📖 So... What Is RAG?
RAG = Retrieval-Augmented Generation.
Let's break the name down:
- Retrieval: find the right pieces of information from your own data
- Augmented: add that information to the question
- Generation: let the LLM write an answer using it
My favorite analogy is an open-book exam. 📚
- A normal LLM is a student who has to answer from memory.
- A RAG-powered LLM is a student who is allowed to flip through the textbook before answering.
Same student. Much better answers.
🔄 How RAG Works (The Big Picture)
There are two phases. Think of them as "prepare the library" and "answer the question."
🏗️ Phase 1: Prepare the Library (done once, or whenever your docs change)
- Collect your documents (PDFs, web pages, markdown files, support tickets, anything).
- Chunk them: cut them into small pieces, like paragraphs.
- Embed each chunk: turn the text into a list of numbers (a vector) that captures its meaning.
- Store those vectors in a vector database (or even just a simple list while you're learning).
🙋 Phase 2: Answer a Question (every time a user asks)
- User asks a question.
- Embed the question the same way.
- Search for the chunks whose meaning is closest to the question.
- Paste those chunks into a prompt together with the question.
- Send it to the LLM. It answers using that context.
That's the whole thing. Really.
🧩 Wait, What Is an "Embedding"?
This was the part that confused me the most, so let me make it simple.
An embedding turns text into numbers so that similar meanings end up close together.
For example, these two sentences use totally different words:
- "How do I reset my password?"
- "I forgot my login credentials."
A plain keyword search might miss the connection. But their embeddings land very close to each other, because the meaning is similar.
That's why RAG feels smarter than a normal search box. It searches by meaning, not just by matching words. 🎯
🍰 Why Do We Chunk the Documents?
Two reasons:
- Precision. If you feed a whole 50-page PDF to the model, the useful sentence is buried. A small chunk about "leave policy" is easier to match.
- Limits and cost. LLMs have a context window, and more text means more cost. Small relevant chunks keep things cheap and focused.
A common starting point is chunks of a few hundred words, with a small overlap between neighbors, so a sentence cut in half still makes sense. There's no magic number, though. You'll tune it based on your content.
💡 Example 1: The HR Policy Bot (No Code)
Let's say your company has this tiny handbook:
- "Full-time employees get 24 paid leave days per year."
- "Work from home is allowed up to 2 days per week."
- "Laptop repairs must be requested through the IT portal."
An employee asks: "Can I work from home 4 days a week?"
Here's what RAG does:
1.Embeds the question.
2.Finds the closest chunk: "Work from home is allowed up to 2 days per week."
3.Builds a prompt like this:
Answer the question using ONLY the context below. If the answer isn't in the context, say you don't know. Context: Work from home is allowed up to 2 days per week. Question: Can I work from home 4 days a week? 4.The LLM replies: "No, the policy allows up to 2 work-from-home days per week." ✅
Notice the line "say you don't know." That small instruction is a powerful way to reduce made-up answers.
🐍 Example 2: A Tiny RAG Demo in Python
Let's build the smallest possible version. No vector database, no framework. Just enough to see the idea working.
Install the packages:
pip install sentence-transformers numpy anthropic Step 1: Our "documents" and the embeddings
from sentence_transformers import SentenceTransformer import numpy as np # A small, free embedding model that runs on your machine model = SentenceTransformer("all-MiniLM-L6-v2") docs = [ "Full-time employees get 24 paid leave days per year.", "Work from home is allowed up to 2 days per week.", "Laptop repairs must be requested through the IT portal.", "The office is closed on public holidays.", ] # Turn every doc into a vector (normalized, so a dot product = cosine similarity) doc_vectors = model.encode(docs, normalize_embeddings=True) Step 2: Retrieval (find the closest chunks)
def retrieve(question: str, top_k: int = 2) -> list[str]: question_vector = model.encode([question], normalize_embeddings=True)[0] scores = doc_vectors @ question_vector # similarity score for each doc best_indexes = np.argsort(scores)[::-1][:top_k] # highest scores first return [docs[i] for i in best_indexes] Step 3: Augment and generate
import anthropic client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from your environment def ask(question: str) -> str: context = "\n".join(retrieve(question)) prompt = f"""Answer the question using ONLY the context below. If the answer isn't in the context, say you don't know. Context: {context} Question: {question}""" response = client.messages.create( model="claude-sonnet-5-5", # use any current model you have access to max_tokens=300, messages=[{"role": "user", "content": prompt}], ) return response.content[0].text print(ask("Can I work from home 4 days a week?")) That's a complete RAG loop: embed → retrieve → augment → generate. 🎉
In real projects, you'd swap the simple list for a vector database (like Chroma, Qdrant, pgvector, or Pinecone) and load real documents. But the flow stays exactly the same.
🌐 Example 3: A Docs Chatbot for a WordPress Plugin
Since I live in the WordPress world, this one got me excited.
Imagine a plugin with 60 documentation pages and a support inbox full of repeat questions like "How do I enable X?" or "Why is Y not saving?"
With RAG, you could:
- Chunk your docs and past support answers.
- Embed them into a vector store.
- Add a small chat box in the plugin settings page.
- When someone asks a question, retrieve the best doc chunks and let the LLM write a friendly answer, with a link back to the source page.
Fewer repeat tickets, faster answers, and the docs finally earn their keep. 😄
🚧 Common Beginner Mistakes (I Made a Few Already)
- Chunks that are too big or too small. Too big gives vague matches. Too small loses context.
- Skipping the "I don't know" instruction. Without it, the model happily fills gaps with guesses.
- Retrieving too few (or too many) chunks. Start with 3 to 5 and adjust.
- Never testing with real questions. Keep a list of 15 to 20 real questions and check the answers by hand. It's the cheapest evaluation you can do.
- Messy source data. RAG can't fix outdated or contradictory documents. Garbage in, confident garbage out.
✅ Quick Recap
- RAG = give the LLM the right information before it answers.
- Phase 1: chunk, embed, store.
- Phase 2: embed the question, retrieve, augment the prompt, generate.
- Embeddings search by meaning, not just keywords.
- Start tiny. A 30-line script teaches more than 3 hours of reading.
🙌 Your Turn: Let's Learn Together
Like I said at the start, I'm still in the learning phase with RAG. This article is me sharing what clicked for me, and I'd love to hear what clicked (or broke) for you.
👇 Drop a comment and tell me:
- 🛠️ What RAG project have you built? (Big or small, a weekend experiment counts!)
- 🧱 What challenges did you face while building it? Chunking? Bad retrieval? Hallucinations? Choosing a vector database?
- 💡 What would you wish you knew before starting?
I'll read every reply, and I'll turn the best tips and challenges into a follow-up article. Let's help each other get past the confusing parts. 🚀
