<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anchal Nigam</title>
    <description>The latest articles on DEV Community by Anchal Nigam (@nigamanchal).</description>
    <link>https://dev.to/nigamanchal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4177125%2Ffa85520e-6455-4720-aab6-69e2a766b66a.png</url>
      <title>DEV Community: Anchal Nigam</title>
      <link>https://dev.to/nigamanchal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nigamanchal"/>
    <language>en</language>
    <item>
      <title>The Chunking Dilemma: Why RAG Fails Before Retrieval Even Starts</title>
      <dc:creator>Anchal Nigam</dc:creator>
      <pubDate>Thu, 05 Nov 2026 10:40:25 +0000</pubDate>
      <link>https://dev.to/nigamanchal/the-chunking-dilemma-why-rag-fails-before-retrieval-even-starts-51n0</link>
      <guid>https://dev.to/nigamanchal/the-chunking-dilemma-why-rag-fails-before-retrieval-even-starts-51n0</guid>
      <description>&lt;p&gt;Hey friends! Welcome back to our ongoing series on building real-world, production-grade Retrieval-Augmented Generation (RAG).&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://anchall-nigamm.medium.com/why-cant-we-just-give-an-llm-all-our-company-documents-understanding-the-context-window-a25edabda521" rel="noopener noreferrer"&gt;Part 3&lt;/a&gt;, we had an honest conversation about the “infinite context window” myth. We saw why simply giving an LLM hundreds of pages of company documentation isn’t the magical solution it sounds like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  It drastically increases API costs.&lt;/li&gt;
&lt;li&gt;  It drives up query latency.&lt;/li&gt;
&lt;li&gt;  It dilutes the model’s attention.&lt;/li&gt;
&lt;li&gt;  When too much information is placed into the prompt, models struggle with the &lt;strong&gt;“Lost in the Middle”&lt;/strong&gt; phenomenon — missing facts buried among irrelevant content.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we arrived at a much more practical conclusion:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Don’t give the LLM everything. Give it only the specific information it actually needs.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sounds simple, right?&lt;/p&gt;

&lt;p&gt;Well… this is where things get interesting. Because before we can retrieve the right information, we first have to decide: &lt;strong&gt;what exactly are we retrieving?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A 500-page employee handbook isn’t stored as one giant text block. We have to break it down into smaller pieces. Those pieces are called &lt;strong&gt;chunks&lt;/strong&gt;. And suddenly, a seemingly simple question appears: &lt;strong&gt;Where exactly should we cut the document?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cut in the wrong place, and you destroy the exact context you were trying to preserve. Cut too large, and your search becomes blurry and imprecise.&lt;/p&gt;

&lt;p&gt;That is the &lt;strong&gt;Chunking Dilemma&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A Quick Road Ahead:&lt;/strong&gt;&lt;br&gt;
A full production RAG pipeline consists of:&lt;br&gt;
&lt;code&gt;Documents → Chunking → Embeddings → Vector / Document DB → Retrieval → LLM Prompt&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If terms like “embeddings” or “vector databases” sound new, don’t worry! We have dedicated deep-dive articles coming up for each of those topics. In fact, because chunking is so foundational, we are splitting it into two digestible reads:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Today (Part 4):&lt;/strong&gt; The Ingestion Pipeline, the Goldilocks chunk-sizing problem, and baseline splitting strategies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 5:&lt;/strong&gt; Advanced chunking architectures (Bounded Semantic Chunking, Parent-Document Retrieval, Breadcrumbs, and Anthropic’s Contextual Retrieval).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Before Retrieval, There Is an Entire Pipeline
&lt;/h2&gt;

&lt;p&gt;Let’s zoom out for a moment. A production RAG system is split into two fundamentally different journeys:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌────────────────────────────────────────────────────────────────────────┐
│                        OFFLINE INGESTION PIPELINE                      │
│                                                                        │
│   Raw Sources (PDFs, Wikis, Codebases, Confluence, DBs, Tickets)       │
│                                  │                                     │
│                                  ▼                                     │
│   ┌────────────────────────────────────────────────────────────────┐   │
│   │ Parse &amp;amp; Clean: Extract text, tables, code, headers             │   │
│   └──────────────────────────────┬─────────────────────────────────┘   │
│                                  ▼                                     │
│   ┌────────────────────────────────────────────────────────────────┐   │
│   │ CHUNKING: Break documents into meaningful, retrievable units   │   │
│   └──────────────────────────────┬─────────────────────────────────┘   │
│                                  ▼                                     │
│   ┌────────────────────────────────────────────────────────────────┐   │
│   │ Metadata Enrichment: Breadcrumbs, page, source, parent_id...   │   │
│   └──────────────────────────────┬─────────────────────────────────┘   │
│                                  ▼                                     │
│   ┌────────────────────────────────────────────────────────────────┐   │
│   │ Embedding Generation: Text → Vector coordinates                │   │
│   └──────────────────────────────┬─────────────────────────────────┘   │
│                                  ▼                                     │
│   Vector Database &amp;amp; Document Store (Indexed for Search)                │
└────────────────────────────────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice something critical: &lt;strong&gt;Chunking isn’t a minor preprocessing detail.&lt;/strong&gt; It defines the physical units of memory your search engine will later evaluate.&lt;/p&gt;

&lt;p&gt;Now compare that with what happens when a user asks a question:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌────────────────────────────────────────────────────────────────────────┐
│                         ONLINE QUERY PIPELINE                          │
│                                                                        │
│   User Question                                                        │
│         │                                                              │
│         ▼                                                              │
│   Query Embedding                                                      │
│         │                                                              │
│         ▼                                                              │
│   Vector Search / Keyword Retrieval                                    │
│         │                                                              │
│         ▼                                                              │
│   Top-K Matching Chunks Retrieved                                      │
│         │                                                              │
│         ▼                                                              │
│   Reranking &amp;amp; Metadata Filtering                                       │
│         │                                                              │
│         ▼                                                              │
│   LLM Prompt + Retrieved Context                                       │
│         │                                                              │
│         ▼                                                              │
│   Accurate Answer                                                      │
└────────────────────────────────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The online query pipeline gets all the attention because it touches the user interface. But the offline ingestion pipeline quietly sets the hard ceiling on what your system is capable of finding. And this is where our story begins.&lt;/p&gt;

&lt;h2&gt;
  
  
  Storytime: The Day the Staging Bot Lied
&lt;/h2&gt;

&lt;p&gt;Imagine you’re building your first enterprise RAG prototype. You follow a quickstart tutorial, load a few sample PDFs, and write those famous three lines of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# The tutorial dream
&lt;/span&gt;&lt;span class="n"&gt;text_splitter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RecursiveCharacterTextSplitter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;chunk_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;chunk_overlap&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;chunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text_splitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split_documents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You test it with five sample questions. Five out of five answers look brilliant! High fives all around. You demo it to your team, everyone is impressed, and you connect the bot to your company’s actual knowledge base: 50,000 files spanning employee policies, SEC financial filings, API documentation, and architecture wikis.&lt;/p&gt;

&lt;p&gt;Two days later, Slack starts blowing up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;em&gt;“The bot says our Q3 revenue dropped by 24%!”&lt;/em&gt; (Yet the official earnings release says global revenue grew by 24%).&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;“The bot told an employee they’re not eligible for parental leave!”&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;“The API endpoint the bot gave me doesn’t exist!”&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your first instinct is to blame the LLM. You assume the model is hallucinating, so you upgrade from GPT-4o-mini to Claude 3.5 Sonnet, lower the temperature to 0.0, and spend four hours rewriting the system prompt. Nothing fixes it.&lt;/p&gt;

&lt;p&gt;Eventually, you stop looking at the LLM. You open your database and inspect the exact chunk that was retrieved for that financial query:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;_"...decreased by 24% due to unexpected supply chain disruptions in the European sector."_&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It looks like a reasonable sentence. Except it isn’t.&lt;/p&gt;

&lt;p&gt;The chunk started right in the middle of a sentence! The text immediately preceding it had said:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“While overall global revenue grew by 24%, regional European shipping volume decreased by 24%…”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The retrieval system didn’t retrieve a complete thought. It retrieved a severed fragment of a thought. And the LLM had no choice but to answer using that mutilated fragment.&lt;/p&gt;

&lt;p&gt;Here is the vital takeaway:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;A retrieval system can only retrieve the pieces you created during ingestion. If those pieces are poorly designed, no multi-billion-parameter LLM or reranker can magically reconstruct the truth.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The “Goldilocks Dilemma” of Chunk Sizing
&lt;/h2&gt;

&lt;p&gt;So, what should your chunk size be? 100 tokens? 500? 1,000? 2,000?&lt;/p&gt;

&lt;p&gt;There is no universal magic number. We face the classic &lt;strong&gt;Goldilocks Problem&lt;/strong&gt;: chunks can be too small, or they can be too large.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌───────────────────────────────┐     ┌───────────────────────────────┐
│       CHUNKS TOO SMALL        │     │       CHUNKS TOO LARGE        │
│       (e.g., 50 tokens)       │     │     (e.g., 2,000 tokens)      │
├───────────────────────────────┤     ├───────────────────────────────┤
│ ✗ Missing critical context    │     │ ✗ Multiple mixed topics       │
│ ✗ Severed rules &amp;amp; exceptions  │     │ ✗ Mean pooling dilution       │
│ ✗ The "Half-Truth" catalyst   │     │ ✗ Semantic blur in search     │
│ ✗ Orphaned pronouns           │     │ ✗ Wasted prompt token budget  │
└───────────────────────────────┘     └───────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1. The Small Chunk Trap (The “Half-Truth” Catalyst)
&lt;/h3&gt;

&lt;p&gt;It’s tempting to think: &lt;em&gt;“If I make my chunks tiny (like 1 or 2 sentences), search precision will be laser-sharp because the chunk will match the user’s exact keywords!”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;While small chunks match search queries easily, they act as a &lt;strong&gt;direct catalyst for hallucinations and dangerous half-truths&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Consider this employee handbook policy:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Employees are entitled to 20 days of paid vacation per year. However, contractors and interns are not eligible for paid vacation.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If your chunker cuts too aggressively, it produces:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Chunk A:&lt;/strong&gt; &lt;em&gt;“Employees are entitled to 20 days of paid vacation per year.”&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Chunk B:&lt;/strong&gt; &lt;em&gt;“However, contractors and interns are not eligible for paid vacation.”&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now an intern asks your company bot:&lt;br&gt;
&lt;strong&gt;“Am I eligible for paid vacation?”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The retrieval system searches for “eligible paid vacation”. Chunk A looks like a 99% match. It retrieves Chunk A and sends it to the LLM.&lt;/p&gt;

&lt;p&gt;The LLM reads: &lt;em&gt;“Employees are entitled to 20 days of paid vacation per year.”&lt;/em&gt;&lt;br&gt;
And what does it conclude?&lt;br&gt;
&lt;strong&gt;“Yes! You are entitled to 20 days of paid vacation!”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7s4ntudpfko9ieo46all.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7s4ntudpfko9ieo46all.jpeg" alt="captionless image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice what happened here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The LLM did &lt;strong&gt;not&lt;/strong&gt; make up fake information from thin air.&lt;/li&gt;
&lt;li&gt;  What it said was technically written in the document.&lt;/li&gt;
&lt;li&gt;  But it produced a &lt;strong&gt;catastrophic half-truth&lt;/strong&gt; because chunking separated the &lt;strong&gt;rule&lt;/strong&gt; from its &lt;strong&gt;exception&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This happens whenever chunking splits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A rule from its exception&lt;/li&gt;
&lt;li&gt;  A condition from its prerequisite&lt;/li&gt;
&lt;li&gt;  A definition from the term it defines&lt;/li&gt;
&lt;li&gt;  A pronoun (“it”, “they”) from its antecedent noun&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Rule of Thumb:&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;The goal of chunking is not&lt;/em&gt; “make every chunk exactly 400 tokens.” &lt;em&gt;The goal is to&lt;/em&gt; &lt;strong&gt;&lt;em&gt;create chunks that are independently meaningful units of thought.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  2. The Large Chunk Trap (Semantic Blur &amp;amp; Mean Pooling Dilution)
&lt;/h3&gt;

&lt;p&gt;So you might think: &lt;em&gt;“If small chunks lose context, let’s make them huge! Say, 2,000 tokens. Modern embedding models can read thousands of tokens, so why not give it the whole section?”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Because you run into the opposite problem: &lt;strong&gt;Loss of Search Precision (Semantic Blur)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;An embedding model does not store 2,000 separate concepts. It must compress that entire 2,000-token chunk into &lt;strong&gt;a single mathematical vector&lt;/strong&gt; (e.g., 768 or 1,536 numbers).&lt;/p&gt;

&lt;p&gt;Under the hood, most transformer embedding models do this through a mathematical operation called &lt;strong&gt;Mean Pooling&lt;/strong&gt;: they calculate the &lt;strong&gt;mathematical average&lt;/strong&gt; of all token vectors across the chunk!&lt;/p&gt;

&lt;p&gt;Now imagine what that means in practice. Suppose a 2,000-token chunk covers four disparate topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Tokens 1–500:&lt;/strong&gt; Office parking garage permits&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Tokens 501–1000:&lt;/strong&gt; Healthcare and dental coverage&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Tokens 1001–1500:&lt;/strong&gt; Kubernetes cluster access for engineers&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Tokens 1501–2000:&lt;/strong&gt; Client dinner expense policies and alcohol limits&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the embedding model computes the mean vector for this chunk, the vector’s direction in mathematical space is pulled in four completely different directions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  25% Parking&lt;/li&gt;
&lt;li&gt;  25% Health&lt;/li&gt;
&lt;li&gt;  25% Kubernetes&lt;/li&gt;
&lt;li&gt;  25% Dinner Expenses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, an employee asks:&lt;br&gt;
&lt;strong&gt;“What is the maximum Food expense allowed for a client dinner?”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The user’s query is only 12 tokens. Its embedding vector is &lt;strong&gt;100% focused on client dinners and expenses&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When your vector database calculates the &lt;strong&gt;Cosine Similarity&lt;/strong&gt; between:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Query Vector&lt;/strong&gt; (100% Dinner Expenses)&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Giant Chunk Vector&lt;/strong&gt; (25% Dinner Expenses + 25% Parking + 25% Health + 25% Kubernetes)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What happens? The similarity score drops significantly (e.g., to &lt;strong&gt;0.48&lt;/strong&gt;) because 75% of the giant chunk’s vector is pulling toward parking garages and server clusters!&lt;/p&gt;

&lt;p&gt;Meanwhile, a focused &lt;strong&gt;120-token chunk&lt;/strong&gt; that &lt;em&gt;only&lt;/em&gt; discussed client dinner expenses will score &lt;strong&gt;0.89&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Note: Those similarity numbers are illustrative, not universal laws. The key takeaway is the difference in semantic focus).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The information hasn’t disappeared from the 2,000-token chunk. But the representation used for retrieval has become an “averaged-out soup.” That is &lt;strong&gt;Semantic Blur&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Chunking Sweet Spot
&lt;/h2&gt;

&lt;p&gt;So we have two competing requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Small chunks:&lt;/strong&gt; High retrieval precision, but risk missing surrounding context.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Large chunks:&lt;/strong&gt; Broad context, but risk semantic dilution and prompt bloat.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of asking: &lt;em&gt;“What is the best chunk size?”&lt;/em&gt;, the better engineering question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;“What is the smallest unit of text that preserves enough self-contained meaning for my retrieval task?”&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At this point, your natural instinct might be: &lt;em&gt;“Okay, I get it. I shouldn’t make my chunks 50 tokens, and I shouldn’t make them 2,000 tokens. Let’s aim for that sweet spot! I’ll just open my Python script, configure&lt;/em&gt; &lt;code&gt;_chunk_size = 500_&lt;/code&gt;&lt;em&gt;, and start slicing.”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Hold on.&lt;/p&gt;

&lt;p&gt;Before you write that line of code, there is a subtle, deceptive trap waiting for you.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Silent Trap: Characters Are Not Tokens
&lt;/h2&gt;

&lt;p&gt;When you configure &lt;code&gt;chunk_size = 500&lt;/code&gt; in a text splitter, ask yourself: &lt;strong&gt;500 of what?&lt;/strong&gt; In Python, length is measured in raw characters (letters, spaces, and punctuation):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;RAG pipeline&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Returns 12 characters
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don’t supply a custom tokenizer, many standard splitters count &lt;strong&gt;500 characters&lt;/strong&gt;. In English prose, an average word is about 5 characters plus a space. That means a 500-character chunk is barely &lt;strong&gt;80 to 100 words&lt;/strong&gt;! Without realizing it, you just walked right back into the &lt;em&gt;Small Chunk Trap&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;And it gets more dangerous: &lt;strong&gt;embedding models and LLMs do not process characters or words. They process tokens.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While clean English prose follows a rough rule of thumb:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;General English Rule of Thumb:
1 Token ≈ 0.75 words ≈ 4 characters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The real world is rarely that clean. Consider what happens when your enterprise document contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Indented Python or JavaScript code&lt;/li&gt;
&lt;li&gt;  Markdown tables with pipes, dashes, and whitespace&lt;/li&gt;
&lt;li&gt;  URLs, JSON payloads, and technical identifiers&lt;/li&gt;
&lt;li&gt;  Text in non-English languages (like Hindi, Japanese, or German)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In these scenarios, character-to-token ratios explode! A chunk that looks like an innocent 1,500 characters can easily expand into &lt;strong&gt;700 tokens&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Why does this matter so much? Because of a number that trips up even experienced AI engineers: &lt;strong&gt;the embedding model’s context ceiling.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The 512-Token Truncation Trap
&lt;/h2&gt;

&lt;p&gt;We often hear about frontier LLMs boasting massive 128k or 1-million-token context windows. But don’t confuse your &lt;strong&gt;Generation LLM&lt;/strong&gt; with your &lt;strong&gt;Embedding Model&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Generation LLM Context Window (e.g., 128k for GPT-4o, 1M for Gemini):&lt;/strong&gt; Determines how much total retrieved context you can feed to the prompt at query time.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Embedding Model Sequence Limit (e.g., 512 tokens for&lt;/strong&gt; &lt;code&gt;**e5-base**&lt;/code&gt;&lt;strong&gt;,&lt;/strong&gt; &lt;code&gt;**bge-base**&lt;/code&gt;&lt;strong&gt;, BERT):&lt;/strong&gt; Determines how much text can be converted into a vector during the offline ingestion pipeline.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many of the most popular, cost-effective open-source embedding models deployed across the industry have a &lt;strong&gt;hard sequence limit of just 512 tokens&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now, picture what happens when that 1,500-character chunk (which expanded to 700 tokens) arrives at a 512-token embedding model:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The tokenizer silently applies&lt;/strong&gt; &lt;code&gt;**truncation=True**&lt;/code&gt;&lt;strong&gt;. It embeds the first 512 tokens, chops off the remaining 188 tokens, and tosses them in the trash!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There is no error thrown in your console. Your ingestion script happily prints &lt;code&gt;100% Ingested&lt;/code&gt;. Your vector database stores a vector. But the final 188 tokens &lt;strong&gt;never participated in the embedding&lt;/strong&gt;. If the exact answer to your user's question lived in those truncated lines, your vector search will never find it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Production Rule:&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;Never design your chunking pipeline using raw character counts alone. Always measure and enforce chunk boundaries using the&lt;/em&gt; &lt;strong&gt;&lt;em&gt;actual tokenizer and token limit of your target embedding model&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we know we must measure in tokens and stay within our model’s physical limits, how do we actually slice the text?&lt;/p&gt;

&lt;p&gt;Let’s explore the three foundational strategies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three Baseline Production Chunking Strategies
&lt;/h2&gt;

&lt;p&gt;Now let’s look at the first three foundational chunking strategies used in real-world systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strategy 1: Fixed-Size Chunking (The Naive Baseline)
&lt;/h3&gt;

&lt;p&gt;This is the simplest approach: slice text every N characters or tokens unconditionally, like a cookie cutter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Original sentence:
"Under clause 14, the total aggregate liability of the Vendor shall not exceed $1,000,000."
Fixed cut at character 60:
[Chunk 1]: "Under clause 14, the total aggregate liability of the Vendor"
[Chunk 2]: " shall not exceed $1,000,000."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a user asks: &lt;em&gt;“What is the vendor’s liability cap?”&lt;/em&gt;, vector search pulls Chunk 1 because it matches “liability of the Vendor”. But Chunk 1 has no numbers! The LLM reads Chunk 1 and answers: &lt;em&gt;“The agreement mentions liability under clause 14, but does not state any dollar amount.”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict:&lt;/strong&gt; 🛑 Do not use this in production. It cuts words, severs numbers from clauses, and destroys grammar.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strategy 2: Recursive Character Splitting (The Enterprise Workhorse)
&lt;/h3&gt;

&lt;p&gt;For general unstructured text — articles, reports, memos, documentation — a recursive splitter is the standard baseline.&lt;/p&gt;

&lt;p&gt;Instead of slicing text at an arbitrary position, it tries progressively more granular separators:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Paragraph (\n\n)  ──►  Line (\n)  ──►  Sentence (. )  ──►  Word ( )  ──►  Character ("")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It acts like a thoughtful editor. It first asks: &lt;em&gt;“Can I keep entire paragraphs together within my chunk size limit?”&lt;/em&gt; If a paragraph is too long, it steps down and tries line breaks. If a line is still too long, it splits on sentences. It only ever splits words as an absolute last resort.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Raw&lt;/span&gt; &lt;span class="n"&gt;Document&lt;/span&gt;
     &lt;span class="err"&gt;│&lt;/span&gt;
     &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="n"&gt;Can&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;paragraph&lt;/span&gt; &lt;span class="n"&gt;fit&lt;/span&gt; &lt;span class="n"&gt;within&lt;/span&gt; &lt;span class="n"&gt;chunk_size&lt;/span&gt;&lt;span class="err"&gt;?&lt;/span&gt;
     &lt;span class="err"&gt;│&lt;/span&gt;     &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="n"&gt;YES&lt;/span&gt; &lt;span class="err"&gt;──►&lt;/span&gt; &lt;span class="n"&gt;Keep&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;together&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt;
     &lt;span class="err"&gt;│&lt;/span&gt;     &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="n"&gt;NO&lt;/span&gt;  &lt;span class="err"&gt;──►&lt;/span&gt; &lt;span class="n"&gt;Step&lt;/span&gt; &lt;span class="n"&gt;down&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="nf"&gt;breaks &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;\&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="err"&gt;│&lt;/span&gt;
     &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="n"&gt;Can&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="n"&gt;fit&lt;/span&gt;&lt;span class="err"&gt;?&lt;/span&gt;
     &lt;span class="err"&gt;│&lt;/span&gt;     &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="n"&gt;YES&lt;/span&gt; &lt;span class="err"&gt;──►&lt;/span&gt; &lt;span class="n"&gt;Keep&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;together&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt;
     &lt;span class="err"&gt;│&lt;/span&gt;     &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="n"&gt;NO&lt;/span&gt;  &lt;span class="err"&gt;──►&lt;/span&gt; &lt;span class="n"&gt;Step&lt;/span&gt; &lt;span class="n"&gt;down&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;sentence&lt;/span&gt; &lt;span class="nf"&gt;endings &lt;/span&gt;&lt;span class="p"&gt;(.&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="err"&gt;│&lt;/span&gt;
     &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="n"&gt;Step&lt;/span&gt; &lt;span class="n"&gt;down&lt;/span&gt; &lt;span class="n"&gt;until&lt;/span&gt; &lt;span class="n"&gt;pieces&lt;/span&gt; &lt;span class="n"&gt;fit&lt;/span&gt; &lt;span class="n"&gt;comfortably&lt;/span&gt; &lt;span class="n"&gt;within&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="n"&gt;bounds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain_text_splitters import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=400,        # Target tokens
    chunk_overlap=80,      # 20% safety net
    separators=["\n\n", "\n", ". ", " ", ""]
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Chunk Overlap: The Safety Net
&lt;/h3&gt;

&lt;p&gt;Whenever you use recursive splitting, you should configure &lt;strong&gt;chunk overlap&lt;/strong&gt; (typically 10% to 20% of your chunk size):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ Chunk 1: Tokens 0 ──────────────► 400 ]
             [ Chunk 2: Tokens 320 ──────────────► 720 ]  (80-token overlap)
                          [ Chunk 3: Tokens 640 ──────────────► 1040 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why overlap matters:&lt;/strong&gt;&lt;br&gt;
Sometimes, a critical clause spans across a chunk boundary:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Without Overlap:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;em&gt;Chunk 1:&lt;/em&gt; &lt;code&gt;"...employees can claim reimbursement after..."&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;Chunk 2:&lt;/em&gt; &lt;code&gt;"...submitting Form 104-B within 30 days."&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The relationship between the requirement and the form is severed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;With Overlap:&lt;/strong&gt;&lt;br&gt;
The sliding window captures the complete boundary clause unbroken in Chunk 2!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Trade-Off:&lt;/strong&gt; More overlap means slightly more storage and duplicate text in your vector index. A starting point of &lt;strong&gt;10%–20% (50 to 80 tokens = roughly 2–3 sentences)&lt;/strong&gt; provides a solid safety margin.&lt;/p&gt;
&lt;h3&gt;
  
  
  Strategy 3: Structure-Aware Chunking (Markdown, Code, and Tables)
&lt;/h3&gt;

&lt;p&gt;Humans don’t write in random blocks of 400 words. We write using explicit structures: &lt;strong&gt;headings, sections, code blocks, and tables.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you run a naive text splitter on structured documents, you throw that structure away:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A sub-bullet point is separated from its parent &lt;code&gt;# Heading&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  A Python function gets cut across its loop body.&lt;/li&gt;
&lt;li&gt;  A table row is disconnected from its column headers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  1. Markdown Header Splitting
&lt;/h3&gt;

&lt;p&gt;In technical manuals and wikis, headings (&lt;code&gt;#&lt;/code&gt;, &lt;code&gt;##&lt;/code&gt;, &lt;code&gt;###&lt;/code&gt;) define logical units of thought.&lt;/p&gt;

&lt;p&gt;Structure-aware splitters divide text based on these headings and &lt;strong&gt;automatically inject the heading path into each chunk’s metadata&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_text_splitters&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;MarkdownHeaderTextSplitter&lt;/span&gt;
&lt;span class="n"&gt;headers_to_split_on&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;#&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Section&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;##&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Subsection&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;###&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Topic&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;markdown_splitter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MarkdownHeaderTextSplitter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;headers_to_split_on&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;headers_to_split_on&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, each chunk isn’t just floating text. It is an indexed object carrying its exact structural hierarchy in its metadata payload:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"metadata"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Section"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Benefits"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Subsection"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Health Insurance"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Topic"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Out-of-Network Coverage"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"page_content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Employees can claim out-of-network reimbursement after..."&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This structural metadata solves two major production problems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Prevents Orphan Text:&lt;/strong&gt; Even if the chunk itself never mentions the words “Health Insurance”, the metadata permanently binds the text to its parent topic.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Enables Metadata Filtering:&lt;/strong&gt; At query time, you can pre-filter searches directly in your database (e.g., only search where &lt;code&gt;"Section": "Benefits"&lt;/code&gt;), eliminating irrelevant company documents before vector search even begins!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  2. Code-Aware Splitting
&lt;/h3&gt;

&lt;p&gt;If you are indexing a software repository, chunking must respect the programming language’s Abstract Syntax Tree (AST):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_payment&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;charge_customer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;update_order&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;PaymentError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;rollback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cutting this code block arbitrarily in the middle of the &lt;code&gt;try&lt;/code&gt; block destroys its syntax and meaning. Language-aware splitters keep entire functions, classes, or imports together as single logical blocks.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. The Table Problem (Financial 10-K &amp;amp; 10-Q Filings)
&lt;/h3&gt;

&lt;p&gt;Tables are one of the fastest ways to break a naive RAG pipeline. Consider this balance sheet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;| Product Segment       | Q1 2024 ($M) | Q2 2024 ($M) | Q3 2024 ($M) |
| --------------------- | -----------: | -----------: | -----------: |
| Cloud Subscriptions   |       $4,200 |       $4,600 |       $5,100 |
| Professional Services |         $320 |         $310 |         $305 |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine a standard text splitter cuts this table horizontally when it hits a character limit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Chunk 1:&lt;/strong&gt;
&lt;code&gt;Product Segment | Q1 2024 | Q2 2024 | Q3 2024&lt;/code&gt;
&lt;code&gt;Cloud Subscriptions | $4,200 | $4,600 | $5,100&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Chunk 2:&lt;/strong&gt;
&lt;code&gt;Professional Services | $320 | $310 | $305&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now an analyst asks:&lt;br&gt;
&lt;strong&gt;“What was Professional Services revenue in Q2 2024?”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The vector search retrieves Chunk 2. But Chunk 2 contains three bare numbers without the header &lt;code&gt;Q2 2024&lt;/code&gt;! The retrieval succeeded, but answer generation fails because the structural context was lost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Production Fix for Tables:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Use &lt;strong&gt;Table-Aware Parsers&lt;/strong&gt; that convert rows into structured formats (like Markdown or JSON) that repeat the column headers on every slice:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Company"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ACME Corp"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Report"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Q3 2024"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Segment"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Professional Services"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Period"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Q2 2024"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Revenue"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"$310M"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Now the number has complete meaning: &lt;code&gt;Professional Services → Q2 2024 → Revenue → $310M&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Where Do We Go From Here?
&lt;/h2&gt;

&lt;p&gt;We’ve now established the fundamentals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The Ingestion Pipeline is the kitchen prep that determines your RAG system’s accuracy ceiling.&lt;/li&gt;
&lt;li&gt;  Tiny chunks cause “half-truth” hallucinations; giant chunks cause “semantic blur” via mean pooling.&lt;/li&gt;
&lt;li&gt;  Token limits like 512 tokens must be strictly respected to avoid silent data truncation.&lt;/li&gt;
&lt;li&gt;  Recursive and structure-aware splitting protect paragraphs, code blocks, and tables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;But what happens when documents don’t have neat headings?&lt;/strong&gt;&lt;br&gt;
What happens when you have messy meeting transcripts where people switch topics unpredictably?&lt;br&gt;
And what happens when your vector search needs &lt;strong&gt;small chunks for search accuracy&lt;/strong&gt;, but your LLM needs &lt;strong&gt;an entire 2-page policy for context&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;Part 5&lt;/strong&gt;, we take chunking to the advanced level:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Bounded Semantic Chunking:&lt;/strong&gt; Detecting topic shifts dynamically with mathematical distance thresholds and size guardrails.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Parent-Document Retrieval (Small-to-Big):&lt;/strong&gt; How to search tiny child chunks while feeding large parent sections to the LLM (and how this prevents an $8,400 medical bill surprise!).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Breadcrumbs vs. Anthropic’s Contextual Retrieval:&lt;/strong&gt; The complete architectural face-off.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;The Production Decision Matrix:&lt;/strong&gt; Choosing the exact strategy for your document types.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 &lt;strong&gt;Continue to Part 5: Advanced RAG Chunking: Hierarchical, Semantic, and Contextual Retrieval&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What’s the trickiest chunking bug you’ve encountered in your own pipelines? Have you seen tables or severed sentences trip up your models? Let’s chat in the comments!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>softwaredevelopment</category>
      <category>rag</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Why Can’t We Just Give an LLM All Our Company Documents? Understanding the Context Window</title>
      <dc:creator>Anchal Nigam</dc:creator>
      <pubDate>Wed, 28 Oct 2026 10:08:32 +0000</pubDate>
      <link>https://dev.to/nigamanchal/why-cant-we-just-give-an-llm-all-our-company-documents-understanding-the-context-window-3j3e</link>
      <guid>https://dev.to/nigamanchal/why-cant-we-just-give-an-llm-all-our-company-documents-understanding-the-context-window-3j3e</guid>
      <description>&lt;p&gt;Imagine your company has 1 lakh documents:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Engineering docs&lt;/li&gt;
&lt;li&gt;Customer conversations&lt;/li&gt;
&lt;li&gt;Incident reports&lt;/li&gt;
&lt;li&gt;Product specifications&lt;/li&gt;
&lt;li&gt;HR policies&lt;/li&gt;
&lt;li&gt;Sales reports&lt;/li&gt;
&lt;li&gt;Meeting notes&lt;/li&gt;
&lt;li&gt;PDFs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you build an AI assistant and someone asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Why did our payment service fail in Germany last November?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You might think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Why do we need RAG or a vector database? Why not just give the LLM all 1 lakh documents and let it figure it out?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It sounds reasonable. After all, newer LLMs can process surprisingly large amounts of text. But there is a difference between:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The model &lt;em&gt;can accept&lt;/em&gt; this much information."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The model &lt;em&gt;can efficiently and reliably use&lt;/em&gt; all of this information."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That difference is where things get interesting.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Happens When You Give Text to an LLM?
&lt;/h2&gt;

&lt;p&gt;When you read a document, you see paragraphs, pages, sections, and diagrams. An LLM doesn't. It first converts everything into &lt;strong&gt;tokens&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"The payment service failed"
           ↓
["The", " payment", " service", " failed"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A long document becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Document
   ↓
Text
   ↓
Tokens
   ↓
Token IDs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This matters because LLMs measure their capacity in &lt;strong&gt;tokens&lt;/strong&gt;, not pages or documents.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Context Window: An LLM's Working Space
&lt;/h2&gt;

&lt;p&gt;Every LLM has a &lt;strong&gt;context window&lt;/strong&gt;. Think of it as the model's working space for a single request.&lt;/p&gt;

&lt;p&gt;Everything must fit inside it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9o0sp56kr2oavfgd9uyr.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9o0sp56kr2oavfgd9uyr.webp" alt="The context window is the model’s working space. Everything the LLM needs for a request must fit inside it." width="799" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But let's make things more interesting.&lt;/p&gt;

&lt;p&gt;Imagine we have a model with a &lt;strong&gt;1 million token context window&lt;/strong&gt;. Now everything fits.&lt;/p&gt;

&lt;p&gt;Problem solved?&lt;/p&gt;

&lt;p&gt;Not quite.&lt;/p&gt;

&lt;p&gt;We've solved the &lt;strong&gt;capacity&lt;/strong&gt; problem. We haven't solved the &lt;strong&gt;processing&lt;/strong&gt; problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Does an LLM Understand Context?
&lt;/h2&gt;

&lt;p&gt;Consider this sentence:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The bank rejected the payment because the card had expired.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To understand the sentence, the model needs to connect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             rejected
            /        \
         bank       payment
                       |
                    because
                       |
                 card expired
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The words matter. But the &lt;strong&gt;relationships between the words&lt;/strong&gt; matter even more. This is where the Transformer's attention mechanism comes in.&lt;/p&gt;




&lt;h2&gt;
  
  
  Attention: How the Model Decides What Matters
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2iy75jnklfeutpk2bfch.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2iy75jnklfeutpk2bfch.jpg" alt="Attention helps tokens determine which other pieces of information are relevant when building meaning and context." width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A useful way to think about attention is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Which other pieces of information should matter to me right now?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Every token looks at other tokens and determines which ones are useful.&lt;/p&gt;

&lt;p&gt;Technically, attention uses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Query (Q)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Key (K)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Value (V)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of them as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Q (Query)&lt;/strong&gt; — "What am I looking for?"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;K (Key)&lt;/strong&gt; — "What do I contain?"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;V (Value)&lt;/strong&gt; — "What information do I carry?"&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  A Tiny Example
&lt;/h3&gt;

&lt;p&gt;Take:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The red apple is on the table.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suppose we're processing &lt;code&gt;apple&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Very roughly, &lt;code&gt;apple&lt;/code&gt; asks: &lt;em&gt;"What helps describe me?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Nearby words respond:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Token&lt;/th&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The&lt;/td&gt;
&lt;td&gt;grammatical information&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;red&lt;/td&gt;
&lt;td&gt;descriptive/color information&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;apple&lt;/td&gt;
&lt;td&gt;noun/entity information&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;table&lt;/td&gt;
&lt;td&gt;location/object information&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The model computes how well the query matches each key. Simplified illustrative scores:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apple's Query × Key:
  The   → 1.2
  red   → 8.5  ← strong match
  apple → 4.1
  table → 0.8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;These aren't actual values from a real model — they're a simple illustration.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;These scores are then converted into attention weights that add up to 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The   →  5%
red   → 80%
apple → 15%
table →  0%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The model then uses those weights to pull information from the corresponding Values. So the important idea is:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmbgwwtkofds35wmbe3xn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmbgwwtkofds35wmbe3xn.jpg" alt="Processing Stages" width="800" height="597"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Mathematically, the core operation looks like:&lt;/p&gt;

&lt;p&gt;Attention(Q, K, V) = softmax((QKᵀ) / √dₖ) V&lt;/p&gt;

&lt;p&gt;The important part is the flow: &lt;strong&gt;Q and K decide how relevant something is; V carries the information that gets passed along.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Think of attention as a very large system of &lt;strong&gt;information routing&lt;/strong&gt; between tokens — much closer to what's happening than simply saying "the LLM reads the sentence."&lt;/p&gt;




&lt;h2&gt;
  
  
  What Changes When the Context Gets Huge?
&lt;/h2&gt;

&lt;p&gt;Our example had only a few words. Now imagine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 tokens
     ↓
1,000 tokens
     ↓
10,000 tokens
     ↓
100,000 tokens
     ↓
1,000,000 tokens
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suddenly there are far more relationships to consider. Think about a networking event:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;10 people&lt;/strong&gt; → Easy to figure out who's important.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;100 people&lt;/strong&gt; → Still manageable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;10,000 people&lt;/strong&gt; → Finding the right person becomes much harder.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The LLM faces a related scaling problem when processing long sequences. In standard self-attention, the number of token-to-token interactions grows roughly with the square of sequence length.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fevp59oxnu0uwfcelv09r.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fevp59oxnu0uwfcelv09r.jpg" alt="As context grows, the number of potential relationships between tokens grows rapidly, making long-context processing increasingly expensive." width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Very roughly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10K tokens  →  N amount of attention work
20K tokens  →  ~4N
40K tokens  →  ~16N
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Modern LLMs use many optimizations and architectural techniques, so these numbers should not be interpreted as the literal cost of every modern model.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The important intuition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A larger context doesn't just mean more text to read. It also means many more relationships between tokens for the model to consider. As context grows, the amount of information the model needs to connect and reason over can grow &lt;strong&gt;much faster&lt;/strong&gt; than the context itself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is one reason long-context inference becomes expensive.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Bigger Problem Isn't Capacity
&lt;/h2&gt;

&lt;p&gt;Let's return to our 1 lakh documents.&lt;/p&gt;

&lt;p&gt;Suppose they contain &lt;strong&gt;1,000,000 tokens&lt;/strong&gt; — and only &lt;strong&gt;10,000 tokens&lt;/strong&gt; are actually relevant to the user's question.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌───────────────────────────────┐
│                               │
│   Mostly irrelevant content   │
│                               │
│      █ Relevant Data █        │
│                               │
│   Mostly irrelevant content   │
│                               │
└───────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The model technically has access to everything. But &lt;strong&gt;why make it process 1 million tokens when only 10,000 matter?&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Bigger Context Isn't Free
&lt;/h2&gt;

&lt;p&gt;Consider two requests:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Request A — focused context&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question              500 tokens
Relevant Context    5,000 tokens
───────────────────────────────
Total               5,500 tokens
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Request B — everything&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question                  500 tokens
Entire Knowledge Base  500,000 tokens
────────────────────────────────────
Total                  500,500 tokens
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Request B has almost &lt;strong&gt;100× more input&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The model now has to process a huge amount of additional information before it can answer. And this is important:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The LLM doesn't first magically know which 10,000 tokens matter and then ignore the rest.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The model receives the context and runs its processing over that context. If we want to reduce the amount of irrelevant information, we generally need something &lt;em&gt;outside&lt;/em&gt; that basic generation step to help select it. That's one reason retrieval is useful. But before we get to RAG, there’s another cost.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Happens Before the LLM Starts Answering?
&lt;/h2&gt;

&lt;p&gt;When you send a long prompt, the model first processes the input. This initial stage is often called &lt;strong&gt;prefill&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think of it like handing someone a 500-page report and saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Read this before answering my question."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The person can’t start answering until they’ve processed the material they need. The same idea applies to LLMs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F736aynnj71xr2pm032sy.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F736aynnj71xr2pm032sy.jpeg" alt="TTFT Stages" width="800" height="1071"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That initial processing contributes to &lt;strong&gt;time-to-first-token&lt;/strong&gt; — how long you wait before the first piece of the answer appears.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Short context → less input to process → potentially faster start
Long context  → more input to process → potentially slower start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one reason a huge context window shouldn't be interpreted as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Send huge prompts everywhere."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A capability can be useful without being free.&lt;/p&gt;




&lt;h2&gt;
  
  
  The KV Cache: The Notes the Model Keeps
&lt;/h2&gt;

&lt;p&gt;While generating text, the model needs information from earlier tokens. Recomputing everything repeatedly would be wasteful. So the model stores useful intermediate information — this is called the &lt;strong&gt;KV Cache&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think of it like taking notes while reading a book. Instead of reopening page 1 every time, you keep notes on your desk.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Long Context
      ↓
 Process
      ↓
   KV Cache
      ↓
Reuse During Generation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes generation faster. But there's a tradeoff:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Larger contexts require larger caches&lt;/strong&gt; — and larger caches consume more memory. This becomes a major challenge when serving thousands of users simultaneously.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Are Larger Context Windows a Bad Thing?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;No.&lt;/strong&gt; This distinction is extremely important.&lt;/p&gt;

&lt;p&gt;A larger context window is genuinely useful. Suppose you're debugging a complicated service. You want to provide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;20K tokens of code&lt;/li&gt;
&lt;li&gt;10K tokens of logs&lt;/li&gt;
&lt;li&gt;5K tokens of architecture docs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Having a model that can consider all of this together can be extremely useful. Or you're analyzing a long contract, comparing several reports, or working through a long conversation. In these cases, a larger context window gives you something valuable:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The ability to keep more &lt;strong&gt;relevant&lt;/strong&gt; information in the model's working context at once.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;A larger context window is a capability, not a guarantee of better answers.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Surprising Part: More Context Can Hurt Performance
&lt;/h2&gt;

&lt;p&gt;Now imagine the answer to our question is buried somewhere in the middle of thousands of documents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Document 1
Document 2
Document 3
…
Document 49,999
★ IMPORTANT INCIDENT REPORT ★
Document 50,001
Document 50,002
…
Document 100,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The information is technically there. But can the model reliably find and use it?&lt;/p&gt;

&lt;p&gt;A well-known study — &lt;strong&gt;"Lost in the Middle: How Language Models Use Long Contexts"&lt;/strong&gt; — tested exactly this kind of behavior. Researchers evaluated models on tasks such as multi-document question answering and key-value retrieval.&lt;/p&gt;

&lt;p&gt;They found that performance could &lt;strong&gt;degrade substantially&lt;/strong&gt; depending on where the relevant information appeared in a long context. Models often performed better when the useful information was near the beginning or end, and worse when it was buried in the middle. This became known as the &lt;strong&gt;lost-in-the-middle problem&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The basic lesson is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Having information available is not the same as using it effectively.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Information ≠ Useful Context
&lt;/h2&gt;

&lt;p&gt;Suppose I ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What is our refund policy?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And I give the model:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;5 relevant paragraphs&lt;/li&gt;
&lt;li&gt;50,000 unrelated paragraphs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Did I give the model more information? &lt;strong&gt;Yes.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Did I give it better context? &lt;strong&gt;No.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I've actually created another job for the model: &lt;em&gt;"Figure out which parts of this enormous pile of information actually matter."&lt;/em&gt; And that's before it even answers my original question.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;More context ≠ automatically better context.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The goal isn't to maximize the amount of context. The goal is to maximize the amount of &lt;strong&gt;useful&lt;/strong&gt; context.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Are Context Windows Getting Bigger?
&lt;/h2&gt;

&lt;p&gt;Because larger context windows are genuinely useful. Think of RAM in a computer — more RAM is helpful. But:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;More RAM doesn't automatically make every program better. It simply allows larger workloads &lt;strong&gt;when needed&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The same applies to context windows.&lt;/p&gt;




&lt;h2&gt;
  
  
  Bringing It All Together
&lt;/h2&gt;

&lt;p&gt;Why can't we simply give an LLM all 1 lakh company documents?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;They may not fit&lt;/strong&gt; — Too many documents → too many tokens.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Most of them are irrelevant&lt;/strong&gt; — Only a small portion may be relevant to the specific question.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;More context means more computation&lt;/strong&gt; — The model has more information to process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;More context can increase latency&lt;/strong&gt; — Large prompts take longer to process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;More context costs more&lt;/strong&gt; — More tokens generally mean more compute.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;More context doesn't guarantee better understanding&lt;/strong&gt; — The model can have access to information and still struggle to use it effectively.&lt;/li&gt;
&lt;li&gt;Why RAG Exists&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmei2wqr6pre9tahab8yh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmei2wqr6pre9tahab8yh.jpg" alt="Retrieval narrows a large knowledge base down to the most relevant context before passing it to the LLM, improving efficiency and focus." width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the model isn't being asked:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Read the entire company."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's being asked:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Here are the pieces of information relevant to this question. Use them to answer."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And that's the real idea behind &lt;strong&gt;RAG&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Mental Model to Remember
&lt;/h2&gt;

&lt;p&gt;If you remember only one thing from this article, remember this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A context window tells you &lt;strong&gt;how much&lt;/strong&gt; information an LLM &lt;em&gt;can accept&lt;/em&gt;. It doesn't tell you &lt;strong&gt;how much&lt;/strong&gt; information you &lt;em&gt;should give&lt;/em&gt; it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The goal isn't &lt;strong&gt;maximum context&lt;/strong&gt;. The goal is &lt;strong&gt;maximum useful context&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;p&gt;Now we know the goal isn't to give an LLM everything. It's to give it the &lt;strong&gt;right&lt;/strong&gt; information.&lt;/p&gt;

&lt;p&gt;But that leads to the next question: When you have thousands or even millions of documents, how do you figure out which pieces of information are actually relevant to a user's question?&lt;/p&gt;

&lt;p&gt;Something has to find that relevant information &lt;em&gt;before&lt;/em&gt; it reaches the LLM. And that's where retrieval comes in. But retrieval is more than simply searching for a document.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, how does retrieval actually work? That's what we'll explore in the next article.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>rag</category>
      <category>machinelearning</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>What RAG Actually Is (And Why It’s More Than Search + LLM)</title>
      <dc:creator>Anchal Nigam</dc:creator>
      <pubDate>Wed, 21 Oct 2026 09:01:20 +0000</pubDate>
      <link>https://dev.to/nigamanchal/what-rag-actually-is-and-why-its-more-than-search-llm-5f85</link>
      <guid>https://dev.to/nigamanchal/what-rag-actually-is-and-why-its-more-than-search-llm-5f85</guid>
      <description>&lt;p&gt;In the &lt;a href="https://medium.com/@anchall-nigamm/why-llms-hallucinate-and-why-rag-exists-8cd0782cf719" rel="noopener noreferrer"&gt;previous article&lt;/a&gt;, we explored why Large Language Models hallucinate and why RAG exists.&lt;/p&gt;

&lt;p&gt;We learned that LLMs don’t magically know everything. Their knowledge is limited to what they learned during training, and that knowledge eventually becomes outdated. RAG helps solve this problem by providing relevant external information at the time of answering a question.&lt;/p&gt;

&lt;p&gt;But that raises another question - &lt;strong&gt;What actually happens inside a RAG system?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When people talk about RAG, terms like chunking, embeddings, vector databases, hybrid search, reranking, and evaluations often appear together. For someone starting their journey, it can feel overwhelming.&lt;/p&gt;

&lt;p&gt;So before diving into each of those topics, let’s first understand the big picture.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Does RAG Actually Mean?
&lt;/h2&gt;

&lt;p&gt;RAG stands for: &lt;strong&gt;Retrieval + Augmentation + Generation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each word represents a step in the process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retrieval&lt;/strong&gt; — Find information relevant to the user’s question.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Augmentation —&lt;/strong&gt; Add the retrieved information to the prompt or context sent to the model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generation&lt;/strong&gt; — The model uses that context to generate a response.&lt;/p&gt;

&lt;p&gt;At a very high level, the flow looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6s2dp52y160eyawgmx70.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6s2dp52y160eyawgmx70.jpeg" alt="Gemini Generated image" width="799" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Key Idea Behind RAG&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One important thing often gets misunderstood. RAG does &lt;strong&gt;not&lt;/strong&gt; teach new knowledge to the model. The model itself remains unchanged. Instead, RAG changes the information available during generation.&lt;/p&gt;

&lt;p&gt;Think about asking a colleague a question. If they don’t have the required information, they may guess. If you provide them with the relevant document before they answer, the quality of the answer improves dramatically.&lt;/p&gt;

&lt;p&gt;Their intelligence didn’t change. The information available to them changed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RAG follows the same principle.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  RAG Architecture
&lt;/h2&gt;

&lt;p&gt;At first, the previous diagram made RAG look surprisingly simple. Retrieve some information. Attach it to the prompt. Generate an answer. Done.&lt;/p&gt;

&lt;p&gt;But when I started exploring production-grade RAG systems, I realized there was much more happening behind the scenes. Most real-world systems consist of two major pipelines:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Ingestion Pipeline&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Retrieval (Inference) Pipeline&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s look at both.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Ingestion Pipeline
&lt;/h2&gt;

&lt;p&gt;Before users can ask questions, the system must prepare its knowledge base.&lt;/p&gt;

&lt;p&gt;A simplified ingestion pipeline looks like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Documents -&amp;gt; Chunking -&amp;gt; Embeddings -&amp;gt; Searchable Storage&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Documents&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;These can be PDFs, support articles, product catalogs, internal documentation, knowledge-base articles, release notes, or any other source of information.&lt;/p&gt;

&lt;h3&gt;
  
  
  Chunking
&lt;/h3&gt;

&lt;p&gt;Large documents are split into smaller pieces so that relevant information can be retrieved efficiently.&lt;/p&gt;

&lt;h3&gt;
  
  
  Embeddings
&lt;/h3&gt;

&lt;p&gt;Each chunk is converted into a machine-understandable representation that can later be searched based on meaning.&lt;/p&gt;

&lt;h3&gt;
  
  
  Searchable Storage
&lt;/h3&gt;

&lt;p&gt;The processed chunks are stored in a system optimized for retrieval.&lt;/p&gt;

&lt;p&gt;At this point, the knowledge is ready to be used.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Retrieval Pipeline
&lt;/h2&gt;

&lt;p&gt;Now imagine a user asks a question. The retrieval pipeline comes into action.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;User Question -&amp;gt; Retrieve Relevant Context -&amp;gt;Prompt Construction -&amp;gt;LLM -&amp;gt; Response&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In production systems, however, several additional steps often exist between these stages. Questions may be transformed. Results may be filtered. Retrieved content may be reranked. Citations may be attached. Responses may be evaluated for quality and faithfulness.&lt;/p&gt;

&lt;p&gt;A more realistic view looks something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frl75bg8r937sslhi7tjv.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frl75bg8r937sslhi7tjv.jpeg" alt="HLD OF Retrieval Augmented Generation(RAG)" width="799" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Part That Surprised Me
&lt;/h2&gt;

&lt;p&gt;At this point, I felt I finally understood RAG. Then every box in the architecture started raising new questions.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Why do we need chunking?&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;What exactly are embeddings?&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Why are vector databases everywhere?&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Why isn’t a traditional database enough?&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Why do production systems use hybrid search?&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Why do people talk about rerankers?&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;And if all the required information already exists, why do retrieval systems still fail sometimes?&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The deeper I went, the more I realized that RAG is not simply &lt;strong&gt;“search + AI.”&lt;/strong&gt; Each component exists because it solves a specific problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next Question
&lt;/h2&gt;

&lt;p&gt;While studying these architectures, one question kept coming back to me. If the goal is to provide context to the model, why don’t we simply give it all the available documents? Why retrieve only a small subset? Why not let the model read everything and figure out what’s relevant on its own? It sounds like a reasonable idea. In fact, that was my first thought as well.&lt;/p&gt;

&lt;p&gt;As I learned later, this approach breaks surprisingly fast because of context-window limitations, cost, latency, and relevance challenges. And understanding those limitations is exactly what led me to the next topic in this series.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Can’t We Just Give the LLM All the Available Context Instead of Retrieving Only What’s Relevant?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That’s what we’ll explore in the next article.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>rag</category>
      <category>machinelearning</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Why LLMs Hallucinate — And Why RAG exists</title>
      <dc:creator>Anchal Nigam</dc:creator>
      <pubDate>Mon, 12 Oct 2026 05:30:00 +0000</pubDate>
      <link>https://dev.to/nigamanchal/why-llms-hallucinate-and-why-rag-exists-1097</link>
      <guid>https://dev.to/nigamanchal/why-llms-hallucinate-and-why-rag-exists-1097</guid>
      <description>&lt;p&gt;I remember the first time I felt something was off.&lt;/p&gt;

&lt;p&gt;I asked an LLM to list research papers on a niche topic. The response came instantly — paper titles, author names, publication years, even DOI links.&lt;/p&gt;

&lt;p&gt;Everything looked legitimate: structured, academic, confident. So, I searched for some of the papers online.&lt;/p&gt;

&lt;p&gt;A few of them simply didn’t exist.&lt;/p&gt;

&lt;p&gt;The titles sounded real. The author names looked believable. Even the citations followed proper academic formatting. But the information itself had been hallucinated.&lt;/p&gt;

&lt;p&gt;Not obviously fake or broken. Just convincingly incorrect. And that’s a very uncomfortable place to be as an engineer because now the problem isn’t failure — &lt;strong&gt;it’s trust.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🤔 “But LLMs are supposed to be smart… right?”
&lt;/h2&gt;

&lt;p&gt;That’s what I thought too. We hear things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  “LLMs understand language”&lt;/li&gt;
&lt;li&gt;  “They reason like humans”&lt;/li&gt;
&lt;li&gt;  “They can answer anything”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So when they generate something incorrect, our instinct is usually:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Maybe the model just isn’t advanced enough yet.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But here’s the uncomfortable truth:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;LLMs don’t store and retrieve knowledge the way humans do.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At a very basic level, an LLM is just doing one thing:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Predicting the next token.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They generate responses based on patterns learned during training and probabilities of what is most likely to come next given the context.&lt;/p&gt;

&lt;p&gt;That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  they don’t inherently verify truth,&lt;/li&gt;
&lt;li&gt;  they don’t automatically know your data,&lt;/li&gt;
&lt;li&gt;  and they don’t “look up” facts while generating responses.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that distinction explains a lot.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚠️ Where do hallucinations come from?
&lt;/h2&gt;

&lt;p&gt;Think about the research paper example again.&lt;/p&gt;

&lt;p&gt;The response looked incredibly convincing because the model had probably already seen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  thousands of research papers&lt;/li&gt;
&lt;li&gt;  academic citations&lt;/li&gt;
&lt;li&gt;  DOI formats&lt;/li&gt;
&lt;li&gt;  author naming patterns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So even when it didn’t actually know the answer, it still tried to generate the most statistically plausible continuation. That’s why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  fake paper titles started sounding real,&lt;/li&gt;
&lt;li&gt;  imaginary authors looked believable,&lt;/li&gt;
&lt;li&gt;  and nonexistent citations appeared academically correct.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The model wasn’t verifying truth. And that’s exactly why hallucinations become dangerous. Because the responses often sound:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  fluent,&lt;/li&gt;
&lt;li&gt;  well-structured,&lt;/li&gt;
&lt;li&gt;  authoritative,&lt;/li&gt;
&lt;li&gt;  and highly confident.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Humans naturally associate confidence with correctness. So over time, fake information starts looking credible.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧩 How do we fix this?
&lt;/h2&gt;

&lt;p&gt;At this point, I started wondering “&lt;strong&gt;&lt;em&gt;If LLMs are just predicting statistically probable text, how do we make them generate more accurate answers?”&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because the model itself hasn’t changed. It’s still doing the same thing — &lt;strong&gt;&lt;em&gt;Predicting the next most probable words.&lt;/em&gt;&lt;/strong&gt; So maybe the real issue wasn’t intelligence.&lt;/p&gt;

&lt;p&gt;Maybe the issue was &lt;strong&gt;context&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbc5pgjf67x39go1dj9mw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbc5pgjf67x39go1dj9mw.png" alt="Image created by author using Gemini" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🚧 The Actual Limitation
&lt;/h2&gt;

&lt;p&gt;The model generated fake citations because it had no way to verify those citations against an authoritative source while answering. It was trying to predict answers with incomplete grounding. And that exposes one of the biggest limitations of LLMs today.&lt;/p&gt;

&lt;p&gt;They:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  don’t automatically have access to real-time information&lt;/li&gt;
&lt;li&gt;  don’t automatically have access to your company/private data&lt;/li&gt;
&lt;li&gt;  can’t access your documents unless you provide them&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your PDFs, databases, Slack discussions, APIs, internal knowledge are invisible to the model by default.&lt;/p&gt;

&lt;h2&gt;
  
  
  💡 The missing piece
&lt;/h2&gt;

&lt;p&gt;So what if, instead of expecting the model to magically know everything, we first give it the right information before generation?&lt;/p&gt;

&lt;p&gt;Think of it like asking a friend something from memory. Without context, they’ll answer based on vague recollection. But if they quickly open the actual document before answering, the quality of the response changes completely.&lt;/p&gt;

&lt;p&gt;Your friend didn’t suddenly become smarter. They just got access to &lt;strong&gt;better context.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🚀 And this is the key insight
&lt;/h2&gt;

&lt;p&gt;When we add relevant information into the prompt, we change what the model sees before predicting the next word. And that changes the direction of generation itself. In simple words:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Better context → better prediction.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This leads to something important: not a magical new model, not some hidden intelligence upgrade, but a &lt;strong&gt;system design shift&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Retrieval-Augmented Generation (RAG)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At its core, RAG is simply a smarter way of grounding generation using relevant external information before the model starts predicting.&lt;/p&gt;

&lt;h2&gt;
  
  
  What’s next?
&lt;/h2&gt;

&lt;p&gt;In the next article, we’ll finally break this down properly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  What RAG actually is&lt;/li&gt;
&lt;li&gt;  How retrieval changes generation&lt;/li&gt;
&lt;li&gt;  Why “just adding a vector DB” is not enough&lt;/li&gt;
&lt;li&gt;  How modern AI systems retrieve useful context&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🏁 Final thought
&lt;/h2&gt;

&lt;p&gt;LLMs don’t hallucinate because they’re broken.&lt;/p&gt;

&lt;p&gt;They hallucinate because they can generate answers that sound convincing, but they don’t automatically verify whether those answers are actually true. And until we give LLMs a way to ground their answers in reliable information, we’ll keep getting answers that sound right — but aren’t.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Next: “What RAG actually is?”&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>rag</category>
      <category>machinelearning</category>
      <category>chatgpt</category>
    </item>
  </channel>
</rss>
