Skip to content

An extension for Pi · open source (MIT)

Make the prompt cache pay off.

pi-cache-keeper compacts a Pi session by reading the prompt cache the session already built. On big sessions the summary costs a fraction of Pi's compaction, and it is written from the full tool output instead of 2,000-character excerpts.

It compacts when the context gets full and just before the cache expires, it can keep the cache warm through a break, and it fills in what Pi doesn't know about a model (how much input it takes, how long its cache lasts, what it costs) from providers.dev.

pi install npm:pi-cache-keeper
InstallHow it works

Then run /reload in Pi. It needs Pi 1.6.0 or newer.

After a compaction: the card under the summary compares its cost with Pi's compaction, and the last line of Pi's footer counts down how long the cache stays warm. A sample session.

What it does

Compacting from the cache, compacting when full, compacting before expiry and model data from providers.dev are on by default. Keeping the cache warm and the fallback compaction model are off until you set them.

Compaction

  • From the warm cacheThe summary request repeats your conversation as it is and reads about 99% of it from the cache, and the summary sees all the tool output.
  • When the context is fullAt 90% of the model's window by default, or at a token cap you set, while a summary still fits.
  • Just before the cache expiresWhen you step away, so your next message doesn't pay to write the whole session again.
  • With another model when the cache can't be usedA cheaper model, or one on a login with spare quota, writes the summary, and the session goes on with its own model. Off until you pick one.

The cache

  • Kept warm through a breakFor up to as long as you choose, and it stops once refreshing has cost as much as one cache miss. Off until you set it.
  • In the footerHow long the cache stays warm, the cache lifetimes and the one in use, what the last request asked for, the price, and what went wrong, fitted to the terminal's width.
  • Model data from providers.devOne safe value per quantity (input window, output limit, cache lifetimes and prices), measured values first, for what Pi doesn't know about a model.
  • Settings per sessionEach session can set its own compaction and keep-warm values over the defaults, with /cache-keeper-settings.

How it works

Pi's compaction swaps the system prompt, drops the tools, and flattens the messages into text. None of that matches what the provider has cached, so every token of the summary request is billed at full price. It also cuts every tool result down to its first 2,000 characters: the summarizer sees the start of a long file or test run, and misses the failures listed at the end.

pi-cache-keeper keeps the system prompt, the tools and the messages as they are, and appends one instruction, as the next turn of the same conversation. The provider reads nearly all of it from the cache, at about a tenth of the input price, and the model writing the summary sees the whole conversation. The result looks like Pi's: its checkpoint format, its recent messages kept as they are, and the read and modified file lists carried forward.

What each compaction sends

Pi's compaction

summarizer promptolder messages as text, tool output cut

nothing from the cache

pi-cache-keeper

toolssystem promptmsg 1 … msg Nthe same bytes as your last turnsummarize thisnew

~99% from the cache

Read from the cache, at about a tenth of the input price Full price

One 40 KB test run, as the summarizer sees it

Pi's compaction

the first 2,000 characters

the failures are down here ↑

pi-cache-keeper

all of it, from the cache

TopicPi's compactionpi-cache-keeper

What it sends

Pi's compactionThe older messages as text, under its own system prompt.

pi-cache-keeperYour conversation as it is, plus one instruction.

Prompt cache

Pi's compactionNot used.

pi-cache-keeperAbout 99% read from it at normal compaction sizes.

Tool output the summary sees

Pi's compactionThe first 2,000 characters of each result.

pi-cache-keeperAll of it, read from the cache.

Cost

Pi's compactionFull price, for the older messages only.

pi-cache-keeper0.1× for the whole session: less once there's a lot to summarize, more on small sessions.

After a long break

Pi's compactionYour next message rewrites the whole expired cache.

pi-cache-keeperIt compacts just before the cache expires, so there's much less to rewrite.

Cache lifetime

Pi's compactionPi's default (5 minutes on Anthropic).

pi-cache-keeper1 hour in interactive sessions, which Anthropic bills at 2× per write instead of 1.25×.

What you see

Pi's compactionThe summary.

pi-cache-keeperThe summary, a card comparing the cost with Pi's compaction, and a cache countdown in the footer.

It checks before it sends. Before sending, the extension checks that the request repeats your last one byte for byte up to the new part, on the exact request Pi would send. When it can't prove that, Pi's own compaction runs as usual, or the fallback compaction model where you picked one.

On small sessions, Pi's is often cheaper. Pi's compaction sends only the older part, so on small sessions, and on sessions that mostly read big files, it has little to send. Once the older part is most of the session, which is usually the case when auto-compaction fires, reading everything at 0.1× costs less. The card shows both numbers every time.

Settings and commands

/cache-keeper-settings changes them in a menu. The defaults, for every session, are the cacheKeeper key in ~/.pi/agent/settings.json. A session's own values win in that session and are saved in the session itself, so they hold when you resume it.

Commands

/cache-keeper
Shows the cache right now, whether the next compaction would read it, the settings in effect, the model data in use, and this session's log.
/cache-keeper-settings
Opens the settings menu on this session's settings, with the defaults for all sessions one step away.
/cache-keeper-warm
Starts keeping the cache warm again for the models that two refreshes in a row without a hit left to Pi's own warming.
/cache-keeper-stop
Stops keeping idle caches warm in every Pi window that uses the same settings, until the next message you send each window.
/cache-keeper-refresh
Fetches the model data from providers.dev now and puts it into Pi's models.json.
/compact
Pi's own command; it goes through the cached path too.

Settings

SettingDefaultWhat it does
compactAtPercent90After a turn, compact once the context fills this share of the model's window; 0 turns it off.
compactAtTokens0Also compact once the context holds this many tokens, on any model, whichever comes first; 0 turns it off.
fallbackCompactionModel""This model ("provider/modelId") writes the summary when the cache can't be used; "" turns it off.
compactBeforeExpirytrueCompact 20 s before the cache expires, when idle.
minContextTokens140000The smallest context compacted before the cache expires.
keepWarmMinutes0Keep the cache warm up to this many minutes after Pi goes idle (up to 1440); it stops once refreshing has cost as much as one cache miss; 0 turns it off.
cacheRetention"long"Ask for the longer cache in interactive sessions (2× writes on Anthropic and Kimi); "short" keeps Pi's default. The same for every session.
modelDatatrueRead each model's input window, cache lifetimes and prices from providers.dev and put them into Pi's models.json; false takes them out again. The same for every session.
{
  "cacheKeeper": {
    "compactBeforeExpiry": false
  }
}

A session can set its own compactAtPercent, compactAtTokens, fallbackCompactionModel, compactBeforeExpiry, minContextTokens and keepWarmMinutes; every other setting follows the defaults. Every setting in full

From providers.dev

Model data

Pi's model catalog doesn't say how long most providers keep a prompt cache, and for some models its input window is smaller than what the provider accepts. Without a lifetime there is no countdown, no compacting before expiry and no warming; with a window that is too small, Pi compacts long before it has to.

providers.dev publishes both, with prices, for each model at each provider and access method (API key or subscription login), from the providers' documentation and from evaluations. The extension puts them into the modelOverrides of ~/.pi/agent/models.json, the place Pi has for it, so Pi's own warming and compaction use the same numbers as the countdown.

  • What is written. providers.dev's input window replaces Pi's (one known only from an evaluation can raise Pi's, never lower it). Cache lifetimes only for a tier Pi has no lifetime for; prices and the output limit only where Pi has none.
  • Your own values stay. Values you set in models.json yourself are never touched.
  • What is sent. The names of the providers Pi has credentials for, as query parameters to a public read-only API. No key, no account, nothing about your sessions or prompts.
  • When. At the start of an interactive session, when the saved data is a day old. Nothing is fetched on its own in print or JSON mode or with PI_OFFLINE set.
  • Off. "modelData": false takes the values it wrote out of models.json, which brings Pi's own back.

Install

pi install npm:pi-cache-keeper

Then run /reload: Pi loads extensions only at startup or on /reload. Compacting from the cache, compacting when full, compacting before expiry and model data from providers.dev are on by default; keeping the cache warm is off until you set keepWarmMinutes. Update it with pi update --extensions.

Requirements

  • Pi 1.6.0 or newer.
  • No runtime dependencies and no install scripts.
  • Published to npm by GitHub Actions, with provenance.

Providers

ProviderCached compactionCountdown and compacting before expiry
Anthropic APIYesYes, 1 hour
Claude via pi-claude-bridgeYes, on a copy of the Claude Code conversationYes, 1 hour
Claude on opencode-goYesYes, 1 hour
GPT-5.6+ on openaiYes, not tested live yetYes, 30 minutes
GPT-5.6+ on opencode-goYesAfter you set a flag in models.json
Kimi K3 on moonshotai and moonshotai-cnYes, not tested live yetYes, 5 minutes or 1 hour
Other Responses and Chat Completions APIsYesWith a lifetime from providers.dev, or one you declare
openai-codex (ChatGPT sign-in)Yes, not tested live yetWith the lifetime providers.dev has for the model
Google and othersNo, Pi's compaction runsNo

Each provider in detail, with the flag opencode-go's GPT models need.

Works well with Provimux and pi-provimux

Through a proxy, one Pi provider serves models of many labs, and a model can come from a subscription login or an API key, whose caches can last differently: a Claude login keeps a cache for an hour, a Claude API key for 5 minutes by default. pi-cache-keeper knows no proxy. It asks over Pi's event bus, and pi-provimux answers for Provimux and CLIProxyAPI with where each model comes from.

Based on pi-cached-compaction

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 the repository's history. This is a separate project; it is not maintained by Nico Bailon.

Read the full README on GitHub