How Cache Influences Your Budget
You've learned that cached tokens are cheap and uncached tokens are expensive. Now let's build the exact formula for how much a multi-turn agent session costs — and see the numbers with real DeepSeek pricing.
The Three Prices
Every token you send or receive is billed at one of three rates. Providers publish all three, and they are far apart:
| Symbol | Applies to | Why it's priced that way |
|---|---|---|
| Pcr | Input tokens that hit the cache | The GPU work was already done on an earlier request |
| Pi | Input tokens the model sees for the first time | Full attention pass required |
| Po | Every token the model generates | One forward pass per token, generated one at a time |
Here are DeepSeek's actual numbers, in ¥ per million tokens:
| Model | Time slot | Pcr | Pi | Po |
|---|---|---|---|---|
| deepseek-v4-flash | Off-peak | 0.05 | 1.50 | 4.50 |
| deepseek-v4-flash | Peak | 0.10 | 3.00 | 9.00 |
| deepseek-v4-pro | Off-peak | 0.15 | 4.50 | 13.50 |
| deepseek-v4-pro | Peak | 0.30 | 9.00 | 27.00 |
Two ratios are worth memorising: fresh input costs 30× a cache read, and output costs 90×. Every design decision you make about agent architecture is really a decision about which bucket your tokens land in.
Now let's build the bill one piece at a time.
Part 1: Cache Reads
The system prompt gets re-read every single turn. It sits at the front of the prefix, it never changes, and it is charged at the cache rate on all $n$ turns:
Everything the conversation produces later gets re-read too, but not $n$ times each. Turn 1's block is re-read by turns 2 through $n$. Turn 2's block by turns 3 through $n$. The last turn's block is never re-read at all.
That decay is exactly $(n - i)$:
This is usually the largest token count in the whole session and one of the smallest line items on the invoice. That's the cache doing its job.
Part 2: Fresh Input
Only truly new tokens cost full price. There are two kinds. First, the cold-start read of $S_0 + U_0$ — somebody has to pay for the very first pass. Second, per turn, the tokens that arrive from outside the model: your user's message $U_i$ and the tool result $TR_i$.
Note what is not here: $R_i$, $C_i$ and $T_i$. The model generated those, so they entered the cache as a side effect of being generated. You never pay input price for them.
Part 3: Output
The model's generation always costs the same, at the highest of the three rates. Reasoning, answer, and tool call arguments are all output:
Nothing can be cached here — output is produced one token at a time, every time. This term is the one you control with prompt design and reasoning_effort, not with caching.
Putting It Together
Stack the three parts and you have the whole bill for an $n$-turn agent session:
For Direct Chat, set $R$, $T$ and $TR$ to zero — those token types don't exist in a plain chat.
This is a model, not an invoice. It counts the cold-start read of $S_0 + U_0$ in both of the first two terms, which overstates a 10-turn session by well under one percent. Cache eviction and block alignment also move real numbers slightly.
Try the Numbers
The calculator below evaluates the formula live. Pick a model and a time slot, size your turns, and watch the three terms move against each other.
缓存如何影响你的预算
你已经知道命中缓存的 token 很便宜、未命中的很贵。这一节我们把多轮 Agent 会话的成本公式完整搭出来,再用 DeepSeek 的真实价格算出具体数字。
三种价格
你发出和收到的每个 token,都按三种价格中的一种计费。服务商会把三种都公示出来,而它们之间差得很远:
| 符号 | 适用对象 | 为什么是这个价 |
|---|---|---|
| Pcr | 命中缓存的输入 token | GPU 的活在更早的请求里就干完了 |
| Pi | 模型第一次见到的输入 token | 需要完整跑一遍注意力 |
| Po | 模型生成的每一个 token | 一个 token 一次前向,只能一个一个出 |
下面是 DeepSeek 的实际价格,单位为元每百万 token:
| 模型 | 时段 | Pcr | Pi | Po |
|---|---|---|---|---|
| deepseek-v4-flash | 空闲 | 0.05 | 1.50 | 4.50 |
| deepseek-v4-flash | 高峰 | 0.10 | 3.00 | 9.00 |
| deepseek-v4-pro | 空闲 | 0.15 | 4.50 | 13.50 |
| deepseek-v4-pro | 高峰 | 0.30 | 9.00 | 27.00 |
有两个倍数值得记住:全价输入是缓存读取的 30 倍,输出是 90 倍。你在 Agent 架构上做的每个设计决策,本质上都是在决定 token 落进哪个桶。
现在一块一块把账单搭起来。
第一块:缓存读取
系统提示词每一轮都要被重读一遍。它待在前缀最前面,从不改动,而且在全部 $n$ 轮里都按缓存价计费:
对话后面产生的内容也会被重读,但不是每块都读 $n$ 次。第 1 轮的块会被第 2 到第 $n$ 轮重读,第 2 轮的块被第 3 到第 $n$ 轮重读,最后一轮的块则完全不会被重读。
这个衰减刚好就是 $(n - i)$:
这一项通常是整场会话里 token 数量最大的部分,同时是账单上金额最小的条目之一。这就是缓存在干活。
第二块:全价输入
只有真正新的 token 才按全价算。它们分两种。第一种是 $S_0 + U_0$ 的冷启动读取 —— 第一遍总得有人付。第二种是每轮从模型外部进来的 token:用户的消息 $U_i$ 和工具返回 $TR_i$。
注意这里没有 $R_i$、$C_i$ 和 $T_i$。这些是模型自己生成的,生成的同时就顺带进了缓存,所以你永远不会为它们付输入价。
第三块:输出
模型的生成部分价格始终不变,而且是三种价格里最贵的那个。推理、回答、工具调用参数,全都算输出:
这里没有任何东西能被缓存 —— 输出永远是一个 token 一个 token 现场生成的。这一项要靠 prompt 设计和 reasoning_effort 来控制,缓存帮不上忙。
拼在一起
把三块叠起来,就是一场 $n$ 轮 Agent 会话的完整账单:
直聊模式下,把 $R$、$T$、$TR$ 设为零即可 —— 普通聊天里没有这些 token 类型。
这是一个模型,不是账单原件。它把 $S_0 + U_0$ 的冷启动读取在前两项里各算了一次,对一场 10 轮会话的高估远低于百分之一。缓存驱逐和块对齐也会让真实数字略有偏移。
拿数字试试
下面的计算器会实时套用这个公式。选一个模型和时段,把每轮的规模调一调,看三项之间怎么此消彼长。
Cost Calculator
成本计算器
The gap between the two curves widens with every turn — caching pays off most in long sessions.
两条曲线的差距每一轮都在拉大 —— 会话越长,缓存越划算。
A Worked Example
Let's say you run a 10-turn coding agent session with deepseek-v4-flash in the off-peak window. Those are the calculator's default settings:
- System prompt plus opening request: $S_0 + U_0$ = 1,350 tokens
- Each turn produces 2,500 reasoning + 450 answer + 40 tool call + 200 tool result + 150 user = 3,340 tokens
- Prices: Pcr = 0.05, Pi = 1.50, Po = 4.50 ¥/M
Term by term:
10-turn session, deepseek-v4-flash, off-peak
| ① Cache read — 1,350 × 10 + 3,340 × 45 = 163,800 tokens @ 0.05 | 0.0082 ¥ |
| ② Fresh input — 1,350 + 10 × 350 = 4,850 tokens @ 1.50 | 0.0073 ¥ |
| ③ Output — 10 × 2,990 = 29,900 tokens @ 4.50 | 0.1346 ¥ |
| Total P₁₀ | 0.150 ¥ |
The 45 in term ① is $\sum_{i=1}^{10}(10-i)$ = 9 + 8 + … + 1 + 0. It's the reason 163,800 tokens flow through the cache in a session that only ever sent 168,650 input tokens: 97% of your input is a re-read.
Now the same session with caching switched off. Every request pays full input price for its entire history:
The same session with no cache
| Input — 167,300 tokens @ 1.50 | 0.2510 ¥ |
| Output — unchanged, 29,900 tokens @ 4.50 | 0.1346 ¥ |
| Total | 0.386 ¥ |
That's a 61% saving on the session. Look only at the input side and it's starker: 0.0155 ¥ with cache versus 0.2510 ¥ without, about 16× cheaper.
But notice what the saving reveals. Once caching is doing its job, output is 0.1346 of a 0.150 ¥ bill — 90% of what you pay. Your input problem is solved; your output problem is now the whole problem.
What This Means for Design
- Keep the prefix frozen. A stable system prompt is worth more than a clever one that gets tweaked per request. Put anything volatile — timestamps, session IDs — at the end of the message list, never the front.
- Prefer long sessions to fresh ones. Cold starts are the expensive part. Ten turns in one session cost far less than ten one-turn sessions.
- Compress reluctantly. Summarizing history to save context window trades a cheap cache read for an expensive cold start. Sometimes worth it, rarely free.
- Spend your effort on output. With cache on, output dominates. That makes
reasoning_effort, answer length, and how many turns a task takes your real cost levers.
That last point is where the rest of these tutorials go. If the cheapest agent is the one that reaches the answer in fewer, better-informed turns, then the interesting question becomes: how do you make an agent that already knows what to do? That's what a Skill is for.
完整算一遍
假设你在空闲时段用 deepseek-v4-flash 跑一场 10 轮的编码 Agent 会话。下面这组就是计算器的默认参数:
- 系统提示词加首个请求:$S_0 + U_0$ = 1350 token
- 每轮产生 2500 推理 + 450 回答 + 40 工具调用 + 200 工具返回 + 150 用户消息 = 3340 token
- 价格:Pcr = 0.05,Pi = 1.50,Po = 4.50 元每百万
逐项算:
10 轮会话,deepseek-v4-flash,空闲时段
| ① 缓存读取 —— 1350 × 10 + 3340 × 45 = 163800 token,单价 0.05 | 0.0082 元 |
| ② 全价输入 —— 1350 + 10 × 350 = 4850 token,单价 1.50 | 0.0073 元 |
| ③ 输出 —— 10 × 2990 = 29900 token,单价 4.50 | 0.1346 元 |
| 总计 P₁₀ | 0.150 元 |
① 里那个 45 就是 $\sum_{i=1}^{10}(10-i)$ = 9 + 8 + …… + 1 + 0。所以这场只发出过 168650 个输入 token 的会话,会有 163800 个 token 从缓存里流过:你的输入有 97% 是重读。
再看同一场会话在没有缓存时的样子。每次请求都要为它的全部历史付全价输入:
同一场会话,没有缓存
| 输入 —— 167300 token,单价 1.50 | 0.2510 元 |
| 输出 —— 不变,29900 token,单价 4.50 | 0.1346 元 |
| 总计 | 0.386 元 |
整场会话省下 61%。只看输入那一侧差距更悬殊:有缓存 0.0155 元,没缓存 0.2510 元,大约便宜 16 倍。
但更值得注意的是这个节省揭示了什么。缓存一旦真正起作用,0.150 元的账单里输出占了 0.1346 元 —— 你付的钱有九成在输出上。输入的问题解决了,输出的问题就变成了全部问题。
这对设计意味着什么
- 把前缀冻住。一个稳定的系统提示词,比一个每次请求都要微调的聪明提示词更值钱。所有会变的东西 —— 时间戳、会话 ID —— 都放到消息列表末尾,绝不要放开头。
- 宁可长会话,不要重开。贵的地方是冷启动。一场十轮会话的成本,远低于十场一轮会话。
- 压缩要慎重。为了省上下文窗口去做历史摘要,是用一次便宜的缓存读取换来一次昂贵的冷启动。有时值得,但从来不免费。
- 把精力花在输出上。缓存开着的时候,输出是主导项。所以
reasoning_effort、回答长度,以及一个任务要走多少轮,才是你真正的成本杠杆。
最后这一点正是后面几篇教程的方向。如果最省钱的 Agent 是那种用更少、更有把握的轮次就把答案拿到手的 Agent,那么有意思的问题就变成了:怎么让一个 Agent 一上来就知道该怎么做?这就是 Skill 要解决的事。