<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Ashish Pandey</title>
<link>https://ashishpandey.co.in/</link>
<atom:link href="https://ashishpandey.co.in/index.xml" rel="self" type="application/rss+xml"/>
<description>Learning data and AI in public — one idea at a time, explained the way I wish someone had explained it to me.</description>
<generator>quarto-1.10.18</generator>
<lastBuildDate>Sat, 19 Sep 2026 18:30:00 GMT</lastBuildDate>
<item>
  <title>The KV cache: why generation is fast at all</title>
  <dc:creator>Ashish Pandey</dc:creator>
  <link>https://ashishpandey.co.in/posts/2026-09-20-kv-cache/</link>
  <description><![CDATA[ 





<div class="intuition">
<p>The model stops redoing work it has already done. Nothing is approximated — it just keeps notes in the margin instead of re-reading the whole chart every time.</p>
</div>
<section id="doing-it-all-again-every-time" class="level2">
<h2 class="anchored" data-anchor-id="doing-it-all-again-every-time">Doing it all again, every time</h2>
<p>Generation is one word at a time, and each word attends to everything before it.</p>
<p>To write word 500, the model considers all 499 words before it. To write word 501, it does all 500 again. Then 501 again for the next one. The same arithmetic, over and over.</p>
<div class="intuition">
<p>A doctor writing a long note, who re-reads the patient’s entire chart before adding each new line. Twenty lines in, she’s read the chart twenty times. Or — she could jot the key facts in the margin as she goes, and only read the new line.</p>
</div>
</section>
<section id="what-the-trick-is-worth" class="level2">
<h2 class="anchored" data-anchor-id="what-the-trick-is-worth">What the trick is worth</h2>
<p>I measured both approaches on the same model, generating the same text:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Approach</th>
<th>Time taken</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Re-reading everything each time (no cache)</td>
<td>2.97 seconds</td>
</tr>
<tr class="even">
<td>Keeping notes (KV cache)</td>
<td><strong>0.18 seconds</strong></td>
</tr>
</tbody>
</table>
<p><strong>Sixteen times faster — and the output was word-for-word identical.</strong> Nothing was approximated or skipped. It simply stopped doing the same work twice.</p>
<p>That’s the rare kind of optimisation: free. The bill arrives as memory.</p>
</section>
<section id="why-k-and-v-but-never-q" class="level2">
<h2 class="anchored" data-anchor-id="why-k-and-v-but-never-q">Why K and V, but never Q</h2>
<p>From <a href="../../posts/2026-09-20-attention/index.html">the attention chapter</a>, every token produces a query, a key and a value. When the model generates a new token:</p>
<ul>
<li>The new token’s <strong>query</strong> is compared against the keys of <em>every</em> token so far.</li>
<li>Those keys and values <strong>have not changed</strong> since they were computed, and never will.</li>
<li>The new query, once used, is thrown away. It’s never needed again.</li>
</ul>
<p>So you cache K and V, not Q. That’s the entire idea, and it’s why it’s called the KV cache.</p>
<p>Without it, generating <img src="https://latex.codecogs.com/png.latex?n"> tokens costs <img src="https://latex.codecogs.com/png.latex?O(n%5E3)"> work overall. With it, <img src="https://latex.codecogs.com/png.latex?O(n%5E2)"> — each step computes one new K and V and attends over the stored ones.</p>
</section>
<section id="it-grows-with-every-token" class="level2">
<h2 class="anchored" data-anchor-id="it-grows-with-every-token">It grows with every token</h2>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Bcache%20bytes%7D%20%5Capprox%202%20%5Ctimes%20L%20%5Ctimes%20H_%7Bkv%7D%20%5Ctimes%20d_%7Bhead%7D%20%5Ctimes%20n%20%5Ctimes%20%5Ctext%7Bbytes%7D%0A"></p>
<p>where the 2 is K and V, <img src="https://latex.codecogs.com/png.latex?L"> is layers, <img src="https://latex.codecogs.com/png.latex?H_%7Bkv%7D"> is key/value heads, <img src="https://latex.codecogs.com/png.latex?d_%7Bhead%7D"> is head dimension, and <img src="https://latex.codecogs.com/png.latex?n"> is tokens so far. It grows <strong>linearly with every token generated</strong>, for <strong>every concurrent user</strong>.</p>
<p>Play with it — these defaults are roughly an 8B-class model in fp16:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb1" data-startfrom="68" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 67;"><span id="cb1-68">viewof layers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">step</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Layers"</span>})</span>
<span id="cb1-69">viewof kvheads <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">step</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"KV heads (8 = GQA, 32 = full MHA)"</span>})</span>
<span id="cb1-70">viewof headdim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">step</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Head dimension"</span>})</span>
<span id="cb1-71">viewof ctx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128000</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">step</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Context length (tokens)"</span>})</span>
<span id="cb1-72">viewof bits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">radio</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cache precision (bits)"</span>})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-3" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-4" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-5" data-nodetype="declaration">

</div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb2" data-startfrom="77" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 76;"><span id="cb2-77">perUserGB <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> layers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> kvheads <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> headdim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ctx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (bits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e9</span></span>
<span id="cb2-78">freeGB <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// an 80GB GPU, minus ~16GB of model weights</span></span>
<span id="cb2-79">users <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(freeGB <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> perUserGB))</span>
<span id="cb2-80"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`&lt;div style="display:flex;gap:2.5rem;flex-wrap:wrap;margin:.6rem 0 1.2rem;font-size:1.05rem"&gt;</span></span>
<span id="cb2-81"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;div&gt;&lt;div style="opacity:.65;font-size:.85rem;text-transform:uppercase;letter-spacing:.04em"&gt;Cache per user&lt;/div&gt;</span></span>
<span id="cb2-82"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;div style="font-size:2rem;font-weight:700"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>perUserGB<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> GB&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb2-83"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;div&gt;&lt;div style="opacity:.65;font-size:.85rem;text-transform:uppercase;letter-spacing:.04em"&gt;Users on one 80GB GPU&lt;/div&gt;</span></span>
<span id="cb2-84"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;div style="font-size:2rem;font-weight:700;color:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>users <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e76f51"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2a9d8f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>users<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;</span></span>
<span id="cb2-85"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;div style="opacity:.6;font-size:.85rem"&gt;after ~16GB of weights&lt;/div&gt;&lt;/div&gt;</span></span>
<span id="cb2-86"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;`</span></span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-3" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-4" data-nodetype="expression">

</div>
</div>
</div>
</div>
<p>Push the context to 128k and watch how few users fit. <strong>This is why long context is expensive to serve, not just to compute.</strong></p>
</section>
<section id="two-phases-two-bottlenecks" class="level2">
<h2 class="anchored" data-anchor-id="two-phases-two-bottlenecks">Two phases, two bottlenecks</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Phase</th>
<th>What happens</th>
<th>Limited by</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Prefill</strong></td>
<td>The whole prompt is processed at once, filling the cache</td>
<td><strong>Compute</strong> — it’s one big matrix multiply</td>
</tr>
<tr class="even">
<td><strong>Decode</strong></td>
<td>One token at a time, each step dragging the entire cache through memory</td>
<td><strong>Memory bandwidth</strong></td>
</tr>
</tbody>
</table>
<p>This is the single most useful mental model for inference performance. Time-to-first-token is a prefill problem; tokens-per-second after that is a bandwidth problem. Adding compute rarely fixes slow decoding — during decode a GPU spends most of its time waiting on memory, not calculating.</p>
</section>
<section id="how-the-cache-is-made-smaller" class="level2">
<h2 class="anchored" data-anchor-id="how-the-cache-is-made-smaller">How the cache is made smaller</h2>
<ul>
<li><strong>GQA / MQA.</strong> Several query heads share one set of keys and values. Multi-query attention is the extreme (one KV head). Grouped-query attention is the common middle ground — it’s why the slider above defaults to 8 KV heads rather than 32, and it shrinks the cache by that same 4×.</li>
<li><strong>Sliding-window attention.</strong> Only keep the last <img src="https://latex.codecogs.com/png.latex?w"> tokens’ keys and values, capping the cache no matter how long the conversation runs. You trade away exact long-range recall.</li>
<li><strong>Quantised cache.</strong> Store K and V in 8 bits instead of 16. Halves the memory for a small quality cost — try the toggle above.</li>
<li><strong>PagedAttention (vLLM).</strong> Don’t shrink the cache; stop wasting it. Allocate in small pages like virtual memory instead of one contiguous block per request, and fragmentation mostly disappears.</li>
<li><strong>Prefix sharing.</strong> Many requests start with the same system prompt. Compute that prefix’s cache once and share it across requests.</li>
</ul>
</section>
<section id="the-same-trick-one-level-up" class="level2">
<h2 class="anchored" data-anchor-id="the-same-trick-one-level-up">The same trick, one level up</h2>
<p><strong>Prompt caching</strong> in a hosted API is this idea at the product layer: a long, unchanging system prompt gets its cache computed once and reused, so you’re billed a fraction for those tokens on later calls. Same principle — stop redoing old work — different altitude.</p>
</section>
<section id="where-it-breaks" class="level2">
<h2 class="anchored" data-anchor-id="where-it-breaks">Where it breaks</h2>
<ul>
<li><strong>Memory, not compute, is usually your serving limit.</strong> Batch size is capped by cache size. People buy faster GPUs when they needed more memory or a smaller cache.</li>
<li><strong>The cache is per-conversation.</strong> A long chat history is re-billed and re-carried on every turn. Trim aggressively.</li>
<li><strong>Change anything early in the prompt and the cache dies.</strong> Cached prefixes are only valid while the prefix is byte-identical. A timestamp at the top of a system prompt quietly destroys every prefix hit.</li>
<li><strong>Sliding windows forget.</strong> If a fact left the window, no amount of attention brings it back. It’s not “compressed” — it’s gone.</li>
</ul>
</section>
<section id="whiteboard-check" class="level2">
<h2 class="anchored" data-anchor-id="whiteboard-check">Whiteboard check</h2>
<p>Marker in hand, someone watching. Could you get through these without notes?</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>What exactly is stored in a KV cache, and why not the queries?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>The key and value vectors for every past token, at every layer and every KV head. Past keys and values are reused at every future step and never change. A query is used once, at the step that created it, and is then irrelevant.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Estimate the KV cache for a 32-layer model, 8 KV heads, head dim 128, fp16, at 8k context.
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p><img src="https://latex.codecogs.com/png.latex?2%20%5Ctimes%2032%20%5Ctimes%208%20%5Ctimes%20128%20%5Ctimes%208000%20%5Ctimes%202"> bytes ≈ <strong>1.05 GB per request</strong>. On an 80GB GPU with ~16GB of weights, that’s around 60 concurrent users before memory runs out — which is why batch size, not FLOPs, usually sets your throughput.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Your time-to-first-token is fine but tokens-per-second is poor. What do you investigate?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>That’s decode, so suspect memory bandwidth, not compute: cache size per request, batch size, GQA vs MHA, cache precision, and whether paged attention is in use. Bigger GPUs with the same bandwidth won’t help; larger batches (amortising weight reads) usually will.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Does a KV cache change the model’s output?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>No.&nbsp;It’s exact — identical tokens, identical probabilities, just without recomputation. That’s what separates it from sliding-window or quantised variants, which <em>do</em> trade quality for memory.</p>
</div>
</div>
</div>
</section>
<section id="tldr" class="level2">
<h2 class="anchored" data-anchor-id="tldr">TL;DR</h2>
<ul>
<li>Cache the <strong>keys and values</strong> of past tokens; queries are single-use.</li>
<li><strong>16× faster, identical output.</strong> Pure win on compute, paid for in memory.</li>
<li>Cache size grows <strong>linearly</strong> with tokens and users: <img src="https://latex.codecogs.com/png.latex?2%20%5Ctimes%20L%20%5Ctimes%20H_%7Bkv%7D%20%5Ctimes%20d_%7Bhead%7D%20%5Ctimes%20n">.</li>
<li><strong>Prefill is compute-bound; decode is bandwidth-bound.</strong> Diagnose them separately.</li>
<li>GQA, sliding windows, quantisation and paged attention all exist to fight the same bill.</li>
</ul>
<div class="chapter-nav">
<p><a href="../../posts/2026-09-20-attention/index.html">← Attention</a></p>
</div>


</section>

 ]]></description>
  <category>kv-cache</category>
  <category>inference</category>
  <category>serving</category>
  <guid>https://ashishpandey.co.in/posts/2026-09-20-kv-cache/</guid>
  <pubDate>Sat, 19 Sep 2026 18:30:00 GMT</pubDate>
</item>
<item>
  <title>Attention: how a model decides what matters</title>
  <dc:creator>Ashish Pandey</dc:creator>
  <link>https://ashishpandey.co.in/posts/2026-09-20-attention/</link>
  <description><![CDATA[ 





<div class="intuition">
<p>Attention repaints every word using the words around it, taking most of its paint from the words that matter most.</p>
</div>
<section id="the-one-job" class="level2">
<h2 class="anchored" data-anchor-id="the-one-job">The one job</h2>
<p>Underneath every chatbot, assistant and clever demo, there is a single operation:</p>
<blockquote class="blockquote">
<p>Given the words so far, guess what comes next.</p>
</blockquote>
<p>That’s it. Everything else is that trick, repeated: write a word, add it to the sentence, guess again.</p>
<p>And it doesn’t give one answer. It scores <strong>every</strong> word it knows — tens of thousands of them. I gave a model <em>“The patient has”</em>:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Next word</th>
<th>Confidence</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>been</td>
<td>20.1%</td>
</tr>
<tr class="even">
<td>a</td>
<td>8.3%</td>
</tr>
<tr class="odd">
<td>not</td>
<td>4.6%</td>
</tr>
<tr class="even">
<td>no</td>
<td>3.9%</td>
</tr>
<tr class="odd">
<td>had</td>
<td>2.2%</td>
</tr>
</tbody>
</table>
<p>Look carefully: there’s no medicine in that list. No fever, no diabetes, no chest pain. The model knows *“The patient has ___“* needs a verb. It has no idea it’s standing in a hospital.</p>
<p>To make a <em>good</em> guess, it needs one more thing: a way to work out which of the earlier words actually matter right now. That’s attention.</p>
</section>
<section id="mixing-paint" class="level2">
<h2 class="anchored" data-anchor-id="mixing-paint">Mixing paint</h2>
<p>Take this half-finished sentence:</p>
<blockquote class="blockquote">
<p>Patient has severe chest ___</p>
</blockquote>
<p>Which earlier words matter for the next word?</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Earlier word</th>
<th>How much it matters</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>severe</td>
<td>85%</td>
</tr>
<tr class="even">
<td>patient</td>
<td>10%</td>
</tr>
<tr class="odd">
<td>has</td>
<td>2%</td>
</tr>
</tbody>
</table>
<div class="intuition">
<p>Every word is a colour. The word <em>chest</em> starts out plain grey — on its own it could be a chest of drawers, a treasure chest, or a chest X-ray. The word <em>severe</em> is red: urgent, intense. Attention <strong>mixes the paint</strong>, stirring 85% of severe’s red into chest’s grey. So <em>chest</em> comes out reddish.</p>
</div>
<p>Drag the slider and watch the word change colour — and change meaning:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb1" data-startfrom="63" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 62;"><span id="cb1-63">viewof att <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">step</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"attention paid to “severe”"</span>})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb2" data-startfrom="69" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 68;"><span id="cb2-69">mixed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">interpolateRgb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#9aa0a6"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e63946"</span>)(att <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb2-70"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`&lt;div style="display:flex;align-items:center;gap:.9rem;flex-wrap:wrap;margin:.5rem 0 1.2rem"&gt;</span></span>
<span id="cb2-71"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;div style="background:#9aa0a6;color:#fff;padding:.55rem 1rem;border-radius:8px;font-weight:600"&gt;chest&lt;/div&gt;</span></span>
<span id="cb2-72"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;div style="font-size:1.3rem;opacity:.6"&gt;+&lt;/div&gt;</span></span>
<span id="cb2-73"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;div style="background:#e63946;color:#fff;padding:.55rem 1rem;border-radius:8px;font-weight:600"&gt;severe · </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>att<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">%&lt;/div&gt;</span></span>
<span id="cb2-74"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;div style="font-size:1.3rem;opacity:.6"&gt;→&lt;/div&gt;</span></span>
<span id="cb2-75"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;div style="background:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>mixed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">;color:#fff;padding:.55rem 1rem;border-radius:8px;font-weight:600"&gt;chest&lt;/div&gt;</span></span>
<span id="cb2-76"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;div style="font-size:1.05rem"&gt;next word likely &lt;b&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>att <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"“pain”"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"“of drawers”"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/b&gt;&lt;/div&gt;</span></span>
<span id="cb2-77"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/div&gt;`</span></span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-2" data-nodetype="expression">

</div>
</div>
</div>
</div>
<p>In “chest of drawers”, <em>drawers</em> is brown, so chest comes out brownish instead. <strong>Same word in, different colour out, depending on its neighbours.</strong></p>
<p>And here’s the part people miss: when the model makes its guess, it doesn’t look at the word. It looks at <strong>the colour the word ended up as</strong>. Reddish chest leads to <em>pain</em>. Brownish chest leads to <em>drawers</em>.</p>
</section>
<section id="queries-keys-and-values" class="level2">
<h2 class="anchored" data-anchor-id="queries-keys-and-values">Queries, keys and values</h2>
<p>Each word produces three different vectors, by multiplying its embedding with three learned weight matrices:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th></th>
<th>Question it answers</th>
<th>In the analogy</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Query</strong> (Q)</td>
<td>“What am I looking for?”</td>
<td><em>chest</em> asking: which word tells me what kind of chest I am?</td>
</tr>
<tr class="even">
<td><strong>Key</strong> (K)</td>
<td>“What do I offer?”</td>
<td><em>severe</em> advertising: I’m an intensity word</td>
</tr>
<tr class="odd">
<td><strong>Value</strong> (V)</td>
<td>“What do I pass on?”</td>
<td>the actual red paint that gets mixed in</td>
</tr>
</tbody>
</table>
<p>The mechanism is three steps:</p>
<ol type="1">
<li><strong>Score.</strong> Compare every query against every key with a dot product. High score = this word is relevant to me.</li>
<li><strong>Normalise.</strong> Divide by <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bd_k%7D">, then softmax. That turns raw scores into the percentages you saw: 85%, 10%, 2%. They always sum to 100%.</li>
<li><strong>Mix.</strong> Take a weighted average of the <strong>values</strong> using those percentages. The result replaces the word’s representation.</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BAttention%7D(Q,K,V)%20=%20%5Coperatorname%7Bsoftmax%7D%5C!%5Cleft(%5Cfrac%7BQK%5E%7B%5Ctop%7D%7D%7B%5Csqrt%7Bd_k%7D%7D%5Cright)V%0A"></p>
<p>That’s the whole formula, and you already know every piece of it. The paint being mixed is <strong>V</strong>. The percentages come from <strong>Q·K</strong>.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Why divide by <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bd_k%7D">?
</div>
</div>
<div class="callout-body-container callout-body">
<p>Dot products of long vectors grow large. Feed a large number into softmax and it saturates: one word gets 99.9% and the rest get nothing, so gradients vanish and learning stalls. Dividing by <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bd_k%7D"> keeps the scores in a sane range. It’s a numerical fix, not a deep idea — but interviewers love asking about it.</p>
</div>
</div>
</section>
<section id="it-cannot-look-forward" class="level2">
<h2 class="anchored" data-anchor-id="it-cannot-look-forward">It cannot look forward</h2>
<p>While generating text, a word may only attend to words <strong>before</strong> it. Future positions are set to <img src="https://latex.codecogs.com/png.latex?-%5Cinfty"> before the softmax, so they get exactly 0% of the paint.</p>
<p>Without that mask, predicting the next word would be trivial: the model would have already seen the answer. This is the <strong>causal mask</strong>, and it’s the difference between GPT-style decoders and BERT-style encoders (which <em>do</em> see both directions, which is why BERT is good at understanding text but can’t generate it well).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb3" data-startfrom="134" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 133;"><span id="cb3-134">toks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Patient"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"has"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"severe"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chest"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"___"</span>]</span>
<span id="cb3-135">weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb3-136">  [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.00</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-137">  [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.30</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.70</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-138">  [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-139">  [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.60</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.28</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-140">  [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb3-141">]</span>
<span id="cb3-142">cells <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> weights<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">flatMap</span>((row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> i) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>((w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> j) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> ({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">q</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> toks[i]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">k</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> toks[j]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">masked</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> i})))</span>
<span id="cb3-143">Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>({</span>
<span id="cb3-144">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginLeft</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginBottom</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">height</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">width</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">480</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-145">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"attends to →"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">domain</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> toks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tickRotate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-146">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word doing the attending"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">domain</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> toks}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-147">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">color</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">scheme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Greens"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">domain</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">legend</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"share of the paint"</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-148">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb3-149">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cell</span>(cells<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">masked</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"k"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"w"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">inset</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-150">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cell</span>(cells<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">masked</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"k"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#f1f1f1"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">inset</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-151">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(cells<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">masked</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"k"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toFixed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fontSize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-152">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(cells<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">masked</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"k"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"—"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#bbb"</span>})</span>
<span id="cb3-153">  ]</span>
<span id="cb3-154">})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-3-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-3-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-3-3" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-3-4" data-nodetype="expression">

</div>
</div>
</div>
</div>
<p>Read a row: it’s one word choosing where to get its paint. The grey cells are the future, blocked out. Notice the last row — the word being predicted takes 85% of its paint from <em>severe</em>, which is exactly the table from earlier.</p>
<p><em>(Weights here are illustrative, to show the shape of a real attention matrix.)</em></p>
</section>
<section id="many-heads-not-one" class="level2">
<h2 class="anchored" data-anchor-id="many-heads-not-one">Many heads, not one</h2>
<p>One attention head mixes paint one way. Real models run <strong>dozens in parallel</strong> — each with its own Q, K and V matrices, each free to learn a different kind of relationship. One head might track which adjective modifies which noun; another, who <em>she</em> refers to; another, which bracket closes which.</p>
<p>Each head works in a smaller space (<img src="https://latex.codecogs.com/png.latex?d_%7B%5Ctext%7Bmodel%7D%7D/h">), their outputs are concatenated, and a final matrix mixes them back together. Same total compute, many more relationships captured.</p>
</section>
<section id="attention-has-no-sense-of-order" class="level2">
<h2 class="anchored" data-anchor-id="attention-has-no-sense-of-order">Attention has no sense of order</h2>
<p>Look at the formula again: it’s a weighted sum. Shuffle the words and you’d get the same answer. Attention on its own has <strong>no idea about word order</strong> — “dog bites man” and “man bites dog” would be identical.</p>
<p>Order is added separately, as <strong>positional information</strong> baked into the vectors (modern models mostly use RoPE, which rotates Q and K by an angle that depends on position). That’s its own chapter.</p>
</section>
<section id="why-long-context-gets-expensive" class="level2">
<h2 class="anchored" data-anchor-id="why-long-context-gets-expensive">Why long context gets expensive</h2>
<p>Every word attends to every other word. <img src="https://latex.codecogs.com/png.latex?n"> words means <img src="https://latex.codecogs.com/png.latex?n%5E2"> scores.</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Tokens</th>
<th>Attention scores</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>1,000</td>
<td>1 million</td>
</tr>
<tr class="even">
<td>10,000</td>
<td>100 million</td>
</tr>
<tr class="odd">
<td>100,000</td>
<td><strong>10 billion</strong></td>
</tr>
</tbody>
</table>
<p>Double the text, quadruple the work. This one line explains most of what long-context pricing looks like, and most of the research into making attention cheaper (FlashAttention makes it memory-efficient without changing the maths; sliding-window and sparse attention change the maths to look at less).</p>
</section>
<section id="where-it-breaks" class="level2">
<h2 class="anchored" data-anchor-id="where-it-breaks">Where it breaks</h2>
<ul>
<li><strong>Attention weights are not explanations.</strong> A high weight shows where the paint came from, not why the model answered as it did. Treating attention maps as interpretability is a known trap — <a href="https://arxiv.org/abs/1902.10186">Jain &amp; Wallace (2019)</a> made this case sharply.</li>
<li><strong>The middle gets lost.</strong> With long inputs, models reliably use the beginning and end more than the middle. Put what matters at the edges of the prompt.</li>
<li><strong>More context isn’t more understanding.</strong> Attention dilutes: with 100k tokens competing, each relevant one gets a thinner slice of the paint.</li>
<li><strong>The paint analogy has a limit.</strong> Mixing colours is lossy and commutative; attention operates in hundreds of dimensions where “mixtures” stay separable. It’s a picture of the weighting, not of the geometry.</li>
</ul>
</section>
<section id="whiteboard-check" class="level2">
<h2 class="anchored" data-anchor-id="whiteboard-check">Whiteboard check</h2>
<p>Marker in hand, someone watching. Could you get through these without notes?</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Explain self-attention to someone non-technical, in under a minute.
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Every word starts with a rough meaning. Attention lets each word look at the others and ask “which of you matters to me?”, then blends in a bit of each one in proportion to the answer. “Chest” next to “severe” ends up meaning something different from “chest” next to “drawers”. The model then predicts from the blended meaning, not the original word.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Why scale by the square root of d_k?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Dot products grow with dimension; large inputs push softmax into saturation, where one position takes almost all the weight and gradients vanish. Scaling by <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bd_k%7D"> keeps the variance of the scores roughly constant so training stays stable.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>What breaks if you remove the causal mask during training of a decoder model?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>The model can see the token it’s meant to predict, so loss drops to near zero and it learns nothing useful. At inference the future doesn’t exist, so the distribution it faces is completely different and generation falls apart.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-5-contents" aria-controls="callout-5" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Why multiple heads instead of one big head?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-5" class="callout-5-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>A single softmax-weighted average forces one relationship per position. Multiple heads in lower-dimensional subspaces let the model attend to several kinds of relationship at once — syntactic, coreference, positional — at the same total cost.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-6-contents" aria-controls="callout-6" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Where does the quadratic cost come from, and how do people avoid it?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-6" class="callout-6-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Every query is scored against every key: <img src="https://latex.codecogs.com/png.latex?O(n%5E2%20d)"> time and, naively, <img src="https://latex.codecogs.com/png.latex?O(n%5E2)"> memory. FlashAttention keeps the exact result while avoiding materialising the full matrix; sliding window, sparse and linear attention approximate by restricting or reformulating who can attend to whom.</p>
</div>
</div>
</div>
</section>
<section id="tldr" class="level2">
<h2 class="anchored" data-anchor-id="tldr">TL;DR</h2>
<ul>
<li>Attention = <strong>weighted averaging</strong>. Queries and keys set the weights, values are what gets mixed.</li>
<li>Softmax makes weights sum to 1; <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bd_k%7D"> keeps them numerically sane.</li>
<li>The <strong>causal mask</strong> stops the model seeing the future — that’s what makes generation possible.</li>
<li><strong>Many heads</strong> capture many kinds of relationship at once.</li>
<li>Cost is <strong>quadratic</strong> in sequence length. That’s the whole long-context story.</li>
</ul>
<div class="chapter-nav">
<p><a href="../../posts/2026-09-20-tokens/index.html">← Tokens</a> <a href="../../posts/2026-09-20-kv-cache/index.html">The KV cache →</a></p>
</div>


</section>

 ]]></description>
  <category>attention</category>
  <category>transformers</category>
  <guid>https://ashishpandey.co.in/posts/2026-09-20-attention/</guid>
  <pubDate>Sat, 19 Sep 2026 18:30:00 GMT</pubDate>
</item>
<item>
  <title>Tokens: how text becomes numbers</title>
  <dc:creator>Ashish Pandey</dc:creator>
  <link>https://ashishpandey.co.in/posts/2026-09-20-tokens/</link>
  <description><![CDATA[ 





<div class="intuition">
<p>A machine cannot read words. Text is chopped into pieces, and each piece becomes a number. Everything downstream — cost, memory, even accuracy — is decided by how well that chopping goes.</p>
</div>
<section id="a-box-of-lego-bricks" class="level2">
<h2 class="anchored" data-anchor-id="a-box-of-lego-bricks">A box of LEGO bricks</h2>
<p>Imagine you can only speak using a fixed box of LEGO bricks. A common word like <em>weather</em> has its own brick: you just grab it. An unusual word has no brick of its own, so you build it out of several smaller ones.</p>
<p>That box is the model’s <strong>vocabulary</strong>, typically 32,000 to 200,000 bricks. Every piece of text you send gets built out of those bricks, and each brick has a number.</p>
<p>Here’s what actually happened when I fed two words into a well-known model:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Word</th>
<th>Pieces</th>
<th></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>weather</code></td>
<td><code>weather</code></td>
<td>one piece</td>
</tr>
<tr class="even">
<td><code>hyperlipidemia</code></td>
<td><code>hyper</code> · <code>lip</code> · <code>id</code> · <code>emia</code></td>
<td>four pieces</td>
</tr>
</tbody>
</table>
<p>Notice something odd. A person would split that word as <em>hyper–lipid–emia</em>. The machine split it as <em>hyper–lip–id–emia</em>. That <code>lip</code> is the lip on your face — nothing to do with fats in the blood.</p>
<p><strong>The machine didn’t choose pieces that carry meaning. It chose pieces that are common on the internet.</strong> And the internet talks about lips far more than lipids.</p>
</section>
<section id="count-what-it-costs" class="level2">
<h2 class="anchored" data-anchor-id="count-what-it-costs">Count what it costs</h2>
<p>Specialist language shatters into more pieces than everyday language. I measured pieces per word on the same model, for two kinds of text:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb1" data-startfrom="43" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 42;"><span id="cb1-43">viewof words <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">step</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Length of the document (words)"</span>})</span>
<span id="cb1-44">viewof kind <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">radio</span>([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Everyday English"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Clinical notes"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Clinical notes"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kind of text"</span>})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-2" data-nodetype="declaration">

</div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb2" data-startfrom="49" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 48;"><span id="cb2-49">rates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Everyday English"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">general</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.08</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">medical</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.17</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Clinical notes"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">general</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.97</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">medical</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.37</span>}})</span>
<span id="cb2-50">gen <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(words <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> rates[kind]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">general</span>)</span>
<span id="cb2-51">med <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(words <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> rates[kind]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">medical</span>)</span>
<span id="cb2-52">Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>({</span>
<span id="cb2-53">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">height</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginLeft</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">110</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marginRight</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-54">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tokens the model actually processes →"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">domain</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(gen<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> med) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.15</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">grid</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-55">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-56">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">color</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">legend</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-57">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb2-58">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">barX</span>([{<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">m</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"General model"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> gen}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">m</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medical model"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> med}]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-59">      {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"v"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"General model"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e76f51"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2a9d8f"</span>})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-60">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>([{<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">m</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"General model"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> gen}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">m</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medical model"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> med}]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-61">      {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"v"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toLocaleString</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">dx</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">textAnchor</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"start"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fontWeight</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>})</span>
<span id="cb2-62">  ]</span>
<span id="cb2-63">})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-3" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-4" data-nodetype="expression">

</div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb3" data-startfrom="67" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 66;"><span id="cb3-67"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">md</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`A **</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>words<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">-word** </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toLowerCase</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> document costs **</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>gen<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toLocaleString</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> tokens**</span></span>
<span id="cb3-68"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">on a general model and **</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>med<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toLocaleString</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">** on a medical one —</span></span>
<span id="cb3-69"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">a **</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Math</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>((gen <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> med <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">%** difference for exactly the same text.`</span></span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-3" data-nodetype="expression">

</div>
</div>
</div>
</section>
<section id="nobody-designed-this-vocabulary" class="level2">
<h2 class="anchored" data-anchor-id="nobody-designed-this-vocabulary">Nobody designed this vocabulary</h2>
<p>The vocabulary is <em>learned</em> from a corpus, usually with <strong>byte-pair encoding (BPE)</strong>: start with individual characters, then repeatedly merge the most frequent adjacent pair into a new token. Do that 50,000 times and you have a vocabulary.</p>
<p>The consequence is the whole chapter in one line: <strong>frequency in the training corpus, not meaning, decides what gets its own token.</strong></p>
</section>
<section id="what-specialist-language-costs" class="level2">
<h2 class="anchored" data-anchor-id="what-specialist-language-costs">What specialist language costs</h2>
<p>Same two models, two kinds of text:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th></th>
<th>Everyday English</th>
<th>Clinical notes</th>
<th>Penalty for medicine</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>General model</td>
<td>1.08</td>
<td>1.97</td>
<td><strong>+82%</strong></td>
</tr>
<tr class="even">
<td>Medical model</td>
<td>1.17</td>
<td>1.37</td>
<td><strong>+17%</strong></td>
</tr>
</tbody>
</table>
<p>A medical-domain tokenizer cuts the cost of clinical text by about 30%, and pays for it by being slightly worse at everyday English. That’s not a flaw — it’s a choice about which world the model lives in.</p>
<pre><code>thrombocytopenia
  general model  →  th · rom · b · ocy · top · en · ia     (7 pieces)
  medical model  →  thrombocytopenia                        (1 piece)</code></pre>
<p>Seven fragments, or one whole idea. That’s a dangerously low platelet count — exactly the kind of thing you want a medical system to understand as one concept.</p>
<p>I looked inside both vocabularies. The general model reserved a slot for <code>harbaugh</code>, an American football coach’s surname. The medical model spent its slot on <code>leishmaniasis</code>, a tropical parasitic disease. Neither is wrong. But you wouldn’t want the football dictionary reading your blood report.</p>
</section>
<section id="the-three-bills" class="level2">
<h2 class="anchored" data-anchor-id="the-three-bills">The three bills</h2>
<p><strong>Money.</strong> These systems bill per token. A 400-word clinical note is ~790 tokens instead of ~430. Every note, forever.</p>
<p><strong>Memory.</strong> A model holds a fixed number of tokens at once. Whatever that limit is, you fit roughly half as much patient history when the text is specialist.</p>
<p><strong>Meaning.</strong> This is the one that hurts. The model isn’t reading “hyperlipidemia” — it’s reading four unrelated fragments and rebuilding the idea from scratch, every single time it appears.</p>
</section>
<section id="where-it-breaks" class="level2">
<h2 class="anchored" data-anchor-id="where-it-breaks">Where it breaks</h2>
<ul>
<li><strong>You can’t swap in a better tokenizer.</strong> The model’s entire learned memory is indexed by those token numbers. Change the numbering and every word points at the wrong meaning. A specialist vocabulary means retraining — a real project, not a setting.</li>
<li><strong>Numbers and code tokenize badly.</strong> <code>2026</code> may be one token or three; that’s part of why models fumble arithmetic.</li>
<li><strong>Non-English text pays a tax.</strong> Languages under-represented in the training corpus need far more tokens per word — sometimes 2–3×, for identical meaning. The same API call costs more in Hindi than in English.</li>
<li><strong>Token boundaries hide characters.</strong> “How many r’s in strawberry?” is hard partly because the model sees two or three chunks, not eleven letters.</li>
</ul>
</section>
<section id="whiteboard-check" class="level2">
<h2 class="anchored" data-anchor-id="whiteboard-check">Whiteboard check</h2>
<p>Marker in hand, someone watching. Could you get through these without notes?</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>What is BPE and why is it used instead of word-level or character-level tokenization?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Byte-pair encoding starts from characters and repeatedly merges the most frequent adjacent pair. Word-level vocabularies can’t handle unseen words and get enormous; character-level sequences are far too long for attention, which costs quadratic time. BPE sits in between: a fixed-size vocabulary, no out-of-vocabulary words (anything can be spelled out from smaller pieces), and sequences short enough to process.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Your RAG system over clinical documents is slower and pricier than expected. Where do you look first?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Tokens per word. Specialist text can nearly double the token count of the same content, which inflates embedding cost, retrieval chunk sizes and prompt length all at once. Measure the ratio on your own corpus before assuming the model or the retriever is at fault.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Would a domain-specific tokenizer fix a model that’s bad at medical text?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>It fixes the <em>representation</em> problem, not the <em>knowledge</em> problem, and only if you retrain or continue pretraining — the vocabulary can’t be swapped into an existing model. Expect a ~30% reduction in tokens for clinical text, and slightly worse performance on general text.</p>
</div>
</div>
</div>
</section>
<section id="tldr" class="level2">
<h2 class="anchored" data-anchor-id="tldr">TL;DR</h2>
<ul>
<li>Text becomes <strong>pieces</strong>, and pieces become numbers. Pieces are chosen by <strong>frequency, not meaning</strong>.</li>
<li>Specialist language shatters worst: <strong>+82%</strong> tokens for clinical text on a general model.</li>
<li>A domain tokenizer cuts that to <strong>+17%</strong>, but it must be baked in from the start.</li>
<li>Cost, context limits and accuracy all trace back to this one step.</li>
</ul>
<div class="chapter-nav">
<p><a href="../../posts/2026-09-20-attention/index.html">Attention: how a model decides what matters →</a></p>
</div>


</section>

 ]]></description>
  <category>tokenization</category>
  <guid>https://ashishpandey.co.in/posts/2026-09-20-tokens/</guid>
  <pubDate>Sat, 19 Sep 2026 18:30:00 GMT</pubDate>
</item>
</channel>
</rss>
