r/PiCodingAgent • u/zaph0d • 22d ago
Plugin pi-percy: Automatic compaction extension for long running sessions
It makes the context smaller before the context window is full. The agent does the compaction itself, in the same session. I have been using it for a while and my experience has been stellar with this extension. The core algorithm is based on the automatic compaction algorithm in Shelley by exe.dev team.
How it works
- When the context gets to 200k tokens, pi-percy sends a nudge to the agent. The nudge has an index of the old messages. pi-percy sends one more nudge for each 50k tokens after that.
- The agent selects the old items that it does not need. It replaces old tool outputs with short pointers. It replaces old message ranges with short notes.
- pi-percy checks the request. If the request is not correct, pi-percy rejects it and gives the agent a list of the problems. The agent then sends a correct request.
- pi-percy writes the changes as Pi context edits. The session keeps all of the original messages.
What you get
- The agent that knows the work decides what to keep. There is no second model and no extra cost.
- With the
recalltool, the agent can search the original messages and read them again. - Idle compaction. If the session is idle for 8 minutes, the agent compacts the context. At that time the provider cache has probably expired, so the cost is low.
- The prompt cache stays valid. pi-percy does not change the tools or the system prompt, and the agent does one large compaction, not many small ones.
- Pi's own compaction stays on as a safety net.
- All of these threshold are configurable and has sane defaults.
Install
pi install npm:pi-percy
You need Pi 1.4 or later.
11
Upvotes
View all comments
Show parent comment
2
u/zaph0d 21d ago
Pi's usual compaction gets triggered at a high threshold (~85%) of context window usage. Then Pi takes all the messages except the newest 20K tokens from the context and summarizes everything via an LLM call. This has its disadvantages because the summarization may lose important details. This is not specific to Pi, most harnesses do compaction the same way. pi-percy on the other hand nudges the same LLM to summarize/collapse individual messages (tool calls, agent messages etc) with notes, decisions etc. Once the summarization is done, context usage is reduced, but nothing is lost because the recall tool from the extension can be used to fetch the actual result if needed. This lookup via recall works beyond compaction boundaries which is normally not possible. This allows the context to never grow too big but at the same time retain all the previous context for recall at a later time when necessary. Please try it out on an actual session and see. All the details about the algorithm are in the README. Feedback appreciated!