@carlitose86/pi-auto-compact

Automatic compaction for Pi before the context fills up, with a threshold per model

Packages

Package details

extension

Install @carlitose86/pi-auto-compact from npm and Pi will load the resources declared by the package manifest.

$ pi install npm:@carlitose86/pi-auto-compact
Package
@carlitose86/pi-auto-compact
Version
0.2.3
Published
Oct 21, 2026
Downloads
279/mo · 7/wk
Author
carlitose86
License
MIT
Types
extension
Size
43.7 KB
Dependencies
2 dependencies · 1 peer
Pi manifest JSON
{
  "extensions": [
    "./extensions/auto-compact.ts"
  ]
}

Security note

Pi packages can execute code and influence agent behavior. Review the source before installing third-party packages.

README

pi-auto-compact

Automatic compaction for Pi: summarize the session before the context window fills up, at a threshold you choose, and a different threshold per model if you want.

A fork of @henryqw/pi-auto-compact 4.0.0 (MIT) with lifecycle repairs, one retry for truncated summaries, plain notices for cancelled or failed compactions, and per-model thresholds.

Install

pi install npm:@carlitose86/pi-auto-compact

It refuses to start while Pi's built-in compaction is on, so the two never compete. Turn that off in Pi's settings.json:

{ "compaction": { "enabled": false } }

Load only one auto-compact package: remove npm:@henryqw/pi-auto-compact if you had it.

Usage

Command Effect
/auto-compact Set the general threshold, in percent of the context window (25 to 99, default 70)
/auto-compact model Set the threshold for the current model only; answer default to remove it

The threshold is checked before each turn, after each tool turn and at the end of a run, against the model the session is using at that moment. Switching model applies its threshold at the next check.

Pi starts a compaction only after the running turn has stopped. A turn that starts in that gap, for example one triggered by a background-terminal notice, is stopped too, so the summary does not wait for it; the turn continues once the summary is in.

Configuration

The commands write <agent-dir>/config/pi-auto-compact/config.json (usually ~/.pi/agent/config/pi-auto-compact/config.json). You can also edit it by hand; open sessions pick up the edit at their next check, without /reload (an invalid edit is ignored and the thresholds in use stay):

{
  "autoCompactThreshold": 70,
  "modelThresholds": {
    "anthropic/claude-opus-5-5": 30,
    "gpt-6-luna": 90
  }
}

A key is provider/id or just the model id.

To cover many models at once, modelPatterns is an ordered list of JavaScript regular expressions tested against provider/id; the first that matches gives the threshold. For example, Anthropic models at 30% and OpenAI models at 90%:

{
  "autoCompactThreshold": 70,
  "modelPatterns": [
    { "match": "^anthropic/", "threshold": 30 },
    { "match": "^openai", "threshold": 90 }
  ]
}

For each check the most specific match wins: modelThresholds by provider/id, then by id, then the first matching modelPatterns entry, then autoCompactThreshold. /auto-compact model writes only modelThresholds; edit patterns in the file. A value out of range, an empty key, an empty or invalid pattern makes the whole file invalid: the extension says so and uses 70% until you fix it.

Summary model

With @henryqw/pi-task-models you can route the summary to a cheaper model (task pi-auto-compact/autoCompact). Without a route, Pi summarizes with the session model.

Log

Cancelled and failed compactions are also appended to <agent-dir>/pi-auto-compact.log, rotated once past 512 KiB. The log holds no session content, summaries or credentials.

Development

npm test
npm run smoke -- <path to the installed Pi SDK dist/index.js>
npm run smoke:recovery -- <path to the installed Pi SDK dist/index.js>

License

MIT. See LICENSE; the original notice is kept.