New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

@capyup/pi-auto-compact

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@capyup/pi-auto-compact

Pre-turn auto-compaction for pi

latest
Source
npmnpm
Version
0.2.4
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

pi-auto-compact

Pre-turn auto-compaction extension for pi. Proactively manages context window usage to prevent overflow errors.

The Problem

Pi's default auto-compaction only checks after agent_end. This means context can overflow during long multi-tool turns before compaction triggers.

Strategy

This extension checks tokens at three points:

  • Pre-turn: Before sending any request to the LLM (turn_start)
  • Mid-turn: After tool execution, before follow-up LLM calls (turn_end)
  • Emergency: Synchronous truncation in the context event as a last resort

It also handles the session resume case: when an existing session is reopened above the threshold, compaction kicks off as soon as the session starts.

Continuing After Auto-Compaction

ctx.compact() aborts the running agent internally, so without a nudge the session would sit idle once the summary is written. After every auto-triggered compaction this extension sends a short English follow-up user message to make pi resume the in-flight task. The exact wording depends on which check fired (pre-turn, mid-turn, emergency, or session-resume).

The follow-up is suppressed in two cases:

  • The user ran /compact manually — the callback path used here only fires for compactions this extension started, so manual compaction is never touched.
  • The agent is no longer idle when the summary finishes, e.g. the user already typed something or another extension started a turn while summarising. Their input acts as the kickoff and we stay quiet.

Compaction Strategies

StrategyDescription
keep-recentKeep only the most recent messages (default)
keep-bookendsKeep oldest + newest messages, compact the middle
summarize-allRemove everything except the last user message

Configuration

Use /auto-compact to open the interactive settings menu:

/auto-compact              # Open settings menu
/auto-compact status       # Show current status
/auto-compact reset        # Reset to defaults

Settings

SettingDefaultOptions
Auto-compact threshold90%80%, 85%, 90%, 95%
Keep recent budget15%5%, 10%, 15%, 20%
Strategykeep-recentkeep-recent, keep-bookends, summarize-all
Debug loggingoffon/off

Example

For a 200K context window model:

  • Compact at 180K tokens (90%)
  • Keep 30K recent tokens (15%)

Install

As a pi package:

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

Or via git:

pi install git:github.com/capyup/pi-auto-compact

Or run directly:

pi -e ./extensions/auto-compact.ts

How It Differs from Pi's Default

AspectPi DefaultThis Extension
Check timingAfter agent_end onlyBefore requests + after tools
Overflow protectionNoneEmergency truncation
Mid-turn handlingNoneChecks after each tool batch
Auto-continueNoYes — sends an English follow-up nudge after auto-compaction

License

MIT

Keywords

pi-package

FAQs

Package last updated on 09 May 2026

Related posts