@diegopetrucci/pi-context-cap0.1.16
A pi extension that caps effective model context windows at 350k tokens for earlier auto-compaction.
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
context-cap
A pi extension that treats large-context models as having an effective 350k-token context window, so pi's built-in auto-compaction starts earlier, and avoids the dumb zone.
By default, pi auto-compacts when:
contextTokens > model.contextWindow - reserveTokens
This extension changes the active model's in-memory contextWindow to:
min(originalContextWindow, 350000)
With pi's default reserveTokens of 16,384, models larger than 350k will proactively compact around 333,616 tokens.
Commands
/context-cap status
/context-cap off
/context-cap on
/context-cap toggle
The extension starts enabled by default. /context-cap off releases this extension instance's hold; a shared model remains capped while another extension instance still holds it. After /reload, /new, /resume, or /fork, a new extension instance starts enabled again.
Install
Standalone npm package
pi install npm:@diegopetrucci/pi-context-cap
Collection package
pi install npm:@diegopetrucci/pi-extensions
GitHub package
pi install git:github.com/diegopetrucci/pi-extensions
Then reload pi:
/reload
Notes
- This extension mutates only the active model handle's in-memory metadata. It does not edit
models.jsonand does not pre-cap every model in the registry. - The cap applies only when the active model is a physical model with a finite, positive
contextWindow. Virtual selections and missing/incompatible limits are left unchanged because Pi 0.99 does not expose a routed physical model before compaction.
Process-wide scope
- The cap is process-wide for physical model objects shared through Pi's
ModelRuntime, not session-local. Sessions without this extension—including child sessions launched bycode-reviewer,agent-workflow-audit,librarian, andtriage-commentswithnoExtensions—can observe the cap. Concurrent-session isolation is not claimed; this is an accepted Pi 0.99 host/API limitation. - A process-wide
WeakMapkeeps the first holder's original window and reference-counts extension instances. Only the last holder restores it, and only when the current value is still the cap written by the extension. External writes are not overwritten during restore, duplicate release is safe, and restarting the process clears the in-memory coordination. Extension copies with incompatible older state cannot coordinate.
Settings guard and alternative
On Pi hosts exposing
getSettings(), the extension skips the cap whenevercompaction.modelOverridescontains the active model's key (provider/model-id), whatever fields that entry sets; it does not readreserveTokensfrom the override. Otherwise it usescompaction.reserveTokens, then the default16,384, and skips the cap when that value is at least350,000(which would make the threshold zero or negative). Hosts withoutgetSettings()use the older cap behavior without this guard.Pi's provider request path also derives an output-token budget from the effective window (
contextWindow - contextTokens - 4096, with any provider-specific clamp applied afterward). The cap can therefore reduce response or thinking-token budgets near the limit, in addition to causing earlier compaction; Pi's overflow handling may also treat a request above 350k as overflow and retry after compaction. Use/context-cap offwhen you need the full model window temporarily.As a session-scoped alternative, configure Pi's own
compaction.modelOverridesinstead of this extension. The exact key isprovider/model-id; to match this extension's default 350k threshold for a model with original windowW, letRbe the configuredreserveTokensvalue (default16,384) and use:reserveTokens = W - 350000 + RFor example, with the default
R, a 1,000,000-token model uses666384, yielding the same333616threshold as350000 - 16384:{ "compaction": { "modelOverrides": { "provider/model-id": { "reserveTokens": 666384 } } } }Choose this alternative or the extension; do not combine them. The extension deliberately skips a model with an explicit
modelOverridesentry, and older extension copies may not detect it./context-cap statusreports the process-wide original/effective window and how many other extension holders still hold the cap. No settings or session files are written by this extension.