Skip to content
tech

What is RAG and Why Your AI Needs Your Data

·6 min read·4 views

You ask ChatGPT about the bug that happened yesterday in your system. It gives a generic answer about debugging. You insist, paste the error log. It gives a better answer, but you had to do the work of feeding the AI manually.

What if the AI could go fetch that information on its own?

That's RAG (Retrieval-Augmented Generation) — and it's probably the most important technique for anyone who wants to use AI for real in day-to-day development.

The fundamental problem with LLMs

Models like GPT-4, Claude, and Gemini know a lot. They were trained on billions of documents. But they have two serious limitations:

  1. They don't know your private data. Your code, your logs, your internal documentation — none of it exists for the model.
  2. Knowledge has a cutoff date. If something happened after training, the model doesn't know.

The naive solution would be to retrain the model with your data. But retraining an LLM costs thousands of dollars and takes weeks. Not viable for 99% of use cases.

RAG solves this elegantly: instead of teaching the model, you search for relevant information and inject it into the context before it responds.

How RAG works, step by step

The process has three stages:

1. Indexing (once)

You take your documents — Markdown files, logs, code, PDFs, anything textual — and transform them into embeddings. An embedding is a numerical representation of a text's meaning. Sentences with similar meanings end up "close" in vector space.

These embeddings are stored in a vector database (like Chroma, Pinecone, pgvector, or FAISS).

2. Retrieval (every question)

When you ask a question, it's also transformed into an embedding. The system searches for documents that are most semantically close to your question.

This is different from keyword search. If you ask "how to fix high CPU load," a keyword search only finds documents containing those exact words. Vector search finds your incident runbook that talks about "load average above 4.0" — because the meaning is close.

3. Generation (the answer)

The found documents are injected into the LLM's context, along with your question. The model now has access to information that wasn't in its training data. It responds based on your actual data, not generic knowledge.

When to use RAG vs. fine-tuning

RAGFine-tuning
CostLow (just embedding)High (retraining)
SpeedMinutes to set upHours/days to train
Fresh dataYes, real-timeNeeds retraining
Best forQuerying private dataChanging style/format
HallucinationLower (citable sources)Can get worse

In practice, RAG is the right answer for 90% of cases where you want AI to know your data. Fine-tuning is for when you want to change how the model responds, not what it knows.

Real example: semantic search over personal notes

In my setup, I use local RAG to semantically search my notes — see the full project.

The MCP server knowledge-rag indexes my Markdown files and lets Claude Code ask questions like:

  • "What was the last security incident?"
  • "How is MySQL backup configured?"
  • "What decisions did Billy make about billing?"

Even without the exact words in the files, vector search finds the right documents. It's the difference between searching Google (keywords) and asking someone who read everything (semantics).

Tools to get started

If you want to implement RAG today:

  • LangChain / LlamaIndex — Python frameworks that abstract the entire pipeline
  • Chroma — simple vector database, runs locally, zero config
  • pgvector — PostgreSQL extension, ideal if you already use Postgres
  • OpenAI Embeddings — API for generating embeddings (text-embedding-3-small is cheap and good)

A minimal setup with LangChain + Chroma + OpenAI runs in 20 lines of Python.

RAG in Practice: How I Use It in HubNews

Theory is nice, but I wanted to show how RAG works for real in production. In HubNews — my AI-powered news platform — the entire publishing pipeline is a multi-stage RAG.

The full flow

RSS (300 chars)  Jina Reader (1800 chars)  Embeddings  Similar Articles + SEO Data  AI Writer  Article

Each step adds a layer of context. This isn't a "dump everything into the prompt and pray" approach. It's a chain where each stage enriches the previous one.

Retrieve: fetching the real content

It all starts with RSS. The problem: an RSS feed gives you ~300 characters of summary. Impossible to write anything decent from that.

The ResearchAgent uses the Jina Reader API to fetch the full source article. It strips navigation, footer, sidebar, comments — keeps only the article body. Content jumps from ~300 to ~1800 characters. If Jina fails, it falls back to direct HTTP with regex extraction. The pipeline doesn't stop.

Deduplicate: embeddings against duplicates

Each article gets a vector embedding via OpenAI (text-embedding-3-small, 512 dimensions). This vector is compared against the last 600 articles using dot product similarity. If the score exceeds 0.82, the article is a duplicate — it gets merged with the existing one instead of becoming a separate post.

This prevents publishing 5 versions of the same story when every outlet covers the same event.

Augment: internal context + real SEO data

This is where RAG truly shines. The WriterAgent receives three types of context injected into the prompt:

  1. The enriched content — the full article from Jina Reader
  2. Up to 3 similar published articles (threshold 0.5) — marked as "previous coverage — don't copy, use to enrich"
  3. Real SEO data from Google Search Console — top-performing titles, CTR data

The model isn't writing in a vacuum. It knows what we've already published on the topic and what works in terms of engagement.

Generate + Validate

DeepSeek v3.2 rewrites the article with all this context, following strict rules: no invented data, no generic headlines, specific numbers required.

Then, CritiqueJob evaluates headline quality and FactCheckerJob verifies claims against the original sources.

The insight that matters

This is not a simple "stuff context into prompt" RAG. It's multi-stage: retrieve external content → enrich with internal knowledge (similar articles) → optimize with real performance data (SEO) → validate output. Each stage is non-blocking — if any retrieval fails, the pipeline continues with what it has.

What changes in practice

RAG transforms AI from a "generic know-it-all" into a specialist in your context. The model stays the same, but now it has access to what matters — your data, your documentation, your history.

The trend is clear: more and more development tools will embed RAG by default. Cursor already does this with your code. Claude Code does it with memory files. GitHub Copilot is adding repository context.

If you're still manually copying and pasting context for AI, it's worth understanding RAG. It's the difference between shouting instructions to someone across the street and sitting next to them with all the documents on the table.

Want to apply this to your project?

Career, code & digital product consulting.

Work with Billy
Billy

Billy

Full Stack Dev & Empreendedor Solo

Building products with code and AI. Creator of HubNews and Sistema Reino.