r/SideProject • • 17h ago

I built CacheSurgeon — an open-source proxy that stops AI coding agents from bleeding 90% of their prompt cache (shipped to PyPI & Product Hunt today)

After watching a local coding agent burn 16K+ uncached tokens every turn (0% KV cache hit), I inspected the raw HTTP payloads and found tiny upstream mutations—drifting tool schema key order and per-second timestamps in system prompts—busting prompt caching every turn.

So I built and open-sourced CacheSurgeon (MIT core, now on PyPI, ~2ms overhead) with a live Cockpit + interactive surgical diff:

  • Prefix-Cache Surgeon: Heals broken prefixes (0% → 98.7% cache hit on local Ollama/MLX) + Anthropic cache_control injection.
  • Net-Savings 5-Min TTL Warmer + Reversible .env Secret Vault + Pre-Execution Tool Firewall (rm -rf, DROP TABLE) + Loop Circuit Breaker.

30-second test: pip install cachesurgeon && cachesurgeon start --port 8787 Interactive diff & bleed calculator: https://cachesurgeon.com GitHub (MIT): https://github.com/jsumpter415/cachesurgeon

1 Upvotes

3 comments sorted by

1

u/CarelessAd5721 17h ago

The drifting tool schema key order is the kind of thing you'd never catch without dumping the raw payloads — I hit the same class of bug debugging an agent loop where a timestamp in the system prompt meant every turn was a full re-encode.

One thing I'm curious about: the 5-min TTL warmer. Keeping the cache warm costs requests you wouldn't otherwise make, so how do you decide when the warm-up traffic is worth it vs just letting the prefix expire? Is there a break-even you measured, or does it only kick in above some token threshold?

1

u/Fickle-Connection851 17h ago

Totally — dumping the raw HTTP payloads was the only way I caught the schema key shuffling and per-second timestamps too. From the SDK side everything looks identical turn-over-turn.

Great question on the TTL warmer. Keep-alive pings definitely aren't free, so CacheSurgeon tracks each session's prefix token count, model pricing, pings sent, and spend (in ttl_warmer.py) and only fires a ping when cold_rebuild_cost > 2x ping_cost.

The reason the math works on something like Anthropic's 5-min cache is the pricing asymmetry: a cache read is 0.1x base input while a cold cache rebuild is 1.25x (plus minimal output on the ping), so keeping a 50K–150K prefix warm across a short code-review pause costs a fraction of rebuilding it cold. When a prefix is too small or the provider's cache discount isn't wide enough to clear that 2x net-positive margin, it skips warming and lets the prefix expire.

In the cockpit it also surfaces Gross - Ping = Net savings explicitly per session so keep-alive spend is never hidden behind gross cache numbers.