<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abhishek Nalin on Medium]]></title>
        <description><![CDATA[Stories by Abhishek Nalin on Medium]]></description>
        <link>https://medium.com/@AbhishekNalin?source=rss-55fa6f5847bc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*snqmNygyx_KIj33rQWmA7g.png</url>
            <title>Stories by Abhishek Nalin on Medium</title>
            <link>https://medium.com/@AbhishekNalin?source=rss-55fa6f5847bc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 02:26:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@AbhishekNalin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Prefix Caching: How LLMs Avoid Recomputing the Same Prompt 1,000 Times]]></title>
            <link>https://medium.com/@AbhishekNalin/prefix-caching-how-llms-avoid-recomputing-the-same-prompt-1-000-times-50b525fedb1e?source=rss-55fa6f5847bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/50b525fedb1e</guid>
            <category><![CDATA[vllm]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[llm-inference]]></category>
            <category><![CDATA[caching]]></category>
            <dc:creator><![CDATA[Abhishek Nalin]]></dc:creator>
            <pubDate>Tue, 25 Aug 2026 14:59:31 GMT</pubDate>
            <atom:updated>2026-08-25T14:59:31.880Z</atom:updated>
            <content:encoded><![CDATA[<p>When you send a request to an LLM, your actual question is usually only one part of the prompt.</p><p>Before the model gets to your message, it may also need to process:</p><ul><li>the system prompt</li><li>developer instructions</li><li>tool definitions</li><li>few-shot examples</li><li>conversation history</li><li>retrieved documents or other context</li></ul><p>A large portion of this context is often identical across requests.</p><p>For example, imagine an application where every request starts with the same 500-word system prompt.</p><p>If 1,000 users make requests, the naive approach is to process those same 500 words 1,000 times.</p><p>That is a lot of repeated GPU work.</p><p>This is the problem <strong>prefix caching</strong> solves.</p><h3>What exactly gets cached?</h3><p>When a transformer processes tokens, the attention layers generate intermediate representations called <strong>keys and values</strong>.</p><p>These are stored in what is commonly called the <strong>KV cache</strong>.</p><p>The KV cache is normally associated with token generation. When the model has already processed:</p><pre>What is the capital of</pre><p>and generates the next token, it doesn’t recompute attention for all the previous tokens from scratch.</p><p>It reuses their cached keys and values.</p><p>The same idea can also be applied <strong>across requests</strong>.</p><p>Suppose every request starts with:</p><pre>You are a helpful customer-support assistant.<br>Always answer politely.<br>Never expose internal information.<br>Follow the refund policy below...</pre><p>Once the model has computed the KV state for this prefix, another request with the same beginning can reuse it.</p><p>Instead of processing:</p><pre>System prompt + user message</pre><p>from scratch, the model can effectively do:</p><pre>Cached system prompt + new user message</pre><p>Only the new tokens need fresh computation.</p><h3>Why keeping your system prompt stable matters</h3><p>This leads to an important practical insight.</p><p>The repeated, boring part of your prompt can be one of the most expensive parts of serving your application.</p><p>Imagine you dynamically modify your system prompt on every request:</p><pre>Request 1:<br>You are a helpful assistant. Current request ID: 12345...<br><br>Request 2:<br>You are a helpful assistant. Current request ID: 12346...</pre><p>Even though these prompts are almost identical, introducing dynamic information early in the prompt can reduce how much cached computation can be reused.</p><p>A better structure is:</p><pre>[Stable system instructions]<br>[Stable tool definitions]<br>[Stable examples]<br>[Dynamic request-specific information]<br>[User message]</pre><p>Put the most reusable information toward the beginning.</p><p>Put highly dynamic information later.</p><p>That gives the serving system a much larger common prefix to reuse.</p><h3>Why a simple hash map isn’t enough</h3><p>At first, prefix caching sounds easy.</p><p>You could imagine storing:</p><pre>hash(prompt) → KV cache</pre><p>If the exact same prompt appears again, look it up and reuse the result.</p><p>This works for identical prompts.</p><p>But real workloads are more interesting.</p><p>Consider these two requests:</p><pre>A B C D E F</pre><p>and:</p><pre>A B C X Y Z</pre><p>They are not identical.</p><p>A hash of the complete sequences would treat them as completely unrelated.</p><p>But they share:</p><pre>A B C</pre><p>The model has already done the computation for those tokens.</p><p>Why should it do that work again?</p><h3>Store prefixes as a tree</h3><p>A better way to think about the cache is as a tree of token sequences.</p><p>For the previous example, the structure might look like:</p><pre>A<br>└── B<br>    └── C<br>        ├── D<br>        │   └── E<br>        │       └── F<br>        │<br>        └── X<br>            └── Y<br>                └── Z</pre><p>The two requests follow the same path through:</p><pre>A → B → C</pre><p>and then diverge.</p><p>The cached KV state for the shared path can be reused by both.</p><p>Only the divergent portions need fresh computation.</p><p>The nice property of this approach is that nobody has to decide beforehand which prompts belong to the same cache entry.</p><p>Sharing emerges naturally from the token sequences themselves.</p><p>The longer the common prefix, the more computation can be reused.</p><h3>Why this matters for real LLM applications</h3><p>Many production LLM workloads naturally contain huge shared prefixes.</p><p>System prompts — Every request to the same application may contain several thousand tokens of instructions.</p><p>Tool definitions — Agentic applications often send descriptions of dozens of tools with every request. These definitions usually change very rarely.</p><p>Few-shot examples — Applications may include several examples showing the model how to respond. Again, these examples are usually identical across requests.</p><p>Documents — Imagine thousands of users asking different questions about the same 100-page document.</p><p>Their requests might look like:</p><pre>[100-page document]<br><br>Question: What was revenue last year?</pre><p>and</p><pre>[100-page document]<br><br>Question: What are the biggest risks?</pre><p>The expensive document prefix is identical.</p><p>Only the question changes.</p><p>Prefix caching makes this kind of workload dramatically more efficient.</p><h3>Conversation history</h3><p>Chat applications also naturally form prefix trees.</p><p>Consider:</p><pre>System<br>↓<br>User message 1<br>↓<br>Assistant message 1<br>↓<br>User message 2</pre><p>A conversation branch can reuse the KV cache corresponding to everything that came before it.</p><p>This becomes particularly useful when conversations branch, agents retry, or multiple generations originate from the same context.</p><h3>But KV cache consumes GPU memory</h3><p>There is an obvious tradeoff.</p><p>KV cache is not free.</p><p>For large models and long contexts, it can consume significant amounts of GPU memory.</p><p>Eventually, the cache fills up.</p><p>Some cached prefixes have to be removed.</p><p>A naive system could delete entries randomly, but that would waste valuable reusable computation.</p><p>Instead, the cache can use an eviction policy similar to <strong>Least Recently Used (LRU)</strong>.</p><p>But because the cache is structured as a tree, eviction has an additional constraint.</p><p>Suppose the cache contains:</p><pre>A → B → C → D<br>        └→ X → Y</pre><p>If requests are still actively using:</p><pre>A → B → C</pre><p>you obviously cannot remove A or B.</p><p>Doing so would invalidate everything below them.</p><p>So eviction naturally happens from the <strong>leaves inward</strong>.</p><p>Old, unused branches disappear first while commonly shared prefixes remain cached.</p><p>In other words:</p><p><strong>keep the trunk, prune the cold branches.</strong></p><h3>The mental model</h3><p>A useful way to think about prefix caching is this:</p><p>Without prefix caching:</p><pre>Request 1: compute A B C D E<br>Request 2: compute A B C X Y<br>Request 3: compute A B C M N</pre><p>The model computes A B C three times.</p><p>With prefix caching:</p><p>Compute once:</p><pre>A → B → C</pre><pre>Then:</pre><pre>├── D → E<br>├── X → Y<br>└── M → N</pre><p>The shared computation happens once.</p><p>Everything after the divergence is computed independently.</p><h3>Why this becomes more important at scale</h3><p>For a single request, prefix caching may look like a small optimization.</p><p>At large scale, it can fundamentally change inference economics.</p><p>Suppose your application has:</p><ul><li>a 4,000-token system prompt</li><li>1,000 tokens of tool definitions</li><li>2,000 tokens of examples</li><li>a 100-token user query</li></ul><p>Without caching, every request requires processing roughly:</p><pre>7,100 input tokens</pre><p>If the first 7,000 tokens are reusable, subsequent requests may require fresh computation for only around:</p><pre>100 tokens</pre><p>The exact improvement depends heavily on the workload, model, cache hit rate, hardware and serving architecture, but the underlying opportunity is obvious.</p><p>Instead of paying repeatedly for identical context, you reuse previous computation.</p><p>This can improve:</p><ul><li><strong>time to first token</strong></li><li><strong>GPU utilization</strong></li><li><strong>throughput</strong></li><li><strong>serving cost</strong></li></ul><h3>Designing prompts for cacheability</h3><p>Prefix caching also changes how I think about prompt engineering.</p><p>Traditionally, prompt optimization focuses almost entirely on output quality.</p><p>At scale, prompt structure can also affect infrastructure efficiency.</p><p>A useful rule is:</p><blockquote><strong><em>Stable content first. Dynamic content later.</em></strong></blockquote><p>For example:</p><pre>GOOD<br><br><br>[System instructions]<br>[Tool definitions]<br>[Few-shot examples]<br>[Request-specific metadata]<br>[User message]</pre><p>instead of:</p><pre>LESS CACHE-FRIENDLY<br><br><br>[Timestamp]<br>[Request ID]<br>[User-specific metadata]<br>[System instructions]<br>[Tool definitions]<br>[User message]</pre><p>Both prompts might contain exactly the same information.</p><p>But their serving characteristics can be very different.</p><h3>The bigger idea</h3><p>Prefix caching is one of those infrastructure optimizations that feels obvious once you understand it.</p><p>LLM inference is expensive partly because prompts can be very large.</p><p>But large prompts are not necessarily unique.</p><p>In many applications, most of the prompt is identical across thousands or millions of requests.</p><p>So rather than asking:</p><blockquote><em>“How do we process this prompt faster?”</em></blockquote><p>a better question is:</p><blockquote><em>“Why are we processing the same tokens again at all?”</em></blockquote><p>That is the idea behind prefix caching.</p><p><strong>Compute shared prefixes once. Reuse them aggressively. Only pay for the tokens that are actually new.</strong></p><p>At small scale, that means lower latency.</p><p>At large scale, it can mean significantly less GPU work for the same amount of user traffic.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50b525fedb1e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>