Introduction¶
When running long sessions in DeepSeek Harness (dsh), if the system prompt, tool list, and the beginning of the context hit the prefix cache, subsequent turns are billed at the cached rate. Conversely, as long as the prefix is rewritten, the entire prefix is re-billed at the full uncached rate—this is a key constraint for saving token costs.
Looking at the inputTokens (uncached input) in the usage alone is insufficient to distinguish between “normal growth” (e.g., large tool results, long pastes) and a “true prefix rewrite.” In the former case, cacheRead often remains high; in the latter, while uncached tokens surge, cacheRead collapses relative to its own baseline. Below is an introduction to the plugin dsh-plugin-cache-guard by maintainer 121212165: it monitors the usage stream, uses a “three-gate” approach to separate the two patterns, estimates the extra tokens paid due to rewrites, and injects a diagnostic prompt into the model when a new prefix rewrite occurs.
What is this¶
dsh-plugin-cache-guard is an admin-security class plugin within the dsh ecosystem, currently at version 0.2.0 (MIT License). It does not modify model behavior but focuses on prefix cache health diagnostics: distinguishing between “prefix rewrites” and “growth,” and reporting hit rates, judgment tiers, and total overpayment.
It complements the series’ price-aware (how much was spent) and cost-ledger (ledger)—this plugin answers why the cost increased.
Directory Page: SkillHub - dsh-plugin-cache-guard
Source Code: GitHub - 121212165/dsh-plugin-cache-guard
Core Features¶
Three-Gate Spike Detection¶
A prefix rewrite is only recorded if all three of the following steps are satisfied simultaneously:
- Uncached input exceeds the sliding baseline band (Size Gate);
- The
cacheReadfor that step collapses relative to its own baseline (Shape Gate); - The re-billed amount is approximately equal to the entire prompt of the previous step (Attribution Gate).
Steps that only pass the size check (large tool results, long pastes: uncached tokens surge but the prefix still hits) are recorded as growth, not counted as jitter, and not included in waste calculations.
The three buckets in dsh usage are mutually exclusive (TokenUsage from @deepseek-ai/dsh-llm: inputTokens is solely uncached input; billable input = inputTokens + cacheReadTokens + cacheWriteTokens). During a true rewrite, cacheRead often collapses to 128 (a block-granularity residual, not 0), so the detection uses ratio collapse rather than cacheRead === 0.
/cache and cache_status¶
/cache: Displays hit rate, judgment (Stable / Watch / Unstable), rewrite details (including before/after collapse and the previous step’s prompt), number of growth steps, and total overpayment.cache_statusTool: Allows the agent to self-check cache health during the session.
adviseModel (Enabled by Default)¶
When a new prefix rewrite occurs, the plugin injects a diagnostic prompt into the context via tools/post-execute (using the same injection path as price-aware). It alerts only once per break to avoid spamming the context at every subsequent step. v0.2.0 uses the plugin:<name> source kind from session format v4, fixing a session interruption issue in v0.1.0 on dsh 0.1.7 caused by old source wrapping.
Configuration¶
Override the following fields in your profile as needed (default values from the README):
| Field | Default | Description |
|---|---|---|
window |
8 | Baseline sliding window (also the span for “recent”) |
factor |
2.5 | Size Gate: Uncached baseline multiplier |
floorTokens |
1500 | Size Gate: Absolute floor (to prevent false positives on normal growth) |
collapseRatio |
0.25 | Shape Gate: cacheRead below this ratio of its baseline is considered collapsed |
collapseMinBaseline |
2000 | Shape Gate: Baselines below this value are not judged for collapse |
rebillShare |
0.6 | Attribution Gate: Re-billed amount must reach this share of the previous step’s prompt |
adviseModel |
true |
In-context alerting |
The first window steps do not participate in judgment (as the sliding window is not yet full), so collapses during the cold start phase will not be reported.
Installation and Activation¶
The method provided in the README is: clone or npm install into the profile’s node_modules; installing from source requires running npm install in the plugin directory first (prepare will auto-build).
The runtime environment requires Node.js >= 22.18. The plugin mounts to the Cordis event surface via cordis.patch.yml (session/event → tools/post-execute injection). No unified dsh plugin add … one-liner command is provided in the materials; please refer to the repository README and your profile’s plugin loading method before installation.
For local verification, execute:
npm run check
This includes typecheck, 13 node --test unit tests, and build. For real usage replay:
npm run replay
On test/replay-corpus.json (39 sessions / 206 assistant/message records, containing only token buckets and turn/step data), out of 17 “accusations” by the old single-gate rule, 6 were downgraded to growth by the three-gate rule, while all 11 true rewrites were preserved.
Typical Usage¶
- Install and enable the plugin in your current dsh profile as described above.
- Observe usage during long sessions: if you suspect the prefix is being repeatedly rewritten, run
/cachein the client to view the judgment and rewrite details. - If you want the model to actively investigate, call
cache_statuswhen the tool list is available (running logs show the model calling it consecutively, outputting summaries like “Cache Hit Rate … · … steps · Status: ✅ Prefix Stable”).
When a new prefix rewrite occurs and adviseModel is true, the plugin injects a one-time diagnostic, removing the need to manually monitor inputTokens at every step.
Use Cases and Notes¶
Who it is for: dsh users relying on DeepSeek prefix caching, with long contexts, and frequent growth in tool results or pastes; specifically when distinguishing between “true rewrite waste” and “normal growth.”
Permissions and Security: The plugin runs with the permissions of the current dsh process. It reads the session usage stream and injects context upon rewrites. Before installation, you should read the Source Repository and the MIT License to ensure compliance with your environment policies. The SkillHub directory is a community site and has no official affiliation with DeepSeek / High-Flyer.
Known Limitations (Key points from the README):
- Judgment relies solely on the usage stream and cannot distinguish between “header re-rendering” and “cache TTL expiration”—the billing shape is identical, and the prompt only marks turn/step boundaries.
cacheWriteTokensis included in prompt and hit rate calculations but not in rewrite judgment (some relays do not return this field).- Sustained breaks are absorbed by the sliding baseline: If the context collapses at every step within the same session, it may only report the first few times, after which the count silences. In this case, look at the hit rate and step-by-step
cacheReaddetails, rather than focusing solely on the rewrite count.
Conclusion¶
dsh-plugin-cache-guard transforms prefix caching from a panic-inducing “uncached numbers are rising” scenario into an explainable three-gate diagnosis. With /cache, cache_status, and one-time adviseModel alerts, it helps monitor prefix stability during long sessions.