What Is RAG and Why It Matters
From the course RAG: Retrieval-Augmented Generation in Practice
Built-in AI Professor Exclusive
Ask anything about the lesson and get an instant answer. The AI Professor knows the course content and helps you learn more effectively.
An LLM will answer with the same confidence whether it knows the answer or is making it up — and in production, a wrong answer delivered with conviction costs more than an honest "I don't know". Retrieval-Augmented Generation — RAG for short — attacks exactly this problem. It is not a model, not a framework, not a library: it is a design principle whereby, instead of relying exclusively on the knowledge an LLM internalized during training, you bring relevant information from external sources at the moment the answer is generated. Without understanding why this principle works, everything that follows — embeddings, chunking, retrieval, re-ranking — remains a mechanical exercise, executed without knowing what you are actually solving.
Why large language models hallucinate
To understand why RAG exists, we first need to understand why LLMs fail in such insidious ways. A model like GPT-5.5, Claude Opus 4.8 or Gemini 3.1 Pro was trained on massive text corpora — trillions of tokens from books, scientific articles, source code, web pages, conversations. Through this training, the model learned statistical distributions over token sequences. When it generates text, the model selects each next token based on probabilities conditioned on the preceding context.
Hallucination arises from at least four distinct mechanisms:
1. Probabilistic completion without factual grounding. The model does not "know" facts in the sense that a database stores records. It has learned statistical co-occurrences. If, in the training data, the phrase "the capital of Australia" frequently appeared near "Sydney" (from contexts discussing the largest city), the model may confidently generate "Sydney", even though the correct answer is Canberra. The model does not verify — it generates.
2. Knowledge cutoff. An LLM's training ends at a fixed date. GPT-5.5 (released April 23, 2026) and Claude Opus 4.8 (released May 28, 2026) have a cutoff in January 2026, but any event, product, law, or change that appeared after that date simply does not exist in the model's "memory". If you ask about an EU regulation adopted last month, the model will improvise based on the earlier regulations it knows — producing an answer that is plausible but potentially incorrect.
3. No access to private data. Even for contemporary information, if the data is private — internal policies, proprietary technical documentation, contracts, enterprise knowledge bases — the model has never seen it. And it cannot. You cannot include your company's proprietary source code in public training data.
4. Conflicts and overlaps in the training data. When different sources provide contradictory information (which happens frequently on the internet), the model internalizes both perspectives. The generated answer may reflect either one, or the other, or a combination, with no guarantee of accuracy.
These mechanisms make hallucination an emergent property of the transformer architecture, not a bug that can be "fixed" with more training. It is intrinsic to how these models work. RAG does not eliminate hallucination — but it reduces it dramatically by grounding generation in verifiable sources.
The knowledge cutoff problem in an enterprise context
In academia or for general questions, the knowledge cutoff is an inconvenience. In an enterprise setting, it is a critical problem. Let's take a concrete example from a heavily regulated industry.
A financial services company uses an LLM to answer questions about the regulations issued by its national banking regulator. Suppose one of the applicable regulations was recently updated with new requirements on operational risk reporting. The model, trained on data up to 2025, knows the old version of the regulation. An analyst who asks "What are the current operational risk reporting requirements?" receives an answer based on the 2024 version — perfectly structured, professionally worded, and completely outdated. If the analyst does not verify manually (and why would they, if they turned to an AI assistant precisely for efficiency?), the company risks non-compliance.
RAG solves exactly this problem. The updated regulations are indexed in the vector database. When the analyst asks the question, the system retrieves the relevant fragments from the 2026 version of the regulation and supplies them to the model as context. The answer reflects current reality, not the fossilized memory of training.
RAG versus fine-tuning versus long context: a comparative analysis
One of the most frequent confusions in the field of LLM applications is the distinction between RAG, fine-tuning, and using extended context windows. All three are mechanisms for adapting an LLM to a specific domain, but they work in fundamentally different ways.
Fine-tuning modifies the model's internal parameters. Through additional training on a specific dataset, the model adjusts its internal probability distributions. It is like sending a general surgeon to a two-year specialization — afterwards, they think differently and approach problems differently. Fine-tuning is suitable for: (a) adapting style and tone — if you want the model to answer like a legal advisor, not a general-purpose assistant; (b) internalizing domain-specific terminology; (c) improving performance on specific tasks (classification, entity extraction). Fine-tuning is expensive (hundreds or thousands of dollars per training run), slow (hours or days), and rigid — once trained, the model reflects the data as of training time. If the information changes, it must be retrained.
Long context takes advantage of the fact that modern models have enormous context windows. By 2026, the 1M-token window has become the default on all relevant variants: Claude Opus 4.8 exposes 1M via the model ID claude-opus-4-8[1m] (with 128K output), and GPT-5.5 offers ~1M natively. You can, in theory, insert tens of thousands of pages directly into the prompt. The advantage is simplicity: no need for vector databases, embeddings, chunking — you insert the documents and ask. The disadvantages are: (a) cost: you pay per token processed — Opus 4.8 costs $5/$25 per million input/output tokens, GPT-5.5 $5/$30 — and 500,000 tokens of context on every question adds significant costs; (b) latency: processing a 1M-token context takes significantly longer than processing 5,000; (c) performance degradation: empirical studies (including the paper "Lost in the Middle" — Liu et al., 2024) show that models tend to pay less attention to information in the middle of the context, even on 1M windows; (d) scalability: if you have 10 million documents, you cannot insert them all.
Important note for 2026 — the RAG vs long-context tradeoff: With Opus 4.8 offering prompt caching at a significant discount for the stable prefix, and GPT-5.5 being token-efficient, the boundary between "stuff" (insert everything) and "retrieve" (classic RAG) has shifted. For stable knowledge bases under ~500K tokens (internal KB, product documentation), an effective strategy becomes: index the entire KB as a cached prefix with Opus 4.8, and the cached input portion of each query becomes much cheaper. For larger corpora, frequently updated ones, or those with granular per-user access, RAG remains the correct solution — and Opus 4.8 as a synthesizer over the retrieved candidates is highly capable at cross-document reasoning, often reducing the need for complex re-ranking. Beware, however, of the new tokenizer in the Opus 4.x generation: it can produce a different token count than previous generations for the same text (the tokenizer recalibration affects effective cost) — recalibrate your RAG context budgets on a representative sample.
RAG is complementary to both. RAG does not modify the model (as fine-tuning does) and does not insert the whole corpus into the context (as long context does). RAG selects only the relevant fragments from the document corpus and inserts them into the context. It is cost-efficient (you pay only for the relevant tokens), fast (retrieval takes milliseconds), dynamic (if the documents change, you update the index, not the model), and scalable (you can index billions of documents).
The comparison table:
| Criterion | Fine-tuning | Long Context | RAG |
|---|---|---|---|
| Modifies the model | Yes | No | No |
| Cost per update | $$$ (retraining) | - | $ (re-indexing) |
| Query latency | Normal | High (long context) | Medium (+retrieval) |
| Corpus scalability | N/A | Limited by context window | Unlimited |
| Freshness | Frozen at training time | Depends on what you insert | Dynamic |
| Domain accuracy | Good (stylistic) | Good (if the info is in context) | Good (if retrieval works) |
| Setup complexity | Medium | Low | High |
The most powerful combination in 2026: fine-tuning for style and behavior + RAG for up-to-date factual knowledge.
RAG architecture: Retrieve → Augment → Generate
The RAG architecture operates in two distinct phases with completely different purposes and execution times.
Offline phase — Indexing (Ingestion Pipeline):
Raw documents → Parsing → Cleaning → Chunking → Embedding → Storage in vector DB
This phase runs before any user asks any question. It can take hours for large collections (tens of thousands of documents), but it runs once, with incremental updates afterwards. Each document goes through the pipeline: it is parsed from its native format (PDF, HTML, DOCX), cleaned of artifacts (headers, footers, navigation elements), segmented into fragments (chunks) of controlled size, transformed into numerical vectors (embeddings), and stored in a vector database alongside the original text and metadata.
Online phase — Querying (Query Pipeline):
User question → Query Embedding → Vector Search → Context Assembly → Prompt Augmentation → LLM Generation → Answer
This phase runs in real time. The full sequence:
- Query Embedding — the user's question is transformed into a numerical vector using exactly the same embedding model used at indexing time. This is a critical requirement: the vectors must live in the same mathematical space.
- Vector Search — the question's vector is compared against the vectors in the database. The top-k most similar fragments are returned (typically k=5 to k=20).
- Context Assembly — the retrieved fragments are ordered, deduplicated, and assembled into a coherent context.
- Prompt Augmentation — the context is inserted into the prompt sent to the LLM, alongside instructions and the original question.
- LLM Generation — the model generates an answer based on the information in the context.
The minimal demonstrative implementation:
from openai import OpenAI
client = OpenAI()
def rag_pipeline(user_question: str, vector_db, top_k: int = 5) -> str:
"""Complete RAG pipeline — from question to answer."""
# Step 1: Transform the question into a vector
query_embedding = client.embeddings.create(
input=user_question,
model="text-embedding-3-large"
).data[0].embedding
# Step 2: Vector search — retrieve the most relevant fragments
relevant_chunks = vector_db.similarity_search(
query_vector=query_embedding,
top_k=top_k
)
# Step 3: Assemble the context
context_parts = []
for i, chunk in enumerate(relevant_chunks, 1):
context_parts.append(
f"[Fragment {i} — Source: {chunk.metadata.get('source', 'N/A')}]\n"
f"{chunk.text}"
)
context = "\n\n---\n\n".join(context_parts)
# Step 4: Augmented prompt with strict instructions
system_prompt = """You are an assistant that answers EXCLUSIVELY based on
the provided context. If the information is not in the context, say
explicitly that you do not have enough information. Never make things up."""
user_prompt = f"""Context from documents:
{context}
User question: {user_question}
Answer precisely, citing the relevant fragments."""
# Step 5: Generate the answer
response = client.chat.completions.create(
model="gpt-5.5",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
temperature=0.1 # Low — maximum fidelity to the context
)
return response.choices[0].message.content
Notice temperature=0.1. This is not a stylistic preference — it is an architectural decision. For factual RAG systems, the temperature should be between 0.0 and 0.2. A high temperature (0.7-1.0) encourages diversity and creativity, which in a RAG context translates to: the model improvises instead of staying faithful to the provided context. The result? Hallucinations masked under a fluent style.
Lewis et al. 2020: the founding paper
The term "Retrieval-Augmented Generation" was formalized in the paper "Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks" by Patrick Lewis et al. (Facebook AI Research, UCL — published at NeurIPS 2020). This paper is the cornerstone of the entire field and deserves a detailed analysis.
The context: In 2020, pre-trained language models (BERT, GPT-2, T5) showed impressive performance on linguistic tasks but failed at "knowledge-intensive" tasks — questions requiring specific factual knowledge. Fine-tuning on QA datasets helped, but the models still hallucinated when the information was not in their parameters.
The key contribution: Lewis et al. proposed an end-to-end model combining a parametric retriever (DPR — Dense Passage Retriever) with a parametric generator (BART). The retriever searches for relevant passages in a knowledge base (Wikipedia), and the generator produces the answer conditioned on the retrieved passages. Crucially, both components are trained jointly — the retriever learns what to look for, and the generator learns how to use what it is given.
Two architectural variants:
- RAG-Sequence: the same set of documents is used to generate the entire answer. The retriever selects documents once, and the generator uses them for the whole output sequence.
- RAG-Token: the retriever can select different documents for each generated token. More flexible, but more computationally expensive.
Results: RAG outperformed fine-tuned models on several QA benchmarks (Natural Questions, TriviaQA, WebQuestions), demonstrating that access to external information at inference time can be more effective than memorization through training.
The legacy in 2026: Although the specific architecture in the original paper (DPR + BART) is technically outdated, the RAG principle has become ubiquitous. Any modern system that combines retrieval with generation is a conceptual descendant of this paper.
Naive RAG vs Advanced RAG vs Modular RAG
The modern taxonomy of RAG systems, crystallized in papers such as Gao et al. (2024) — "Retrieval-Augmented Generation for Large Language Models: A Survey" — identifies three generations:
Naive RAG (Generation 1): The linear pipeline described above — indexing → retrieval → generation. Simple to implement, but suffering from three main problems: (a) retrieval can be imprecise (garbage in → garbage out); (b) the retrieved chunks may contain redundant or contradictory information; (c) the model may ignore the context or misinterpret it.
Advanced RAG (Generation 2): Adds improvement techniques at every stage. Pre-retrieval: query expansion (reformulating the question into multiple variants), HyDE (Hypothetical Document Embeddings — generating a hypothetical document that would answer the question, then searching by its embedding), query routing (directing questions to specific sources). Post-retrieval: re-ranking (reordering results with a more precise cross-encoder model), compression (removing irrelevant passages from the context), fusion (combining results from multiple sources).
# Example: HyDE — Hypothetical Document Embeddings
def hyde_retrieval(question: str, vector_db, client) -> list:
"""Generate a hypothetical document, then search by it."""
# Step 1: The LLM generates a hypothetical answer (it may be imprecise)
hypothetical = client.chat.completions.create(
model="gpt-5.5",
messages=[{
"role": "user",
"content": f"Write a paragraph that would answer: {question}"
}],
temperature=0.7
).choices[0].message.content
# Step 2: Embed the hypothetical document
hyde_embedding = client.embeddings.create(
input=hypothetical,
model="text-embedding-3-large"
).data[0].embedding
# Step 3: Search with the hypothetical embedding (not with the question)
return vector_db.similarity_search(
query_vector=hyde_embedding,
top_k=10
)
Modular RAG (Generation 3, the current state in 2026): Decomposes the pipeline into a graph of interchangeable modules. Each module (routing, retrieval, re-ranking, generation, verification) can be implemented independently, combined differently for different scenarios, and orchestrated by a controller (often an LLM agent). Example: a modular system may decide that for simple questions it does not need retrieval (answering directly from the model's knowledge), for factual questions it uses standard retrieval, and for complex questions it performs iterative retrieval with progressive refinement.
Real-world use cases in 2026
RAG is no longer an academic curiosity — it is production infrastructure. Here are concrete, market-validated cases:
1. Enterprise Customer Support: Companies such as Klarna, Zendesk, and Intercom integrate RAG into support assistants. The knowledge base (help center articles, the history of resolved tickets, product documentation) is indexed. When a customer writes a question, the system retrieves the relevant fragments and generates a personalized answer. Companies that adopted such assistants have publicly reported significant reductions in average resolution time and increases in self-service resolution rates.
2. Legal Assistance: The RAG system indexes statutes, case law, and official interpretations. A lawyer can ask "What does recent case law say about the right to be forgotten under the GDPR?" and receives an answer anchored in real decisions, with precise citations. More and more LegalTech platforms across the EU use a similar architecture.
3. Medical Assistance (with human supervision): Medical knowledge bases (UpToDate, NICE/WHO clinical guidelines, PubMed studies) are indexed. Physicians use RAG systems to quickly check drug interactions or treatment protocols. Crucially, these systems operate as assistance, not autonomous decision-making — human supervision remains mandatory.
4. Technical Documentation and Developer Experience: Companies with complex software products (Stripe, Vercel, Supabase) use RAG to build assistants that answer questions about APIs, SDKs, and configurations. The database is the index of the official documentation, and answers include relevant code examples.
5. Applications in regulated national markets: An assistant for frequently changing tax legislation, a chatbot for the constantly updated labour code, an assistant for EU funding programme guides (such as the Recovery and Resilience Facility), Q&A systems for universities based on their internal regulations.
When to use RAG versus the alternatives
The decision to implement RAG is not universal. Here is a decision framework:
Use RAG when:
- The document corpus is updated periodically (weekly, monthly).
- You have private data that cannot be included in training.
- You need verifiable answers with citable sources.
- The domain is factual and precise (legal, medical, technical, regulatory).
- The corpus is too large for the context window (>100,000 documents).
- Cost per query must be controlled (vs. inserting hundreds of thousands of tokens into the context).
Use Long Context instead of RAG when:
- The corpus is small (<50 short documents).
- You have no infrastructure for vector databases.
- Implementation simplicity is a higher priority than cost efficiency.
- You need a quick solution, a prototype, an MVP.
Use Fine-tuning instead of (or alongside) RAG when:
- The model must adopt a specific style (legal tone, medical language).
- You need superior performance on specific tasks (classification, extraction).
- The domain terminology is specialized and the general model handles it incorrectly.
Do not use RAG when:
- The task is creative (content generation, brainstorming).
- The problem does not depend on factual information from documents.
- The latency added by retrieval is unacceptable (real-time systems under 100ms).
Cost-benefit analysis
Implementing RAG involves real costs. An indicative calculation for a medium production system (100,000 documents, 10,000 queries per day):
Setup costs:
- Embeddings at indexing time: ~100,000 documents × 5 chunks/doc × $0.00013/1K tokens (text-embedding-3-large) ≈ $30-50 (one-time).
- Managed vector database (Pinecone, Weaviate Cloud): $70-300/month.
- Pipeline development: 2-4 weeks of a senior engineer's time.
Recurring costs per query:
- Query embedding: ~$0.0001 per question.
- LLM generation with context: ~$0.01-0.06 per query (GPT-5.5 at $5/$30, ~2000 tokens of context + answer; Opus 4.8 similar at $5/$25 — with prompt caching for static context, the effective cost drops considerably).
- Total per query: ~$0.01-0.06.
- 10,000 queries/day: ~$100-600/month.
Comparison with long context (no RAG):
- Inserting 200,000 tokens of context per query: ~$0.50-1.00 per query.
- 10,000 queries/day: ~$5,000-10,000/month.
- RAG delivers 10-100x savings per query.
Applications in regulated European markets
Regulated European markets present unique opportunities for RAG systems, given:
- Constantly changing legislation: Tax codes are amended every year (VAT rates, new thresholds for micro-enterprises). A RAG system indexed on up-to-date tax legislation is a necessity for consulting firms.
- Multilingualism: Official EU documents in the local language, correspondence in English, mixed technical terminology. Multilingual embeddings (BGE-M3, multilingual-e5-large) enable cross-lingual search.
- A digitalized public sector: Tax portals, e-government platforms, company registries — all generate documents that can be indexed for citizen-guidance assistants.
- The legal domain: Civil codes, criminal codes, labour codes, supreme court case law — an immense corpus with frequent updates, ideal for RAG.
You now have the whole map — why RAG exists, how it compares with the alternatives, when it makes sense and when it does not, what it costs and what value it produces. But between understanding why RAG works and building a system that actually retrieves the right information at the right moment lies the fine mechanics of each component — and that is exactly where it is decided whether your system answers with sources or hallucinates with confidence. Starting with the next lesson we enter that mechanics, beginning with embeddings and vector representations — the piece without which nothing in retrieval works — and each lesson adds a layer until you can design a complete RAG pipeline on your own.
[Easy] What is the main difference between RAG and fine-tuning as methods of adapting an LLM?
Enjoyed it? All 27 lessons look like this.
You just read a complete lesson, exactly as it appears in the platform. Create your account in under a minute and pick the option that fits you best:
Up next in the course
Unlock all 27 lessonsEverything you'll learn in this course
1 RAG Fundamentals 3 lessons
- What Is RAG and Why It Matters Reading now 55 min
- Embeddings and Vector Representations 55 min
- Chunking Strategies: The Art of Document Fragmentation 55 min
2 Implementing the RAG Pipeline 3 lessons
- Building an End-to-End RAG Pipeline 55 min
- Vector Databases in Practice 55 min
- Semantic Search vs Keyword Search 55 min
3 Embedding Models and Document Processing 3 lessons
- Embedding Models: Comparison and Selection 2026 55 min
- Document Processing: PDF, HTML, Office, and Complex Structures 55 min
- Multimodal Embeddings: Images, Tables, and Audio 55 min
4 Advanced RAG 3 lessons
- Hybrid Search and Re-ranking 55 min
- Multi-Modal RAG: Images, Tables, and Charts 55 min
- RAG Evaluation Metrics 55 min
5 Advanced RAG Architectures 3 lessons
- Agentic RAG, Self-RAG, and Corrective RAG 55 min
- RAG with Knowledge Graphs 55 min
- Conversational RAG: Memory and Multi-Turn Context 55 min
6 RAG in Production 3 lessons
- Scaling RAG Systems 55 min
- Caching and RAG Optimization 55 min
- Security and Data Governance in RAG Systems 55 min
7 RAG Tools and Frameworks 2 lessons
- LangChain, LlamaIndex, and Haystack: Complete 2026 Comparison 55 min
- Orchestrating RAG Pipelines in Production 55 min
8 Systematic RAG Evaluation and Testing 2 lessons
- Systematic RAG Evaluation with RAGAS and DeepEval 55 min
- CI/CD Testing and RAG Monitoring in Production 55 min
9 Case Studies and Hands-On Projects 2 lessons
- Case Study: RAG System for Technical Documentation 55 min
- Hands-On Project: Build a Complete RAG System 60 min
10 Final Quiz — RAG in Practice 1 lessons
- Final Assessment — RAG: Retrieval-Augmented Generation 60 min
11 Appendix: Official Resources, 2026 Updates and Learning Paths 2 lessons
- Official Resources, 2026 Updates and Learning Paths 32 min
- Contextual Retrieval: Enriching Chunks before Embedding 24 min
Everything you need to learn effectively
Interactive quizzes
Check your knowledge at the end of every lesson with scored quizzes and feedback.
Personal notes
Save notes on every lesson, accessible anytime from your dashboard.
Scheduled reviews
Revisit lessons exactly when it matters, at the right intervals — so you remember for the long term.
Progress & Achievements
Track your progress, unlock achievements, and visualize what you've learned.
Bookmarks
Save the lessons that matter and find them instantly when you need them.
Questions & Answers
Ask questions right on the lesson and get answers from our team.
Good to know before you start
How do I get access to the course?
You can read the first lesson in full for free, right on this page — no account needed. For the rest of the course you create an account, pick the subscription that fits — a single course or a bundle — and get access immediately after your payment is confirmed. Everything happens 100% online.
Can I cancel my subscription anytime?
Yes. Cancel anytime, straight from your account, in just a few clicks. Your access stays active until the end of the period you have already paid for.
What does the subscription for this course include?
All 27 lessons in the course, interactive quizzes, the AI professor built into every lesson (select any passage and it explains it on the spot), personal notes, automatically saved progress, and content updates included.
Is there a fixed learning schedule?
No. You learn at your own pace, on any device. Lessons are structured step by step, and the platform saves your progress automatically, so you can pick up right where you left off — anytime.
Ready to unlock all the content?
Just this course — €49 + VAT / month — or every IT Pro course, with smart quizzes and the full AI Professor, in the bundle at €399 + VAT / month.
