r/codex • • 5d ago

Showcase I built kvdiff (MIT): local wire-level proxy for debugging OpenAI Responses API prompt-cache misses in Codex

Full disclosure: I built this (https://github.com/flipcyde101/kvdiff).

Prompt caching on the OpenAI Responses and Chat APIs requires an exact prefix match past the 1,024-token floor. If a tool list reorders, instructions mutates slightly, or a prefix drops under 1,024 tokens, cached_tokens quietly collapses to 0 and burns through your quota and spend.

I built kvdiff to wrap Codex with zero code changes:

# Try pre-seeded demo UI (no API key needed)
npx @drod915/kvdiff --demo

# Wrap Codex CLI (strict zero-mutation passthrough by default)
npx @drod915/kvdiff -- codex

# Opt-in deterministic tool-schema sorting
npx @drod915/kvdiff --heal -- codex

It computes the UTF-8 byte-level Longest Common Prefix between consecutive turns, maps the divergent byte directly to its JSONPath ($.tools[9].name, $.instructions, etc.), flags TOOL_REORDER, TOOL_MUTATED, VOLATILE_SYSTEM, PREFIX_TOO_SHORT (<1,024 tokens), and TTL_EXPIRED, and tracks wasted dollars in a local UI at 127.0.0.1:4318.

Technical guardrails: 63 kB tarball, Node 24 built-in node:sqlite (zero native addons), strict 127.0.0.1 bind, zero telemetry, zero-buffering SSE flush, and --heal never reorders input[], messages[], or schema enum[]. Let me know if you hit any Codex Responses API edge cases.

0 Upvotes

5 comments sorted by

•

u/dexterthebot 5d ago

You might want to consider listing your project on the weekly Show-Us-What-You-Built post. Look out for it on Tuesday/Wednesday. Highest commented project wins a week promotion on r/Codex and gets on the Hall of Fame sidebar. See below for last week's winner.**


Light Wars AR: real-life laser tag app with no extra gear. Your phone is the gun and your room is the map. Play up to 6 friends in the same room, or fight drone waves and bosses solo. The vision models run on the phone, and camera frames never leave it. Free on iPhone and Android.

Website: https://lightwarsar.com

iPhone: https://apps.apple.com/us/app/light-wars-ar-laser-tag-party/id6508168951

Android: https://play.google.com/store/apps/details?id=com.lasertag.lightwars.

Contact: contact@lightwarsar.com

1

u/quarrelau 5d ago

Do you have any stats on how effective --heal is in practice?

Cool idea and project.

2

u/BringinItDirty 3d ago

Only synthetic demo numbers so far. The 94% to 0% to 97% sequence and $0.38 in the clip are fixture data. For a real benchmark, I’d run the same tool shuffle with and without --heal, hold the model, prompt, and cache timing fixed, and compare the provider’s cached input tokens.

1

u/VShulcz 4d ago

Does --heal only sort the top-level tools array by name, or does it canonicalize nested JSON schema keys inside each tool too? I've seen MCP servers emit tool input schemas with non-deterministic property ordering after a reconnect, which breaks prefix caching even when the tool list order stays the same. Mapping the first divergent byte straight to JSONPath is a neat way to spot that.

1

u/BringinItDirty 3d ago

Both. On the Anthropic and OpenAI paths, --heal sorts the outgoing tools[] list by tool name and recursively sorts object keys within each definition, including nested schema properties. It preserves the order of arrays inside the schema, including required and enum. A reconnect that only shuffles schema keys is exactly the case this can normalize.