PREFIXWISE
MIT license
PREFIXWISE OPEN SOURCE

Why did that AI call pay full price?

It names the one cause of every cache miss, prices it the way your provider bills it, and fixes the common ones. It runs on your machines and never stores prompts.

View on GitHub Integrations
# see it work: no key, no network
$ pip install prefixwise
$ prefixwise demo

# then on your own traffic
$ prefixwise import claude-code
$ prefixwise run -- claude
$ prefixwise serve --upstream https://api.anthropic.com
WHAT IT DOES

Every call, explained.

Names the causeOne of 19 causes per miss, from a timestamp in the instructions to a GPU that evicted the cache. Where the evidence is missing it says unknown, rather than guess.
Prices itEach call costed from the provider's own usage and a dated price table, and the cost of each completed task.
Fixes the common onesTool order and cache markers corrected before the request is sent, off by default, stepping aside if the provider rejects the change.
Guards releasesprefixwise check runs your agent's scenarios against a local stand-in and fails the build when a change would break reuse.
THE 19 CAUSES

The first thing that broke reuse.

Each request is compared with the previous turn of its own conversation, on the same model. The first difference in prompt order is the cause; anything later is kept as a secondary cause.

CauseWhat happenedWaste?Typical fix
volatile_systemOnly dates, times, ids or numbers changed in the instructionswasteMove them to the end of the prompt or the latest message, in one fixed format
tool_reserializeThe same tools, in a different order or serializationwasteSort tools, or let the proxy do it (canonical_tools)
history_editEarlier messages rewritten without a compactionwasteAppend; don’t rewrite history
tool_result_mutationAn earlier tool result re-rendered or removedwasteFreeze tool output once sent
retry_replayAn identical request within 60 seconds, nothing cachedwasteCheck the retry logic
breakpoint_misplacedAnthropic: no cache marker, or one left behind as the prompt grewwasteMark the end of the stable prefix, or use auto_breakpoints
system_editThe instructions were rewordeddependsBatch prompt edits; don’t reword every turn
param_renderedTool choice, thinking, reasoning or format changeddependsKeep those settings stable within a session
below_min_prefixShorter than the provider’s minimum cacheable promptdependsMark a prefix at least the model’s minimum
evictedThe engine dropped this conversation’s blocksdependsMore KV-cache memory, or an offload tier
ttl_expiredThe cache entry outlived its lifetimenot wasteCompact before long breaks, or a longer TTL
model_switchFirst call to a different modelnot waste—
compaction_rewriteThe context was compactednot wasteCompact at task boundaries, not every turn
tool_catalog_changeA tool was added or removednot wasteMask tools instead of removing them, if frequent
routing_missSent to a server that never saw the conversationnot wasteSession affinity or a stable prompt_cache_key
fork_boundaryFirst turn of a forked conversationnot waste—
first_writeFirst request of a session or conversationnot waste—
nonePrefix reused; only new content uncachednot waste—
unknownNot enough evidence; never guessed—Send session and TTL headers, or turn on engine events
How each cause is decided →
TEST RECORDS

It only tells you what is true.

TestSetupResult
False alarms on real conversations71,346 calls replayed through LiteLLM, GitHub and eight Hugging Face datasets0
Planted causes on open modelsQwen3-4B on vLLM, vLLM with LMCache, and SGLang120 / 120
Changes named at scale5,994 requests, six kinds of change injected at random1,772 seen, 0 misnamed
A public two-week traceReal provider traffic, read from cache-block fingerprints252,549 / 252,549 appends
Reasoning models re-read their last answerQwen3 and a DeepSeek-R1 distill on vLLM, engine events onnamed on 47/50 and 25/25 turns
Tool-order fix: saving deliveredSame sessions, fix off and on, on a hosted API and on vLLM97.5–99.9%
Every verdict on a real session10,517 Claude Code calls, checked against the raw logsall match
Proxy overhead2,000-token requests on a 4-CPU container2 to 4 ms
Under load300-second soak, one process33,358 requests, 0 errors

Not yet measured live: Gemini and Azure against the real services. Each record is in the repository under experiments/.

THE PAPER

Why Did the Prompt Cache Miss? Per-Request Cause Attribution for Agent Workloads

Tejaswi Chavali · Zenodo, 2026

The method, the evidence each cause needs, and the evaluation on real and injected agent traces.

Read the paper
CITE
@misc{chavali2026prefixwise,
  title     = {Why Did the Prompt Cache Miss? Per-Request
               Cause Attribution for Agent Workloads},
  author    = {Chavali, Tejaswi},
  year      = {2026},
  publisher = {Zenodo},
  doi       = {10.5281/zenodo.24331594}
}
WORKS WITH

Wherever your calls already go.

ProvidersAnthropic, OpenAI, Gemini, Bedrock, Vertex AI, Azure and Foundry, OpenAI-compatible APIs
AgentsClaude Code, Codex, opencode, any agent that reads a base URL
GatewaysLiteLLM, Envoy AI Gateway, Bifrost, Plano, Portkey
EnginesvLLM, SGLang, LMCache, HiCache, several replicas behind a router
OutPrometheus and Grafana, OpenTelemetry, ClickHouse, JSONL, an HTTP API
Setup for each →