Tested liverun against the real serviceStand-intested against a local stand-in onlySupportedbuilt and tested in code, no live record yet
01Try it
No key, no network
See a scripted agent break its cache three ways, each miss named and priced, then run again with the fixes on. About five seconds.
Tested live
pip install prefixwise
prefixwise demo
# step through it, or use your own key (18 small billed calls)
prefixwise demo --step
prefixwise demo --live --provider anthropic
Needs Python 3.10 or later. Runs against a built-in stand-in; --live uses ANTHROPIC_API_KEY or OPENAI_API_KEY.
Supported
docker build -t prefixwise .
docker run -p 127.0.0.1:8080:8080 -v pw:/data prefixwise
# a real upstream instead of the stand-in
docker run -p 127.0.0.1:8080:8080 -v pw:/data \
-e PREFIXWISE_UPSTREAM_URL=https://api.openai.com prefixwise
From a clone of the repository. Starts the proxy in stand-in mode with the ledger at /data/prefixwise.db. With a key and an upstream it refuses to start until PREFIXWISE_AUTH_TOKEN is set.
02Coding agents
Claude Code, Codex, opencode, your own
Run the agent through a temporary local proxy. It keeps its own key and calls its provider as before; the report prints when it exits.
Tested live
export ANTHROPIC_API_KEY=sk-ant-...
prefixwise run -- claude
# the same, with the proven fixes applied
prefixwise run --optimize -- claude
prefixwise report --session latest
Needs an API key: a Pro or Max sign-in can’t be proxied, so read its logs instead (section 03).
Tested live
export OPENAI_API_KEY=sk-...
prefixwise run -- codex exec "fix the failing test"
Codex ignores OPENAI_BASE_URL, so run sets up a model provider for it. Signed in with ChatGPT? Import its logs instead.
Tested live
prefixwise run -- opencode run "add a --verbose flag"
opencode reads ANTHROPIC_BASE_URL, so nothing else is needed.
Tested live
prefixwise run -- python agent.py
# already behind a gateway? run forwards to it
ANTHROPIC_BASE_URL=https://gateway.internal prefixwise run -- python agent.py
Works with anything that reads ANTHROPIC_BASE_URL or OPENAI_BASE_URL. --optimize applies tool ordering and, on Anthropic, cache markers.
03Logs already on disk
No API key, no setup
Read the logs an agent or gateway already wrote. Nothing is sent anywhere, and re-running adds only new calls.
For Pro and Max. Reads ~/.claude/projects read-only, subagents included. Dollars are the API-price value, not what a subscription paid. Checked call by call on a real 10,517-call session.
The region in the URL sets the price. Claude on Vertex: set ANTHROPIC_VERTEX_BASE_URL=http://localhost:8080/v1 with --upstream https://aiplatform.googleapis.com.
Foundry: --upstream https://my-resource.services.ai.azure.com, and AnthropicFoundry(base_url="http://localhost:8080/anthropic/"). Deployments map to a zone for pricing. Not yet run against a live Azure resource.
DeepSeek, Groq, Together, OpenRouter and other OpenAI-compatible APIs are handled as a generic upstream. OpenRouter keeps the body session_id it routes on.
Amazon Bedrock goes through the Python wrapper, not the proxy (section 05).
05In your Python code
No proxy, one line
Wrap the client you already use. Calls go straight to the provider; each one is recorded after it returns. Sync and async, streams included.
Tested live
import prefixwise
from anthropic import Anthropic
client = prefixwise.wrap(Anthropic())
with prefixwise.session("fix-bug-42", task_id="fix-bug-42"):
client.messages.create(model=MODEL, max_tokens=1024, messages=msgs)
messages.create, beta.messages.create and the .stream() helpers. Nothing in the request is changed.
Claude via Anthropic, Vertex or Bedrock is judged by Anthropic’s rules; DeepSeek and Gemini are recorded too. LiteLLM response-cache hits are not.
Tested live: 20 of 20 live conversations named correctly, and 0 false alarms in 71,346 calls of real agent conversations replayed through it.
07Other gateways
Keep your gateway. Put Prefixwise behind it.
Point the gateway's provider address at prefixwise serve, which forwards to the real provider. Each was checked streamed and not, OpenAI and Anthropic formats, against a cache stand-in.
Forwarded with x-portkey-forward-headers. Non-streamed only: Portkey’s own streaming failed with or without us.
Stand-in
LiteLLM
Use the callback (section 06)
litellm_session_id or metadata.session_id
Tested live
Self-hosted routers: llm-d, vLLM production-stack, AIBrix and the SGLang model gateway pass the session id through; their routing headers pass by default.
08Your own GPUs
vLLM, SGLang, LMCache, HiCache
Read the engine’s own cache events, so an eviction or a request sent to the wrong replica is confirmed, not guessed.
--enable-cache-report is required; KV events need SGLang 0.5.20 or later. Pass --full-attention only for models with no sliding-window or Mamba layers. HiCache restores count as hits. Run live.
A prefix restored from LMCache counts as a hit, marked host_restored: 5 of 5 live. LMCache’s HTTP port moves to 8090 because 8080 is ours.
Tested live
# replicas behind a router
prefixwise serve --upstream http://router:8000 \
--kv-events tcp://replica-a:5557,tcp://replica-b:5557
# data-parallel ranks on one server
prefixwise serve --upstream http://127.0.0.1:8000 --kv-events auto
A turn served by a replica that never saw the conversation is a confirmed routing miss: 5 of 5 live with two vLLM replicas behind a router. Data-parallel auto is tested in code, not yet live.
pip install "prefixwise[engines]" for KV events. GET /engine shows the event stream’s health.
09Catch it in CI
Before a change ships
Run your agent’s test scenarios against a local stand-in, with no API calls. The build fails when a change would break reuse.
Tested live
prefixwise check -- pytest tests/agent_scenarios
# also fail on edited history, instructions or parameters
prefixwise check --strict -- pytest tests/agent_scenarios
Fails (exit 2) on tools sent in a new order, changing instructions, misplaced cache markers and edited tool results. Exit 1 when nothing was checked.
Inputs: command, fail-on, strict, allow, allow-empty, baseline, version, python-version. Writes the findings to the job summary.
Supported
# nothing to add: the plugin switches on under prefixwise check
prefixwise check -- pytest -x tests/agent_scenarios
# turn it off
pytest -p no:prefixwise
Labels each finding with the test that caused it, like scenario=tests/test_refactor.py::test_rename.
10Turn on the fixes
Proxy, run, or vLLM middleware
Fixes rewrite a request before it is sent, and step aside if the provider rejects the change. Each one predicts its saving from your own traffic first, so you know what it is worth before turning it on.
Tested live
prefixwise serve --upstream https://api.anthropic.com --optimize
prefixwise run --optimize -- claude
# what each would save on your traffic
prefixwise fixes
Turns on every fix with a live proof behind it, today tool ordering and, on Anthropic, cache markers. A new proven fix joins without a flag change.
Anthropic, for a client that sets no markers: adds them once the same start has been seen, never touching a client’s own. Live: 106.8% of the predicted saving, prompt cost down 85.6%.
Adds a stable prompt_cache_key per conversation, so similar conversations do not crowd one cache machine. Measured, not yet proven by our live bar, so --optimize leaves it off.
prefixwise fixes ranks them by what your ledger says each would save, and whether placed cache markers have paid for themselves.
11Sessions, tasks and prices
Sharper answers from what you already know
Optional headers tell Prefixwise which calls belong together and what they were for. They are stripped before the request is forwarded.
Without a session, Claude Code’s and Codex’s own ids are used, then x-session-id or an end-user id. Compaction and fork headers turn an unexplained miss into a named, legitimate one.
Join calls to a task, mark whether it succeeded, and the report gives the cost per successful task, failed attempts included. Compare two prompt or agent versions on that number.
Supported
prefixwise prices --json > my-prices.json
# edit the rates to your contract
PREFIXWISE_PRICES=my-prices.json prefixwise serve --upstream URL
prefixwise report --prices my-prices.json
Every dollar figure uses the dated public price table unless you give your own rates, for a discount or a self-hosted model.
12Fleet and data out
Many machines, one ledger, your own tools.
CollectorOne ledger for many proxies, wrappers and middleware. Records go over HTTP, up to 1,000 a batch.
HTTP API and exportSummaries, breakdowns, sessions and findings as JSON; rows out as JSONL or CSV.
GET /api/v1/summary
GET /api/v1/breakdown?by=cause
prefixwise export --format csv --since 7d -o rows.csv
13Privacy and security
Prompt text never leaves the process.
What is storedFingerprints of each part of a call, token counts, causes, prices and your own session, task and agent labels. Never prompt text or API keys. Exports never include the fingerprints.
prefixwise export --format jsonl -o rows.jsonl
Keyed fingerprintsSet a long random key so a short or predictable message cannot be guessed from its fingerprint.
PREFIXWISE_HMAC_KEY=<long random string>
Beyond localhostListens on 127.0.0.1 by default. To serve a team, require a token; with an API key set it refuses to listen wider without one.
A scripted agent breaks its cache three ways; each miss named and priced.
prefixwise run -- CMD
Run an agent through a temporary proxy; report when it exits. --optimize applies the fixes.
prefixwise import SOURCE
Read claude-code, codex or gateway logs into their own ledger.
prefixwise serve
The proxy, for an API or a vLLM / SGLang server.
prefixwise report
Summary, causes and fixes; --session for one conversation turn by turn; --share for a pasteable block.
prefixwise fixes
Fixes ranked by what your own traffic says each would save.
prefixwise check -- CMD
CI gate on a local stand-in; exit 2 when a change breaks reuse.
prefixwise explain FILES
Diagnose saved request bodies offline; --diff shows what changed.
prefixwise prices
The dated price table in use. Override with PREFIXWISE_PRICES.
prefixwise request ID
One ledger row, by the X-Prefixwise-Request-Id response header.
prefixwise task ID
Mark a task a success or failure, for cost per successful task.
prefixwise export
Rows as JSONL or CSV, or a --baseline file for check.
prefixwise prune
Delete rows older than an age, for retention.
prefixwise collect
A collector: one ledger for many machines.
Worked examples: eight real scenarios, from a bill that jumped to gating prompt changes in CI. Every command takes --help. Install extras: [engines] for KV events, [otel] for OpenTelemetry, [log] for the Redis record log.