@lukeramsden/pi-context-cap
Extension for the pi coding agent: enforce a context token budget (default 200k) by forcing compaction, including mid-turn during long tool loops, instead of riding long-context models up to 1M.
Package details
Install @lukeramsden/pi-context-cap from npm and Pi will load the resources declared by the package manifest.
$ pi install npm:@lukeramsden/pi-context-cap- Package
@lukeramsden/pi-context-cap- Version
1.0.1- Published
- Aug 4, 2026
- Downloads
- 172/mo · 15/wk
- Author
- lukeramsden
- License
- MIT
- Types
- extension
- Size
- 14 KB
- Dependencies
- 0 dependencies · 2 peers
Pi manifest JSON
{
"extensions": [
"./extensions"
]
}Security note
Pi packages can execute code and influence agent behavior. Review the source before installing third-party packages.
README
@lukeramsden/pi-context-cap
A pi extension that enforces a context token budget (default 200k) by forcing compaction — including mid-turn during long tool loops — instead of riding a long-context model up to 1M tokens.
Why
Pi compacts when context passes contextWindow - reserveTokens. On a 1M-window model that means ~984k tokens: slow requests, degraded attention, and a large bill before the first compaction.
Two obvious workarounds don't work:
- Lowering
contextWindow(viamodelOverridesor an extension) poisons output. Pi clamps every request'smax_tokenstocontextWindow − estimatedInput − 4096, floor 1 token. As usage nears a lowered cap, the output budget shrinks to nothing and turns die with "Model stopped because it reached the maximum output token limit" — right before compaction would have fired. This extension leavesmodel.contextWindowuntouched. - Relying on pi's auto-compaction misses long tool loops. Pi (as of 0.83.0) checks compaction only after a full agent run and before a new user prompt — never between LLM calls inside a tool loop. One long turn can grow unbounded until the provider rejects it (pi issues #2871, #5512, #6879).
How it works
The budget lives only in the extension. It triggers compaction from three hooks:
turn_endwith tool results — mid-loop backpressure.turn_endfires after every LLM response inside a tool loop, andgetContextUsage()includes estimated tokens for trailing tool results (the exact blind spot in pi's own check). Becausectx.compact()aborts the running agent, the extension sends a follow-up prompt after compaction so the task resumes (resume offto disable).agent_settled— the run is done and pi will not continue on its own; compact quietly so the next prompt starts under budget.session_start— a resumed session that is already over budget gets compacted immediately.
Compaction fires when estimated tokens exceed budget − reserve (defaults: 200,000 − 16,384 ≈ 184k). A footer status line shows usage against the budget (cap 132k/200k (66%)), since pi's own percentage is relative to the model's real window.
Guards: no overlapping compactions, a 20k token growth requirement between retries after a failure, and the watcher disables itself for the session after two consecutive compaction failures.
Known limit
The request that crosses the threshold still goes out before its turn_end fires. Overshoot is bounded to roughly one request past the threshold — an extension cannot stop the loop before the next LLM call. Removing that needs a compaction check inside the agent loop itself; the enabling shouldStopAfterTurn hook is tracked in #7299 / PR #7367.
Install
pi install npm:@lukeramsden/pi-context-cap
Or try it for a single run without installing:
pi -e npm:@lukeramsden/pi-context-cap
Configure
CLI flags (set the session defaults):
pi --context-cap 150000 --context-cap-reserve 24000
/context-cap command (changes last for the current session):
| Command | Effect |
|---|---|
/context-cap or /context-cap status |
Show budget, threshold, usage, and state |
/context-cap <tokens> |
Set the budget (e.g. /context-cap 150000) |
/context-cap off / on |
Disable / re-enable enforcement |
/context-cap resume on|off |
Toggle the auto-resume prompt after mid-task compaction |
Verify
npm run verify
Type-checks the extension and runs a headless functional test with a mocked pi API — no pi binary, models, or API keys required.
License
MIT