<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Prompt-Cache on Tarragon</title><link>https://tarrragon.github.io/blog/tags/prompt-cache/</link><description>Recent content in Prompt-Cache on Tarragon</description><generator>Hugo -- gohugo.io</generator><language>zh-TW</language><copyright>Tarragon (CC BY 4.0)</copyright><lastBuildDate>Tue, 12 May 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://tarrragon.github.io/blog/tags/prompt-cache/index.xml" rel="self" type="application/rss+xml"/><item><title>4.18 Prompt caching 工程實務：cost / latency 最大槓桿</title><link>https://tarrragon.github.io/blog/llm/04-applications/prompt-caching-engineering/</link><pubDate>Tue, 12 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/llm/04-applications/prompt-caching-engineering/</guid><description>&lt;p>&lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/prompt-cache/" data-link-title="Prompt Cache" data-link-desc="重複出現的 prompt prefix 在推論伺服器或 LLM 服務端被 cache、後續 query 跳過 prefill、大幅降 cost 跟 TTFT">Prompt cache&lt;/a> 把重複 prefix 的計算結果在 LLM 服務端跨 request 持久化、後續 query 跳過 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/prefill/" data-link-title="Prefill" data-link-desc="Prompt 首次處理時的計算階段：把整段輸入跑過模型、產生 KV cache">prefill&lt;/a> 階段。Anthropic / OpenAI / Bedrock / Gemini 都列為 cost 跟 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/ttft/" data-link-title="TTFT" data-link-desc="Time To First Token：送出 prompt 到第一個 token 出現的等待時間">TTFT&lt;/a> 的最大單一槓桿 — 90% cost 折扣 + 顯著 latency 改善。本章把 prompt caching 的運作機制、設計原則、coding agent / long-context 場景的 pattern、常見 anti-pattern 拆成可操作的工程實務。&lt;/p>
&lt;p>注意三層 cache 概念的層次差異（&lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/prompt-cache/" data-link-title="Prompt Cache" data-link-desc="重複出現的 prompt prefix 在推論伺服器或 LLM 服務端被 cache、後續 query 跳過 prefill、大幅降 cost 跟 TTFT">prompt cache 卡片&lt;/a> 有完整對比表）：&lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache&lt;/a> 是單次推論內、過去 token 的 K/V 暫存（autoregressive 才省重算）；&lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/prefix-cache/" data-link-title="Prefix Cache" data-link-desc="把多個請求共用的前綴 prompt 的 KV cache 重用、省下重複 prefill 算力的優化、production 多用戶服務的常見設計">prefix cache&lt;/a> 是同一推論伺服器內跨 request 共用 KV cache；&lt;strong>prompt cache（本章聚焦）&lt;/strong> 是雲端 LLM API 商業 feature、跨 request 跨時間、有 TTL。三者不同層、要區分。&lt;/p>
&lt;h2 id="本章目標">本章目標&lt;/h2>
&lt;p>讀完本章後、你應該能：&lt;/p>
&lt;ol>
&lt;li>解釋 prompt cache 跟 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache&lt;/a> / &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/prefix-cache/" data-link-title="Prefix Cache" data-link-desc="把多個請求共用的前綴 prompt 的 KV cache 重用、省下重複 prefill 算力的優化、production 多用戶服務的常見設計">prefix cache&lt;/a> 的層次差異。&lt;/li>
&lt;li>對 coding agent / RAG / long-conversation 場景設計 cache breakpoint。&lt;/li>
&lt;li>估算自己應用開 prompt cache 的 cost / latency 收益。&lt;/li>
&lt;li>看到「cache 不命中」訊號時、能定位 anti-pattern 並修。&lt;/li>
&lt;/ol>
&lt;h2 id="prompt-cache-怎麼運作">Prompt cache 怎麼運作&lt;/h2>
&lt;p>LLM 推論的 prefill 階段對整個 prompt 算 KV cache、是長 prompt 的主要 latency 跟 compute 成本：&lt;/p>





&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">無 cache：
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl"> Request 1：[10K system prompt] + [tool schema 5K] + [user query 500] = 15.5K prefill
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl"> Request 2：[10K system prompt] + [tool schema 5K] + [user query 700] = 15.7K prefill
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl"> → 兩次都付 15K prefill 成本&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>開 prompt cache 後：&lt;/p></description><content:encoded><![CDATA[<p><a href="/blog/llm/knowledge-cards/prompt-cache/" data-link-title="Prompt Cache" data-link-desc="重複出現的 prompt prefix 在推論伺服器或 LLM 服務端被 cache、後續 query 跳過 prefill、大幅降 cost 跟 TTFT">Prompt cache</a> 把重複 prefix 的計算結果在 LLM 服務端跨 request 持久化、後續 query 跳過 <a href="/blog/llm/knowledge-cards/prefill/" data-link-title="Prefill" data-link-desc="Prompt 首次處理時的計算階段：把整段輸入跑過模型、產生 KV cache">prefill</a> 階段。Anthropic / OpenAI / Bedrock / Gemini 都列為 cost 跟 <a href="/blog/llm/knowledge-cards/ttft/" data-link-title="TTFT" data-link-desc="Time To First Token：送出 prompt 到第一個 token 出現的等待時間">TTFT</a> 的最大單一槓桿 — 90% cost 折扣 + 顯著 latency 改善。本章把 prompt caching 的運作機制、設計原則、coding agent / long-context 場景的 pattern、常見 anti-pattern 拆成可操作的工程實務。</p>
<p>注意三層 cache 概念的層次差異（<a href="/blog/llm/knowledge-cards/prompt-cache/" data-link-title="Prompt Cache" data-link-desc="重複出現的 prompt prefix 在推論伺服器或 LLM 服務端被 cache、後續 query 跳過 prefill、大幅降 cost 跟 TTFT">prompt cache 卡片</a> 有完整對比表）：<a href="/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache</a> 是單次推論內、過去 token 的 K/V 暫存（autoregressive 才省重算）；<a href="/blog/llm/knowledge-cards/prefix-cache/" data-link-title="Prefix Cache" data-link-desc="把多個請求共用的前綴 prompt 的 KV cache 重用、省下重複 prefill 算力的優化、production 多用戶服務的常見設計">prefix cache</a> 是同一推論伺服器內跨 request 共用 KV cache；<strong>prompt cache（本章聚焦）</strong> 是雲端 LLM API 商業 feature、跨 request 跨時間、有 TTL。三者不同層、要區分。</p>
<h2 id="本章目標">本章目標</h2>
<p>讀完本章後、你應該能：</p>
<ol>
<li>解釋 prompt cache 跟 <a href="/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache</a> / <a href="/blog/llm/knowledge-cards/prefix-cache/" data-link-title="Prefix Cache" data-link-desc="把多個請求共用的前綴 prompt 的 KV cache 重用、省下重複 prefill 算力的優化、production 多用戶服務的常見設計">prefix cache</a> 的層次差異。</li>
<li>對 coding agent / RAG / long-conversation 場景設計 cache breakpoint。</li>
<li>估算自己應用開 prompt cache 的 cost / latency 收益。</li>
<li>看到「cache 不命中」訊號時、能定位 anti-pattern 並修。</li>
</ol>
<h2 id="prompt-cache-怎麼運作">Prompt cache 怎麼運作</h2>
<p>LLM 推論的 prefill 階段對整個 prompt 算 KV cache、是長 prompt 的主要 latency 跟 compute 成本：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">無 cache：
</span></span><span class="line"><span class="ln">2</span><span class="cl">  Request 1：[10K system prompt] + [tool schema 5K] + [user query 500] = 15.5K prefill
</span></span><span class="line"><span class="ln">3</span><span class="cl">  Request 2：[10K system prompt] + [tool schema 5K] + [user query 700] = 15.7K prefill
</span></span><span class="line"><span class="ln">4</span><span class="cl">  → 兩次都付 15K prefill 成本</span></span></code></pre></div><p>開 prompt cache 後：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">Request 1：[10K system + 5K tool schema] | cache_control | + [user query 500]
</span></span><span class="line"><span class="ln">2</span><span class="cl">  → 算出 prefix 的 KV cache、寫進服務端 cache（付 1.25× cost）
</span></span><span class="line"><span class="ln">3</span><span class="cl">  → 後段 prefill 500 token
</span></span><span class="line"><span class="ln">4</span><span class="cl">
</span></span><span class="line"><span class="ln">5</span><span class="cl">Request 2（5 分鐘內）：[10K system + 5K tool schema] | + [user query 700]
</span></span><span class="line"><span class="ln">6</span><span class="cl">  → 服務端命中 cache、跳過 prefix 的 prefill（付 0.1× cost = 90% 折扣）
</span></span><span class="line"><span class="ln">7</span><span class="cl">  → 只 prefill 700 token
</span></span><span class="line"><span class="ln">8</span><span class="cl">  → TTFT 大幅降低</span></span></code></pre></div><p>關鍵運作細節：</p>
<ol>
<li><strong>Cache key = prefix 的 token sequence</strong>：完全相同的 token sequence 才命中、差一個 token 就 miss</li>
<li><strong>TTL（time-to-live）</strong>：cache 過一段時間（多數 5 min）自動失效、要 ext 1h 通常付額外 cost</li>
<li><strong>Write 比原價略貴、Read 大幅打折</strong>：Anthropic 模型 write 1.25×、read 0.1×；OpenAI 模型 read 0.5×</li>
<li><strong>Minimum cacheable size</strong>：通常 1K-4K token 起跳、短 prompt 不適合</li>
<li><strong>Cache 範圍</strong>：跨 request、跨 conversation、跨 session、但同一 model + 同一 region</li>
</ol>
<h2 id="cache-breakpoint-設計">Cache breakpoint 設計</h2>
<p>Anthropic 用 <code>cache_control</code> 標記顯式 breakpoint、OpenAI 用自動偵測。但設計原則一致：<strong>把不變的內容放 prefix、變動的放後面</strong>。</p>
<p>典型 coding agent 的 prompt 結構：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">[1. System prompt]：agent 角色、規則、輸出格式             ← 不變
</span></span><span class="line"><span class="ln">2</span><span class="cl">[2. Tool schema]：所有 tool 的 spec                       ← 不變（除非加新 tool）
</span></span><span class="line"><span class="ln">3</span><span class="cl">[3. Skill registry / playbook]：known recipes              ← 半變（偶爾更新）
</span></span><span class="line"><span class="ln">4</span><span class="cl">[4. Codebase context]：固定載入的核心檔案                  ← 半變
</span></span><span class="line"><span class="ln">5</span><span class="cl">       ↓ cache_control breakpoint ↑
</span></span><span class="line"><span class="ln">6</span><span class="cl">[5. Conversation history]：過去回合                       ← 變動
</span></span><span class="line"><span class="ln">7</span><span class="cl">[6. Current user query]：當前 query                       ← 變動
</span></span><span class="line"><span class="ln">8</span><span class="cl">[7. Current tool result]：剛跑完的 tool output             ← 變動</span></span></code></pre></div><p>Breakpoint 放在「不變 vs 變動」交界處、讓 [1-4] 永遠 cache hit。</p>
<p>Anthropic 最多 4 個 breakpoint、可分層：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">breakpoint 1（最早）：[system prompt] → 永久 cache
</span></span><span class="line"><span class="ln">2</span><span class="cl">breakpoint 2：       [+ tool schema] → 永久 cache
</span></span><span class="line"><span class="ln">3</span><span class="cl">breakpoint 3：       [+ skill registry] → 半永久 cache
</span></span><span class="line"><span class="ln">4</span><span class="cl">breakpoint 4（最晚）：[+ recent stable context] → 短期 cache
</span></span><span class="line"><span class="ln">5</span><span class="cl">[後段]：             variable content（不 cache）</span></span></code></pre></div><p>每個 breakpoint 各自命中 / miss、layered cache 讓「加新 skill」只 invalidate breakpoint 3 之後、不影響 breakpoint 1-2。</p>
<h2 id="場景-1coding-agent">場景 1：Coding agent</h2>
<p>Coding agent 是 prompt cache 命中區 — system prompt + tool schema 動輒 10K-30K token、每個 user turn 都重用。</p>
<p>收益估算（200K context 模型、10K scaffold、5K user query、3K answer）：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln"> 1</span><span class="cl">無 cache：
</span></span><span class="line"><span class="ln"> 2</span><span class="cl">  每 turn input cost = (10K + 5K) × $3/M = $0.045
</span></span><span class="line"><span class="ln"> 3</span><span class="cl">  每 turn TTFT = 10K-15K prefill time（200-400ms）
</span></span><span class="line"><span class="ln"> 4</span><span class="cl">
</span></span><span class="line"><span class="ln"> 5</span><span class="cl">開 cache：
</span></span><span class="line"><span class="ln"> 6</span><span class="cl">  Turn 1（write）：(10K × 1.25 + 5K) × $3/M = $0.0525
</span></span><span class="line"><span class="ln"> 7</span><span class="cl">  Turn 2-N（read）：(10K × 0.1 + 5K) × $3/M = $0.018
</span></span><span class="line"><span class="ln"> 8</span><span class="cl">  TTFT：read 階段省掉 10K prefill、只剩 5K
</span></span><span class="line"><span class="ln"> 9</span><span class="cl">
</span></span><span class="line"><span class="ln">10</span><span class="cl">10 turns 的累計 cost：
</span></span><span class="line"><span class="ln">11</span><span class="cl">  無 cache：10 × $0.045 = $0.45
</span></span><span class="line"><span class="ln">12</span><span class="cl">  開 cache：$0.0525 + 9 × $0.018 = $0.215
</span></span><span class="line"><span class="ln">13</span><span class="cl">  → 節省 52%</span></span></code></pre></div><p>長對話越長、cache 收益越大（cache write 是一次性成本）。</p>
<h2 id="場景-2rag--long-context">場景 2：RAG / long-context</h2>
<p>RAG 場景把 retrieved chunks 放 prefix、user query 放後面、可以 cache retrieved chunks：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">[system prompt]
</span></span><span class="line"><span class="ln">2</span><span class="cl">       ↓ breakpoint 1（system 永久 cache）
</span></span><span class="line"><span class="ln">3</span><span class="cl">[retrieved chunks 來自 RAG]
</span></span><span class="line"><span class="ln">4</span><span class="cl">       ↓ breakpoint 2（同 chunks 在 5min 內 cache）
</span></span><span class="line"><span class="ln">5</span><span class="cl">[user query]</span></span></code></pre></div><p>注意：每次 retrieval 不同 chunks 就 cache miss、所以 cache 適合「同個對話多輪、retrieval 結果穩定」、不適合「每 query 都 fresh retrieve」；後者要回到 <a href="/blog/llm/knowledge-cards/retrieval-cost/" data-link-title="Retrieval Cost" data-link-desc="RAG 檢索帶來的 latency、token、embedding、reranker、LLM call 與維護成本，用來判斷增強是否划算">retrieval cost</a> 評估。</p>
<h2 id="場景-3long-document-qa">場景 3：Long document Q&amp;A</h2>
<p>讀者上傳 PDF / 文件、多輪問問題：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">[system prompt]
</span></span><span class="line"><span class="ln">2</span><span class="cl">       ↓ breakpoint 1
</span></span><span class="line"><span class="ln">3</span><span class="cl">[完整文件內容（可能 100K token）]
</span></span><span class="line"><span class="ln">4</span><span class="cl">       ↓ breakpoint 2（文件永久 cache）
</span></span><span class="line"><span class="ln">5</span><span class="cl">[user query]</span></span></code></pre></div><p>第一次 query 付 1.25× 文件成本、後續 query 都 0.1×。100K 文件 + 10 個問題的場景下、節省極顯著（&gt; 80% cost）。</p>
<h2 id="常見-anti-pattern">常見 anti-pattern</h2>
<ol>
<li><strong>在 prefix 插入 timestamp / request-id</strong></li>
</ol>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">反例：System prompt: &#34;你是 coding assistant、當前時間 2026-05-12 16:30:42、...&#34;
</span></span><span class="line"><span class="ln">2</span><span class="cl">   → 每秒不同 cache key、永遠 cache miss、付 1.25× write 不回本
</span></span><span class="line"><span class="ln">3</span><span class="cl">正解：把 timestamp 放後段、或省略（多數場景模型不需要精確時間）</span></span></code></pre></div><ol start="2">
<li><strong>在 prefix 動態插入 user metadata</strong></li>
</ol>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">反例：System prompt: &#34;User: alice@example.com, plan: premium、...&#34;
</span></span><span class="line"><span class="ln">2</span><span class="cl">   → 每個 user 不同 cache、命中率低
</span></span><span class="line"><span class="ln">3</span><span class="cl">正解：User metadata 放後段、prefix 保持 user-agnostic</span></span></code></pre></div><ol start="3">
<li><strong>Tool schema 順序不固定</strong></li>
</ol>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">反例：每次 request 把 tool list 隨機 shuffle
</span></span><span class="line"><span class="ln">2</span><span class="cl">   → 同樣 tool 但 token sequence 不同、cache miss
</span></span><span class="line"><span class="ln">3</span><span class="cl">正解：Tool list 順序固定、新加 tool 都 append 到末尾</span></span></code></pre></div><ol start="4">
<li><strong>太短的 prompt 也想 cache</strong></li>
</ol>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">反例：500 token system prompt 開 cache
</span></span><span class="line"><span class="ln">2</span><span class="cl">   → 多數服務商 minimum 1K-4K、不到門檻不 cache、且 write cost 不回本
</span></span><span class="line"><span class="ln">3</span><span class="cl">正解：Cache 留給 &gt; 1K 的 prefix、短 prompt 不必開</span></span></code></pre></div><ol start="5">
<li><strong>混用 stream + cache 卻不檢查命中</strong></li>
</ol>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">反例：開 cache 後不檢查 response 的 cache_read_input_tokens 欄位
</span></span><span class="line"><span class="ln">2</span><span class="cl">   → 不知道實際命中率、可能 anti-pattern 已在燒 cost 沒察覺
</span></span><span class="line"><span class="ln">3</span><span class="cl">正解：監控 cache_read / cache_creation token 比例、低於 80% 命中率時 debug</span></span></code></pre></div><h2 id="cache-miss-訊號跟診斷">Cache miss 訊號跟診斷</h2>
<p>訊號：</p>
<ol>
<li><strong>Cost 比預期高</strong>：應該命中的場景仍付 full price</li>
<li><strong>TTFT 沒改善</strong>：cache hit 應該大幅降 TTFT、沒改善 = miss</li>
<li><strong>Response 的 usage 顯示 cache_read = 0</strong>：直接訊號</li>
</ol>
<p>診斷流程：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">1. 印出 raw request 的 prefix（cache_control 之前）
</span></span><span class="line"><span class="ln">2</span><span class="cl">2. 比對連續兩次 request 的 prefix token sequence
</span></span><span class="line"><span class="ln">3</span><span class="cl">3. 找出差異位置（diff）
</span></span><span class="line"><span class="ln">4</span><span class="cl">4. 移除 / 重構讓兩次 prefix 完全相同
</span></span><span class="line"><span class="ln">5</span><span class="cl">5. 跑 2-3 次 request、看 cache_read_input_tokens 是否上升</span></span></code></pre></div><p>常見差異：timestamp、request id、user id、tool list 順序、retrieved chunks 順序、conversation summary 變動。</p>
<h2 id="跟其他-cost-優化技巧的關係">跟其他 cost 優化技巧的關係</h2>
<table>
  <thead>
      <tr>
          <th>技巧</th>
          <th>攻擊的 cost / latency 來源</th>
          <th>跟 prompt cache 的關係</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><a href="/blog/llm/knowledge-cards/speculative-decoding/" data-link-title="Speculative Decoding" data-link-desc="用小模型猜未來 token、大模型並行驗證的加速技巧">Speculative decoding</a></td>
          <td>Generation 階段 token cost</td>
          <td>正交、可疊加</td>
      </tr>
      <tr>
          <td><a href="/blog/llm/knowledge-cards/batching/" data-link-title="Batching" data-link-desc="多 request 一起跑、攤平 model load 成本：production LLM inference 的核心優化、決定 throughput vs latency 取捨">Batching</a></td>
          <td>Throughput per GPU</td>
          <td>Production 才用、跟 prompt cache 都用</td>
      </tr>
      <tr>
          <td><a href="/blog/llm/knowledge-cards/prefix-cache/" data-link-title="Prefix Cache" data-link-desc="把多個請求共用的前綴 prompt 的 KV cache 重用、省下重複 prefill 算力的優化、production 多用戶服務的常見設計">Prefix cache</a></td>
          <td>同 server 跨 request 共用 KV cache</td>
          <td>本地推論伺服器特性、prompt cache 是雲端 API 商業 feature</td>
      </tr>
      <tr>
          <td>模型量化</td>
          <td>Generation tok/s</td>
          <td>正交、可疊加</td>
      </tr>
      <tr>
          <td>RAG 而非 long context</td>
          <td>Input token 量</td>
          <td>RAG + cache 可同時用</td>
      </tr>
  </tbody>
</table>
<h2 id="本地推論伺服器有沒有類似機制">本地推論伺服器有沒有類似機制</h2>
<p>Ollama / LM Studio / llama.cpp 自身的 prompt cache：</p>
<table>
  <thead>
      <tr>
          <th>工具</th>
          <th>機制</th>
          <th>範圍</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>llama.cpp</td>
          <td><code>--prompt-cache</code> flag、persistent file</td>
          <td>重複跑同樣 prompt 時跳過 prefill</td>
      </tr>
      <tr>
          <td>Ollama</td>
          <td>內建 prefix cache、跨 request 共用</td>
          <td>同 server 跨 request</td>
      </tr>
      <tr>
          <td>LM Studio</td>
          <td>同 Ollama 級別、視版本</td>
          <td>同上</td>
      </tr>
      <tr>
          <td>vLLM</td>
          <td>強 prefix cache（PagedAttention 設計支援）</td>
          <td>高併發 production</td>
      </tr>
  </tbody>
</table>
<p>本地推論的 cache 主要靠 <a href="/blog/llm/knowledge-cards/prefix-cache/" data-link-title="Prefix Cache" data-link-desc="把多個請求共用的前綴 prompt 的 KV cache 重用、省下重複 prefill 算力的優化、production 多用戶服務的常見設計">prefix cache</a> 機制、跟雲端 API 的 prompt cache 商業 feature 同源、但定價 / TTL / 顯式 control 是雲端 API 才有的 product layer。</p>
<h2 id="何時不適合用-prompt-cache">何時不適合用 prompt cache</h2>
<ol>
<li><strong>每 request prefix 必變</strong>：streaming 任務、每 query 都帶 fresh 上下文</li>
<li><strong>Single-shot 對話</strong>：用完就丟、沒有重複使用、write cost 不回本</li>
<li><strong>Prefix &lt; 1K token</strong>：不到 minimum、cache 不生效</li>
<li><strong>Cost 不敏感場景</strong>：個人小流量、cache 設計 overhead 大於收益</li>
<li><strong>本地推論為主</strong>：本地多用 prefix cache、prompt cache 是雲端 API 概念</li>
</ol>
<h2 id="何時過時--何時不過時">何時過時 / 何時不過時</h2>
<p><strong>不會過時的部分</strong>：</p>
<ul>
<li>「不變放 prefix、變動放後段」的設計原則</li>
<li>Cache breakpoint 分層（system / tool schema / skill / context）</li>
<li>Anti-pattern 分類（timestamp、user metadata、tool 順序）</li>
<li>Cache miss 診斷流程</li>
</ul>
<p><strong>會變的部分</strong>：</p>
<ul>
<li>各 vendor 的具體定價（write × / read × 折扣）</li>
<li>TTL（5min vs 1h）的可選性跟價格</li>
<li>Automatic vs explicit cache（OpenAI vs Anthropic 路線）</li>
<li>Breakpoint 上限數量</li>
<li>本地推論伺服器的 cache 功能（持續演化）</li>
</ul>
<h2 id="下一章">下一章</h2>
<p>下一章：<a href="/blog/llm/04-applications/agent-memory-architecture/" data-link-title="4.19 Agent memory 分層架構" data-link-desc="Agent 在 context window 之外管理長期狀態的設計：working / short-term / long-term episodic / semantic / procedural 五個層次、寫入時機、retrieval 設計、失敗模式">4.19 Agent memory 分層</a>、看 agent 如何在 context window 之外管理長期狀態。</p>
]]></content:encoded></item></channel></rss>