r/ChatGPTCoding • u/BringinItDirty • 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].nameat byte 1,670). - Root-cause classification: automatically flags
TOOL_REORDER,TOOL_MUTATED,VOLATILE_SYSTEM,PREFIX_TOO_SHORT(<1,024 tokens on OpenAI),TTL_EXPIRED, andMODEL_CHANGED, and tallies wasted spend in a local UI at127.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 sortstools[]and tool-schema object keys so flaky MCP clients keep hitting the cache, while never touchingmessages[],input[],content[], or schemaenum[].
Quick commands:
# Pre-seeded 16-turn demo UI (no API key needed)
# Wrap Codex CLI or Claude Code (zero-mutation passthrough)
# Opt-in live healing
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.
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
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.
3
u/ByronScottJones 5d ago
https://github.com/flipcyde101/kvdiff