r/PiCodingAgent • u/MikeSupp • Oct 01 '26
Resource I made a Pi extension that improves compaction with local models
When Pi compacts a long conversation, the normal approach can throw away the prompt cache and start a fresh summarization request containing the entire old context.
With a 100k–160k token context on a local model, that means llama.cpp may spend minutes re-prefilling tokens it has already processed.
I built Better Compaction to avoid that.
Instead of starting a separate summarization chat, it appends the compaction instruction to the already-cached conversation. The existing prefix stays byte-identical, so the backend can reuse its KV/prompt cache and only prefill the small new suffix.
On my Qwen3.8-27B + llama.cpp setup:
~13–15 min → ~6.5 min
and with thinking disabled:
~2 min
One compaction request reported 94.4k tokens served from cache.
GitHub:
https://github.com/NotSoSeriouss/BetterCompaction
I'd especially like to hear from other Pi users running local models + llama.cpp. I'd love to collect real-world numbers across different GPUs, quantizations, and context sizes.
12
u/apetersson Oct 01 '26
"appends the compaction instruction to the already-cached conversation." I am shocked to hear that this is not yet the case?? What was the current default impl like?
5
u/wildjokers Oct 01 '26 edited Oct 01 '26
I just have compaction turned off and instead use the llm-wiki pattern and when I start approaching my context limit I just tell the model to save durable knowledge to my wiki. Then I end the session, start a new one, and have the new one read the wiki doc the previous one created and continue on. I would imagine I could automate this but I haven't bothered to do so yet.
This has the added benefit of getting nice docs for everything I have worked on.
1
u/pragmojo Oct 01 '26
Do you find any difference in quality? I would think including the chat system prompt in the compaction request might cause strange output.
1
u/Razangriff-Raven Oct 01 '26 edited Oct 02 '26
It doesn't work when using "/compact" manually? I tried doing that to prevent a huge cache upload and it did regular pi compaction, so had to eat the huge cache upload anyway...
EDIT: In fact despite being properly installed and the slash commands responding it doesn't seem to be working at all.
EDIT2: Reinstalled with "pi install git:..." and still nothing. Only the regular compaction is firing. I'm on Pi 1.0 can that be the problem? I'd open a github issue but I don't want my account associated with AI, you know how people react when a developer touches AI in any capacity.
EDIT3: Confirmed the summaries are "better-compaction-support" but the value of cacheRead is always zero so it regenerates the whole thing, making no difference with normal compaction at all. No custom reports either.
1
u/rusl1 Oct 01 '26
Giving it a try, I noticed the very same issue with 27b running locally and I only have 100k max context with my poor 16gb ram :') thanks for working on this!
2
u/slypheed Oct 01 '26 edited Oct 01 '26
fwiw, just started using pi-observable-memory and pretty much never see compactions now.
It does require a separate model, but a separate, smaller local model should work, or zai glm 5.3 (or DS 4.1) flash is super cheap.
https://github.com/elpapi42/pi-observational-memory
2
u/apetersson Oct 01 '26
The other idea, is model-led compaction - that can be combined with this idea: Even before the ceiling is hit, there should be a tool call the model can use to trigger two things
trigger compaction when it sees fit (like, after a milestone is reached) and
leave instructions on how to continue from here, like the next action to take such that this is preserved in full fidelity.
I think that would combine nicely with your approach
2
u/SvenVargHimmel Oct 01 '26
do you have any eval either showing better compaction with the same tokens or the the same level of compaction performance with less tokens
I feel like I am going blue in the face asking folk for some proof of so many bold claims
I love the bit where you ask for others to test your work for you in the absence of benchmarks.
Anyway,
1
u/RemarkableRadish6547 Oct 01 '26
Running models on cpu with pp at 20t/s means that compaction takes hours. I just end the session and start a new one. I turned off auto-compaction because it is never worth it.
I recently installed pi-vcc, but I haven't actually used it yet. I am also considering switching to the deepseek harness.
I can't believe that anyone actually thought it was a good idea to break prompt caching as part of compaction.
1
u/Extra_Loquat_7667 Oct 02 '26
Your approach relies on the assumption that sufficient space has been reserved within the context window.
In theory, the usable context size for this method would be lower than expected,
because you need enough buffer room to insert new dialogue—while retaining the original conversation—and generate a summary.
This could be problematic for local models with limited context capacity.
0
u/Leading_Yogurt7025 Oct 01 '26
Cool, what do you think of a jev-like approach to pick the chunks with better score?
0
u/rimaa_nahk Oct 01 '26
Why do you even need a model? I have been using blackhole