IDInternals Decoded
RAG, Properly
Deep DivesIntermediate10 min readMay 2026

Why RAG Exists: The Context Problem

Models can't know your data. RAG is the architecture that fixes it, and its failure modes start here.

Part 1 of 8RAG, ProperlyView series →

A language model does not know your company handbook. It cannot. The weights are frozen at training time, so every query you ask today hits a snapshot of the public internet from months or years ago. Retrieval-Augmented Generation (RAG) is the architecture that bridges this gap. It gives the model a read-only memory of your private documents, injecting only the relevant bits at query time instead of forcing everything into the prompt.

Here is the surprising part. Even if you could stuff the entire handbook into a context window that claims to hold 128,000 tokens, the model would still fail. It would ignore facts buried in the middle, fabricate answers, or get overwhelmed by noise. The real limit is not the maximum context window advertised by the provider. It is the maximum effective context window, and that window is shockingly small.

Why can’t you just dump all your documents into the prompt?

Think of a language model’s context as a whiteboard, not a library. The whiteboard has a fixed size. You can write a few thousand words on it before you run out of space. The model can read everything on the board, but it is not scanning the entire surface with equal attention. It focuses on the edges and lets the middle fade into the background.

Now imagine you are building a “chat with your company handbook” assistant. The handbook is 200 pages of policies, leave rules, and expense guidelines. You want to answer questions like “How many vacation days does a new hire get after six months?” If you paste the whole handbook onto the whiteboard, the model will see the answer somewhere in the middle, but it will often fail to find it. It will instead hallucinate a plausible number based on the first few paragraphs or the last few lines. That is the context problem, and it is why RAG exists.

The formal limit is the maximum context window. Most models today offer 8k, 32k, or 128k tokens. That is the hard cap before the model throws an error. But the effective limit is the point where adding more tokens stops helping and starts hurting. Empirical studies show that models with a 128k maximum context window routinely degrade on tasks after just 1,000 to 2,000 tokens Lost in the Middle. The reason is not just about capacity. It is about attention.

Transformer attention is quadratic in sequence length. That means the computational cost of relating every token to every other token explodes as you add more text. But more importantly, the model’s training distribution rarely includes examples where the answer is buried in the middle of a long, unrelated document. The model learns to attend to beginnings and ends, where titles, summaries, and conclusions tend to live. This creates a U-shaped accuracy curve: the model performs best when the relevant information is near the start or the end of the prompt, and worst when it is in the middle Lost in the Middle.

The same pattern appears in the induction heads literature. Induction heads are attention heads that copy patterns from earlier in the sequence. They are a big part of how models do in-context learning. But those heads are trained on sequences that are mostly coherent and contiguous. When you concatenate 50 unrelated handbook sections separated by “Section 3.4.1,” you break the pattern. The model’s internal algorithms for pulling information from the past become unreliable. The whiteboard becomes a mess.

What happens when you try to bypass the problem with fine-tuning?

Every few months someone asks: “Why not just fine-tune the model on the handbook?” The answer is that fine-tuning does not solve the context problem. It solves a different problem: it adjusts the model’s parametric memory, the knowledge baked into the weights. That sounds perfect until you consider the operational reality.

Parametric memory is fast at inference time. The weights are already on the GPU (graphics processing unit), and retrieving a fact is just a few matrix multiplications. But updating that memory is slow, expensive, and fragile. You need a dataset of question-answer pairs. You need to avoid catastrophic forgetting, where the model unlearns its general language skills while memorizing the handbook. You need to re-run the whole process every time the handbook changes, which might be weekly. And you still have no guarantee that the model will retrieve the exact fact you need when a user asks a slightly different question. You have traded a retrieval problem for a training problem.

The original RAG paper from 2020 framed this as a split between parametric and non-parametric memory Lewis et al.. Parametric memory is the knowledge in the weights. Non-parametric memory is an external index that the model can query. The insight was that you can treat the external index as a mutable, queryable database. You update the index by re-embedding changed documents, not by re-training the model. The model stays frozen, and the index becomes the source of truth. This is the core idea behind RAG, and it is a direct response to the context problem and the cost of fine-tuning.

How does RAG solve the context problem at a high level?

RAG does not make the model smarter. It builds a compression pipeline that turns your giant knowledge base into a tiny, high-value package that fits inside the model’s effective context window. Instead of dumping the whole handbook into the prompt, you dump only the two or three most relevant sections. The model then reads those sections and answers the question.

Context stuffing vs RAG
Context stuffing
  • Dump entire handbook into prompt
  • 50,000+ tokens
  • Model loses attention in the middle
  • High compute cost, slow
RAG
  • Retrieve only relevant chunks
  • ~1,000 tokens
  • Model focuses on precise info
  • Fast, low cost
Illustrative comparison of context stuffing vs RAG for a 200-page handbook.

Here is the flow applied to the company handbook assistant. You have a user query: “What is the expense limit for client dinners?” The system embeds that query into a vector. It searches a vector database that holds embeddings of every section of the handbook. It finds the top few sections that are semantically similar to the query. Those sections are small; maybe a few hundred tokens each. They are packed into the prompt, usually at the top or bottom where the model is most attentive. The model sees the query, sees the relevant handbook text, and generates a grounded answer.

The magic is not in the model. It is in the retrieval stack. The vector database, the embedding model, the chunking strategy, and the reranker all work together to pick the handful of tokens that will actually influence the answer. If the retrieval picks the wrong sections, the model never sees the right information. If the retrieval picks too many sections, the effective context window overflows and the model gets lost in the middle. RAG is, at its core, a memory hierarchy that compresses an arbitrarily large knowledge store into the few kilobytes of text that the model can actually use.

This is why RAG engineers obsess over chunking, embedding quality, and hybrid search. A bad chunking strategy splits a critical policy across two chunks, and the retrieval system never surfaces the complete fact. A pure vector search on a dense embedding model might miss an exact match for “expense code 45B” because the embedding model was not trained on that kind of jargon. Those failure modes are not model failures. They are engineering failures in the retrieval pipeline, and they all trace back to the context problem.

The next episode will dig into the first half of that pipeline: how you turn a messy company handbook into a searchable index. The choices you make about chunk size, overlap, and metadata will haunt every query that comes after.

Quick Reference

PropertyValue
Typical maximum context window8k to 128k tokens
Effective context window (practical)1k to 2k tokens for many tasks
Position sensitivityU-shaped: best at start and end, worst in middle
Primary cause of failureAttention patterns, lost in the middle, training distribution
RAG’s fixRetrieval compresses knowledge into the effective window
Key architectural splitParametric memory (weights) vs. non-parametric memory (index)
Key numbers
128k
Max context tokens
1k-2k
Effective context
O(n^2)
Attention cost
~200ms
RAG latency
Typical values for context, attention, and RAG performance.

Frequently Asked Questions

Q: If a model has a 128k context window, why not just put everything in the prompt and let the model figure it out? The model’s attention mechanism is not a random-access memory. It prioritizes the beginning and end of the prompt. Information in the middle gets ignored, and adding more tokens beyond a few thousand often degrades the answer quality. You pay for the full context window in latency and cost, but you get no benefit from the extra tokens.

Q: Can’t I just fine-tune the model on my company data instead of using RAG? Fine-tuning bakes the data into the model’s weights, but it is expensive, slow to update, and prone to forgetting. Every time your handbook changes, you need a new fine-tuning run. RAG keeps the model frozen and updates an external index, which is cheaper and faster.

Q: What is the difference between maximum context window and effective context window? The maximum context window is the hard token limit the API (application programming interface) enforces. The effective context window is the point where adding more tokens stops improving the model’s output and often worsens it. This effective limit is usually much smaller, and it varies by task and model.

Q: Does RAG completely solve the hallucination problem? No. RAG reduces hallucinations by giving the model ground truth text to reference. But if the retrieval system picks the wrong chunk, or if the model ignores the provided text and confabulates, the output can still be wrong. RAG makes the model’s answer grounded in a source, but you still need to verify the output.

Q: Why is chunking strategy so important if the model only sees a few chunks anyway? The chunks that the model sees are the ones the retrieval system selected. If the retrieval system cannot find the right chunk because the information was split badly, the model never gets the chance to produce a correct answer. Chunking determines what the retrieval system can ever find, so it is the foundation of the whole pipeline.

Test yourself

You are building the “chat with your company handbook” assistant. The handbook contains a policy that reads: “Employees may submit expense reports for client entertainment up to $150 per person, but advance approval is required for any expense exceeding $75.” You chunk the handbook into fixed 512-token chunks with no overlap. A user asks: “What is the client entertainment expense limit without approval?” The retrieval system returns a chunk that starts with the second half of the policy. The chunk begins with “$75. For events above 10 people, a separate approval process applies.” The model answers: “The limit without approval is $75.” What went wrong, and how would you fix it?

Answer: The fixed-size chunk split the policy right in the middle. The first half of the sentence, which contained the critical condition “up to $150 per person, but advance approval is required for any expense exceeding $75,” was placed in the previous chunk. The retrieval system returned the second chunk because it had high semantic similarity to the query, but that chunk was missing the key context. The model saw the number $75 and assumed it was the limit. To fix this, switch to content-aware chunking that respects sentence and paragraph boundaries. Use a moderate overlap of 10 to 20 percent between chunks so that the entire policy appears intact in at least one chunk. This ensures a complete fact is retrievable and the model sees the full condition.

If you want this kind of breakdown every week, how real systems actually work under the hood, not how the marketing says they work, subscribe to Internals Decoded at internalsdecoded.com.

Sources

#rag#retrieval-augmented-generation
More from the library
The Newsletter

Keep up with AI. One email a week.

One thoughtful email each week. Unsubscribe whenever you like.