<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>定价 - 标签 - 豆浆小屋</title><link>https://soybeanmilk.netlify.app/tags/%E5%AE%9A%E4%BB%B7/</link><description>定价 - 标签 - 豆浆小屋</description><generator>Hugo -- gohugo.io</generator><language>zh-CN</language><copyright>This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.</copyright><lastBuildDate>Sat, 08 Aug 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://soybeanmilk.netlify.app/tags/%E5%AE%9A%E4%BB%B7/" rel="self" type="application/rss+xml"/><item><title>LLM Prompt 缓存机制</title><link>https://soybeanmilk.netlify.app/llm-prompt%E7%BC%93%E5%AD%98%E6%9C%BA%E5%88%B6/</link><pubDate>Sat, 08 Aug 2026 00:00:00 +0000</pubDate><author><name>作者</name></author><guid>https://soybeanmilk.netlify.app/llm-prompt%E7%BC%93%E5%AD%98%E6%9C%BA%E5%88%B6/</guid><description><![CDATA[<h2 id="简要回答" class="headerLink">
    <a href="#%e7%ae%80%e8%a6%81%e5%9b%9e%e7%ad%94" class="header-mark"></a>1 简要回答</h2><p>LLM API 对请求的 <strong>前缀（prefix）</strong> 做自动缓存。两次请求的 prompt 开头部分完全相同时，相同部分的 token 不再重新计算，只付缓存价（通常低 100 倍以上）。</p>
<h2 id="详细回答" class="headerLink">
    <a href="#%e8%af%a6%e7%bb%86%e5%9b%9e%e7%ad%94" class="header-mark"></a>2 详细回答</h2><h3 id="命中条件" class="headerLink">
    <a href="#%e5%91%bd%e4%b8%ad%e6%9d%a1%e4%bb%b6" class="header-mark"></a>2.1 命中条件</h3><ol>
<li><strong>必须是前缀</strong>：缓存只认从 prompt 第 1 个 token 开始的连续相同段</li>
<li><strong>完全匹配</strong>：字符级一模一样才命中，多一个空格就失效</li>
<li><strong>至少 N 个 token</strong>：大部分模型要求缓存段 ≥ 1024 token</li>
<li><strong>TTL 内</strong>：缓存有效期通常 5-10 分钟，超时重新计算</li>
</ol>
<h3 id="什么最容易命中" class="headerLink">
    <a href="#%e4%bb%80%e4%b9%88%e6%9c%80%e5%ae%b9%e6%98%93%e5%91%bd%e4%b8%ad" class="header-mark"></a>2.2 什么最容易命中</h3><ul>
<li><strong>System prompt / 系统指令</strong>：每轮对话都一样 → 必中</li>
<li><strong>多轮对话（重点）</strong>：同一对话框里持续聊天，每轮请求都会把&quot;全部历史&quot;重发给 API。历史部分和上一轮请求完全相同 → 前缀越来越大，缓存命中越来越多</li>
<li><strong>反复引用的长文档/代码</strong>：同一个文件的 content 放前面</li>
<li><strong>AGENTS.md</strong>：每次都是 system prompt 开头 → 必中</li>
</ul>
<h4 id="多轮对话为什么自动命中" class="headerLink">
    <a href="#%e5%a4%9a%e8%bd%ae%e5%af%b9%e8%af%9d%e4%b8%ba%e4%bb%80%e4%b9%88%e8%87%aa%e5%8a%a8%e5%91%bd%e4%b8%ad" class="header-mark"></a>2.2.1 多轮对话为什么自动命中</h4><p>关键认知：<strong>每次发消息，API 收到的是&quot;全部聊天记录 + 你的新消息&quot;，不是只发新的一句。</strong></p>
<table>
  <thead>
      <tr>
          <th>轮次</th>
          <th>发送内容</th>
          <th>缓存情况</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>第1轮</td>
          <td><code>[系统指令] + &quot;问题A&quot;</code></td>
          <td>全部走输入价</td>
      </tr>
      <tr>
          <td>第2轮</td>
          <td><code>[系统指令] + &quot;问题A&quot; + [AI回答1] + &quot;问题B&quot;</code></td>
          <td>和第一轮完全相同的前缀 → 缓存价；新增部分 → 输入价</td>
      </tr>
      <tr>
          <td>第3轮</td>
          <td><code>[系统指令] + [历史1-2全部] + &quot;问题C&quot;</code></td>
          <td>更长前缀 → 更多缓存命中；新增部分 → 输入价</td>
      </tr>
      <tr>
          <td>&hellip;</td>
          <td>&hellip;</td>
          <td>聊越久，缓存 hit 越多</td>
      </tr>
  </tbody>
</table>
<p>所以同一个框连聊 20 轮，从第 2 轮起前面绝大部分都是缓存价，真正按输入价算的只有最新一句。</p>
<h3 id="为什么便宜百倍" class="headerLink">
    <a href="#%e4%b8%ba%e4%bb%80%e4%b9%88%e4%be%bf%e5%ae%9c%e7%99%be%e5%80%8d" class="header-mark"></a>2.3 为什么便宜百倍</h3><p>Transformer 推理最贵的步骤是 <strong>self-attention 的 KV 矩阵计算</strong>。如果 prefix 相同，上一轮算好的 KV cache 直接复用，跳过计算，只剩后面新增 token 的增量计算。服务商 GPU 成本大头被消除，所以定价极低。</p>
<h3 id="实际影响" class="headerLink">
    <a href="#%e5%ae%9e%e9%99%85%e5%bd%b1%e5%93%8d" class="header-mark"></a>2.4 实际影响</h3><ul>
<li>第一次请求：按输入价全额计费</li>
<li>后续请求（前缀相同）：前缀部分按缓存价，新增部分按输入价</li>
<li>AGENTS.md / 项目 context 只有第一次会话贵，之后近乎免费</li>
</ul>
<h2 id="知识扩展" class="headerLink">
    <a href="#%e7%9f%a5%e8%af%86%e6%89%a9%e5%b1%95" class="header-mark"></a>3 知识扩展</h2><p>不同模型的缓存定价差异很大：</p>
<ul>
<li>DeepSeek V4 Pro：缓存 ¥0.02 → 是输入价的 1/147</li>
<li>Qwen3.7 Plus：缓存 ¥0.27 → 是输入价的 1/10</li>
<li>缓存折扣越大，说明该模型在长对话/大 context 场景越划算</li>
</ul>
]]></description></item></channel></rss>