← deck$Dhruv Verma~/writing
applied security & backend
$cat ~/writing/from-hallucination-to-precision.mdEssay

Jun 11, 2026

From Hallucination to Precision: The RAG Approach

Understanding how retrieval enhances language models with external knowledge

Retrieval-Augmented Generation (RAG) is a pattern where a language model looks up relevant information from your data first, then uses that information to write an answer. It is, in effect, an LLM paired with a search engine over your own knowledge base, rather than a model relying solely on what it absorbed during training.

What RAG Tries to Solve

LLMs are trained once on a snapshot of the world and then frozen. They drift out of date, have no awareness of private documents or internal systems, and sometimes invent details because they are forced to answer from memory even when they do not actually know the fact.

RAG addresses this by letting the model pull in fresh, trusted information at query time from external sources: your documentation, databases, or APIs. The model then answers using that retrieved context, which usually yields more accurate, specific, and explainable responses.

High-Level Idea

A RAG system has two parts working together:

So instead of prompt → LLM → answer, the flow becomes prompt → search your data → combine prompt and results → LLM → answer.

Main Building Blocks

Most RAG setups, regardless of stack, share the same core pieces:

ComponentRole
Knowledge baseThe raw content: PDFs, wiki pages, tickets, SQL rows, Markdown docs, and so on.
Text chunkingSplits long documents into smaller pieces (paragraphs, sections, sliding windows) so retrieval returns focused snippets rather than huge blobs.
Embedding modelTurns each chunk (and later each query) into a numeric vector that captures semantic meaning, enabling similarity search rather than keyword matching.
Vector store / indexA database or index that stores those vectors plus metadata and answers the question, "which stored vectors are closest to this query vector?"
Retriever (and optional reranker)Embeds the user query, pulls the top matching chunks, and optionally reorders them with a reranking model for better quality.
LLMTakes the user question plus the retrieved chunks and produces a natural-language response grounded in that context.
Orchestration codeThe glue: ingestion scripts, retrieval functions, prompt templates, and API calls to the LLM.

Two Phases: Indexing and Answering

RAG has a "prepare once, answer many times" structure.

1. Indexing Your Data

This typically runs as a batch job or background process:

You repeat this whenever new docs appear or existing docs change.

2. Answering a User Query

This runs on every request:

From the user's point of view it still feels like chatting with a bot, but internally the bot is constantly doing search and read before answering.

Why Developers Like RAG

RAG is popular because it lets you inject your own data into an LLM workflow without retraining or fine-tuning the model itself. You can:

Compared to training a custom model from scratch, RAG is usually cheaper, faster to iterate on, and easier to reason about for most application teams.

Where RAG Shines

RAG tends to fit well when:

For simple, generic questions where correctness is not critical, a plain LLM call may be enough and simpler to manage.

Common Pitfalls

RAG is not a silver bullet; it can still fail in predictable ways.

Because of this, teams often spend considerable time tuning chunk sizes, retrieval strategies (top-K, filters, hybrid search), and prompt templates to reach acceptable quality.

How You'd Typically Implement It

A realistic first RAG project for a dev team often looks like this:

From there, you iterate: add filters (by product, date, customer), rerankers, caching, feedback collection, and so on. The architecture stays the same; what changes is how carefully each piece is tuned to your data and your users.

ready · open to security & backend rolesutf-8 · writing.md