AI Coding Agents
DeepSeek Context Caching: How the Disk Cache Decides Hits
DeepSeek’s context caching is a disk-based prefix cache that is on by default for every request and needs no configuration. A request hits the cache only when it fully matches a previously persisted cache prefix unit; matched input tokens bill at a far lower cache-hit rate, with separate peak and off-peak prices.
DeepSeek’s context caching needs no setup. It is a disk-based prefix cache that is enabled by default for all users, and the docs say users benefit “without needing to modify their code.” Every request you send triggers construction of a hard disk cache, and when a later request shares a matching prefix with a stored one, the overlapping part is served from cache and billed at the cache-hit rate instead of the full input rate.
DeepSeek matches whole stored objects it calls cache prefix units. In an append-only conversation, a request can reuse a unit written at the end of an earlier turn. Independent requests with different questions can benefit once the service has identified and stored their shared prefix. Measure these two patterns separately before estimating agent input costs.
How the disk cache decides what hits
A cache hit requires the incoming request to fully match a previously persisted cache prefix unit. DeepSeek notes that because of its Sliding Window Attention mechanism, each cached prefix is stored as an independent, complete unit, so the overlap test is all-or-nothing against a stored unit rather than a partial-token discount. The docs describe three triggers that persist a unit:
- Persistence at request boundaries. Each request produces two cache prefix units, one at the end position of the user input and one at the end position of the model output. A later request hits if it fully matches one of them.
- Common prefix detection. When the system sees a common prefix across multiple requests, it persists that shared prefix as its own unit.
- Persistence at fixed token intervals. For long inputs or outputs, the system carves out units at fixed token intervals so long prefixes are not left completely uncacheable simply because no end position is reached. The docs do not publish the interval size, so do not design around a specific number.
The doc’s own two examples show why the mechanics feel different from a naive “shared prefix” mental model:
- Request one sends
A + B; request two sendsA + B + C. The second request fully matches the storedA + Bunit and hits. - Request one sends
A + B; request two sendsA + C. No hit, becauseA + Cdoes not fully match theA + Bunit. But the system now detects the common prefixAand persists it. A third requestA + Dfully matchesAand hits.
In DeepSeek’s long-text example, repeated questions about one financial report miss on the first two requests. After the second call, the service identifies the system message plus the report content in the user message as a cache prefix unit. The third request can reuse the unit after persistence finishes.
A complete request-boundary unit can be reused by the next request if persistence has finished. DeepSeek says construction takes seconds and unused entries are usually cleared within a few hours to a few days. The guide publishes no fixed TTL or pinning control. Budget for misses even when calls repeat a prefix.
One consequence worth internalizing for agents: the disk cache only matches the prefix of your input. Output is still computed normally and is influenced by parameters like temperature, so a full cache hit does not mean identical responses, only a cheaper and faster prefill.
Reading hit and miss counts
When the response includes usage, record its hit and miss counts.
| Field | Meaning |
|---|---|
prompt_cache_hit_tokens | Input tokens in this request that resulted in a cache hit |
prompt_cache_miss_tokens | Input tokens in this request that did not result in a cache hit |
The two sum to the request’s input tokens. For a session-level hit rate, add the hit counts across requests and divide by the sum of all hit and miss tokens. That gives a long turn more weight than a short one. Keep the per-request counts for diagnosis. If misses rise, compare the rendered prompt with the earlier stored boundary and check the gap between calls. An edit can leave a shorter stored unit eligible. Treat a zero hit count as an observation and test whether the prefix is reproducibly reusable.
What DeepSeek charges
The rates below are the committed first-party figures in our dataset for DeepSeek, USD per 1M tokens. DeepSeek splits the day into peak and off-peak windows: peak is 01:00 to 04:00 and 06:00 to 10:00 UTC, Monday through Friday, excluding Chinese public holidays; all other hours, including weekends and Chinese public holidays in full, are off-peak at half the peak rate.
| Model | Input (cache miss), peak | Cache hit, peak | Output, peak | Off-peak |
|---|---|---|---|---|
| deepseek-v4-pro | $1.32 | $0.044 | $3.96 | Half of peak |
| deepseek-v4-flash | $0.44 | $0.014 | $1.32 | Half of peak |
At these rates a hit token costs about 3.3% of a miss on v4-pro and about 3.2% on v4-flash, roughly a 97% discount on reused input. The DeepSeek pricing table lists only input hit, input miss, and output prices; the docs describe no separate cache-write or storage fee.
The table uses the committed November 29, 2026 rates for the estimates below. DeepSeek’s November 30 pricing snapshot recommends deepseek-flash and says requests using the legacy deepseek-v4-flash name are served by DeepSeek-V4.1-Flash-1017. The snapshot lists peak cache-miss input, cache-hit input, and output at $0.30, $0.006, and $1.20 per million tokens respectively for the replacement model. Use DeepSeek’s current pricing schedule when budgeting a deployment. The same snapshot lists concurrency limits of 2,500 for deepseek-flash and 500 for deepseek-v4-pro, which can affect how many batch jobs run at once.
Worked estimate: a coding-agent session at 90% hits
Assume 5 million input tokens across 40 turns, a 90% input-token hit rate, peak pricing, and the same output volume in both cases. These are input-cost estimates. Calculate input cost = (hit tokens × cache-hit price + miss tokens × cache-miss price) / 1,000,000 with prices per million tokens. In the calculations below, token counts are already expressed in millions.
deepseek-v4-pro at peak rates
- Uncached input costs
5 × $1.32 = $6.60. - With 90% hits, input costs
(4.5 × $0.044) + (0.5 × $1.32) = $0.198 + $0.66 = $0.858, or about $0.86. - Estimated input savings are 87.0%.
deepseek-v4-flash at committed peak rates
- Uncached input costs
5 × $0.44 = $2.20. - With 90% hits, input costs
(4.5 × $0.014) + (0.5 × $0.44) = $0.063 + $0.22 = $0.283, or about $0.28. - Estimated input savings are 87.1%.
At a 60% hit rate on deepseek-v4-pro, the same session costs an estimated 3 × $0.044 + 2 × $1.32 = $0.132 + $2.64 = $2.772, or about $2.77. That is 58.0% below the uncached input estimate. A session wholly off-peak costs half as much at the same token mix. For a session spanning both windows, subtotal the hit and miss tokens billed in each window and add the subtotals. Include separately billed output when estimating the full session cost.
Structuring prompts for hits
- Put stable content first. System prompt, tool definitions, repository or document context, then the volatile instruction last. A hit requires fully matching a stored unit, and units are anchored at the start of the prompt, so any early change forfeits the whole match.
- Append follow-ups. Conversation history that only grows can still match the unit persisted at a previous turn’s end. Editing an earlier message or reordering content means later stored units no longer match in full; only a stored unit that ends before the edit, such as a persisted system prompt and document, can still hit.
- Cluster similar requests. Common-prefix persistence starts when the system observes the shared prefix across requests. Run jobs that query one document or repo close together and measure reuse after longer gaps, since DeepSeek usually reclaims unused entries within hours to days.
- Do not inject churn early. Timestamps, request IDs, and session-specific metadata at the top of a prompt change the prefix every call. Keep them at the tail if you need them at all.
- Budget for misses. Reuse is best-effort. Estimate the uncached prefill cost for calls that arrive before a unit is available or after it has been cleared.
Test which prefix your workload actually reuses
Start with a fixed system message and a document that represents your production requests. Send a first request with the document and a question. Record the model, the time, and both cache usage fields. Keep the system message and document unchanged throughout each sequence so you can associate changes in the token counts with request structure.
First, test an append-only continuation. Preserve the original messages and append the follow-up as a new message. Check whether hit tokens cover the previous request’s input prefix. If you also include the model’s previous answer in the history, the unit at the end of that answer may be eligible too. Keep track of what you sent so you can compare the token count to the boundary you expected to reuse.
Then start a separate sequence with a fresh shared prefix and change only the question on each call. The second request can expose a shared prefix that had never been persisted on its own. A later request can reuse it after persistence. Log the time between calls because construction takes seconds. A run with back-to-back requests and a run with your usual spacing can produce different results even if the requests contain the same text.
Compare the hit and miss token totals for both sequences. If the append-only test reuses the earlier input immediately and independent requests start hitting later, account for that warmup in estimates for batch jobs. If misses continue, inspect what your client actually sent, including any text or tool definitions it inserts before the document. Repeat after a normal idle gap to check whether reuse survives on your schedule.
If you use LiteLLM, its DeepSeek adapter example sends repeated messages and inspects the returned usage. Check DeepSeek’s pricing page for the model identifier and billing rates used in your own test. For cross-provider guidance, see cache-friendly prompt structure.
What to do next
- Start with the default API behavior. The cache needs no opt-in, and DeepSeek’s pricing table lists cache-hit input, cache-miss input, and output charges.
- Record both usage fields. Track
prompt_cache_hit_tokensandprompt_cache_miss_tokensper session before changing how you build prompts. See how to measure your prompt cache hit rate for the cross-provider version. - Schedule eligible jobs off-peak. Peak is 01:00 to 04:00 and 06:00 to 10:00 UTC, Monday through Friday, excluding Chinese public holidays. All other hours, including weekends and Chinese public holidays in full, charge half the peak rates for input hits, input misses, and output.
- Recheck rates before budgeting. Consult DeepSeek’s pricing page and the AI Provider Pricing Index for rates.
- Compare providers. Use DeepSeek vs Kimi open-model cost and how LLM providers price prompt caching to compare how they bill the same workload.
Sources
- Context Caching (DeepSeek API Docs, accessed 2026-11-30)
- Models and Pricing (DeepSeek API Docs, accessed 2026-11-30)
- Prompt caching: DeepSeek example (LiteLLM, accessed 2026-11-30)
Interactive report
- Who Pays to Remember: Give four APIs the same repeated prompt and open the bill one line at a time. The read discount is only the first entry.
How this note was produced
Drafted with AI assistance from committed ByteCosts pricing snapshots and deterministic cost formulas. Figures are grounded in the linked sources (last updated 2026-11-30). Read our editorial & automation policy, inspect the methodology, or report a discrepancy.
DeepSeek Context Caching: How the Disk Cache Decides Hits. ByteCosts. Updated 2026-11-30. https://bytecosts.com/blog/deepseek-context-caching-guide/