<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Devanshu]]></title><description><![CDATA[Devanshu]]></description><link>https://devanshumishra.hashnode.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1593680282896/kNC7E8IR4.png</url><title>Devanshu</title><link>https://devanshumishra.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Sun, 06 Sep 2026 21:06:37 GMT</lastBuildDate><atom:link href="https://devanshumishra.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[RAG as an Attention Optimizer]]></title><description><![CDATA[Why are we building this pipeline at all?
You already know what RAG is. Take the user's query, hit a vector database, grab the relevant chunks, stuff them into the prompt, ask the LLM to answer. We've]]></description><link>https://devanshumishra.hashnode.dev/rag-as-an-attention-optimizer</link><guid isPermaLink="true">https://devanshumishra.hashnode.dev/rag-as-an-attention-optimizer</guid><category><![CDATA[RAG ]]></category><category><![CDATA[llm]]></category><category><![CDATA[AI]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[System Design]]></category><dc:creator><![CDATA[Devanshu Mishra]]></dc:creator><pubDate>Fri, 04 Sep 2026 11:55:15 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/637e21bc38f6e7d3eab3f6ee/3831b6a0-739d-4fa1-b3db-64207cd08779.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Why are we building this pipeline at all?</h2>
<p>You already know what RAG is. Take the user's query, hit a vector database, grab the relevant chunks, stuff them into the prompt, ask the LLM to answer. We've all seen the tutorials.</p>
<p>But as engineers we should be asking <em>why</em>. Why build this whole pipeline? Why can't we take the entire company wiki, the codebase and the Slack history, and drop all of it into a 2-million-token context window?</p>
<p>It isn't just about cost. It's about a fundamental architectural bottleneck inside the model itself. It's about the math of <strong>attention</strong>.</p>
<p>(Yes, attention. The same thing humans have been running short of since TikTok.)</p>
<h2>Self-attention: the engine</h2>
<p>LLMs don't read left to right the way we do. To understand context, they use <strong>self-attention</strong>.</p>
<p>Read this sentence: <strong>"The bank of the river."</strong> Now this one: <strong>"The bank on the corner."</strong></p>
<p>How does the model know the first <em>bank</em> is dirt and water, and the second is money? It doesn't look at "bank" in isolation. It looks at the words <em>around</em> it.</p>
<p>When an LLM processes a prompt, it treats every token like a node in a network and asks one question: <strong>"How much does <em>this</em> token matter to <em>every other</em> token?"</strong> It draws an invisible web of connections across the whole input to work out what each word means in context.</p>
<h2>The bottleneck: the handshake problem</h2>
<p>That web of connections is a massive math problem. I call it the handshake problem.</p>
<p>If 5 people are in a room and everyone shakes hands with everyone else, that's 10 handshakes. With 1,000 people, it's 499,500 — roughly half a million.</p>
<p>Drag the slider and watch the connections explode:</p>
<p><a class="embed-card" href="https://codepen.io/Devanshu-Mishra-the-scripter/pen/bNqEYLE">https://codepen.io/Devanshu-Mishra-the-scripter/pen/bNqEYLE</a></p>

<table>
<thead>
<tr>
<th>People / tokens (N)</th>
<th>Handshakes: N(N−1)/2</th>
<th>Attention scores: N²</th>
</tr>
</thead>
<tbody><tr>
<td>5</td>
<td>10</td>
<td>25</td>
</tr>
<tr>
<td>100</td>
<td>4,950</td>
<td>10,000</td>
</tr>
<tr>
<td>1,000</td>
<td>499,500</td>
<td>1,000,000</td>
</tr>
<tr>
<td>100,000</td>
<td>~5 billion</td>
<td>10 billion</td>
</tr>
</tbody></table>
<p>Notice the third column. A handshake is symmetric — you and I shake once and we're done. Attention isn't. Every token scores every token (including itself), and it does so in both directions: how much <em>bank</em> cares about <em>river</em> is a different number from how much <em>river</em> cares about <em>bank</em>. So the count isn't N(N−1)/2. It's N².</p>
<pre><code class="language-plaintext">1,000 tokens    →     1,000,000 attention scores
100,000 tokens  → 10,000,000,000 attention scores
</code></pre>
<p>That's quadratic, O(N²) — the kind of complexity that gets you flagged in code review. And every one of those scores is floating-point math on a GPU, in every layer, for every token the model generates. Clever kernels like FlashAttention have made the <em>memory</em> side of this manageable, but the compute doesn't go away: double the context and you quadruple the work. You can't buy your way out of a quadratic.</p>
<h2>Signal degradation: lost in the middle</h2>
<p>Even with unlimited budget, forcing a million tokens into the context breaks something else: the attention itself. This is what the <a href="https://arxiv.org/abs/2307.03172">"Lost in the Middle"</a> paper documented — models get noticeably worse at using a fact when it's buried in the middle of a long context than when it sits near the start or the end.</p>
<p>Here's the mental model I use. Think of attention as a pie. For each token it processes, the model has exactly one pie — 100% — to divide among everything in the prompt.</p>
<p>You have one paragraph of facts that actually answers the question. Watch what happens to its share of the pie as you dump more raw documents into the context:</p>
<p><a class="embed-card" href="https://codepen.io/Devanshu-Mishra-the-scripter/pen/yyMePpM">https://codepen.io/Devanshu-Mishra-the-scripter/pen/yyMePpM</a></p>

<table>
<thead>
<tr>
<th>Paragraphs in context</th>
<th>Share for the one that matters</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>100%</td>
</tr>
<tr>
<td>5</td>
<td>20%</td>
</tr>
<tr>
<td>50</td>
<td>2%</td>
</tr>
<tr>
<td>1,000</td>
<td>0.1%</td>
</tr>
</tbody></table>
<p>One caveat, because the pie is a simplification: attention isn't spread evenly. The model learns where to look, and modern long-context models are good at pulling a single needle out of a haystack. But the more noise you pack around your fact — near-duplicates, unrelated docs, chunks that <em>look</em> relevant but aren't — the more likely the model is to weight the wrong thing. Dump the entire wiki into the prompt and you're slicing the pie into a hundred thousand crumbs, and the fact you needed gets one of them.</p>
<p>The result: the model gets distracted, skips the data you gave it, and starts making things up. In other words — hallucination.</p>
<img src="https://cdn.hashnode.com/uploads/covers/637e21bc38f6e7d3eab3f6ee/add03899-866f-4668-8943-5ab918839648.gif" alt="" style="display:block;margin:0 auto" />

<h2>RAG as an attention optimizer</h2>
<p>This is the real purpose of RAG. <strong>RAG is not just a search engine. RAG is a compute-optimization layer for self-attention.</strong></p>
<pre><code class="language-mermaid">flowchart LR
    A["All company data&lt;br/&gt;(millions of documents)"] --&gt; B["Cheap compute layer&lt;br/&gt;(vector search filters for relevance)"]
    B --&gt; C["Intensive compute layer&lt;br/&gt;(LLM self-attention runs only on the top 5 chunks)"]
</code></pre>
<p>It uses a cheap computation — vector search, keyword search, metadata filters — to pick out the handful of chunks that matter, and only that handful goes into the expensive computation: the LLM's attention. You pay O(N²) on five paragraphs instead of on the whole wiki.</p>
<p>Attention isn't the <em>only</em> reason RAG exists, and I'd be cheating if I pretended it was:</p>
<ul>
<li><p><strong>Fresh data.</strong> The model's weights stopped learning at its training cutoff. Retrieval is how it gets to see yesterday's incident report.</p>
</li>
<li><p><strong>Access control.</strong> In a multi-tenant product, the retrieval layer is also where you enforce which documents a given user or tenant is allowed to see. The model can't leak what it was never shown.</p>
</li>
<li><p><strong>Citations.</strong> If the answer came from chunk #3, you can show the reader chunk #3. Try doing that with something the model "remembers".</p>
</li>
<li><p><strong>Cost and latency.</strong> Every token in the prompt costs money and milliseconds. Fewer, better tokens win on both.</p>
</li>
</ul>
<p>But the attention argument is the one that explains why the pipeline has to exist <em>at all</em>, even in a world of 2-million-token context windows. Bigger windows don't kill RAG. They just move the line where the cheap filter starts paying for itself.</p>
<hr />
<p><strong>Further reading:</strong> <a href="https://arxiv.org/abs/1706.03762">Attention Is All You Need</a> (the original transformer paper) · <a href="https://arxiv.org/abs/2307.03172">Lost in the Middle</a> · <a href="https://arxiv.org/abs/2205.14135">FlashAttention</a></p>
]]></content:encoded></item></channel></rss>