r/PiCodingAgent • u/Upstairs_Addendum148 • 18d ago
Plugin I no longer compact or handoff. Context paging has been running my long sessions for weeks
Hey all, I wanted to share an extension I've developed called pi-context-paging.
https://www.npmjs.com/package/@rochecompaan/pi-context-paging
Instead of compacting your session into a summary when the context fills up, it treats the context window like a rolling window over the full history. When a request goes over a token budget (128k by default), the oldest turns move out of the window. The session file on disk is never changed. The model gets a short paging notice and a few recovery tools to search, browse and load older history when it needs it.
It has made my workflow a lot smoother. I don't get a surprise compaction in the middle of a long-running task and I don't have to write handoffs that inevitably loses important detail. It has been running very stably for me for a couple of weeks now. It is especially useful for agents doing long-running tasks unattended.
It's lossless. Nothing is summarized or compressed. The recent window is sent exactly as stored, and anything older can be loaded back as the original text.
Tony Mason wrote a good paper on this: The Missing Memory Hierarchy: Demand Paging for LLM Context Windows (https://arxiv.org/abs/2603.09023). It treats the context window like RAM and the full history like disk, and pages content in and out on demand. The extension is a practical version of that idea for Pi.
"But what about prompt caching?" I know this is the first thing a lot of you will ask, so here's how it works:
- It doesn't drop messages one at a time. That would change the start of the prompt on every request and break the cache constantly.
- When the window hits the budget, it pages out one large block at once, trimming down to about 80k by default. After that cut, the start of the prompt stays the same over many turns, so you get normal cache hits until the next cut.
- Each cut costs one cache rewrite. In practice that's rare, and whatever you lose in cache efficiency you get back in continuity. There's no summary step, no re-explaining, and no "the agent forgot what we were doing" moments.
Some real numbers from my sessions (Opus 5.5, Fable 5.1 and GPT 6.1, with zero compactions in any of them):

Each of these is millions of tokens of history in a single continuous session, and every request still stayed at or below the 128k budget. Cache hit rates stayed above 90% even with the paging cuts.
You can set the budget in settings. I'd suggest setting it below the point where your model starts to get worse, so it stays out of the "dumb zone." You can also run `/context-paging stats` during a session to see what's going on.
This extension works well with https://github.com/elidickinson/pi-claude-bridge too if you want to use you Anthropic subscription. I submitted patches to it specifically to make pi-context-paging work.
PS. My current defaults are set to higher than what is documented.
"contextPaging": {
"enabled": true,
"tokenBudget": 200000,
"trimToTokens": 140000
},Play around and give me feedback as to what works for you.
51
u/ohthetrees 18d ago
My intuition is that this is a bad approach. I’m willing to be wrong, but the rolling context window approach has been tried many times with bad results, and it seems to me that your main innovation is offering tools to search earlier parts of the context window. Separate from agent performance, A big problem is it busts cache inherently. You say 90% cache hit. Well, A that isn’t very good and B I have a hard time seeing how that is possible on long sessions.
34
u/Karmak0ma 18d ago
If there is no summary of the paged out context, how does the model know there is useful information there to retrieve? How do you ensure it uses the search tools? Does the model not repeat work it already did because it's not aware the info is already in the paged out blocks?
9
u/Nullhaunt 18d ago
How is this different from billion-context-pi?
8
u/mrgreatheart 18d ago
So this means your original prompt is guaranteed to be entirely lost as soon as the window shifts. Do you use plan and todo files to solve that?
4
u/jaegernut 18d ago
How does the model know when it needs to do a history search?
2
u/Glum-Drop-5724 18d ago
Very intersting. But how does it know when to search the old paged context in a session? Say I have a long conversation where somehwere in it I say "I don't like apples". This eventually goes into the paged context and is out of the active context. If I then ask later "Should I bake an apple pie or a carrot cake?", will the model know to search trough all the paged context to find out that I don't like apples and should in fact bake a carrot cake? Or does it simply always search through the paged context for every prompt?
2
u/wikitopian 18d ago
Still feels like this is going to result in sharp reductions in orientation after each sliding event, with the model facing a rumsfeldian "not knowing what it no longer knows?"
What's the smallest window you would recommend benchmarking it with? My own harness goes in a similar direction, but has the model itself decide which chunks to page out, and when.
github.com/plurnk/plurnk
4
u/bitzap_sr 18d ago
There are two parts to this.
1 - tools to search session text past the compression event.
2 - the compression algorithm.
It is not true that you don't compress. Yes you do. You just compress it all down to a single sentence.
You could take your session search tools from #1 and mix with the standard compression algo too.
2
u/sigmoia 18d ago
Observe this:
Now apply a rolling window function and chop off the prompt and the first tool call. Then all you are sending to the model is the last tool call and its result.
Models are stateless. So how will it know what it's supposed to do from that lobotomized prompt?
1
u/zebedeolo 18d ago
I do something similar, but with multiple parts, so this is interesting and I'll definitely check it out. currently I use an extension that stops at a certain threshold (I currently use 200k). then another extension that runs pi-vcc when the threshold is reached and then continues. it's been working really well for me, but your solution seems simpler.
1
u/philip_laureano 18d ago
Interesting. Is this on the same single-threaded agent loop? How is this different from say, the Star Wars opening crawl where context falls off the cliff?
Truncating the tail end of the context window makes sense, but how do you get it to remember the important things that matter if they fall off the visible context?
Yes, I see that it is lossless in the sense that it can be recovered from disk, but what happens when a critical piece of information drops off the edge and an agent forgets about it and does something they wouldn't have done if they knew about the information was lost?
1
u/civilian_discourse 18d ago
How is this different than pi-observational-memory?
1
u/GoingOnYourTomb 18d ago
I feel like this could be genius for local models. Thank you I will investigate
1
u/ImagineSquirr3l 18d ago
Isn't it the same thing than pi-vcc?
1
u/jinnyjuice 18d ago
Any benchmarks for intelligence/needle-in-haystack/correctness/accuracy/etc.?
1
u/Pbd1194 18d ago
i do sth similar where i drop tool calls nd responses on demand. so my cache takes a one time hit every few turns but net net my ctx remains small. user asst messages are always retained. only tools like bash logs python executions are deleted on demand.
1
u/wegster 18d ago edited 18d ago
Did you happen to do some simpler tests simply measuring wall time and tokens used with and without your extension for a moderate complexity job? (along with quality of output)
I was experimenting with something similar, focused on ensuring the original prompt and latest results remained lossless through compaction, but so far, have mostly seen longer execution times and token use. Still early testing though.
1
u/Immediate_Theory_467 18d ago
Interesting for sure. However, what if you want to store this for long term context / memory for recall?
1
u/isty2e 18d ago
Cache hit rate of <95% does not seem promising to me. I usually hit >99.5%, so I feel like this will be detrimental to me costwise.
1
u/mooky-bear 18d ago
Curious if you’ve measured the performance difference of cache loss on every page eviction vs normal compaction at 90% context
1
u/runnystool 18d ago
Interesting. Did you experiment with large compaction summaries? If you have 128k context and chop out 80k, that mean it leaves 48k in context. What if you triggered compaction with a 40k-50k budget? I wonder if it would retain more relevant stuff than this drop approach. Combine that with the search and lossless history.
1
u/firebat-66 18d ago
Thank you for including the paper. Just for a gist, do you follow the c9nventions in that paper?
1
u/johnzadok 18d ago
Will this break kv caching and need to prefill from scratch every time window rolls?
1
u/Imanari 18d ago
> When the window hits the budget, it pages out one large block at once
How does this not have a negative impact on performance? This is the moment you would write a summary of the part you are cutting off. How is not writing the summary better? I am genuinely curious, nobody has solved memory so appreciate you putting your ideas out here.
1
u/nescedral 18d ago
I've thought about doing this very thing, and it seems reasonable. I might try your implementation.
I have this nervous feeling that the rambling and chaotic nature of the way my ADHD brain works in a project is going to dump way too much information into this "endless" context book, though. I can't help but feel like keeping *everything* is overkill? And potentially destructive to the ability to find "relevant" and insightful information. But maybe I'm overblowing that angle.
1
u/jumski 18d ago
Very interesting idea! I'm using pi-blackhole and I'm mostly satisfied with it. Its a little bit different, merging two ideas - determinictic compaction (pattern matching, rulex, regexes) with refined observations (facts) with a recall tool.
Isn't it missign critical facts, session goals etc when they go out of the page window? Model must explicitely ask for them, and given it is not aware of those facts, it does not know what to ask for?
I like this about blackhole - i often read what it collected after deterministic compaction or observer/reflector runs and it is really substantial stuff.
I need to test your plugin as i think there is merit in simplicity. Also, pi-blackhole will run into a really long memory and facts list after lot of turns, just much much later than regular summarization compaction.
1
u/DaShrub 18d ago
Magic-context takes a good approach to something similar with refined context compaction and retention/search tools and built in memory promotion. One major issue in pi with this approach is that long session files will bloat your memory usage hugely especially on stock pi with node. This means that long running sessions with aggressive context management inevitably slow down and take gigabytes of RAM and slow down UX... Pi-Bolt helps a lot but the fundamental problem remains.
1
u/petter_s 17d ago
You should perhaps keep all user messages. Those could contain important information
1
u/noferengi 17d ago
Thanks for trying to tackle a shitty problem.
Do you cut along vLLM memory block sizes, so removing the front still allows later blocks without recompute, or is it a full prompt processing cycle every time you shift? I've seen some models really not like prefix caching because of SWA or something and I'm exhausted by all this.
If I'm reading your post, for a 200k prompt you shift the first 60k tokens out of it roughly and treat it as a continuous byte stream for your tools and searching. At 1k PP that's a little over two minutes very heavy wattage and recomputing. And you do that every 60k tokens. Hmm.
One of the things I was recently surprised by was the visual compaction in OhMyPi isn't actually tuned for Qwen 3.8 27B, in my own recall tests it was absolutely garbage as a method. Compaction sucks, you're just sitting there token generating maybe for 30k context or up to 9 minutes just writing summary. (In practice I'm seeing much less than that but bleck). So yeah I'll actually go back down to 25% used context and have all the way up to 80% again before I need to compact. But then sometimes it'll spend time rebuilding what it lost from compaction.
So yeah, it's a shitty problem.
1
u/davblaster 16d ago
I’m assuming this would take some proper work to port to pi-durable. Have you considered what’d be involved?
1
u/wadamek65 16d ago
I've been trying out the extension for the past 3 days, with one session I had running for ~200M tokens total. It seems to be working well, didn't have any issues with agents drifting during implementation, the extension works fine. It also seems to be rather cost-efficient, usually with a workload like this I'd expect to pay much more in API costs using my usual manual handoffs and compactions.
One issue I had is that the automatic message to the agent made it confused. It thought it was about to reach context budget and will be unable to work, so it continuously refused to keep going because it thought it'd run out of context and I had to instruct it that it's not a problem for it. Is it possible to fix that?
1
u/davblaster 6d ago
This works really well.
1
u/adamshand 18d ago
This seems like a .... remarkably obvious solution. Why isn't everyone doing this? The only downside seems like you loose the compressed older history, but with a note to the agent and a search tool, that seems reasonable?
Do you know if it'll work with: https://github.com/earendil-works/pi-claude-code
2
u/me0here 18d ago
https://github.com/rochecompaan/pi-context-paging
1
u/whiterose0000001 18d ago
Same kind of thing t3 code usesage for context handoff
1
u/adcostanza 18d ago
I do this but on demand as an /offload command and I use it sparingly. Like other people said paging context isn’t really a great approach because you loose your input tokens prefix cache every time you paginate and the session is likely to degrade a lot if you do that a ton of times.
I personally only offload when I have a old session I want to come back to without having to start a new one - I use it more as a mechanism to not take the hit of resending all the cold uncached input tokens to the model but still giving it the basic context of where we were (ie the last 1-2 turns depending on what fits) and a tool that enables it to search the offloaded history. I would only do this if I’m at 15%+ of my context window and am coming back to a many hours old session.
1
u/Antitesla 18d ago
Port to opencode please
1
u/Kiedrola 18d ago
Tomorrow I am going to give a try. Thanks
1
u/zephyr_33 18d ago
na. if u sit and see. u will notice that 80% of ur context is eaten by - 1. reasoning tokens. 2. tool output.
simply replace them with reasoning summaries and tool / tool outcome summaries and u save a tonne with a very little context loss.
1
u/Laicbeias 18d ago
I kinda have the llm collect just whats open and then move that into a new chat. Splitting it into multiple main tasks and sub tasks.
With all the techniques going around for compressing or dropping stuff it seems to just work. The worst is when the llm doesnt know anymore whats in and then has to reload some files mid conversation.
The ground truth also does change when you move forward and you also see what fell off on the sides. For me thats the easiest way. You get tasks posted and you can later just pick off from there. And for main stuff you can just continue and you know whats loaded. Cache hit is around 97% with this and
1
u/LordMoridin84 17d ago
Compared to other compact systems, this is a longer tail, no summary and better search right?
The problem is that you are going to get more compaction events because the agent has less context to work with after compaction.
I am not sure that keeping more of the tail is better since agents seem pretty good at continuing from a summary now.
0
u/[deleted] 18d ago
[deleted] — view deleted comment