Last week I wrote about the trust-and-cost side of DeepSeeker-Code — the ledger, the budget caps, the night shift (part one). A reader then asked a sharp question: the batch runner gives every task its own git worktree, and tool results embed absolute paths — doesn't that break prompt caching across tasks, since cache hits need byte-identical prefixes?
I knew the answer, because I had run the experiments in September and never published them. And the answer is more interesting than the question: the prompt cache is not a byte-prefix you can bisect. It's block-granular — and one wrong byte inside the block costs you the whole block.
That one measured fact explains half the "caching ghosts" I had been chasing. Here are the four experiments, with real numbers.
The harness: real loop, real API, raw fields
The agent keeps full traces of every run. Each request logs fingerprints — sysHash (sha1 of the system message), toolsHash (sha1 of the tool specs), sumHash (summary slot) — and each response records the raw usage the API returned: prompt tokens and cache-hit tokens, as reported, not derived. A test script drives the real agent loop with the full 44-tool table against the real API. No mocks. Fresh process, fresh session, every run.
R1 — baseline. Re-run the same prompt minutes apart in a brand-new process: first-round hit 99.8% (11,264 of 11,285 prompt tokens). One number, two conclusions: the vendor's cache persists across sessions and processes, and it is byte-exact — when the prefix really is byte-identical.
The experiment that broke my mental model
Byte-level intuition says: a change at the tail of the prefix should only invalidate the tail. Change ~100 bytes at byte ~11,000 and you'd expect most of the cached prefix to survive.
R4 flips exactly that: one fence inside the system prompt — the language rule ("reply in Chinese" vs "reply in English") — roughly 100 bytes at the tail of message[0]. Same question, same everything else.
Measured first-round hit: 0%. Not 75%. Not "the part after the divergence". The entire block died and was re-prefilled at full price.
R2 locates the boundary. Same project, different question: hit = 11,264 exactly — the whole block — and the miss is ~19 tokens: just the user message. So the first cacheable unit ends right after the summary slot, and the task text lives beyond it. (Side finding: tools are serialized into the request before the messages — the block includes them.)
R3 changes the project directory entirely: fingerprints byte-identical, 99.8% again. The directory is not, by itself, a divergence source — in this harness, project state enters the prefix only through injected indexes (memory, skills), which is the next section's story.
For scale: the fixed prefix is ≈12.7K real tokens per fresh session, ~9.4K of which is tool schemas (44 tools). The first cacheable block is ≈11.3K tokens: tools + system prompt + summary slot, as one unit.
What the block model explains
Once you accept "a byte inside the block is worth as much as the whole block", several ghosts get names:
Real-world fresh sessions hit ~15%, not 99.8%. Six weeks of traces settled it: toolsHash was constant, sysHash differed every session — the injected indexes (memory index, skills list) drift across sessions, and any drift inside the block kills it. The shared head shrank to ~2K tokens. Cost came out to ≈¥0.02 per session, which I judged structural and left alone — but at least it had a name.
The silent-killer list. Language/output-style switches (user-initiated, rare — acceptable). Skills/agents directory updates — readdir order used to flip bytes; sort-deterministic injection fixed it. MCP servers connecting or disconnecting (tool table change = new block). Env-gated tools flipping. Memory-index drift. And compaction rewriting history — the biggest single collapse I've measured: one compaction event, −163K tokens re-prefilled, next-round hit 12.8%. Making compaction cache-aware (timing it by hit rate) beat every compaction-efficiency trick I tried.
The parallel-worker trap. N concurrent agents with byte-identical fingerprints still miss each other: cache writes take seconds to become visible. Parallel same-hash workers measured 0–14% mutual hits; the identical chain run serially hits 95–98%. If you fan out N workers, budget N full-price first rounds.
Historical reasoning. Assistant reasoning_content was 25–29% of resent input across turns. Stripping it entirely on the wire keeps the prefix byte-stable; keeping "just the latest turn" would flip bytes every turn and kill the block. (Verify against your provider before copying this one.)
So: do worktree paths break caching?
Back to the reader's question. No — because of where the divergence sits.
Each batch task is a fresh conversation whose first user message — the task description — differs from every other task's. That is the first divergence, and it sits below the fixed block. The block itself is byte-stable by construction (templated system prompt, machine-constant paths, session-frozen injections), and serial runs hit 95–98% on it. Everything the worktree path strings would pollute — tool results — lies below the first divergence: the region that never hits across tasks anyway, because the task texts differ first. Within a task, the path is constant.
So a fixed-path mount would buy ≈0 in this design. It only starts paying if tasks share large identical prompts, which batch tickets never do. Sometimes the loss is bounded not because you fixed something, but because of where the divergence point sits.
Two things worth copying
Record the raw hit/miss fields per request. Don't derive them from totals: caching is block-quantized, so a short tail after the last block boundary never caches, and a derived metric happily reads it as normal input. Raw fields plus fingerprints are what let me bisect every anomaly to a specific cause.
Audit cost with real billing weights. Cache-hit tokens cost roughly 10× less than misses; an aggregate console number cannot tell you which turn burned the money. That's the ledger half — part one.
The whole thing is one open-source repo — DeepSeeker-Code — VS Code extension, terminal CLI, and local API sharing one engine. If you build agents on DeepSeek, the two posts together are the complete playbook: know what it costs, know why the cache dies, and instrument the raw numbers before you optimize anything.
后续:一位读者把这套信任设计盘了十轮——九问九答的完整访谈:Ten rounds with a sharp reader: how approval memory ages in a single-user AI coding agent
Editor's note (Dec 4, 2026): I re-ran R4 on today's build — fresh process, real API, four calls. The 0% did not reproduce. Flipping only the locale fence (per-segment trace hashes confirm: exactly one segment changed) lands at 17% (2,176/12,954), and the surviving slice is exactly the bytes before the flipped fence — the template header, ≈2.2K tokens. A Chinese control run immediately after recovered to 99% (12,800/12,947), ruling out eviction. So the stronger claim in this post — one wrong byte costs the whole block — overreached: matching is in-order and partial; bytes before the divergence survive. The hits still land on 64-token multiples (2,176 = 34×64, 12,800 = 200×64), so the block-quantized accounting stands; what doesn't is atomicity. Also revised: the tool schemas now measure as serialized after the system message, not before the messages as R2's side finding had it. The September 0% didn't reproduce and I haven't spent calls chasing why — could be build drift, could be server-side. Anything I publish next will use the corrected model.





Top comments (4)
This matches something we kept hitting and couldn't explain cleanly until we started logging raw cache-hit tokens the way you did — deriving the number instead of reading the API's reported field is how half the "caching ghosts" get created in the first place. The block-granular framing is the piece I was missing.
The absolute-paths-in-tool-results detail is the quietly expensive one. We saw the same class of problem with per-task worktrees and with timestamps sneaking into system context. The fix that bought us the most was hoisting everything volatile below the stable prefix — tools, system, and any summary slot get frozen and ordered first, and anything per-task (paths, ids, clocks) goes last so a miss only invalidates the tail. Did you find the block boundary was stable enough to engineer around deliberately, or did it move between model versions? If it's stable, "pack your prefix to the block edge" becomes a real optimization rather than a hope.
Stable enough to engineer around, with one honest caveat: it's reverse-engineered, not documented, so we never hard-code the number.
Within a model version it held across months — the ≈11.3K block I measured in September reproduced in November: the same fixed head (tools + system + summary, byte-stable by construction) was still hitting 95–98% on serial same-prefix chains. So on a fixed version we treat the boundary as a fact.
But the way we exploit it isn't really "pack to the edge" against a measured boundary — it's choosing what lives inside it by construction. We serialize tool specs first, freeze the system prompt and the summary slot per session, and keep everything volatile below; the boundary lands where that layout puts it. Our stable head estimates at ≈12.7K while the measured block came out ≈11.3K, so some seam is lost to write quantization — bounded and boring, so we never chased it. Padding the prefix to fill a block perfectly would optimize a number we can't see; keeping the block 100% full of content we actually reuse (schemas + persona + frozen state) gets the same win without depending on internals.
Across model versions: unknown by design — that's what the instrumentation is for. Nothing in the architecture depends on 11.3K. If an upgrade moves or dissolves the block, the symptom is unmistakable: first-round hits collapse while sysHash/toolsHash stay identical, and that page gets flagged within a day. The number is allowed to move; the alarm doesn't care.
One refinement on "a miss only invalidates the tail": that's true only below the block. Inside it, a tail byte kills the whole thing — R4's 0% was ~100 bytes at the very end of the system prompt. Your layout (volatile last) already respects the boundary, so you're safe; just keep the distinction explicit in your head, because "it's a tiny tail change in the system prompt" is precisely the trap the locale fence sprang. Your timestamps-in-system-context point is the same disease: we freeze injected indexes at session start specifically so mid-session clock and format flips can't rewrite bytes.
Logging sysHash tells you the block died, not which section drifted it. In your harness the injected index is memory + skills, two independent generators — hash them separately (memHash, skillsHash) and a bad session becomes one query instead of a bisect by hand. Same for the tool table: toolsHash is constant until one MCP server reconnects, so a per-server hash names the culprit directly.
Worth folding into the cost model: 9.4K of the 11.3K block is tool schemas, so trimming tools pays twice — on every request, and again on every full re-prefill after an invalidation. R4's 0% is exactly why that second half matters. Does your ledger record the hit-rate drop at compaction time, or do you reconstruct it afterwards from the traces?
On splitting the hashes: I argued the coarse hash wins for one machine — I've since changed my mind, and it cost ~20 lines. The injections aren't two generators (memory/skills); message[0] is a fixed template plus six fence-delimited blocks (locale, output style, skill catalog, agent catalog, project guide, memory index), so I cut by fence generically: the trace now carries [name, sha1] per segment in order (tpl = template header) plus a tool count. When sysHash moves, the changed segment name is the first line of the answer — no bisect, even solo. Your per-MCP-server tool hashes stay un-adopted on purpose (tool count + connect/disconnect events are enough forensics for one machine). The raw hit/miss fields paragraph is the part I'd defend at any scale, unchanged.
And a retest while I was in there: the September R4 (flip only the locale fence → 0%) didn't reproduce on today's build. The hit lands at 17% (2,176/12,954) — exactly the template header before the flip — and a control run immediately after recovers 99% (12,800/12,947). So the article's "one wrong byte costs the whole block" overreached: matching is in-order and partial, bytes before the divergence survive; hits still land on 64-token multiples, so the block-quantized accounting holds and atomicity doesn't. I've added an editor's note on the article rather than rewriting it. The per-segment hashes are what caught it — with the old single sysHash I'd have just seen "system changed" again and kept believing the old model.
On tools paying twice: exactly, and it's already been acted on once — 44 tools went 38.0KB → 34.2KB of schema (~10%), with a deliberate stop there because the remaining prose was <100 tokens per item with real contract risk. Every one of those tokens sits inside the block, so it's saved on every first round and again on every R4-style full re-prefill, which is precisely the second half of your point. And since then a size gate went in (total schema bytes ≤ 45KB in CI), so the silent regrowth that crept it back to 41.6KB can't happen unnoticed again.
On compaction: reconstructed from traces, not recorded in-band. The per-request hit/miss fields and the compaction events (with before/after sizes) both land in the trace, and the "−163K, next-round hit 12.8%" figure came from joining them afterwards. Reconstruction is exact for what happened; what it can't give me is the counterfactual — what the session would have hit had compaction not fired — so I don't lean on the ledger for that decision at all. Compaction timing is driven by live hit rate (cache-aware: high hit rate defers, low hit rate triggers earlier), which is the only place the number exists before the fact.