pi-max-context
v1.0.0
Published
Set a persistent maximum context length for pi. When reached, compacts the session and prompts the model to continue.
Maintainers
Readme
pi-max-context
A pi extension that limits how much context a conversation can use. It does three things:
- Set a limit.
/max-context 100Ksets the limit and saves it in.pi. - Keep the limit. The limit is loaded again on every restart, new session,
fork, and
/reload. - Act on the limit. When context usage reaches the limit, pi compacts the conversation. Then the model is asked to continue where it left off.
Install
pi install /path/to/pi-max-contextThe extension is now listed with pi list. Restart pi or run /reload.
Usage
/max-context show the current limit and context usage
/max-context 100K limit = 100,000 tokens (K = 1,000)
/max-context 1.5M limit = 1,500,000 tokens (M = 1,000,000)
/max-context 250000 limit = 250,000 tokens (plain number)
/max-context off disable the limit (also: 0, none)Rules:
- Units are case-insensitive.
100kand100Kare the same. - Decimals are allowed, for example
1.5M. - Invalid values are rejected with a message.
While pi runs, the footer shows context usage: ctx <used>/<limit>.
Where the limit is saved
Project file (used when it exists):
<cwd>/.pi/max-context.json{
"maxContextTokens": 100000,
"continuePrompt": "Continue the task. Use the compaction summary for earlier context."
}maxContextTokens— the limit in tokens.0means disabled.continuePrompt— the message sent to the model after compaction. Optional. The default is "continue where you left off".
If the project has no file, pi reads ~/.pi/agent/max-context.json instead.
The project file always wins.
How it works
- The limit is checked before each model call and after each turn. Large tool results are caught too.
- When the limit is reached, pi compacts the conversation. After compaction, pi sends a follow-up message so the model continues the work.
- Safety rules stop loops:
- 15 seconds must pass between two compactions.
- After 3 compactions in a row that do not lower usage below the limit, auto-compaction pauses with a warning. It starts again when usage drops or you set a new limit.
- If the limit is higher than the model's real context window, pi warns you. The model's own window wins.
Files
pi-max-context/
├── index.ts # the extension code
├── units.ts # token parsing and formatting (K, M units)
├── package.json # pi package manifest
└── README.mdLicense
MIT
