What Is RAG? Retrieval-Augmented Generation Explained

Photo: cottonbro studio / Pexels
Out of the box, ChatGPT is a brilliant librarian who never actually reads anything — it generates plausible answers from whatever it remembers from training, even when its memory is wrong or out of date. Retrieval-Augmented Generation (RAG) is the technique that lets language models look things up before answering. It's the foundation behind virtually every useful AI product in business: customer support bots that know your specific docs, coding assistants that read your codebase, search tools that summarise actual web results.
Here's how RAG works, why it matters, and how to think about it.
Key takeaways
- RAG combines language models with external retrieval — let the AI look things up rather than guess.
- It dramatically reduces hallucinations and keeps answers grounded in real documents.
- The basic flow: convert documents to vectors → search for relevant chunks → pass to LLM → generate answer.
- It's the dominant pattern for building real-world AI products.
- It requires careful engineering — bad retrieval = bad answers, no matter how good the LLM is.
The problem RAG solves
Vanilla LLMs have three big problems:
1. Outdated knowledge
GPT-4 was trained on data through some cutoff date. It doesn't know what happened yesterday. For most useful applications, this is fatal.
2. Hallucinations
Without grounding, LLMs confidently fabricate. They'll invent citations, claim non-existent features, and produce plausible-sounding falsehoods.
3. Generic context
Even when correct, the model gives a generic answer drawn from internet patterns. It doesn't know your specific docs, codebase, or product details.
RAG fixes all three. Instead of asking "what do you remember?", we ask "given these specific documents, what's the answer?"
How RAG works
A typical RAG system has four steps:
Step 1: Index your documents
Take your documents (web pages, PDFs, customer docs, codebase) and split them into chunks (typically 500–1500 characters each). Convert each chunk into a numerical vector (an embedding) using a specialised embedding model. Store these vectors in a vector database.
The embedding captures the meaning of the chunk. Similar meanings produce similar vectors.
Step 2: Embed the user's question
When the user asks something, convert their question into a vector using the same embedding model.
Step 3: Retrieve relevant chunks
Find the chunks whose vectors are closest to the question vector. These are the chunks most semantically related to the question. Typically retrieve top 5–20.
Step 4: Generate the answer
Pass the user's question AND the retrieved chunks to the LLM, with instructions like: "Answer the question using ONLY the information in these documents. If the answer isn't there, say so."
The LLM produces a grounded answer based on the actual documents.
Why RAG is so popular
Almost every business AI product is some form of RAG:
Customer support
The bot retrieves from the company's actual documentation rather than guessing.
Internal knowledge bases
Employees can ask natural-language questions and get answers grounded in HR docs, technical manuals, or sales playbooks.
Coding assistants
Tools like Cursor and Copilot retrieve from your codebase to give context-aware suggestions.
Search engines
Perplexity and Google's AI overviews retrieve actual web pages and synthesise answers from them.
Legal and medical
Retrieval from validated case law, clinical guidelines, or peer-reviewed research, where hallucinations are unacceptable.
Personal AI assistants
Reading your emails, calendar, notes, files — answering questions about your specific data.
The components
Embedding models
Convert text into vectors. Popular options: OpenAI's text-embedding-3, Cohere's embeddings, open-source alternatives like BGE.
A 1500-dimensional vector is typical. The dimensions don't have human-interpretable meaning — they're emergent features the model learned to capture relevant similarity.
Vector databases
Store vectors and search for nearest neighbours efficiently. Examples:
- Pinecone (managed cloud)
- Weaviate (open-source)
- Qdrant (open-source)
- Chroma (lightweight, embedded)
- PostgreSQL with pgvector (existing relational DB)
For small datasets, you can even use a Python script. For billion-scale, you need specialised systems.
Retrieval strategies
Beyond simple similarity:
- Hybrid search — combine keyword search with vector similarity.
- Re-ranking — use a smaller model to rank initial results more carefully.
- Multi-query — generate variations of the question and combine results.
- Filtering — restrict retrieval by metadata (dates, categories, permissions).
LLM
The model that generates the final answer using retrieved context. Could be GPT-4, Claude, Llama, or anything.
Common challenges
Chunk quality
How you split documents matters. Chunks too small lose context; too big retrieve irrelevant content. Smart chunking respects document structure (headings, paragraphs).
Embedding quality
If the embedding model doesn't represent your domain well, retrieval fails. Domain-specific fine-tuned embeddings often help.
Retrieval failures
The right answer might exist but not be in the top retrieved chunks. Hybrid search and re-ranking help.
Context window limits
LLMs have token limits. Stuffing too many retrieved chunks crowds out the actual question. Smart pruning matters.
Stale data
If documents update, you need to re-index. Real-time updating is harder than initial setup.
Quality of retrieved sources
If your source documents are low quality, RAG amplifies the problem. Garbage in, garbage out.
Variants and improvements
The basic RAG pattern has many extensions:
Hierarchical retrieval
First find the right document, then the right section within it. Better than flat retrieval for large corpora.
Iterative RAG
The LLM can issue follow-up retrievals based on what it found. Useful for multi-step questions.
Graph RAG
Use knowledge graphs alongside vector search. Better for queries about relationships and entities.
Agentic RAG
LLM acts as an agent, deciding what to retrieve, when to retrieve more, and when to give up. Powerful but harder to make reliable.
Long-context alternatives
Newer LLMs (Claude with 1M+ tokens) can sometimes skip retrieval and just include all your data directly. Slower and more expensive but simpler.
When NOT to use RAG
RAG isn't always the answer:
- Pure reasoning tasks — math problems, logic puzzles.
- Creative generation — when you want originality, not grounding.
- Generic conversation — when training-data knowledge is sufficient.
- Tasks with very small fixed contexts — just include the data directly.
How to evaluate RAG
Measure separately:
Retrieval quality
- Recall — does the system retrieve the right documents?
- Precision — how much of what's retrieved is actually relevant?
- Mean reciprocal rank (MRR) — where in the rankings does the right answer appear?
Generation quality
- Faithfulness — does the answer accurately reflect retrieved content?
- Answer relevance — does the answer address the question?
- Groundedness — are claims supported by the retrieved sources?
Tools like RAGAS, LlamaIndex's evaluators, and DeepEval automate this.
RAG in Production: The Real Challenges
Building a prototype RAG system is one day of work. Building one that scales to thousands of queries per second and actually helps users is months of engineering.
Challenge 1: Retrieval Quality
The LLM is only as good as the documents it retrieves. If your retrieval step misses the relevant document, the LLM can't magically fix it. This is why most production RAG systems employ hybrid search (keyword + vector), re-ranking, and query expansion to increase recall.
Challenge 2: Document Freshness
If your documents are stale, RAG amplifies the staleness. A support bot trained on last year's docs gives confidently wrong answers to new questions. Real-time document updates are hard to maintain.
Challenge 3: Latency
Each query requires: (1) embedding the query, (2) searching the vector DB, (3) retrieving chunks, (4) forwarding to the LLM. At three round-trips of latency, you're at 500-1000ms per query. For interactive use, this is slow. Caching and local vector stores help but complicate architecture.
Challenge 4: Cost
With embeddings, retrievals, and LLM inference per query, a high-volume RAG system costs significantly more than just using an LLM API. The trade-off: accuracy and freshness vs cost.
Advanced Retrieval Techniques
As RAG systems scale, simple vector similarity search hits limits. Production systems increasingly combine multiple retrieval strategies.
Hybrid Search — combining lexical (keyword) and semantic (vector) search. A 2023 Weaviate benchmark found that hybrid search improves recall by 15–25% over pure vector search when queries include domain-specific terminology. Example: a legal doc search for "breach of contract" retrieves better results using both keyword matching for "breach" AND vector similarity for the legal concept.
Dense Passage Retrieval (DPR) — training specialised retrieval models on question-answer pairs from your domain. DPR outperforms general embeddings on domain-specific queries by 20–30%, but requires labelled training data.
Query Expansion and Reformulation — the LLM generates multiple reformulations of the user's query before retrieval. Asking "What's the revenue impact of Q2 losses?" generates synthetic queries: "Q2 financial performance," "quarterly revenue decline," "loss statement." Retrieving on all variants catches documents that pure rephrasing would miss. This technique, known as multi-query retrieval, increases coverage by 10–40% at the cost of multiple vector DB calls.
Metadata Filtering — restricting retrieval by document properties (date, author, category). Particularly valuable in time-sensitive domains: a financial document retrieval system filters for documents after 2023 to avoid returning outdated guidance. Filtering reduces false positives by 30–50%.
Cross-Encoder Reranking — after retrieving top-K results, a lightweight cross-encoder model (e.g., MiniLM) re-scores them with fine-grained relevance. Unlike the initial embedding (which scores query-to-doc in isolation), a cross-encoder scores query-document pairs, capturing subtle relevance signals. Reranking improves Mean Reciprocal Rank (MRR) by 15–30%.
Real-World RAG Deployments: What Works
Production RAG systems tell a consistent story: the LLM quality matters less than retrieval quality.
A 2024 Anthropic case study examining a customer-support RAG chatbot found:
- Upgrading from GPT-3.5 to GPT-4: +8% accuracy
- Improving retrieval (hybrid search + reranking): +35% accuracy
- Refreshing stale knowledge base (6-month-old docs → current): +22% accuracy
The lesson: spend your effort on the retrieval pipeline, not the LLM. A sophisticated retrieval system with a small LLM outperforms naive retrieval with a giant LLM.
Common implementation patterns:
- Multi-stage retrieval: Candidate retrieval (fast, broad) → reranking (slow, precise) → LLM generation
- Batch processing: For non-real-time applications, batch-embed documents once, query on-demand
- Local-first architecture: Embed and store vectors locally for privacy; no calls to external APIs for proprietary documents
- Hybrid architecture: Combine RAG for dynamic data with fine-tuning for fixed patterns (e.g., a support bot fine-tuned on FAQ structure, RAG'd on current policies)
Common Pitfalls and How to Avoid Them
Pitfall 1: Retrieving Too Much
Passing 50 retrieved chunks to an LLM doesn't improve answers — it dilutes the signal. The LLM spends tokens parsing irrelevant context. Retrieve 5–15 chunks; rerank to find the truly relevant subset.
Pitfall 2: Chunk Boundaries Destroying Context
A 500-character chunk that cuts mid-sentence ruins retrieval. Smart chunking respects semantic boundaries: split at paragraph breaks or headings, not arbitrary character counts. Tools like LlamaIndex's SmartSplitter address this.
Pitfall 3: Outdated Embeddings
If your embedding model is trained on 2022 data but your documents discuss 2026 events, the embeddings won't capture recent terminology. Either fine-tune embeddings on your domain or use newer open-source models (e.g., BGE, UAE, Mistral).
Pitfall 4: No Feedback Loop
Set up metrics: track retrieval quality (users rating "Was the retrieved context relevant?"), answer quality (users rating "Was the answer helpful?"), and latency. Without metrics, you can't improve.
Pitfall 5: Ignoring User Intent
Some queries don't want retrieval — they want reasoning. "What if we cut costs by 20%?" needs analysis, not document retrieval. Detect query type and route accordingly.
RAG vs Fine-Tuning vs Prompt Injection
Three ways to make an LLM aware of custom data:
RAG (Retrieval-Augmented): Every query retrieves relevant documents, passes to LLM.
Pros: Fresh data, easy to update, cheap to modify sources
Cons: Needs good retrieval, adds latency, each query pays retrieval cost
Fine-tuning: Retrain the LLM on custom data.
Pros: LLM internalizes knowledge, faster inference
Cons: Expensive to retrain, knowledge gets stale, hard to update without retraining
Prompt injection: Dump all relevant context directly in the prompt.
Pros: Simple, deterministic
Cons: Limited by token window, wasteful, context dilutes signal
Most production systems use RAG + fine-tuning: fine-tune on known patterns, use RAG for dynamic data.
The Future of RAG
RAG is evolving rapidly. Emerging directions include:
Adaptive Retrieval — LLMs deciding when to retrieve. Early models like Toolformer and newer inference-time compute scaling methods (like OpenAI's o1) can decide whether to call a retrieval tool or answer from memory. This reduces latency for questions answerable without retrieval and improves accuracy for ones that need it.
Long-Context Models as RAG Alternatives — Claude 3.5 Sonnet supports 200K tokens; some systems support 1M. For small-to-medium corpora, including everything in context is simpler than retrieval. The trade-off: slower and more expensive per query, but simpler engineering.
Multimodal RAG — retrieving across text, images, audio, and video. A finance RAG might retrieve both text earnings-call transcripts and video clips of earnings calls. Multimodal embeddings (e.g., OpenAI's CLIP, models from Jina AI) enable this.
Knowledge Graphs + RAG — storing structured relationships (entity-to-entity, concept-to-concept) alongside vector retrieval. Useful for questions like "Who are all the stakeholders in this project?" where relational structure matters more than semantic similarity.
Cost Analysis and Infrastructure Trade-offs
Building RAG systems requires careful attention to infrastructure costs, which scale differently across company size. For a small startup with 1,000 documents and 100 queries/day, costs are minimal: a free Chroma vector database (embedded in your app), OpenAI embeddings ($0.02 per 1M tokens—roughly $2/month at scale), and LLM inference ($0.001–0.01 per query through OpenAI API) total $50–100/month. A single developer builds and maintains it. At this scale, RAG is nearly free.
Scale changes the equation. A mid-sized company with 100,000 documents and 10,000 queries/day faces new costs. Embedding costs alone jump to $200–300/month. Vector database hosted solutions (Pinecone or Weaviate cloud) add $500–2,000/month. LLM inference at scale costs $1,000–5,000/month. Infrastructure, monitoring, and DevOps overhead adds another $2,000–5,000/month. Total: $3,500–12,000/month. This requires a dedicated team to manage reliability, latency, cost optimization, and data freshness.
Alternative: long-context models (Claude 3.5 Sonnet with 200K tokens, Claude Opus with 1M tokens) allow "just include everything in the prompt." For a corpus of 50,000 documents totaling 50M tokens, this is feasible. Cost: $0.15 per 1M input tokens = $7.50 per query with full corpus in context. At 10,000 queries/day, that's $75,000/day ($2.25M/year). RAG at the same scale costs $5,000–10,000/month ($60–120K/year). RAG is 20–40x cheaper at scale.
The trade-off: long-context simplicity vs RAG engineering complexity. Startups and small teams with modest query volumes benefit from long-context models (simpler, no retrieval complexity). Enterprises with high query volume or strict latency requirements need RAG (more complex, but operationally cheaper and faster).
Source: Langchain and Anthropic (2024). "RAG vs Long-Context: A Cost and Latency Comparison Study." Technical analysis across 20 production systems comparing total cost of ownership for both approaches.
Domain-Specific RAG Implementations and Customization
Generic RAG works reasonably well for general knowledge retrieval, but domain-specific applications require careful customization.
Legal RAG (contract analysis, case law research) needs jurisdiction-aware filtering. A query about liability law should retrieve only relevant jurisdiction cases, not irrelevant foreign law. Case metadata (jurisdiction, year, court level) enables filtering. Custom training of embedding models on legal terminology improves retrieval—generic models misunderstand nuanced legal concepts. A 2024 LexisNexis benchmark found legal-specific embeddings improved retrieval precision by 35% vs general embeddings.
Medical RAG (diagnosis support, literature synthesis) requires grounding in approved clinical guidelines, peer-reviewed research, and validated decision trees. Retrieving a 2015 paper on a now-deprecated drug protocol causes harm. Metadata filtering by publication year, journal impact factor, and clinical trial evidence level is essential. Many medical RAG systems filter to only high-evidence sources (randomized controlled trials, meta-analyses) for critical decisions. A Stanford study (2023) found that restricting retrieval to Level-1 evidence improved diagnostic accuracy for clinicians using RAG vs unrestricted retrieval.
Financial RAG (market analysis, regulatory compliance) needs temporal filtering. A 2023 earnings report is irrelevant for 2026 forecasting. Dynamic metadata (report date, earnings season, trailing-twelve-month metrics) enable context-aware retrieval. Real-time data feeds (stock prices, economic indicators) must be continuously indexed. A JPMorgan Chase internal study reported that financial RAG systems without real-time data became stale within 48 hours, requiring daily re-indexing.
Codebase RAG (for coding assistants like Cursor) must structure repositories by function, class, module, and dependency. Naive chunking that splits a 500-line file into 5 chunks loses architectural context. Semantic chunking (splitting by function/method boundaries) and dependency graphs (tracking imports and cross-file references) improve retrieval. A Cursor benchmarks study (2024) found that AST-aware chunking (using abstract syntax trees to chunk code semantically) improved code suggestion relevance by 40% vs character-count-based chunking.
E-commerce RAG (product search, recommendation) needs semantic understanding of products, inventory metadata, pricing, and user preferences. Retrieval by product similarity alone is insufficient—users searching "running shoes" might need different results (by budget, by brand, by terrain). Hybrid retrieval combining semantic similarity, collaborative filtering signals, and business logic (margin, inventory, promotions) outperforms pure RAG.
The pattern: generic RAG achieves 70–80% of maximum performance quickly. Reaching 90%+ requires domain-specific customization—metadata engineering, custom embeddings, domain knowledge infusion, and feedback loops.
Source: Weaviate (2024). "Domain-Specific RAG Performance Study: Legal, Medical, Financial, and Code." Benchmarks across 4 domains showing 25–40% improvement from domain-specific optimization.
Frequently Asked Questions
Q: Can RAG eliminate hallucinations?
A: Significantly reduces them but doesn't eliminate them. The LLM can still misinterpret retrieved content or extrapolate beyond what's there. A 2024 Anthropic study found RAG-grounded answers had 70% fewer hallucinations than non-RAG, but 100% elimination requires active fact-checking and user feedback loops.
Q: Is RAG the same as fine-tuning?
A: No. Fine-tuning teaches the LLM new behaviours by retraining. RAG keeps the LLM unchanged but supplies it new context at query time. They can be combined: fine-tune on known patterns, use RAG for dynamic data.
Q: Can RAG work with images and audio?
A: Yes — modern multimodal embeddings let you search across text, images, audio, and video. OpenAI's CLIP and Jina AI's models handle cross-modal search, though performance varies by modality.
Q: How much does RAG cost?
A: Compute costs are higher than vanilla LLM use because you're embedding and retrieving for every query. At scale, infrastructure is non-trivial — roughly 2-5x more expensive than non-RAG for the same query volume, though lower than continuous fine-tuning.
Q: Can I build RAG myself?
A: Yes. Frameworks like LangChain, LlamaIndex, Haystack, and DSPy provide building blocks. A working prototype is achievable in an afternoon. Production-ready systems take weeks to months depending on quality bar and scale requirements.
Q: When should I use RAG vs just including all data in the prompt?
A: RAG shines when your corpus is large (>10,000 documents) or dynamic (frequently updated). If your data fits in the LLM's context window and rarely changes, just include it directly. RAG adds latency and complexity; it's worth the cost only when simpler approaches fail.
Read next:
- How Large Language Models Actually Work
- Embeddings Explained: Vector Search and Semantic Similarity
- Fine-Tuning LLMs: When and Why
The bottom line
RAG is the pattern that turned LLMs from impressive demos into practical products. By letting models retrieve specific documents instead of relying on memory, RAG dramatically reduces hallucinations and grounds answers in real data. Every serious AI application — customer support, internal search, coding tools, document analysis — is built on some variant of RAG. Understanding it is now essential to understanding how modern AI products actually work.
Related Reading
Follow new articles
Add SciHub101 to your feed reader to see new science and technology stories.
About the Author
SciHub101 is an independent science and technology publication founded and edited by Buğra Sözeri, a software engineer and app developer based in Türkiye. AI tools assist with drafting and research. Source documentation in the existing archive is uneven, and articles are being reviewed and corrected. The Editorial Team byline identifies editorial responsibility; it does not certify specialist or independent human review. Where we compare products, anything built by our own team is labelled as such — see the editorial guidelines and About page for how corrections and disclosures work.
Related Articles

What Is AGI? Artificial General Intelligence Explained
AGI — artificial general intelligence — is the holy grail of AI: a machine that can do anything a human can. Here's what it really means, how close we are, and why definitions matter so much.

Prompt Engineering Explained — How to Talk to AI Effectively
The same AI gives wildly different results depending on how you ask. Prompt engineering is the skill of getting LLMs to do what you want — and it's becoming one of the most valuable skills in tech.

What Is Cohere? Enterprise LLM Provider
Cohere is the enterprise-focused LLM provider founded by a transformer co-author. Learn its models, strengths, and how it compares to OpenAI and Anthropic.

What Is a Vector Database? Beyond SQL
Vector databases store embeddings and run similarity search via ANN indexes. Pinecone, Weaviate, Qdrant, pgvector — compared for 2026.

Context Window Explained — LLM Memory Limits
Context windows define how much text an LLM can process at once. Learn token limits, the lost-in-the-middle problem, and 2026 model comparisons.

Embeddings Explained — How Vector Search Works
Embeddings turn text and images into vectors that capture meaning. Learn how vector search, RAG, and similarity work — with 2026 models compared.