pi-subagents-cache-warm0.1.1
pi-subagents companion: keeps the main pi agent's prompt cache warm while async pi-subagents run, by steering pi's built-in cache warmer.
Untested on omp
This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.
Reports 0
No reports yet.
Readme
pi-subagents-cache-warm
A compatibility layer between pi-subagents and pi's built-in cache warmer. It keeps the main pi agent's prompt cache warm while async pi-subagents run, so the turn that reads their answer hits the cache instead of paying a full cache write.
It is an unofficial companion to pi-subagents, not part of it or a general keep-alive. In the default subagents mode it does nothing unless pi-subagents is loaded in the same pi process and one of this session's async runs is active. It reads pi-subagents' public bus events and status RPC only, and needs no changes to pi-subagents. Tested against pi-subagents 0.90.0. For warming during any idle time without subagents, on mode exists, but other packages focus on that.
Upstream issue: pi's idle warmer stops silently once any extension handles
turn_endand the context holds a custom or compaction message (pi#10956, still open at the time of writing). pi-subagents meets both conditions, so from its first wake-up on this extension can only detect the silence, name the cause and give you the fix: see When pi's warmer stops silently.
Install
pi install npm:pi-subagents-cache-warm
pi update --extensions picks up new releases. From a checkout: pi -e ./pi-subagents-cache-warm for one run, or pi install "$PWD/pi-subagents-cache-warm".
Why
An async subagent can run for longer than the cache lifetime (5 minutes on Anthropic's default tier). When it answers, pi-subagents wakes the main agent, and that turn re-bills the whole prompt.
pi already has a cache warmer (cacheWarming: "idle", the default). It re-sends the last request with a one-token output cap shortly before the cache expires. While idle, though, it assumes only a 15% chance that the conversation continues, so for small and medium prompts it decides a warm is not worth it. With a subagent running, an answer is close to certain.
This extension answers pi's cache_warming_decision event with "warm" while a subagent of the session is running. It sends no requests of its own:
- every warm is pi's own replay of the last request, with the same model, context and options;
- each warm is recorded only as a
usageentry, so neither the conversation nor the cached prefix changes; - a real turn (yours, or the subagent's wake-up) restarts pi's timer, so traffic that already refreshed the cache causes no extra warm.
Settings
/subagents-cache-warm-configopens a settings panel. Enter or Space changes the selected value, Esc closes, and each change is saved at once./subagents-cache-warmshows the status: settings, pi's own warming setting, the model, running subagents, and spend while forcing./subagents-cache-warm <args>changes settings from the command line.
Settings are saved in <agent dir>/pi-subagents-cache-warm.json.
| Setting | Values | Default | Env override |
|---|---|---|---|
| mode | subagents (warm while one of this session's subagents runs), on (warm whenever the agent is idle), off (pi decides alone) |
subagents |
PI_CACHE_WARM_MODE |
| cap | 1 to 30 minutes after the start of the last real request, or max |
30 |
PI_CACHE_WARM_CAP_MIN |
| smart | on (stop before warms cost as much as a miss), off |
on |
PI_CACHE_WARM_SMART_SAVING |
| notices | all (a line per warm, the summary, early stops), summary (the summary and early stops), off |
all |
PI_CACHE_WARM_NOTICES |
Example: /subagents-cache-warm subagents cap 15 notices summary smart on.
The file holds four keys: mode, capMinutes, notices and smartSaving. An environment variable overrides its key for one process, and saving that key from the command or the panel reports that the variable still wins. In a host without a terminal UI (RPC mode), /subagents-cache-warm-config asks twice instead of opening the panel: first the setting, then its value.
Smart saving. Before each warm, the extension adds what warms have cost since the last real request to the price of the next one. If that reaches what a cache miss would cost (both prices are pi's estimates), it shows a warning and tells pi to stop, so neither of them warms again until the next real turn. It applies only where the extension would otherwise force a warm. It matters mostly for models with expensive cache reads; on Anthropic's 5-minute tier the 30-minute limit is reached first. It is stored as smartSaving in the settings file. A model without prices is never stopped.
30 minutes is the most pi allows. pi stops idle warming 30 minutes after the last real request, and extensions cannot extend that, so a longer cap would never apply. Settings saved by an earlier draft with a longer cap, or with none, load as 30.
Warnings are shown whatever the notices setting: a smart-saving stop, a cache that is not confirmed, a model pi cannot warm, and pi's own cacheWarming set to off or streaming.
What you see
- Status line while forcing:
cache warm · 2 subagents · 3 warms $0.012 · stops 14:32 (pi 30m limit). - Status command: a
cacherow with the tier, the lifetime and the expiry countdown (cache short 300s — expires 14:32 (in 2m 13s),expired 3m ago, orcold: model changed); acaprow note on how many forced warms the lifetime allows;pi warmingnotes forat risk,silent sinceandnot neededstates; thefilerow only when the settings file exists. - After each forced warm:
Cache warmed for 2 subagents: $0.004, read 99% (3 warms, $0.012 so far). This line is skipped when pi's ownnotices.cacheWarmingis on, because pi then printsCache warmed (extension override): $…itself. - At the wake-up turn:
Cache kept warm: 3 warms $0.012, read 97% from cache, avoided ≈ $0.620 miss. If the turn still missed, the line says so. After a smart-saving stop the line is dropped on a miss, because that miss was expected and already announced. - When forcing stops early (cap, pi's limit, unconfirmed, off; not with
notices off):Cache warming stopped: …, spent $…. - When pi's warmer stays silent through the time its first refresh was due (see below):
pi's cache warmer went silent …naming what the context holds. The status showsat riskfrom the run end on. After a miss that the silence explains, one line follows pi's ownCache miss after …notice.
All of these use ctx.ui.notify and setStatus. A custom message would enter the LLM context and break the very cache this extension keeps.
When pi's warmer stops silently
pi's idle warmer stops without telling anyone (earendil-works/pi#10956). Two conditions together do it:
- any loaded extension handles
turn_end— pi-subagents always does, its watchdog registers one at load, feature on or off; - the context holds a custom message, a compaction or a branch summary — pi-subagents puts a
subagent-notifymessage into the context at the first wake-up, so this holds from the first wait on.
pi rebuilds the context after every turn when a turn_end handler exists, the rebuilt custom messages are new objects, and pi's warmer compares message objects by identity. The comparison fails, the warmer stops, and it stops before the event this extension answers — so no answer can revive it. On such a release, idle warming while subagents wait works only for the first wait of a session. This extension detects that, names the cause, and tells you the fixes; it cannot restore warming.
The fixes, in order:
PI_CACHE_RETENTION=long— a 1-hour (OpenAI: 24-hour) cache, so waits under an hour stop missing with no warmer at all. Each cache write costs 2× input instead of 1.25×. Check that your gateway honours it: run one turn with it set and look forcacheWrite1h > 0in the last assistant's usage (visible in the session file).- Upgrade pi once a release closes #10956.
The detectors are careful not to cry wolf: with the long tier pi by design never warms idle waits (its first refresh would come at 54 minutes, past its own 30-minute idle limit), so nothing is reported; the run-end warning is certain only when pi-subagents was detected; and any warming decision arriving while a risk is armed proves pi's warmer alive and silences the diagnostics for the session — a pi update that fixes #10956 turns them off by itself.
Providers
The extension is provider-agnostic: it reads the model's promptCache lifetime and tier (PI_CACHE_RETENTION picks the tier), the usage numbers pi records, and pi's own cacheWarming and notices.cacheWarming settings. It sends no request of its own and never picks the provider. Whether a model gets warmed is pi's call, and pi warms only models with a cache lifetime.
| Anthropic (direct, Copilot, Radius) | Claude through a gateway or proxy | OpenAI | Other gateway models | |
|---|---|---|---|---|
| Cache lifetime | 5 min; 1 h with PI_CACHE_RETENTION=long |
as Anthropic, if the gateway passes caching through | In memory, 5–10 min of inactivity; 24 h with long on supported models |
As upstream, if passed through |
| pi warms it | yes | yes, when the built-in anthropic provider gets only a new baseUrl (models keep their lifetime); a model defined under a provider of its own needs promptCache |
only with a promptCache override |
only with promptCache |
| Warm vs miss | a warm costs about 0.1× input; a miss costs 1.25× (or 2× on the 1 h tier) | as Anthropic, plus any gateway markup | warm = cached price; miss = full input | from pi's cost table |
A gateway for Claude, in ~/.pi/agent/models.json:
{ "providers": { "anthropic": { "baseUrl": "https://gateway.example.com/anthropic" } } }
For a model with no cache lifetime, the extension prints the fix once, when a subagent starts, and in /subagents-cache-warm status. Either add a lifetime in ~/.pi/agent/models.json (merged with what is there; the key is the provider id, then the model id):
{ "providers": { "openai": { "modelOverrides": { "gpt-5.5": { "promptCache": { "short": 300 } } } } } }
For a model you define yourself under models, put "promptCache": { "short": 300 } in its entry instead. Or set PI_CACHE_RETENTION=long. On OpenAI this gives 24-hour retention, so no warming is needed. On Anthropic it gives a 1-hour cache, but every cache write then costs 2× input instead of 1.25×.
Unconfirmed caches. If a forced warm reads less than half of the prompt from cache, or two forced warms in a row leave no usage record, the model is marked unconfirmed. Forcing then stops for the session. This is how a gateway that does not really cache is caught after one warm.
Requirements
- pi: 1.5 or newer (tested with 1.5.0 and 1.7.0), with
cacheWarmingset toidle(the default). Withstreamingoroff, pi never warms while idle;/subagents-cache-warm statuswarns about this. - pi-subagents: 0.90 or newer, loaded in the same pi process. It is detected through its bus events and
statusRPC. While the status line is up, asubagent:jobs-changedevent makes the extension re-read that status, coalesced to one read per 250 ms, so a run whose finish event was lost still ends the forcing. If the RPC does not answer, the extension falls back to the start and finish events it saw. If neither is available, it never forces a warm.
Checking it works
npm test runs the extension inside a real pi session with pi's own cache warmer. tests/pi-subagents.test.ts also loads the real pi-subagents, and skips itself when pi-subagents is not installed; the other tests use a stub. Only the model provider is faked, with a simulated prompt cache. A live check against a real provider:
- Setup. Use an Anthropic model with a small main prompt, and
/subagents-cache-warm subagents. - Prompt. Ask the main agent to launch an async subagent that runs
sleep 700; echo done, then stop. - While waiting. Expect forced warms at about 4.5 and 9 minutes, and the
Cache kept warmline at the wake-up turn. - Check the session file:
scripts/e2e-check.sh <session.jsonl>, in a checkout. It reads only the usage data. It confirms that:- the forced warms and the wake-up turn read at least 90% of the prompt from cache;
- no forced warm came after that turn.
- Control. Repeat with
/subagents-cache-warm off. The wake-up turn should have a largecacheWrite.
License
Apache-2.0, see also NOTICE.