RAG, explained without the hype

What Retrieval-Augmented Generation actually is, why it solves the "LLM made it up" problem, and how to build a minimal version in under 50 lines of Python.

The first time I used an LLM to answer a question about my own notes, it confidently made up three citations that didn’t exist. That’s the problem RAG was built to solve.

This post is the short version of what RAG actually is, why it works, and the smallest useful example I could write.

The problem in one sentence

An LLM only knows what it saw during training. Ask it about your private documents, your company’s internal wiki, last week’s news, or this morning’s log files — it has no idea, but it will often answer anyway. That’s hallucination.

User question”What’s in doc X?”LLMtrained on the publicinternet up to some dateno access to doc XConfident answerbut made upthe hallucination problem

What RAG actually does

RAG stands for Retrieval-Augmented Generation. The name describes the architecture exactly:

  1. Retrieval — when the user asks a question, first search your own documents for relevant chunks
  2. Augmented — paste those chunks into the prompt as context
  3. Generation — let the LLM answer using that context

The model isn’t doing anything new or magical. It’s just answering with material that’s now sitting right there in the prompt. The intelligence is in how you fetch the right chunks.

Why “just search” isn’t enough

Old-school search uses keywords. If you search “automobile” but the document says “car”, keyword search misses it.

RAG uses embeddings instead — a way to represent meaning as a list of numbers (typically 768 or 1536 of them). Texts with similar meaning end up close to each other in that high-dimensional space. So “automobile” and “car” land in roughly the same neighborhood, and a similarity search finds both.

embedding space (simplified to 2 dimensions)carautomobilevehicledogpuppycaninepythonprogrammingvehicles clusteranimals clustercode cluster

The vector database is just a fast way to ask: “which of my 10,000 chunks are closest to this query, by cosine distance?”

The whole pipeline

Here’s the architecture every RAG system follows, from indexing your documents (one-time setup) to answering a question (every query):

// indexing — once, when you add new docsDocumentsPDFs, wikis,notes, codeChunkssplit into~500 token bitsEmbeddertext to vector[0.12, -0.4, …]Vector Databasestores vectors +original text// query — every time the user asks somethingQuestionfrom the userEmbed querysame embedderTop-k searchclosest chunksLLM + contextgrounded answer

That’s it. Every RAG system in production is some elaboration on this loop.

A minimal working example

Here’s a complete RAG in ~40 lines using local models so it costs nothing to run:

# pip install sentence-transformers chromadb ollama
from sentence_transformers import SentenceTransformer
import chromadb
import ollama
# 1. Your knowledge base — in real life this is PDFs, docs, web pages, etc.
documents = [
"Caddy is a web server that handles HTTPS automatically using Let's Encrypt.",
"Ollama lets you run large language models locally on your own machine.",
"Astro is a static site generator optimized for content-heavy websites.",
"ChromaDB is an open-source vector database for embeddings.",
]
# 2. Set up the embedder and vector store
embedder = SentenceTransformer('all-MiniLM-L6-v2')
client = chromadb.Client()
collection = client.create_collection("notes")
# 3. Embed and store the documents
embeddings = embedder.encode(documents).tolist()
collection.add(
embeddings=embeddings,
documents=documents,
ids=[f"doc_{i}" for i in range(len(documents))]
)
# 4. Ask a question
question = "How do I get free SSL certificates on my server?"
query_embedding = embedder.encode([question]).tolist()
# 5. Retrieve the top 2 most relevant chunks
results = collection.query(query_embeddings=query_embedding, n_results=2)
context = "\n".join(results['documents'][0])
# 6. Send to the LLM with context
prompt = f"""Answer the question using only the context below.
Context:
{context}
Question: {question}
"""
response = ollama.chat(model='llama3:8b', messages=[
{'role': 'user', 'content': prompt}
])
print(response['message']['content'])

Run it and you’ll get a grounded answer pointing to Caddy and Let’s Encrypt, drawn from your own knowledge base instead of the model’s training data.

What this skips (a lot)

This example is the skeleton. Real RAG systems wrestle with:

  • Chunking strategy — how big should each chunk be? Where do you split? Overlap?
  • Retrieval quality — top-k isn’t always right; hybrid search usually wins
  • Re-ranking — cheaper models retrieve a wide net, then a heavier model re-orders
  • Evaluation — how do you know your RAG got better? You need a test set
  • Metadata filtering — “only search docs from the last 30 days”
  • Citations — showing the user which source the answer came from

Each one is its own rabbit hole. The example above is enough to feel why RAG works; everything else is engineering polish.

When NOT to use RAG

RAG isn’t the answer to every problem. Skip it when:

  • The answer is purely about how to reason or write, not about facts (e.g., “rewrite this email”)
  • The knowledge is already in the model’s training data and not changing
  • You need structured data — RAG retrieves text; for numbers, query a real database

The honest mental model: RAG is how you give an LLM access to facts it doesn’t have. If facts aren’t the bottleneck, RAG won’t help.

What I’d build next

If I were starting a serious RAG project today, this is the stack I’d pick:

  • Embeddings: bge-large-en-v1.5 or OpenAI’s text-embedding-3-small
  • Vector DB: ChromaDB for prototyping, Qdrant or pgvector for production
  • Re-ranker: Cohere Rerank or bge-reranker-large
  • LLM: Llama 3 locally for private docs, otherwise Claude or GPT-4o

But honestly, the 40-line example above is a great place to start. Build something that almost works, see where it fails, then add complexity only where you can measure the improvement.

Next post: I’ll wire this exact pipeline into a small “ask my homelab docs” assistant and run it on the lab server.

// discussion

Comments

Replies use GitHub Discussions. You'll need a GitHub account to post.