Most tools handle context compaction and prompt caching separately, or link them with fixed rules. But each one changes what the other is worth:
- Compacting rewrites the prompt prefix, so it throws away the cache you were paying to keep.
- Keeping a cache warm costs in proportion to the prefix, so it depends on what you kept.
They should be decided together, with two goals: the lowest cost per run, and a request that still holds what the model needs.
What it costs as a chat grows
A modeled 30-turn chat on Claude Sonnet 5.5. It is retrieval-heavy: 25k tokens of retrieved text per turn. Sending the whole history with prompt caching, each turn costs more as the chat grows: from 8 cents to about a dollar. OpenClaw's defaults level off around 28 cents once it starts clearing old tool results. compache stays near 12 cents a turn. For the whole chat: $16.48, $7.69 and $3.47. Lighter chats cost less on every line, and reach OpenClaw's thresholds later or not at all.
The best policy is different for every app
It depends on how long your users are away, what later turns need from earlier ones, and your model's prices and cache rules. Same chats and same model, fitted for three kinds of users:
| Users come back after | What the fit chose |
|---|
| 30 seconds | 5-minute cache, no pings |
| 4 minutes | 5-minute cache, up to 3 pings |
| 30 minutes | 1-hour cache |
Modeled on conversations the fit had not seen, that cost 30% to 70% less than the best cache-only setting.
compache fits two things on your own traces.
Fit 1: what to keep when compacting
A turn adds three kinds of content: the question, the retrieved text and the answer. From your traces, compache measures two things for each kind: how many tokens it adds, and how far back later turns reach for it. Content that is large and rarely needed again goes first. Content that is small or often needed stays.
In the sample chats, retrieved text is 95% of the history, but only about one turn in eight needs any of it again. Questions and answers are the other 5%, so keeping them is nearly free. The fit keeps questions for 4 turns, answers for 9 and retrieved text for 1. With that, 82% of the turns that need history still have it in the request. The rest fetch it back, and that call is in the cost.
Fit 2: when to keep the cache warm, when to compact
From the timestamps in your traces, compache measures how long users stay away at each point in a conversation. With that it makes two decisions:
- After every answer: let the cache lapse, or send a tiny ping to keep it warm. A ping is sent when that costs less than losing the cache.
- On every new message: send the history as it is, or cut. A cut waits for a return that finds the cache expired. The prefix has to be written again then anyway, so the cut costs nothing extra.
In one 26-turn sample chat, the user was back within the 5-minute cache lifetime 13 times, a ping bridged a longer pause 4 times, and the history was cut on 8 returns to an expired cache. The last request was 25k tokens instead of 89k.
The calculator
Neither fit is tuned by hand. Every input can be measured from your traces or read from a price sheet: tokens per turn, thread length, time away, what later turns need, prices, cache lifetime. compache prices every candidate policy on your own conversations by the provider's cache rules, and the cheapest one wins. So it is a calculation, not a guess.
The cost calculator does the same in your browser. Give it your workload and it shows the cheapest policy. Click any turn of a conversation to see the expected cost of cutting or not, and of each number of pings. You can drop a chat log on it: it is read in your browser and sent nowhere.
Try it
pip install compache
compache demo # four sample chats, no API key
In your own app it is one line around the client you already have:
client = compache.wrap(Anthropic(), "policy.json")
You first run it in record mode, which keeps sizes and timings and never text. Then compache fit writes the policy that this line applies. For a coding agent: compache wrap claude.
Live, as billed on Claude Haiku 5.5: $2.68 â $1.45 per 1,000 messages for a document chatbot, with answers right as often. Where it does not help: short chats, and models whose cache outlasts your users' breaks (2% to 5% on GPT-6 Luna).
Repo (Apache-2.0, no dependencies): https://github.com/BarendPotijk/compache