Learn AI · Foundations
RAG Explained
Retrieval-Augmented Generation is how you give LLMs access to your own data without retraining them. Here's how it actually works.
Key takeaways
- 1RAG is retrieval + generation: find relevant chunks, then ask the LLM to answer using them.
- 2Most RAG failures are chunking failures, not model failures.
- 3Hybrid search (dense embeddings + BM25 keyword + reranker) beats embeddings-only on real corpora.
- 4Always cite sources back to the user — it builds trust and lets you debug retrieval quickly.
- 5Skip RAG if your docs fit in context, or if you need exact answers from structured data (use SQL instead).
The problem RAG solves
LLMs are frozen at their training cutoff and they don't know about your private documents, your company wiki, or last week's incident report. Retrieval-Augmented Generation (RAG) fixes that without retraining the model.
The basic loop
- Index time — Chunk your documents, generate embeddings for each chunk, store them in a vector database.
- Query time — Embed the user's question, find the top-k most similar chunks, paste them into the prompt as context, then ask the LLM to answer using only that context.
That's it. The "augmentation" is just stuffing relevant snippets into the prompt before the question.
Chunking is where most RAG systems fail
Chunk size matters more than people think:
- Too small — chunks lose context (a paragraph cut in half is meaningless).
- Too large — top-k retrieval surfaces too much noise.
- Naive splitting — splitting on character count breaks tables, code, lists.
Practical defaults: 400–800 tokens per chunk, 10–15% overlap, split on semantic boundaries (paragraphs, headers, code blocks).
For specialized content, customize further:
- Code: split on function boundaries, keep the file path in metadata.
- Tables: keep the whole table as a single chunk, even if large.
- Q&A docs: each Q+A is one chunk.
Retrieval quality
Plain cosine similarity on embeddings often fails. Real systems combine:
- Dense retrieval (embeddings) — captures meaning.
- Sparse retrieval (BM25 / keyword) — captures exact terms.
- Reranking — a small model re-scores the top-50 down to top-5.
This hybrid setup beats embeddings-only by a wide margin on real corpora.
The full pipeline
User question
→ embed query
→ vector search (top-50)
→ BM25 search (top-50)
→ merge + dedupe
→ cross-encoder rerank (top-5)
→ format as context
→ LLM with system prompt + context + question
→ response with citations
You can skip steps for simple cases. Add them as quality demands.
When NOT to use RAG
- The data fits in a single context window — just paste it.
- The user needs exact answers from structured data — query the database directly.
- The task is creative, not factual — retrieval will hurt, not help.
Common failure modes
- Stale index — documents updated but vectors weren't re-embedded.
- Mismatched embedding models at index vs query time.
- No citations in the response, so users can't verify.
- Prompt doesn't actually instruct the model to use only retrieved context — it just gets ignored.
RAG isn't magic. It's just "find the right paragraph, then paste it in." Get the retrieval right and the LLM does the rest.
Common misconceptions
The wrong-but-common takes worth correcting.
Myth
RAG means fine-tuning the model on my data.
Reality
No fine-tuning. RAG retrieves passages at query time and pastes them into the prompt. The model is unchanged.
Myth
More retrieved chunks = better answers.
Reality
Past 5–10 chunks, you usually add noise. Quality of retrieval beats quantity.
Myth
RAG is solved — just plug in a vector DB.
Reality
Vector DBs are the easy part. Chunking, hybrid retrieval, reranking, and citation handling are where real systems differentiate.
Real-world use cases
Internal knowledge bases
Ask questions across Confluence, Notion, or wikis without indexing them into another tool.
Customer support copilots
Surface the right help-doc passage before the support agent has to search.
Research assistants
Pair RAG with web search to build a Perplexity-style agent over your own corpus.
Open
Codebase Q&A
Embed your repo, retrieve the right files, then ask the LLM to explain or modify.
Frequently asked questions
Watch
Hand-picked videos from official + trusted channels. Opens in a new tab.
- AnthropicOfficial
Anthropic talks and tutorials
Official channel — search 'RAG' or 'retrieval' for current best videos.
- DeepLearning.AI
DeepLearning.AI courses on RAG
Andrew Ng's channel — multiple short courses cover RAG end-to-end.
- LangChain
LangChain tutorials and patterns
Practical RAG patterns and reference implementations.
Related on AIKnowHub
Concept
Vector Databases Explained
What vector databases actually do, when you need one, and what the real tradeoffs between Pinecone, Weaviate, Qdrant, and pgvector look like.
Concept
Embeddings Explained
Embeddings turn text into numbers that capture meaning. They power search, RAG, recommendations, and clustering — here's how.
Workflow
Build an AI Research Assistant
A research agent that takes a question, searches the web, reads sources, and produces a cited briefing.
Comparison
Best Embedding Model
OpenAI vs Voyage vs Cohere vs open-source — which embedding model wins on quality, cost, and ecosystem in 2026?
Tool Guide
Dify Guide
The definitive guide to Dify for low-code AI apps — RAG chatbots, agent workflows, MCP import, self-host vs cloud, and when to pick it over Flowise or n8n.
Tool Guide
Flowise Guide
The definitive guide to Flowise for visual RAG and agent prototyping — drag-and-drop chains, 2.0 templates, MCP nodes, and when to pick it over Dify or n8n.