A Pi extension that writes the compaction summary from your provider's prompt cache, and compacts just before that cache expires, so a long chat costs less to summarize and less to pick up after a break.
| One real session: 642k tokens on Opus 5.5, at Anthropic's list prices | Without pi-cache-keeper | With pi-cache-keeper |
|---|---|---|
| Compacting it | $0.796, Pi's own compaction | $0.224, 72% less: 99.7% read from the cache |
| The next message after an idle hour | $3.21 with plain Pi (its default 5-minute cache), computed from the measured tokens; or $5.14 if you ask for the 1-hour cache | $0.53: compacted 19 s before the cache expired, then the same message. 84% less than plain Pi, 90% less than the 1-hour cache |
Measured with pi-cache-keeper 0.11.1 on 2026-11-30. Both compactions, the $0.53 and the $5.14 were run; the $3.21 is the same rewrite's tokens at the 5-minute write price ($5 per million instead of $8), not counting any refreshes Pi's own idle warming sends in its first 30 minutes. Setup, raw usage and arithmetic: Measured by us.
pi install npm:pi-cache-keeperOn by default, and what costs money. In interactive sessions it asks for the longer cache: 1 hour instead of 5 minutes on Anthropic, where a cache write then costs more (on Opus 5.5, $8 instead of $5 per million tokens; reads cost the same). With Pi's automatic compaction on, it compacts after a turn once the chat fills 90% of the model's window, and, for an idle chat of 140k tokens or more, 20 s before the cache expires, where the last answer reported writing the cache in the lifetime the model declares (Anthropic's 5-minute/1-hour split; elsewhere that is Pi's). It writes providers.dev's cache lifetimes, input limits and prices for your models into Pi's models.json. Keeping the cache warm during breaks is off. Each is a setting.
Based on Nico Bailon's pi-cached-compaction: compaction from the cache and compacting before the cache expires are his work.
After installing, run /reload in Pi (Pi loads extensions only at startup or on /reload). It needs Pi 1.6.0 or newer.
On by default, and measured above:
- Compaction from the cache. When a chat gets long, Pi replaces older messages with a summary. pi-cache-keeper sends the conversation exactly as it was, plus one instruction, so the provider reads it at its cache-read price instead of the full input price. See it: after each summary, a card compares its cost with an estimate for Pi's compaction (token counts where price data is missing).
- Less to resend after a break. If Pi stays open and idle, and the model's last answer reported writing all of its cache write in the lifetime the model declares for the tier asked (Anthropic's 5-minute/1-hour split), automatic compaction summarizes a chat of 140,000 tokens or more from the warm cache 20 s before it expires (
compactBeforeExpiry,minContextTokens). Your next message then carries the summary and recent messages. A smaller chat, one whose answers report no such split (OpenAI, Kimi and the other formats), or a chat whose Pi was closed while you were away can pay to send everything again. - More of your tool output reaches the summarizer. Cached compaction uses the text already in the conversation, including the part past Pi's usual 2,000-character cut: in the session above, Pi's compaction showed its summarizer 21% of the tool output. That can include failures at the bottom of a log. Through pi-claude-bridge, Claude Code keeps a tool result over about 50 KB as a 2 KB preview, so the summary sees that preview. See it: Nico Bailon's tool-output comparison.
Also on by default:
- Compacts at a threshold you set. With Pi's automatic compaction on, it checks after a turn and can summarize when the chat reaches 90% of the model's input capacity (the default), or a token cap you set. One turn can carry the chat past the threshold; the check after that turn can still produce a cached summary. See it:
/cache-keeper. - Every value from providers.dev. The cache lifetimes, context window, output limit and price the extension shows, counts and decides with are providers.dev's for the model's entry, read by providers.dev's own rules, and written into Pi's
models.jsonfor Pi (modelData). Where providers.dev publishes no value, it is unknown: nothing is taken from Pi's catalog, assumed for a model family, or worked out from another entry. The lookup sends only the names of providers, nothing about your sessions. See it: the footer, e.g.long 1h (in use) · api $3/$15 per M. - A countdown in the footer. Where the cache lifetime is known, the footer shows how long the cache stays warm:
cache warm · 59m left, thencache cold · next message full price. - Settings per session. One session can compact earlier, or stay warm longer, than the others.
Off until you turn them on:
- Keeps the cache warm during breaks (
keepWarmMinutes). Shortly before the cache expires, it sends a small refresh, timed by the cache lifetime that providers.dev supplies where Pi has none. It works on the Anthropic Messages, OpenAI Chat Completions and OpenAI Responses connections where it can safely re-send your last request. Claude through pi-claude-bridge,openai-codex, Gemini and other APIs are left to Pi's own warmer. At the idle period's start it counts how many refreshes one cache miss pays for at providers.dev's prices (23 for Opus 5.5 at 200k tokens on the 5-minute cache) and sends at most that many; warming also ends once the refreshes have together written a tenth of the context, or at a miss. What they cost is shown as an estimate at providers.dev's published prices, and decides nothing. Without every price published, it sends no refresh. See it: the footer, e.g.keep-warm 2h until 16:40, 2 of 11 refreshes. Keeping the cache warm.
On small chats, and on chats that mostly read big files, Pi's own compaction is often cheaper: it has little to send. When it takes over from this extension, Pi starts its lists of read and modified files over.
The table at the top is one real session, measured by us with pi-cache-keeper 0.11.1, at Anthropic's list prices for Opus 5.5.
- What Pi's summarizer saw. Pi's compaction showed its summarizer 21% of the tool output. Of one test run, our summary recorded "438 of 488 pass and 50 fail"; Pi's wrote "about 50 failures".
- A break. After an idle hour the cache had expired, and the next message wrote the whole session to the cache again. With the 1-hour cache pi-cache-keeper asks for, that was measured at $5.14; with plain Pi's default 5-minute cache, the same tokens cost $3.21. Compacting 19 s before the hour ran out, and then the same message, cost $0.53: 84% less than plain Pi, 90% less than the 1-hour cache.
- The longer cache costs too. On Anthropic and Kimi K3 every cache write in the session is billed at the 1-hour price while it is asked for ($8 instead of $5 per million on Opus 5.5);
"cacheRetention": "short"keeps Pi's 5-minute cache. - One session. These are one session's numbers, at list prices. On small chats, and on chats that mostly read big files, Pi's own compaction is often cheaper: Nico's runs below include two.
The setup, the raw usage and the arithmetic: Measured by us.
Nico Bailon measured these for pi-cached-compaction, the extension this one is based on, on 2026-10-21 with Pi 1.3.0: Nico's cost table and Nico's live tests. His 667k-token chat was replayed on a Claude subscription through pi-claude-bridge; its dollars are the recorded tokens at API rates, and Pi's side is the card's estimate (2–12% high in his tests).
| Session at compaction | Pi's compaction | pi-cached-compaction (Nico's run) |
|---|---|---|
667k tokens, a real coding session (claude-opus-5-5) |
~328k tokens sent, ~$1.40 (estimated) | 667k sent, 99% from the cache; recorded tokens at API rates: $0.19 |
44k tokens, ten whole docs read (gpt-6-luna) |
5.9k sent, $0.00090 | 44k sent, 99% from the cache, $0.00097 |
2k tokens, a few tool calls (gpt-6-luna) |
0.3k sent, $0.00009 | 2.2k sent, 83% from the cache, $0.00035 |
In his 667k chat, Pi's compaction would have dropped 61% of the tool output, and a cold cache would have cost $5.33 to write again. More rows and the reasons: What it costs.
pi-cache-keeper is based on pi-cached-compaction by Nico Bailon (MIT), from its 0.2.0 release. The original already did these, and they are his work, kept here: compaction from the warm cache, with its byte-for-byte check of the request's start; the summary that stops at the first kept message; the compaction card; the cache countdown (from the model's lifetime, the cache write the provider reports, OpenAI's minimum or Kimi's options); compacting before the cache expires; the longer cache; pi-claude-bridge support; handling Pi's own automatic compaction; and the log. What this fork adds or changes:
| pi-cached-compaction 0.2.0 | pi-cache-keeper | |
|---|---|---|
| When to compact | Pi's own automatic compaction, near the end of the window | adds a configurable after-turn threshold (90% by default, compactAtPercent) and a token cap (compactAtTokens) |
| Summary length | at most 80% of Pi's reserveTokens (about 13k tokens) |
up to the model's output limit, fitted to the window |
| Cache warming during breaks, within an estimated budget | no | optional (keepWarmMinutes) |
| Stopping new idle refreshes in every window | no | /cache-keeper-stop |
| Warming a model again after two misses | no | /cache-keeper-warm |
| Settings | 3 keys, in JSON | 8 keys, a menu (/cache-keeper-settings), and per-session values |
| Model data (cache lifetimes, input limits, prices) | no | from providers.dev, /cache-keeper-refresh |
| Countdowns | the model's lifetime, the reported write, OpenAI's minimum, Kimi | providers.dev's lifetime for the model's entry, or the reported write |
| Compacting before expiry while Pi is busy | one try | tried again every second, with a last check just before sending |
| The footer | ⏱ 59m |
cache warm · 59m left, cache cold · next message full price, the lifetimes, the price and warnings, fitted to the width |
| Status command | /cached-compaction |
/cache-keeper, with the settings, keep-warm and model data |
| Proxies (Provimux, CLIProxyAPI) | no | what answered, with its price and cache lifetimes (Through a proxy) |
cache-flag for GPT on opencode-go |
/cached-compaction cache-flag |
removed: set the flag by hand in models.json (Providers) |
When switching, copy any settings you want from cachedCompaction to cacheKeeper in ~/.pi/agent/settings.json: this fork doesn't read the old key, so a value you set there (compactBeforeExpiry: false, say) goes back to this fork's default. This fork uses Pi 1.6.0 or newer and displays its own compaction cards. Earlier cards from the original stay in the session file but no longer appear.
The CHANGELOG has each version.
| Command | What it does |
|---|---|
/cache-keeper |
Shows the cache's time left, whether the next compaction can read the cache, the settings, the model data and this session's latest log entries. |
/cache-keeper-settings |
Opens a menu of the settings, for this session or for all sessions. |
/cache-keeper-refresh |
Fetches the model data from providers.dev again, now. |
/cache-keeper-stop |
Stops new idle refreshes in the Pi windows that share this agent directory and have loaded the extension. Each window resumes after your next message there. A refresh already sent can still cost. |
/cache-keeper-warm |
Lets the extension warm a model again after two refreshes in a row missed the cache. |
Pi's own /compact goes through the extension too. Each command, with its output: Commands.
Change them with /cache-keeper-settings, or under cacheKeeper in ~/.pi/agent/settings.json. A setting marked "per session" can also be set for one session only.
| Setting | Default | Per session | What it does |
|---|---|---|---|
compactAtPercent |
90 |
yes | Compact once the context fills this share of the window. 0 is off. |
compactAtTokens |
0 (off) |
yes | Also compact once the context holds this many tokens, on any model. |
compactBeforeExpiry |
true |
yes | Compact 20 s before the cache expires, when Pi is idle and the last answer's write agrees with the declared lifetime. |
minContextTokens |
140000 |
yes | Compact before expiry only from this many tokens. |
keepWarmMinutes |
0 (off) |
yes | Keep the cache warm up to this many minutes after Pi goes idle. |
cacheRetention |
"long" |
no | Ask for the longer cache (1 hour on Anthropic, where a write costs 2× the input price, not 1.25×). "short" keeps Pi's. |
modelData |
true |
no | Read model data from providers.dev into Pi's models.json. |
Both layers, and a list of common tasks: Settings.
Cached compaction, the footer's countdown and the model data are three separate things, and each provider has its own mix:
| Provider | Compaction from the cache | Countdown in the footer | Compact before expiry | Longer cache |
|---|---|---|---|---|
| Anthropic API, Claude on opencode-go | yes | 1 hour (5 minutes with "cacheRetention": "short") |
where the last answer wrote all of it in the lifetime declared for the tier asked | 1 hour instead of 5 minutes |
| Claude through pi-claude-bridge | no: the gate can't check a request at its send there, so this extension sends no summary through the bridge; Pi's own compaction writes it | 1 hour | no | Claude Code decides |
GPT-5.6, GPT-6 and Daybreak on openai |
yes, not tested live yet | 30 minutes, the conversation cache the request asks for | no: its answers report no 5-minute/1-hour split, so Pi's compaction runs | a 12-hour shared system prompt on the models whose request starts with it (which ones); 30 minutes on the others |
| GPT-5.6 Luna and GPT-6 Luna on opencode-go | yes | after a flag in models.json |
no, as for GPT above | after the flag |
Kimi K3 on moonshotai, moonshotai-cn |
yes, not tested live yet | 5 minutes or 1 hour | no: its answers report no 5-minute/1-hour split, so Pi's compaction runs | 1 hour instead of 5 minutes |
openai-codex (ChatGPT sign-in) |
no: Pi's Codex adapter tries a WebSocket first, where the gate can't check a request at its send, so in 0.12 this extension sends no summary or refresh on it; Pi's own compaction runs | with providers.dev's lifetime | no | no |
| Other Responses and Chat Completions APIs | yes | with a lifetime from providers.dev, or the write an Anthropic Messages answer reports | only an Anthropic Messages answer that reports the split, as for Anthropic | the provider's own |
| Google and others | no: Pi's compaction runs | when providers.dev publishes a lifetime and the answer reports cache use | no cached pre-expiry summary on this API | no |
The lifetimes in this table are what providers.dev publishes today for those entries; the extension reads them, and a model providers.dev publishes none for has no countdown. Which providers.dev provider each Pi provider is, with an API key and with a login, comes from providers.dev's own match. Where it names two (ChatGPT through Pi's openai route: openai-codex or openai), the extension takes the smallest lifetime and window and the dearest whole price, as providers.dev's rule R8 says. Through a gateway, the values are those of the entry the gateway named for the last answer. After a switch of route, the countdown can show the old route's time until its next check, at most 15 seconds later. Works with lists every provider and login, and the providers.dev provider it is looked up at.
Details, and the opencode-go flag: Providers.
Using a proxy such as Provimux? The values are those of the providers.dev entry the gateway named, and the footer says which account really answered: Through a proxy.
The fallback bound. Through a gateway with a fallback account (a paid API key behind your subscription, say), pi-cache-keeper refreshes and summarizes only while the latest answer came from the one account at the top priority. A gateway can still hand one request to the fallback without warning: so one refresh or summary may be answered by the fallback key, and it costs at most about one write of the context at that key's price. pi-cache-keeper then sends nothing more until your next message.
The reverse case: the gateway adds an hour, the provider writes five minutes. Where the gateway lists an account as adding the 1-hour cache (adds: 1h) and Pi's answer does not say which cache was written (Pi reports a split the provider left out as 0, which is never taken for five minutes), the period counts with the hour the gateway adds. If the provider in fact wrote the 5-minute cache, the first refresh, about 57 minutes later, finds it gone and writes the whole context again; that write is past a tenth of the context, so warming ends after it. The bound is one write of the context at that account's price, beside the fallback bound above.
Codex in 0.12. Pi's Codex adapter (openai-codex-responses), and pi-provimux's copy of it for Provimux's Codex format (provimux-codex-responses), try a WebSocket first and fall back to HTTP only after it fails, so a request can leave before the gate's check at its send runs. This extension sends no summary request and no refresh on them in 0.12: Pi's own compaction and Pi's own warming apply.
pi-cache-keeper reads providers.dev for 41 of Pi's 42 providers that take an API key, and for 6 of its 7 subscription logins. Which providers.dev provider each one is comes from providers.dev's own match (/api/v1/pi-providers).
| Pi provider | providers.dev provider | How it is known |
|---|---|---|
anthropic |
Claude Code (claude-subscription) |
stated, with sources |
github-copilot |
GitHub Copilot (github-copilot) |
stated, with sources |
kimi-coding |
Kimi For Coding (kimi.com) (kimi-code-plan-cn) |
stated, with sources |
meta |
Muse Code (meta-subscription) |
stated, with sources |
openai |
OpenAI Codex (ChatGPT plan) (openai-codex) or OpenAI (openai) |
stated, with sources |
openai-codex |
OpenAI Codex (ChatGPT plan) (openai-codex) |
same id |
xai |
not on providers.dev | stated, with sources |
| Pi provider | providers.dev provider | How it is known |
|---|---|---|
amazon-bedrock |
Amazon Bedrock (amazon-bedrock) |
same id |
ant-ling |
Bailing (bailing) |
paired by base URL |
anthropic |
Anthropic (anthropic) |
same id |
azure |
Azure (azure) |
same id |
baseten |
Baseten (baseten) |
same id |
cerebras |
Cerebras (cerebras) |
same id |
cloudflare-ai-gateway |
Cloudflare AI Gateway (cloudflare-ai-gateway) |
same id |
cloudflare-workers-ai |
Cloudflare Workers AI (cloudflare-workers-ai) |
same id |
deepseek |
DeepSeek (deepseek) |
same id |
fireworks |
Fireworks AI (fireworks-ai) |
paired by base URL |
github-copilot |
GitHub Copilot (github-copilot) |
same id |
google |
Google (google) |
same id |
google-vertex |
Vertex (google-vertex) |
same id |
groq |
Groq (groq) |
same id |
huggingface |
Hugging Face (huggingface) |
same id |
kimi-coding |
Kimi For Coding (kimi.com) (kimi-code-plan-cn) |
paired by base URL |
meta |
Meta (meta) |
same id |
minimax |
MiniMax (minimax.io) (minimax) or MiniMax Token Plan (minimax.io) (minimax-coding-plan) |
stated, with sources |
minimax-cn |
MiniMax (minimax.cn) (minimax-cn) or MiniMax Token Plan (minimax.cn) (minimax-cn-coding-plan) |
stated, with sources |
mistral |
Mistral (mistral) |
same id |
moonshotai |
Moonshot AI (moonshotai) |
same id |
moonshotai-cn |
Moonshot AI (China) (moonshotai-cn) |
same id |
nvidia |
Nvidia (nvidia) |
same id |
openai |
OpenAI (openai) |
same id |
opencode |
OpenCode (opencode) |
same id |
opencode-go |
OpenCode Go (opencode-go) |
same id |
openrouter |
OpenRouter (openrouter) |
same id |
qwen-token-plan |
Alibaba Token Plan (alibaba-token-plan) |
paired by base URL |
qwen-token-plan-cn |
Alibaba Token Plan (China) (alibaba-token-plan-cn) |
paired by base URL |
qwen-token-plan-individual |
Alibaba Token Plan (alibaba-token-plan) |
stated, with sources |
qwen-token-plan-individual-cn |
Alibaba Token Plan (China) (alibaba-token-plan-cn) |
stated, with sources |
radius |
not on providers.dev | stated, with sources |
together |
Together AI (togetherai) |
paired by base URL |
typesafe |
TypeSafe (typesafe) |
same id |
vercel-ai-gateway |
Vercel AI Gateway (vercel) |
paired by base URL |
xai |
xAI (xai) |
same id |
xiaomi |
Xiaomi (xiaomi) |
same id |
xiaomi-token-plan-ams |
Xiaomi Token Plan (Europe) (xiaomi-token-plan-ams) |
same id |
xiaomi-token-plan-cn |
Xiaomi Token Plan (China) (xiaomi-token-plan-cn) |
same id |
xiaomi-token-plan-sgp |
Xiaomi Token Plan (Singapore) (xiaomi-token-plan-sgp) |
same id |
zai |
Z.AI Coding Plan (zai-coding-plan) |
stated, with sources |
zai-coding-cn |
Zhipu AI Coding Plan (zhipuai-coding-plan) |
paired by base URL |
Where two providers are named, the same key reaches either at the same address (a pay-as-you-go API or a coding plan), and pi-cache-keeper takes the smallest lifetime and window and the dearest whole price (providers.dev's rule R8).
radius(API key): Pi's own gateway. providers.dev has no provider for it.xai(login): Pi's SuperGrok or X Premium sign-in. providers.dev has no provider for a Grok subscription; xai is the API, paid per token.
Where providers.dev's match can't be read and none was saved, and for a provider it doesn't list (one of your models.json, say), the model has no entry and its values are unknown: the extension keeps no table of its own.
- Commands: each command, its output, and what it changes.
- The footer: every part of the footer line.
- Settings: both layers, and common tasks.
- Compaction: from the cache, the card and the log, when full, before expiry, and how it works.
- Keeping the cache warm: the budget, who warms what, and stopping it.
- Model data from providers.dev: what is fetched and written, and privacy.
- Through a proxy: the
model-upstreamsevents, and Provimux. - Providers · What it costs · Limitations
- Live tests: our measured session behind the table at the top, then Nico Bailon's runs.
- A measured working day: 9 hours of real work on GPT-6.2 Astra, plain Pi against pi-cache-keeper and two other setups, with the raw records.
- Keep-warm coverage, stopping warming (design), publishing.
pi-cache-keeper is based on pi-cached-compaction by Nico Bailon, released under the MIT licence, starting from its 0.2.0 release. Compaction from the warm cache, the compaction card, the cache countdown, compacting before the cache expires and the longer cache are his work, and his commits are kept in this repository's history. His releases are kept in the CHANGELOG as he wrote them, and the LICENSE carries his copyright line beside this project's. This is a separate project; it is not maintained by Nico Bailon.
Watch Nico Bailon's demo in the original README (78 seconds; it uses the old name).
Website: https://pi-cache-keeper.providers.dev
Setup and workshops for teams: ngvoicu.dev/consulting.