All articles
Guides12 min readJune 4, 2026

What Is Retrieval-Augmented Generation (RAG)? A Practical Guide

Retrieval-augmented generation (RAG) is a technique that fetches relevant documents from your own data and hands them to a language model before it answers, so the response is grounded in real sources instead of the model's memory. It works in two phases: retrieval (search your knowledge base) and generation (write an answer using what was found). RAG cuts hallucinations and lets a model cite up-to-date, private information without retraining.

Quick answer

Retrieval-augmented generation (RAG) is a technique that fetches relevant documents from your own data and hands them to a language model before it answers, so the response is grounded in real sources instead of the model's memory. It works in two phases: retrieval (search your knowledge base) and generation (write an answer using what was found). RAG cuts hallucinations and lets a model cite up-to-date, private information without retraining.

Retrieval-augmented generation (RAG) is a technique that retrieves relevant text from your own knowledge base and feeds it to a language model at query time, so the model answers from real source material instead of whatever it absorbed during training. The idea was named in a 2020 paper by Lewis and colleagues at what was then Facebook AI Research, and it has since become the default way teams connect large language models to private, current, or domain-specific data.

The mechanics are simpler than the acronym suggests. When a user asks a question, the system searches a collection of documents, pulls back the few passages most likely to contain the answer, and pastes them into the prompt alongside the question. The model then writes its response using that supplied context. Nothing about the model's weights changes. You are not teaching it anything permanently; you are handing it an open book right before it has to answer.

That distinction matters because LLMs have two well-known weaknesses. They make things up when they do not know something, and their knowledge is frozen at the point their training data was cut off. RAG attacks both. It gives the model fresh facts it never saw in training, and it gives it specific passages to point at, which makes confident fabrication far less likely. If you have ever wanted a model to answer questions about your product docs, your support history, or last quarter's numbers without an expensive fine-tuning project, RAG is usually the right tool.

The rest of this guide walks the full pipeline: how documents get chunked and embedded, how vector search finds candidates, why reranking matters, how the final answer gets grounded, and where the whole thing breaks. We will also cover how RAG fits inside an AI agent that does not just answer but then takes action on what it found.

Why RAG beats stuffing everything into the prompt or fine-tuning

There are three ways to give a model knowledge it did not learn in training: put everything in the prompt, fine-tune the weights, or retrieve on demand. Each has a place, and confusing them wastes money.

Stuffing your entire knowledge base into the context window does not scale. Even with large context windows, you pay for every token on every call, latency climbs, and models get measurably worse at finding the relevant needle when the haystack is huge. Fine-tuning, meanwhile, is good at teaching a model a style, a format, or a narrow skill, but it is a poor way to inject facts. Facts go stale, retraining is slow and costly, and you cannot easily trace which document an answer came from.

RAG sidesteps both problems. You keep your knowledge in a searchable index, retrieve only the handful of passages that matter for a given question, and update the index whenever a document changes. Cost stays roughly flat per query, answers reflect today's data, and because you know exactly which passages were retrieved, you can show citations. For a deeper side-by-side, see the dedicated comparison on RAG versus fine-tuning.

RAG gives a model an open book at query time. Fine-tuning changes how it writes; retrieval changes what it knows.

How does RAG actually work, step by step?

A production RAG pipeline has an offline stage that prepares your data and an online stage that runs on every question. The offline work happens once per document (and again when that document changes); the online work happens in the second or two between a user hitting enter and an answer appearing.

The RAG pipeline, end to end
  1. 1

    Chunk

    Split source documents into passages small enough to be specific but large enough to stand alone.

  2. 2

    Embed

    Convert each chunk into a vector with an embedding model, then store it in a vector index.

  3. 3

    Retrieve

    Embed the user's question and find the nearest chunks by vector similarity, often blended with keyword search.

  4. 4

    Rerank

    Score the top candidates with a cross-encoder to push the truly relevant passages to the top.

  5. 5

    Generate

    Insert the best passages into the prompt and have the model write a grounded, cited answer.

The first two steps run offline when documents change; the last three run live on every query.

Most teams get the offline half right and underinvest in the online half. As the 2026 RAG literature keeps repeating, retrieval quality, not model size, is the usual bottleneck. If the wrong passages come back, even the best model will write a confident, wrong answer from them.

Chunking: where most RAG pipelines quietly fail

Chunking is the act of cutting your documents into passages before you embed them, and it is the single most common place a RAG system goes wrong. The reason is subtle: you retrieve chunks, not documents, so a chunk has to make sense on its own. If you split a contract in the middle of a clause, the retrieved fragment is missing the words that gave it meaning.

A good rule of thumb is that every chunk should be able to answer a question by itself. That pushes you toward semantically complete units: a full FAQ entry, a complete policy paragraph, a single API endpoint's documentation. Naive fixed-size splitting (every 500 characters, no regard for boundaries) is fast to build and slow to debug later.

  • Fixed-size chunking splits on a token or character count with some overlap; it is simple and predictable, but it happily cuts sentences and tables in half.
  • Recursive chunking respects structure by splitting on paragraphs first, then sentences, then words only if needed, which keeps related text together far more often.
  • Semantic chunking measures embedding similarity between consecutive sentences and starts a new chunk when the topic shifts, so each chunk maps to one idea.
  • Document-aware chunking uses the file's own markup (headings, list items, code blocks, table rows) as natural boundaries, which works especially well for structured docs.
  • Overlap between adjacent chunks (a sentence or two of shared text) reduces the odds that the one sentence you need lands exactly on a boundary and gets orphaned.

Sizing is a trade-off, not a constant. Smaller chunks in the 256 to 512 token range tend to win for narrow, fact-style questions because they are precise. Larger chunks of 1,000 or more preserve context for sprawling, multi-part topics but dilute relevance. The honest answer is that you tune this against your own queries and measure, rather than copying a number from a blog post.

Embeddings and vector search: turning meaning into math

An embedding is a list of numbers (a vector) that captures the meaning of a piece of text, produced by an embedding model. Two passages about cancellation policies land near each other in this vector space even if one says 'refund window' and the other says 'money-back period.' That is the whole trick: similar meaning, nearby vectors. Keyword search would miss the connection; embeddings catch it.

At index time, you embed every chunk and store the vectors in a vector database built for fast nearest-neighbor lookup. At query time, you embed the user's question with the same model and ask the index for the chunks whose vectors sit closest, usually measured by cosine similarity. Those nearest neighbors are your candidate passages.

Pure vector search has a blind spot: it can fumble exact strings like product SKUs, error codes, or person names, where the literal token matters more than the vibe. That is why strong systems run hybrid search, blending vector similarity with traditional keyword scoring so you get both semantic recall and exact-match precision. If you want the full mechanics of this layer, the explainer on vector databases and embeddings goes deeper, and connecting an agent to structured data is covered in the guide on connecting an AI agent to your database.

Reranking: the cheap step that fixes most bad answers

Vector search is fast but approximate. It is good at recall (getting the right passage somewhere in the top 50) and weaker at precision (getting it into the top 3). Reranking closes that gap. After the initial search returns a generous candidate set, a reranker re-scores each candidate against the question and reorders them so the most relevant land at the top, where the model will actually read them.

The reranker is usually a cross-encoder, a model that reads the question and a candidate passage together and outputs a single relevance score. It is slower per pair than the embedding lookup, which is exactly why you do not run it on your whole corpus: you run it on the 20 to 50 candidates retrieval already narrowed down. The cost is small and the quality lift is often the largest single improvement you can make to a struggling pipeline.

The pattern that consistently performs well in production, and that the 2026 best-practice writeups keep landing on, is hybrid retrieval followed by reranking. It gives the best quality-to-cost ratio because cheap, broad search does the heavy lifting and an expensive, accurate model only touches a short list.

ApproachFinds the right passageRanks it near the topCost per queryBest for
Keyword onlyMisses paraphrasesGood on exact termsLowestCodes, SKUs, names
Vector onlyStrong on meaningOften buries the best hitLowConceptual questions
Hybrid searchStrong on bothBetter, still imperfectLow to mediumGeneral knowledge bases
Hybrid + rerankStrong on bothBest of the fourMediumMost production systems
How the main retrieval approaches trade off recall, precision, and cost. Directional, based on common production patterns.

Grounded generation: where retrieval becomes a real answer

Once the best passages are selected, the model finally does its job. The retrieved chunks go into the prompt as context, the user's question follows, and the instructions tell the model to answer using only what was provided and to say so when the context does not cover the question. That last instruction is what turns a fluent guesser into a grounded answerer.

Grounding is also what makes citations possible. Because you know which chunks were inserted, you can attach each claim to its source document, which is the difference between an answer a user trusts and one they have to re-verify. A well-built RAG response reads like a sharp colleague who actually looked things up, not one talking from memory.

Grounding is not a guarantee, though. A model can still misread a passage, blend two sources, or pad a thin context with invented detail. The defense is partly prompt design and partly evaluation, which is why teams pair RAG with automated checks and red-teaming. The articles on AI hallucinations and on AI guardrails go further on keeping generation honest.

If the right passage never reaches the prompt, no amount of clever prompting will save the answer. Retrieval comes first.

A worked example: a support rep asks about a refund policy

Picture a support rep at a 200-person SaaS company. A customer is disputing a charge, and the rep types: 'What is our refund policy for annual plans cancelled in month two?' Here is what RAG does behind that one question.

The question gets embedded and run against the index, which holds chunked versions of the billing policy, the terms of service, and a year of resolved support tickets. Hybrid search returns about 30 candidate chunks: some from the official policy, a few near-duplicate ticket replies, one outdated policy version from last year. The reranker scores them against the exact phrasing of the question and pushes the current annual-plan clause to the top while demoting the stale version.

The model then writes a two-sentence answer: annual plans cancelled after the 14-day window are prorated from the cancellation date, with a link to the policy section it pulled from. The rep reads it, sees the citation, and pastes a customer-ready version into the ticket in under a minute. No guessing, no digging through a wiki, and crucially, no fabricated policy that creates a compliance problem later.

Notice that the agent stopped at answering. The next obvious move is to update the deal, issue the credit, or close the ticket, and that is where retrieval hands off to action.

How RAG fits into an AI agent that then acts

RAG retrieves and explains; an AI agent retrieves and then does something with what it found. That is the leap from a smart search box to a coworker that finishes the task. In the refund example, an agent would not just surface the policy. It would check the customer's plan in the CRM, confirm the cancellation date, calculate the prorated amount, and either issue the credit or, for anything above a threshold, pause for a human to approve.

Retrieval becomes one tool the agent calls among many. The agent might use RAG to read the policy, a CRM tool to look up the account, and a billing tool to apply the change, reasoning across all three. This is also where governance stops being optional. An agent that can act on retrieved data needs least-privilege permissions so it only touches what it should, human-in-the-loop approvals for risky actions, and an audit log of what it read and changed. Onpilot is built around exactly that combination: AI agents that retrieve from your systems, take action, and deliver finished work to Slack, Teams, or your app under approvals, RBAC, and full audit trails.

The retrieval quality you invested in pays off twice here. A grounded read of the policy is what makes the resulting action safe to take. If you want the broader picture, the pieces on what an AI agent is and on human-in-the-loop AI agents connect retrieval to autonomy and oversight.

Where RAG fails, and how to see it coming

RAG reduces hallucinations; it does not abolish them, and most failures trace back to retrieval rather than the model. When a RAG system gives a wrong answer, resist blaming the LLM first. Pull the retrieved chunks and look at what it was actually given to work with. Nine times out of ten the problem is upstream.

  • Bad retrieval is the top culprit: the relevant passage was chunked badly, embedded poorly, or buried below the cutoff, so the model never saw it and answered from memory instead.
  • A stale index quietly serves yesterday's truth: if a policy changed but the document was not re-embedded, retrieval confidently returns the old version with no warning that it is outdated.
  • Context overflow happens when you stuff too many chunks into the prompt; the model loses the important passage in the middle and quality drops even though the right text was technically present.
  • Conflicting sources confuse the model when two retrieved chunks disagree (an old wiki page and a new one), and without metadata to prefer the fresh source it may average them into something false.
  • Over-trusting similarity bites when no chunk is actually relevant but the search still returns its nearest neighbors, and the model dutifully answers from passages that only look related.
  • No abstain path means the system never says 'I do not have that,' so a thin or empty retrieval gets padded with plausible invention instead of an honest miss.

The fix for almost all of these is measurement. Track retrieval metrics (did the right chunk come back, and how high did it rank) separately from answer metrics, keep a refresh schedule so the index never drifts far from source, and give the model an explicit way to decline. The guide on AI agent evaluation metrics covers how to instrument this end to end.

Common chunking and retrieval mistakes to avoid

A handful of mistakes show up in nearly every first RAG build. They are easy to avoid once you know to look for them, and expensive to debug if you do not.

  • Embedding the question and the documents with different models: the two vector spaces do not line up, and retrieval returns near-random results that look like a model problem.
  • Skipping the reranker to save a few milliseconds: you give up the single biggest precision gain available and then wonder why the right passage keeps landing fifth.
  • Treating chunk size as a universal constant copied from a tutorial instead of tuning it against your real queries and document types.
  • Forgetting to re-embed updated documents, which turns your index into a museum and produces the most dangerous failure of all: a confident, well-cited, wrong answer.
  • Indexing without metadata (source, date, access level), which means you cannot prefer fresh content, filter by who is allowed to see what, or enforce permissions at retrieval time.
  • Measuring only the final answer and never the retrieval step, so you optimize prompts blindly while the actual bottleneck sits one layer below your dashboards.

A simple decision framework: when to use RAG

RAG is the right call when the knowledge a model needs lives in documents that change, are private, or are too large to fit in a prompt. It is the wrong call when the problem is about behavior rather than facts.

Reach for RAG when answers must cite a source, when the underlying data updates often (pricing, policies, inventory, tickets), or when the corpus is private and you would never put it in a public model's training set. Support knowledge bases, internal wikis, product documentation, and contract repositories are textbook fits.

Lean toward fine-tuning instead when you need to change how the model writes or behaves: a consistent tone, a strict output format, or a specialized classification skill. These are style and behavior problems, not knowledge problems, and retrieval will not solve them. Many mature systems use both, fine-tuning for format and RAG for facts.

And when the task is not really a question at all but an action across systems (update this record, run this report, resolve this ticket), RAG is only the first step. You want an agent that retrieves, reasons, and then does the work under governance. The decision is rarely RAG or nothing; it is RAG for the facts, an agent for the follow-through.

Where retrieval quality comes from
Retrieval (chunking + search + rerank)
55%
Embedding model quality
20%
Prompt + grounding instructions
15%
Base LLM capability
10%

Illustrative weighting of what typically drives RAG answer quality in production systems. Directional, not a benchmark; your mix will vary by corpus.

The bottom line on RAG

Retrieval-augmented generation turns a language model from a confident generalist into a grounded specialist on your data, without retraining and without exposing your private documents to anyone's training set. The pipeline is straightforward to describe (chunk, embed, retrieve, rerank, generate) and genuinely hard to get right, because quality lives in the unglamorous middle: clean chunks, hybrid search, and a reranker doing the precision work.

Get retrieval right and the model has little reason to hallucinate. Get it wrong and no model, however large, can rescue an answer built on the wrong passages. That single insight, that retrieval is the bottleneck, is the most useful thing to carry out of this guide.

RAG is also a beginning, not an end. The most valuable systems do not stop at a grounded answer; they retrieve, decide, and act, with a human in the loop where it counts and an audit trail behind every move. That is the difference between a smarter search box and an AI agent that actually finishes the work.

Frequently asked questions

What is retrieval-augmented generation (RAG) in simple terms?

+

RAG is a technique that searches your own documents for relevant passages and gives them to a language model right before it answers a question. The model writes its response using that supplied text instead of relying only on what it learned during training. The result is an answer grounded in your real, current data with sources you can point to.

Does RAG eliminate AI hallucinations?

+

RAG reduces hallucinations significantly but does not eliminate them. By grounding answers in retrieved source text, it gives the model real facts to work from and makes confident fabrication much less likely. A model can still misread a passage or pad a thin context, so RAG works best alongside grounding instructions, evaluation, and a way for the model to decline when it lacks the answer.

What is the difference between RAG and fine-tuning?

+

Fine-tuning changes the model's weights to teach it a style, format, or skill, while RAG leaves the model unchanged and feeds it knowledge at query time. Fine-tuning is good for behavior and tone but poor for facts that change. RAG is good for facts, fresh data, and citations. Many production systems use both: fine-tuning for how the model writes and RAG for what it knows.

What is chunking in RAG and why does it matter?

+

Chunking is splitting your documents into smaller passages before embedding them, and it matters because you retrieve chunks rather than whole documents. Each chunk should be able to answer a question on its own, so bad splits that cut sentences or clauses in half produce useless retrievals. Chunking strategy is one of the most common places a RAG pipeline silently underperforms.

What is reranking and do I need it?

+

Reranking is a second scoring pass that reorders the candidate passages from initial search so the most relevant ones land at the top of the prompt. It usually uses a cross-encoder that reads the question and each candidate together for a precise relevance score. For most production systems it delivers the biggest single quality improvement for a small added cost, so it is worth adding.

What is the difference between vector search and keyword search in RAG?

+

Vector search compares the meaning of text using embeddings, so it matches paraphrases like 'refund window' and 'money-back period' even when the words differ. Keyword search matches exact terms, which is better for SKUs, error codes, and names. Strong RAG systems use hybrid search that blends both, getting semantic recall and exact-match precision together.

How does RAG work inside an AI agent?

+

Inside an agent, RAG is one tool the agent calls to read relevant information, after which the agent reasons and then takes action, such as updating a record or resolving a ticket. The grounded read is what makes the resulting action safe to perform. Because the agent can act, it needs least-privilege permissions, human-in-the-loop approvals for risky steps, and an audit log of what it read and changed.

Why does my RAG system give wrong answers?

+

Most wrong RAG answers come from retrieval, not the model. Common causes are bad chunking, a stale index serving outdated documents, too many chunks crowding the prompt, conflicting sources, and no path for the model to say it does not know. The fix is to inspect the retrieved chunks, measure retrieval quality separately from answer quality, and keep your index refreshed.

Build a grounded AI agent that acts on what it retrieves

See how to wire retrieval, tools, and governed actions together with the Onpilot SDK, REST API, and embeddable widget. Start with the quickstart.

Read the developer docs