explainx.ai350K+ learn from us
TrendingAI News TodayExplainx for Business
Pricing
explainx.ai

Upskill in AI — 16 free pathways, live workshops & bootcamps, and 50+ courses from practitioners. Plus the skills, tools, and MCP servers to practice on.

follow us

follow on google

Add explainx.ai as a preferred source

corporate training

support@explainx.ai

get started

Find your pathTake Free Evaluation

community

Join the community

learn

mind: share how you thinkpathways — start freeworkshopsbootcampscoursescompare Explainxcertificationsmock testsexplainx universitycorporate traininglearn skills & mcp

discover

skillsmcp serversexplainx mcpestacktoolsmdx readeragentsllmsdesignsdictionarypeopleagi trackerfelony benchranks

company

aboutvisionmissionteaminstructorsteach on explainxpartnershipscommunityhackathonscareers

content

daily AI newsstate of AI — live resultsblogreleasespromptsgeneratorsresource libraryfor LLMsexplainx.ai kids

solutions

all solutionsdeveloper upskillingmarketing upskillingproduct manager upskillingleadership upskilling

newsletter · weekly

Get AI news, tools, and insights in your inbox.

supportcontactprivacytermsdata rightshow we create contentsubmission guidelines

© 2026 AISOLO Technologies Pvt Ltd

explainx.ai

On this page

  • TL;DR
  • The problem RAG solves
  • How RAG works, in four steps
  • A worked example
  • When RAG is the right choice
  • Where RAG goes wrong
  • How to test a RAG system before you trust it
  • Beyond basic RAG
  • What to take away
  • Related reading
← Back to blog

explainx / blog

What Is RAG? Retrieval-Augmented Generation Explained Step by Step

RAG, AI Agents, Context Engineering, Guides

Part of AI Agents

RAG lets an AI answer from your documents: find the relevant passages, put them in the prompt, then generate. The four steps, a worked example and fixes.

Dec 5, 2026·9 min read·Yash Thakker
add explainx.ai
go deep
What Is RAG? Retrieval-Augmented Generation Explained Step by Step

A company launches an internal assistant. An employee asks, "How many days of parental leave do we get?" The assistant answers confidently: twelve weeks. The real policy, updated in March, says sixteen. The model was never wrong about language. It simply never saw the policy.

Retrieval-augmented generation (RAG) fixes this by adding one step before the model writes: find the relevant passages in your documents and put them in the prompt. It is the pattern behind most "chat with your documents" tools, help-centre bots and internal knowledge assistants. The idea comes from a 2020 research paper by Lewis and colleagues at Facebook AI Research, and today it is the most common way to give a model knowledge it was not trained on.

TL;DR

table · 2 cols
QuestionShort answer
What is RAG?Search your documents for the relevant passages, add them to the prompt, then let the model answer
What problem does it solve?The model does not know your documents or anything after its training cut-off
The four steps?Index the documents, retrieve passages for the question, add them to the prompt, generate the answer
Do I need a vector database?Not always; keyword or hybrid search often works as well
RAG or fine-tuning?RAG for facts that change or need sources; fine-tuning for behaviour and style
Why does RAG fail?Most often the search found the wrong passages
First thing to check?Which chunks were retrieved for the failing question

Catch up on AI

One AI story explained in plain English, plus one thing to try. Every day, free. Unsubscribe anytime.

350K+ Students Already Joined

The problem RAG solves

A large language model answers from two sources: the patterns it learned in training, and whatever is in its prompt. Your HR policy, product docs and last quarter's contracts are in neither. Ask about them and the model does what it always does: it writes the most likely answer. When it has no facts to work from, that answer is a plausible guess, which is how hallucinations happen.

You could paste the whole handbook into every prompt. For a two-page document, that is fine. For thousands of pages, it is slow, expensive and unreliable, because a model's context window is limited and details buried in very long inputs get less attention. RAG sends only the few passages that matter for this question.

How RAG works, in four steps

How RAG works: documents split into chunks and indexed so the right passages can be retrievedHow RAG works: documents split into chunks and indexed so the right passages can be retrieved

1. Index the documents (once, then on every change). Split each document into chunks: a few paragraphs each, ideally along headings so a chunk holds one idea. For each chunk, compute an embedding, a list of numbers that captures its meaning, and store it in a search index, often a vector database. Many systems also keep a keyword index of the same chunks.

2. Retrieve for the question. When a question arrives, embed it the same way and find the chunks whose meaning is closest, usually the top five to twenty. Keyword search finds exact names and codes that meaning-based search can miss, which is why many systems combine both in hybrid search. An optional reranking step then puts the best matches first.

3. Augment the prompt. Put the retrieved chunks into the prompt with clear instructions: answer only from these passages, cite the source of each claim, and say so when the passages do not contain the answer.

4. Generate. The model writes the answer from the passages it was given, with citations the user can follow.

Play · search problem or answer problem?
A RAG assistant gave a bad answer. From what you see, is the fault in retrieval (the search step) or in generation (the model's answer)?
It says "I don't know", but the answer is in the handbook
1/6

A worked example

Take the parental-leave question again, with RAG in place.

  1. Index. The HR handbook was split into 400 chunks when it was uploaded. When HR updated the leave policy in March, the changed page was re-indexed.
  2. Retrieve. The question "How many days of parental leave do we get?" matches three chunks: the March leave policy, an older FAQ entry, and a section on adoption leave.
  3. Augment. The prompt now reads, in short: "Answer the employee's question using only the passages below. Cite the passage title. If the passages disagree, prefer the most recent and say so." Then the three passages and the question follow.
  4. Generate. The answer: "Sixteen weeks of paid parental leave, under the policy updated in March (Leave Policy, section 2). An older FAQ entry still says twelve weeks; HR has replaced it."

The model did not get smarter. It got the right page, and an instruction to use it.

When RAG is the right choice

RAG is one of three ways to change what a model produces. They solve different problems, and the prompt engineering vs fine-tuning vs RAG guide goes deeper on the trade-offs.

table · 3 cols
Your problemBest first choiceWhy
The model should follow a format, tone or ruleA better promptCheapest, instant to change
The model needs facts from your documentsRAGReads current documents at question time and can cite them
Facts change often (prices, policies, stock)RAGRe-index the changed document; no retraining
The model must behave in a specialised way at scaleFine-tuningChanges behaviour, not stored facts
A small, fixed set of documentsA long promptSimpler than a search pipeline

A useful test from our grounding vs RAG vs fine-tuning guide: if the wrong answer comes from missing knowledge, retrieve; if it comes from wrong behaviour, prompt or tune.

Where RAG goes wrong

Most RAG problems are not the model's fault. They happen before the model writes a word.

RAG answers that cite their source passages, so a wrong answer can be traced to its search stepRAG answers that cite their source passages, so a wrong answer can be traced to its search step

table · 3 cols
SymptomLikely causeFix
Answer says "I don't know" when the document has itSearch did not retrieve the right chunkCheck chunking, add keyword or hybrid search, add a reranker
Answer uses an outdated factOld and new versions are both indexedRemove or re-index stale documents; prefer recent passages
Answer mixes two unrelated topicsChunks are too large or split mid-ideaChunk along headings, one idea per chunk
Right passage retrieved, answer still wrongPrompt does not make the model stick to the passagesTell it to answer only from the passages and to cite them
A user sees content they should notSearch ignores document permissionsFilter results by the user's access before they reach the model

The habit that saves the most time: debug retrieval and generation separately. For a failing question, look at which chunks were retrieved. If the right passage is missing, the fix is in indexing or search. If it is there and the answer is still wrong, the fix is in the prompt. Our guide to adding a reranker covers the most common retrieval upgrade, and the Bedrock access-control guide shows how to enforce permissions per user.

How to test a RAG system before you trust it

A RAG demo that answers three hand-picked questions proves little. Before people rely on it, test it the way they will use it.

  1. Collect 20 to 50 real questions. Take them from support tickets, chat logs or the people who will use the assistant, not from the documents themselves. Real questions use different words from the documents, and that gap is where search fails.
  2. Write down where each answer lives. For every question, note the document and section that holds the answer, or note that no document answers it. This list is your answer key.
  3. Score retrieval first. Run each question through the search step only. Did the right section appear in the top results? The share of questions where it did is your retrieval hit rate, and it caps the quality of everything after it.
  4. Then score the answers. For questions where retrieval succeeded, check whether the answer is correct, cites the right passage and adds nothing that is not in the passages. For questions with no answer in the documents, the right response is "I don't know".
  5. Re-run after every change. New chunking, a new search setting or a new prompt can fix five questions and break two. Keep the question set and run it each time.

This is the same discipline as evaluating prompts, with one extra column for retrieval. It turns "the bot seems worse this week" into a number you can act on, and it tells you which half of the pipeline to fix.

Beyond basic RAG

Basic RAG searches once and answers once. Two extensions are common:

  • Agentic RAG. An AI agent decides when to search, rewrites the query, searches again if the first results are weak, and combines several searches. It handles harder questions at the cost of more time and tokens. See RAG vs agentic RAG.
  • RAG through tools. Instead of a fixed pipeline, the agent calls a search tool, often exposed through MCP. Our RAG vs MCP comparison explains when each fits.

Both build on the same four steps. If the basic pipeline retrieves the wrong passages, an agent on top of it will retrieve wrong passages more times.

What to take away

RAG gives a model the right pages before it answers. It is the standard way to build an assistant on your own documents, because it stays current and can cite its sources.

When you build one, spend your effort where the failures are: clean documents, sensible chunks, search that finds the right passage, and a prompt that keeps the model to it. Then measure retrieval on real questions before you judge the answers. For the full pipeline design, continue with RAG context injection: pipeline design. To build agents that use retrieval with an instructor, see the AI Builder workshop.

Related reading

  • What are embeddings? Vector search explained
  • RAG context injection: pipeline design
  • Semantic vs vector vs hybrid search
  • Vector databases for RAG compared
  • RAG vs agentic RAG
  • Prompt engineering vs fine-tuning vs RAG
  • What is context engineering?

Primary source: Lewis et al., "Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks" (2020) · Anthropic: Contextual Retrieval


Retrieval tools and vector databases change often; the four steps do not. Accurate as of December 2026. Follow @explainx_ai for updates.

Spotted something out of date? Let us know.
Daily briefFrom Yash · 350K+ students taught

Catch up on AI

One AI story explained in plain English, plus one thing to try. Every day, free. Unsubscribe anytime.

  • ✓ Free
  • ✓ One email a day, no spam
  • ✓ Unsubscribe anytime
Yash Thakker

Written by

Yash Thakker

Yash is an AI expert with over 300K learners. Join his workshops →

View Yash Thakker in People in AI →

Share

RAG
Workshops

Learn by building

Live, hands-on workshops on Claude, agents, and AI tooling — taught by practitioners, not slides.

Explore workshops →

Related posts

Nov 29, 2026

How to Upskill in AI in 2027: A 12-Month Roadmap, Quarter by Quarter

Most people who want to upskill in AI try tools at random and stop after two weeks. This roadmap gives you one tool, one skill and one project per quarter for 2027, plus the map of AI tools and a checklist for the first 30 days.

Nov 27, 2026

Claude Sabotage Claims: Drew Breunig on Proactivity vs Malice

On November 27, 2026 Drew Breunig published an essay saying claims that Claude silently sabotages machine learning projects are mostly unsubstantiated hearsay, but proactive instruction-following can feel like sabotage when experiments need frozen variables. He links the anger to Anthropic's earlier hidden Fable 5 ML limits and shares a scientific-method skill that reduced unwanted edits.

Nov 27, 2026

Muxlio: An Open-Source Durable Workflow Coordinator for Claude, Codex, Cursor and Shell Tools

Muxlio is a new open-source coordinator that turns a multi-step agent job into an immutable plan and a durable run that survives your chat session. This guide explains the plan, run, worker and adapter model, how to install it, how its security policy works, and what its own docs say it has not measured.