跪拜 Guibai
← All articles
Agent · DeepSeek · AI Programming

How Reasonix Keeps DeepSeek Prefix Cache Hits at 99%

By HLAIA光子 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

DeepSeek's cache-hit pricing can cut token costs by 90%, but most integrations leak hits through small prefix mutations. Reasonix's approach—immutable prefixes, tail-appended state, and CI-enforced cache discipline—is a replicable blueprint for any project that wants to stop burning money on avoidable cache misses.

Summary

DeepSeek's automatic prefix caching slashes API costs for repeated prompt prefixes, but most projects see hit rates of 20–30%. Reasonix, an agent harness built specifically for DeepSeek, routinely hits 99% by treating the prompt prefix as immutable infrastructure. The base prompt, output style, language policy, persistent memory, skill index, and tool schemas are assembled once at boot and never touched mid-session.

Every state change that the model must know about—plan mode toggles, mid-session memory additions, background job completions—gets wrapped in XML blocks and prepended to the current user message, at the tail of the prompt. The stable prefix stays cache-warm; only the small tail misses. At the provider layer, Reasonix also patches a DeepSeek thinking-mode edge case where dropping `reasoning_content` on a tool-call replay causes a 400 error.

Engineering discipline locks in the gains. A run-loop diagnostic reports per-turn prefix changes and reasons for misses. CI scripts block any PR that touches cache-sensitive files unless the PR body explicitly declares cache impact, rejecting placeholder answers like "n/a" or "todo." For dual-model planner-executor setups, Reasonix keeps sessions fully independent so neither model's prefix is disturbed by the other's turns.

Takeaways
DeepSeek's automatic prefix cache matches from the first token; any divergence anywhere in the prefix invalidates everything after it.
Reasonix assembles the full system prompt prefix—base prompt, output style, language, memory, skill index, and tool schemas—once at boot and never mutates it mid-session.
Mid-session state changes like plan mode toggles, memory additions, and background job notifications are wrapped in XML blocks and prepended to the current user message, keeping them in the prompt tail.
Plan mode does not remove write tools from the schema; it intercepts non-read-only tool calls at execution time so the tool list stays cache-stable.
DeepSeek's thinking mode requires `reasoning_content` to be sent back on any historical assistant turn that carries tool calls, or the API returns a 400 error. Reasonix only sends it back on the specific turn that needs it.
A per-turn prefix diagnostic reports `PrefixChanged` and `PrefixChangeReasons` so developers can see exactly why a cache miss occurred.
CI cache-guard scripts block PRs that modify cache-sensitive directories unless the PR body includes explicit, non-placeholder `Cache-impact:` and `Cache-guard:` declarations.
Dual-model planner-executor setups use independent sessions with no shared history, so switching models never disturbs either prefix.
The cache-hit zone—system prompt, tools, and append-only history—can reach hundreds of thousands of tokens, while the miss zone is only the few hundred tokens of the current turn's tail.
Conclusions

Most projects leak DeepSeek cache hits not through architectural flaws but through undisciplined prefix mutation—a memory note slipped into the system prompt, a tool schema tweaked mid-session. Reasonix shows that the real lever is organizational: making cache impact a required field on every PR.

Reasonix's plan-mode implementation is a sharp example of doing the simple thing that works: don't mutate the tool schema to disable writes, just intercept the call and let the model see the error. The schema stays cache-stable, and the model adapts.

The cumulative hit-rate metric that survives log compression is an underappreciated detail. Resetting the counter on compression would hide real degradation behind a misleading number.

Independent sessions for planner and executor solve a problem that many multi-agent systems ignore: model-switching inside a shared conversation destroys prefix cache entirely. The cost of separate sessions is trivial compared to the cache savings.

Concepts & terms
Prefix caching
An LLM API optimization where the service caches the key-value (KV) computations for the initial tokens of a prompt. When a subsequent request shares the exact same prefix, those tokens are served from cache at a steep discount (often 90% cheaper). DeepSeek applies this automatically without explicit markers.
Byte-stable prefix
A prompt prefix that remains identical at the byte level across multiple API requests. Any change—even a single character—breaks the prefix match and causes a cache miss from that point onward.
Agent harness
The orchestration layer around an LLM that manages sessions, tool calling, memory, and multi-turn loops. It sits between application code and the model provider's API.
Reasoning content (reasoning_content)
In DeepSeek's thinking mode, the model's internal chain-of-thought tokens that precede the final response. The API requires this field to be included when replaying a historical assistant message that contains tool calls, or it returns a 400 error.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗