r/ChatGPTCoding • • 7d ago

Resources And Tips I built kvdiff (MIT): a local proxy that catches prompt-cache busts across Codex, Claude Code, and local LLMs byte-by-byte

Full disclosure: I built this open-source tool (MIT, https://github.com/flipcyde101/kvdiff).

If you use AI coding agents like Codex, Claude Code, or local llama.cpp / vLLM setups with MCP servers, a subtle quirk of LLM prompt caching can quietly multiply your token burn and eat through 5h/7d rate limits:

Anthropic, OpenAI (1,024-token prefix floor), and local KV-cache servers require an exact byte-for-byte prefix match from turn to turn. If an MCP client or harness reorders tools[], mutates a tool schema key order, or injects a volatile timestamp into instructions / system, your cache hit rate collapses from ~94% to 0%—forcing full prompt re-evaluation on every turn. Because CLI transcripts do not expose the raw wire JSON, it is almost impossible to tell which field invalidated the prefix.

How kvdiff diagnoses and fixes it:

  • Byte-level Longest Common Prefix (LCP): sits locally between your coding agent and the upstream API, computes the exact zero-based UTF-8 byte where consecutive turns diverge, and maps it to a JSONPath (e.g. $.tools[9].name at byte 1,670).
  • Root-cause classification: automatically flags TOOL_REORDER, TOOL_MUTATED, VOLATILE_SYSTEM, PREFIX_TOO_SHORT (<1,024 tokens on OpenAI), TTL_EXPIRED, and MODEL_CHANGED, and tallies wasted spend in a local UI at 127.0.0.1:4318.
  • Zero-mutation passthrough by default: preserves OAuth and beta headers and flushes SSE chunks immediately with zero buffering.
  • Opt-in live healing (--heal): deterministically sorts tools[] and tool-schema object keys so flaky MCP clients keep hitting the cache, while never touching messages[], input[], content[], or schema enum[].

Quick commands:

# Pre-seeded 16-turn demo UI (no API key needed)
npx @drod915/kvdiff --demo

# Wrap Codex CLI or Claude Code (zero-mutation passthrough)
npx @drod915/kvdiff -- codex
npx @drod915/kvdiff -- claude

# Opt-in live healing
npx @drod915/kvdiff --heal -- claude

Technical details & caveats: 63 kB package, Node >= 24 required (uses built-in node:sqlite with zero native addons), binds strictly to 127.0.0.1, zero telemetry, and only inspects live proxied traffic. Curious which MCP servers or coding harnesses are causing the worst cache misses in your setup.

11 Upvotes

12 comments sorted by

4

u/Far_Reindeer8428 7d ago

byte level diff is what made cache misses debuggable for us, a timestamp in the system prompt was the culprit twice. i built oah with the same gate idea, repo is public, star it if it helps https://github.com/KenSuh/open-agent-harness

1

u/BringinItDirty 5d ago

Appreciate the Open Agent Harness link. Which coding agent were you using, and did those timestamp-related misses happen on the first request after compaction?

1

u/Far_Reindeer8428 4d ago

mostly claude code, some codex. the misses clustered right after compaction, which is what made me think the summary was dropping the timestamps rather than the model ignoring them

1

u/BringinItDirty 3d ago

That timing gives us a good place to look. Did you get to check whether the timestamp was still in the first request after compaction? If it had disappeared by then, that would explain why it stopped showing up in the answers. I’d check the cache-read count on that same request too.

2

u/Poprouter 6d ago

history trimming is a surprisingly easy cache trap. when the harness drops older messages, the prefix can shift and later turns can pay for a full prefill, with misses appearing at intervals instead of on every turn.

1

u/BringinItDirty 5d ago

That timing is a useful clue. Which coding agent are you using, and do the cache misses happen on the first request right after compaction?

1

u/Poprouter 4d ago

mostly claude code here, curious what you see

1

u/BringinItDirty 3d ago

With Claude Code, I’d check whether cache-read tokens drop on the first request after compaction and recover on later turns. That gives us a specific pair of requests to compare. Are you seeing the misses in the token counters, or mostly noticing slower turns?