DeepSeekDSH
Independent community guideNot affiliated with DeepSeek.Download versions and guide references

DeepSeek Prompt Caching: Cut Agent Token Costs

How DeepSeek automatic prefix caching works, cache hit vs miss rates, and how to structure DSH prompts to sustain high cache hit ratios.

On this page
Editorial snapshot: 2026-10-10

In multi-turn coding sessions, input tokens quickly outpace output tokens because repository maps, tool schemas and conversation logs are resent on every turn. DeepSeek applies server-side prompt caching (KV cache reuse) automatically across matching 64-token prefixes, lowering cached input charges without manual cache_control tags. Keeping prompt prefixes stable makes long-context DSH loops far cheaper to run.

Server-side prefix matching across 64-token chunks without manual tags

Unlike providers that require explicit cache_control markers in client payloads, DeepSeek implements automatic prompt caching at the gateway layer. When an API request arrives, the cluster hashes the prompt token sequence from left to right against KV states already held in server memory and SSDs.

Caching operates in 64-token blocks. Whenever an incoming request shares an identical prefix with cached states on the serving nodes, the engine reuses precomputed key-value tensors, cutting prefill latency and billing the matched prefix at the cached-input rate.

DeepSeek's Multi-head Latent Attention (MLA) architecture shrinks KV cache memory to roughly one quarter of previous HBM usage and one eighth of SSD storage, allowing prefix caching to stay enabled by default across public API routes.

Section sources: DeepSeek API Docs — Context Caching (KV Cache), DeepSeek — Introducing DeepSeek-V4.1-Flash

Cache hit rates cost a fraction of cold prompt misses

DeepSeek splits input billing into cache-miss and cache-hit tiers, with additional off-peak reductions outside workday peak hours. On the current V4.1 Flash route (`deepseek-flash`), peak uncached input is $0.30 per million tokens while cached input drops to $0.006 per million tokens; off-peak rates halve both figures.

For one-off prompts, the savings are minor. In a 20-to-30-turn DSH coding loop over a medium repository, cumulative input regularly passes 500,000 tokens. When stable prefixes keep 70%–85% of input tokens in cache, uncached prefill charges stop dominating the bill:

Model route & windowCache hit input (per 1M)Cache miss input (per 1M)Output tokens (per 1M)Cache-hit reduction
DeepSeek V4.1 Flash (Peak)$0.006$0.30$1.2098%
DeepSeek V4.1 Flash (Off-peak)$0.003$0.15$0.6098%
DeepSeek V4 Pro 0813 (Peak / Off-peak)Per live console$1.32 / $0.66$3.96 / $1.98Check console

USD prices checked against DeepSeek's API pricing documentation. Peak hours are workdays 09:00–12:00 and 14:00–18:00 UTC+8; all other times use off-peak rates. Recheck your DeepSeek console before forecasting.

Section sources: DeepSeek API Docs — Models and pricing, DeepSeek API Docs — Context Caching (KV Cache)

Structuring prompts for high cache hit rates in DSH

Because DeepSeek enforces strict left-to-right prefix matching, prompt ordering determines whether caching works. A single dynamic token inserted near the start of the payload invalidates the hash for every token that follows.

When configuring DeepSeek Harness (DSH) or custom agent pipelines, keep a static-first, append-only layout. Place unchanging system instructions, tool declarations and repository rules at the front, and append new tool outputs and user messages at the tail.

  • Keep system instructions static: do not inject live timestamps, random request UUIDs or changing host metrics at the start of the system prompt.
  • Freeze tool declarations: keep MCP and built-in tool schemas in a deterministic order across every turn in a session.
  • Anchor repository rules early: place AGENTS.md or fixed architecture notes right after the system prompt so turns share the same prefix.
  • Append conversation history without rewriting earlier turns: add new user prompts and tool results at the end rather than mutating prior messages.

Section sources: DeepSeek API Docs — Context Caching (KV Cache), DeepSeek Harness — Official GitHub Repository

Diagnosing cache misses: usage metrics and architectural pitfalls

Check cache behavior directly from the `usage` object in each API response. DeepSeek returns `prompt_cache_hit_tokens` and `prompt_cache_miss_tokens` alongside `prompt_tokens`. In a multi-turn DSH session with a stable prefix, `prompt_cache_hit_tokens` should rise sharply from the second turn onward.

Low hit ratios usually come from two causes: cache eviction after a long idle gap, or naive sliding-window truncation that drops early messages from the front of the conversation. Context compaction works better when it preserves the fixed system/tool prefix and summarizes older middle turns instead.

API usage response check

A response reporting prompt_tokens: 32768, prompt_cache_hit_tokens: 28416, and prompt_cache_miss_tokens: 4352 shows 86.7% of input tokens billed at the cached rate rather than full prefill price.

Section sources: DeepSeek API Docs — Context Caching (KV Cache), DeepSeek API Docs — Models and pricing

Continue with model and DSH workflow guides