compache
Stop paying for your whole chat history on every message.
compache sits between your chatbot or agent and the model API. It learns from your own conversations which parts of the history still matter and when your users come back, then sends less and caches better. The rest of your application stays as it is.
Get started · Coding agents · Works with · Results · Cost calculator · Docs
Why long chats get expensive
Models have no memory, so your app sends the whole conversation with every message: each question, each answer, each document it ever retrieved. There are two ways to pay less, and they work against each other:
- Prompt caching makes repeated text cheap, until the user takes a break and the cache expires. On Claude that takes five minutes.
- Trimming (compaction) sends less text, but rewriting the history throws the cache away, and the model can lose something it needs.
compache is compaction + cache, decided together:
- Trims at the right moment. When the cache has expired, the history has to be paid for in full anyway. That is when compache trims it.
- Keeps the cache warm. While a user is away for a short break, a tiny request (a ping) keeps the cache alive, if that costs less than losing it. Where your users stay away longer, it asks Claude for its one-hour cache instead.
- Keeps the rest in reach. Trimmed text leaves a short notice. When the model needs it, it can fetch it back before your app sees the reply.
Get started
pip install compache
1. Record. One line around the client you already have. Your requests and replies do not change.
import compache
from anthropic import Anthropic
client = compache.wrap(Anthropic(), record="compache-record", sample=0.1)
The record holds sizes, times and token usage, never text. For one message in ten, the model is asked afterwards which earlier parts its reply needed, which adds about 5% to the bill while you record. In our runs some 600 messages were enough.
2. Fit. compache prices every candidate setting on your own traffic and prints what you would save, before anything in your app changes.
compache fit compache-record --model claude-haiku-5-5
3. Apply. Same line, with the result.
client = compache.wrap(Anthropic(), "policy.json")
That is all. There are no thresholds to tune. Or have your coding agent put it in, with /plugin install compache --marketplace BarendPotijk/compache in Claude Code and npx skills add BarendPotijk/compache in any other.
Try it now, on four sample chats in a few seconds, without an API key
compache demo -o policy.json
Fitted on 4 sample threads, 71 turns (71 labelled) for claude-sonnet-5-5.
Keep questions from the last 4 turns, answers from the last 9 turns and retrieved text from the last turn.
Send up to 1 keep-warm ping while a user is away.
82% of what the labelled turns need from earlier turns stays in view. The rest is fetched back, and that call is in the cost.
$16.10 per 1,000 turns against $50.25 keeping everything: 68% less.
Caching alone, at its best, would cost $18.66.
Wrote policy.json
In a clone of this repository, python examples/quickstart/apply.py then plays one of those chats, the one in the figure above, message by message.
Already have chat logs? Skip the recording
compache pull langfuse -o threads.jsonl.gz # or: mlflow, otel. Your own chat log works as it is
compache fit threads.jsonl.gz --labels labels.jsonl --model claude-haiku-5-5
Labels say, for a sample of messages, which earlier parts were needed. A coding-agent skill writes them. Without labels compache keeps the whole history and tunes the pings. Guide
Coding agents
compache wrap claude # or codex, opencode, gemini, copilot, goose, or any command after --
An agent sends every tool result again with every call. compache keeps the newest ones in view, lets older ones go when that is cheap, keeps the cache through long tool runs and breaks, and fetches a result back when the model asks for it. It starts from what 174 public runs taught and goes on from your own sessions. There is nothing to set. Agents
As many tests passed and issues solved. Where the price does not jump with the size of a request, as on Claude Sonnet 5.5, it saves 5% to 43%, modeled, by how an agent is worked with. compache stats says what it saved. In your own agent it is one argument: compache.wrap(client, learn="compache-record").
Works with
| Your stack | What you add |
|---|---|
| Claude Code, Codex, OpenCode, Gemini CLI, Qwen Code, Copilot CLI, Aider, Goose, Crush, Kilo, pi, OpenHands and other agents on the command line | compache wrap <agent> |
| OpenAI, Anthropic and Google GenAI SDKs, sync, async and streaming | compache.wrap(client, "policy.json") |
| LangChain and LangGraph, any chat model | compache.langchain.middleware(...) |
| OpenAI Agents SDK, Claude Agent SDK, Google ADK, Pydantic AI, LlamaIndex, Microsoft Agent Framework, Semantic Kernel, Agno, AutoGen, Strands, Haystack, Langfuse's OpenAI drop-in | One line each |
| LiteLLM | compache.litellm.install(...), or a callback in your LiteLLM proxy |
| Hermes | context.engine: compache |
| Anything with a base URL: the Vercel AI SDK, OpenClaw and other agent harnesses | compache proxy --policy policy.json |
| Your own prompt assembly | policy.session(thread_id) |
Everything runs in your own process and talks only to your model provider. No state is kept outside the process, so trimming works in serverless functions and across deploys. Keep-warm pings need a process that stays up; without one, fit with ping_counts=(0,). A request the provider refuses after compache changed it is sent again as your app wrote it. All integrations
Why learn it from your data?
Because every chatbot needs a different answer. Three workloads, three settings, each priced by the provider's cache rules on conversations compache had not seen, against sending everything:
| Chatbot | What compache learned | Cost |
|---|---|---|
| Chat with documents (DocChat) | Questions and answers matter for long. Retrieved text is rarely needed after one message, so it goes | 43% to 89% less |
| Long personal chats (BEAM) | What the user said matters. Earlier replies of the assistant can go | 57% to 91% less |
| Data analysis (LongDS) | Almost every step builds on earlier ones. Keep nearly everything, and time the pings | 0% to 32% less |
How long users stay away matters as much. The same document chats, with three kinds of users, and what compache picked for each:
A fixed rule such as "keep the last 20 messages" or "summarize at 75% of the window" is one setting for all of these. DocChat's 43% to 89% is against the 5-minute cache. On the 1-hour cache it is 30% to 79%.
Results
| Measured on | Cost | |
|---|---|---|
| Claude Haiku 5.5, against the 5-minute cache | Live calls, billed, four integrations | 42% to 49% less |
| Claude Haiku 5.5, against the 1-hour cache | Live calls, billed, short chats | 18% less |
| GPT-6 Luna, against the 30-minute cache | Live calls, billed, one integration | 2% to 5% less |
| Claude Sonnet 5.5, against the 5-minute cache | DocChat, modeled cost | 43% to 89% less |
| Claude Sonnet 5.5, against the 1-hour cache | DocChat, modeled cost | 30% to 79% less |
| Claude Code on Claude Haiku 5.5, long sessions | Live sessions, billed | 51% to 85% less |
| Claude Code on Claude Haiku 5.5, sessions of some 50 calls | Live sessions, billed | 16% less |
| Coding agents on Claude Sonnet 5.5, against the 5-minute cache | 174 public runs, modeled cost | 5% to 43% less |
It pays off most in long chats with a lot of retrieved or tool text, where users pause longer than the cache lasts. It pays off least in short chats and where the cache outlasts the breaks: on GPT-6 Luna these users were always back in time, so there was little to save. If your app already uses Claude's 1-hour cache, the 1-hour row is yours: its writes cost more and its entries last longer, so there is less left to save, most of all in short chats. The long cache
Answers hold up. In every measurement, the number of right answers with and without compache differs by no more than chance, and as many issues and tasks are solved. On GPT-6 Luna it was slightly lower, on too few messages to call. Live runs · All benchmarks
What would you save? Put your numbers in the cost calculator, or drop a chat log or a policy.json on it: it is read in your browser and sent nowhere. It compares with your own rules, other tools' defaults or what you pay now.
Next to what you already use
| You use | compache adds |
|---|---|
| Prompt caching, the 1-hour cache included | What to do when the cache is about to expire, and what to drop once it has |
| Keep the last N messages, or summarize at a threshold | N learned per kind of text, cuts timed to the cache, and trimmed text the model can fetch back |
| OpenClaw or provider compaction at fixed thresholds | A setting fitted to your traffic. Modeled on Claude Sonnet 5.5, 48% to 54% cheaper than OpenClaw's defaults |
| A cache tool such as caching.ai, or one-hour cache entries | Sending less. Modeled on document chats, 30% to 70% cheaper than the cheapest cache setting alone |
| Headroom or OptScale Kompressor | They shrink single tool results, compache decides which parts stay and when the cache is kept warm. Live, with as many answers right: on document chats 36% and 26% cheaper, on a long session with Claude Code, run once each, 66% and 53% |
Guide · Integrations · How it works · Other tools compared · Calculator · Issues · Apache-2.0
Metadata
Release files for compache 0.15.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| compache-0.15.0.tar.gz | 1.3 MB | With provenance | Details for compache-0.15.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| compache-0.15.0-py3-none-any.whl | Python 3 | none | any | Details for compache-0.15.0-py3-none-any.whl |
Total release size: 1.9 MB
Release files / compache-0.15.0.tar.gz
| Download URL | compache-0.15.0.tar.gz |
|---|---|
| Size | 1.3 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7aafc3efe5fa57e8b0e0af88481dd3e0545d28a0a45780a74affee829b268fc0
|
|
BLAKE2b-256 checksum How to use checksums |
5d1cf793084352bf0e300a1d58fc79f064ff99d4b63cd1988f1364d9c17d5e4b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / compache-0.15.0-py3-none-any.whl
| Download URL | compache-0.15.0-py3-none-any.whl |
|---|---|
| Size | 538.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2ea91b6677d51a25df8d73a67fa06567580f5ee98978b98c64529850e346b526
|
|
BLAKE2b-256 checksum How to use checksums |
f3882bf4a7962763eddded0d1f460497ebc71bc15c8f7a48da719ec2bc456818
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logSecurity for compache 0.15.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for compache-0.15.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.