Skip to main content

compache

Stop paying for your whole chat history on every message.

Tests Python 3.11+ No dependencies Apache-2.0

compache sits between your chatbot 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.

Billed per 1,000 user messages on Claude Haiku 5.5: $2.68 without compache, $1.45 with. Right answers out of 208: 125 without, 135 with

Get started · 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:

A 26-message chat: the whole history grows to 89k tokens, compache sends at most 25k. It trims on every message where the cache had expired, and pings keep the cache warm through other breaks

  • 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.

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

Works with

Your stack What you add
OpenAI, Anthropic and Google GenAI SDKs, sync, async and streaming compache.wrap(client, "policy.json")
LangChain and LangGraph on ChatOpenAI compache.langchain.clients(...) and middleware()
OpenAI Agents SDK, Pydantic AI, LlamaIndex, Agno, AutoGen, Strands, Haystack, Langfuse's OpenAI drop-in The wrapped client, 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

On DocChat that is against Claude's 5-minute cache, and against its 1-hour cache 30% to 79%. How long users stay away matters as much. The same document chats, with three kinds of users:

The same chats with users who return after 30 seconds, 4 minutes and 30 minutes: sending everything costs 1.8, 5.9 and 8.9 times what compache's setting costs

A fixed rule such as "keep the last 20 messages" or "summarize at 75% of the window" is one setting for all of these.

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

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. 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 on document chats, 36% and 26% cheaper with the answers of the whole history

Guide · Integrations · How it works · Other tools compared · Calculator · Issues · Apache-2.0

Metadata

Release files for compache 0.14.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for compache 0.14.0
File Size Uploaded Provenance Actions
compache-0.14.0.tar.gz 1.1 MB With provenance Details for compache-0.14.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for compache 0.14.0
File Interpreter ABI Platform Actions
compache-0.14.0-py3-none-any.whl Python 3 none any Details for compache-0.14.0-py3-none-any.whl

Total release size: 1.6 MB

Release files / compache-0.14.0.tar.gz

Download URL compache-0.14.0.tar.gz
Size 1.1 MB
Tags Source
SHA-256 checksum
How to use checksums
10098f3b950a61a06e825db879a5bf43cc9fbaa88c8878f85cdd048940933143
BLAKE2b-256 checksum
How to use checksums
34ad236b55032cd090e43e416a2ef24e2ae88e958fccb314e31d8e1fc13c888a
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 24, 2026.

Transparency log

Release files / compache-0.14.0-py3-none-any.whl

Download URL compache-0.14.0-py3-none-any.whl
Size 448.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e1755969383e5749c0c014370bc38720e17358188e811d1a26d1ee60126de94
BLAKE2b-256 checksum
How to use checksums
c024d0a6101b32ee5ef9df79004836d28078472314df65d924d497b280e37f56
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 24, 2026.

Transparency log

Security for compache 0.14.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.

Select from 2 files

Attestation for compache-0.14.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 24, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page