ProBackend
ai ai coding tools
2 weeks ago6 min read

DeepSeek cache strategy in coding agents: what actually hits

How coding agent loops affect DeepSeek cache hit rates and what harness designs preserve prefix caching benefits

DeepSeek cache strategy in coding agents: what actually hits

There's a specific moment when a coding agent first reaches for DeepSeek's API and the cache decides whether the next thousand tokens cost nothing or full price. The difference isn't magic — it's prefix matching, and most agent loops break the very condition that makes it work.

Why most agent loops starve the cache

The fundamental rule is simple: a cache hit requires the exact byte prefix of the previous request to match again. But most coding agents treat every turn as a fresh start. They reorder tool calls, rewrite system instructions, and inject fresh timestamps at the start of each context window. The DeepSeek Reasonix documentation puts it bluntly: automatic prefix caching activates only when the exact byte prefix of the previous request matches. Most agent loops reorder, rewrite, or inject fresh timestamps each turn — and in practice, cache hit rates drop below 20%.

That's not a model shortcoming. It's a harness design problem. The evidence comes straight from DeepSeek users who've measured it. One user ran a script tracking 486 million input tokens across DeepSeek V4 Pro and reported a 97.27% cache hit rate — 472,971,520 cached tokens versus 13,299,013 cache-miss tokens. The total bill came to $241.79, with $141.89 attributed to cached input and $49.87 to cache misses. All of this usage was with an OpenCode subagent exclusively.

The contrast is striking. When the same user compared against Sonnet pricing, the actual cost was a fraction of what equivalent Opus usage would have been. The cache hit rate alone explains why: 97% of input tokens were served from cache, meaning only 3% incurred full model pricing.

The bridge problem

A separate Hacker News thread described writing "a tiny little bridge so I could use DeepSeek V4 Pro via Codex." The bridge didn't do anything special with caching — it just massaged the DeepSeek API shape into what Codex expects. The user reported that basically everything they did was cached as far as they could tell: 39 million input tokens with cache hits versus 1.7 million without. The bridge was coded by DeepSeek V4 Flash locally, and the author took no responsibility for edge cases or fire hazards.

What the bridge reveals is that the caching benefit exists regardless of the wrapping harness. The real question is whether the harness preserves stable prefixes across turns. Some agents deliberately inject randomization — timestamps, session IDs, rearranged tool call orders — specifically to prevent stale context from being reused. From the discussion, the consensus forms around a practical guideline: keep the stable prompt, reasoning, and files content at the beginning, and append tool calls, summaries, and results at the end. That way the prefix that the cache can match remains undisturbed from one turn to the next.

Pruning, context compaction, and the 20% rule

The Hacker News discussion returned again and again to the same tension. Some participants argued that most agent loops do reorder or rewrite on every turn, others pushed back as overstated. The Reasonix documentation acknowledges the problem but offers optimization techniques: automatic prefix caching combined with selective pruning of tool calls from more than three user messages ago, when the context exceeds 40K tokens and at least 20K tokens can be removed. Under those conditions, the remaining history still caches fine, and the pruned calls — often irrelevant to the current task — disappear without breaking the prefix match.

One user reported keeping current context utilization at 18% of total context length across all models with 400K context or more, which they found best for minimizing spend. Another noted that pruning improves user experience because models are smarter with less context, even if cache hits don't improve dramatically. The net savings from pruning alone were estimated at around 5%, but the qualitative improvement in model performance with less noise often matters more.

There's also the question of what gets pruned. The conventional wisdom is to prune tool call results from earlier in the conversation, keeping the most recent turns intact. One user described a strategy of sending stable prompt, reasoning, and files content first, then tool calls summary and actual tool calls at the very end. That arrangement lets the cache match the stable prefix while the mutable portion accumulates at the tail.

Measuring what matters

Token-level metrics are the most concrete. The DeepSeek V4 Pro user reported 97.27% cache hit rate calculated as cache read divided by total input token. Another user tracking OpenCode measured 71% cache hit rate in very limited use. The calculation varies by provider — for some it's cache read divided by total input, for others it includes cache write in the denominator — but the direction is consistent: stable prefixes produce high cache ratios, and every mutation to the prefix resets the counter.

Cost tracking follows the same pattern. The $141.89 spent on cached input versus $49.87 on cache misses in the V4 Pro run shows that cache efficiency directly translates to dollars. When cache hit rates drop into the 20% range, as most agent loops produce, the effective cost per token doubles or triples. When they stay above 90%, the effective cost approaches the cached-input rate.

What actually changes the numbers

The source material identifies a few concrete factors:

  • Prefix stability: Keeping the same system prompt, reasoning structure, and file contents across turns is the single biggest factor. Any change at the beginning of the context window invalidates the entire prefix.

  • Timestamp injection: Several users noted that injecting fresh timestamps at the start of each turn breaks cache matching. If the model needs to know the current time, prepend it once and leave it undisturbed.

  • Tool call ordering: Reordering tool calls across turns breaks prefix matching for the rearranged portion. Keeping tool calls in a consistent order, or appending new calls rather than reordering existing ones, preserves more cache.

  • Context compaction: Pruning old tool call results while keeping the stable prefix intact can improve both cache hit rates and model performance by reducing noise. But aggressive pruning that removes parts of the prefix defeats the purpose.

  • Model-specific behavior: DeepSeek's caching architecture appears more forgiving than OpenCode's, which some users report has "really bad cache stability issues." The difference likely comes down to how each provider's API handles prefix matching and whether they offer TTL or invalidation controls.

The practical takeaway

If you're using a coding agent with DeepSeek and want to take advantage of cache pricing, the cheapest path is also the simplest: keep your system prompt stable, don't inject new timestamps on every turn, keep tool calls in a consistent order, and prune only the oldest tool call results when the context exceeds 40K tokens. Those four habits alone can push cache hit rates well above 90%, based on the user reports in the threads.

The bridge to use DeepSeek via Codex works fine for caching — it doesn't need to do anything special. The caching happens at the API level, and any harness that preserves prefix stability will get the benefit. The opposite, however: agents that rearrange, reorder, and randomize on each turn will systematically starve the cache, and the cost difference is real enough to show up on a monthly bill.

The DeepSeek native advantage isn't about the model being inherently better at caching. It's about the combination of a caching-friendly API and a harness that actually preserves the conditions caching requires. Get the harness right, and the savings follow.


Source: Hacker News item 48256953 (https://news.ycombinator.com/item?id=48256953)

deepseek cache strategy in coding agents

More blogs