r/PiCodingAgent • • 16d 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

  1. 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.
  2. 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.
  3. 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.
  4. 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 recall tool, 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.

Source: https://github.com/ghoseb/pi-percy

11 Upvotes

15 comments sorted by

8

u/Zya1re-V 16d ago

"We like compaction so much, we add a little bit more compaction on top of your compaction so the compaction can compact compactly." type of stuffs.

1

u/zaph0d 16d ago

Haha! Compaction is a necessary evil and we are always looking for methods to optimize the process without too many trade-offs.

3

u/random_user-1234 15d ago

You need to run some long agentic benchmarks so you can actually prove that it increases performance or at the very least has no degradation and you can get numbers on how much the cost increase is because this will break the cache more frequently causing higher read costs, and those frequent breaks might also increase the time it takes to finish a task as well. Nvidia ran agent loops to find agent optimizations they came up with Sol-Pi which this is very similar to and their own benchmarks gave mixed results. One thing I realized from making my own extensions is that we might think we have a genius solution but if the model was not trained on it during RL it won't be used to it causing worse performance. The frontier models are just so good that they can generally just power through any brain damage my extensions gave them without me noticing that they were doing worse because they still worked.

1

u/zaph0d 15d ago

Very fair points, all of them are accepted. The core algorithm was benchmarked by the Exe.dev team and they used frontier models. The extension definitely has some serious degradation with DeepSeek 4.1 flash. I will see what I can do about a proper benchmark. Thanks!

2

u/Agreeable-Pirate-886 15d ago edited 15d ago

I gave it a try for 3 hours of heavy lifting. I had high hopes. I was using pi-blackhole.

My previously superb agent (DS41F max) quickly began deteriorating, hallucinating data points, forgetting basic instructions in agents.md, and writing code only vaguely related to the assigned task that didn't work and it couldn't fix.

I had no idea how strongly compaction affects your results.

1

u/zaph0d 15d ago

Wow. Could you please tell me how many times the context nudge or idle nudge was provided by the extension during the three hour session? What's the max context window of DS41F? I am sorry about your poor experience. I have used it heavily with OpenAI and Anthropic models and my experience was quite different from yours. I will try it with DeepSeek and see what's going on. Thank you for giving it a shot, I am sorry.

1

u/Agreeable-Pirate-886 15d ago

I can look at the session tomorrow. DS41F has 1M context window.

1

u/zaph0d 15d ago

Thanks.

2

u/Agreeable-Pirate-886 15d ago

Five nudges, all resulting in compaction.

1

u/Conscious-Expert-455 16d ago

Can the context be configured? My maximum context size is 131 k. Tnks

1

u/zaph0d 16d ago

yes, everything is configurable.

{
"percy": {
"nudgeTokens": 200000,
"nudgeStepTokens": 50000,
"keepRecentTokens": 20000,
"maxCollapseTokens": 20000,
"idleMinutes": 8,
"idleMinTokens": 50000
}
}

Details are in the README - https://github.com/ghoseb/pi-percy/blob/main/README.md

1

u/alexis_moscow 16d ago

how it's different from built-in auto-compaction?

2

u/zaph0d 16d 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!

1

u/khaleelu 15d ago

how does your solution differ from this one https://www.reddit.com/r/PiCodingAgent/s/8TmUBxyb9q

1

u/archiblad 15d ago

Check pi-condense with summary capabilities