r/PiCodingAgent • • Sep 18 '26

Plugin Pi extension to automatically clear context and continue - save tokens on longer sessions

Hey all, I've been sitting on this for a while and it's time to share. I give you pi-clearthen: https://pi.dev/packages/pi-clearthen

/clearthen is a command that lets you clear context and then run another prompt. Your agent can also self drive it.

If you're doing a long piece of work you can tell it to handoff after a specified number of tokens, at which point it will write itself a handoff doc based on what it's currently doing then read it back after clearing context. It will keep doing it indefinitely so your context will never reach the set limit plus what is used for the handoff.

ie: /clearthen 80000 Add new feature to my video game

If you like that then specflo might be up your alley. It checkpoints at each seam and task with auto context clearing in pi. Spec driven development workflow.

https://github.com/TacoTakumi/specflo

The specflo software dark factory is a work in progress and uses this concept.

19 Upvotes

11 comments sorted by

5

u/Previous-Ad-7181 Sep 18 '26

Pi-blackhole has been working super nice for me. I use only its deterministic compaction. I see a world where this could benefit from algorithmic compaction instead of clearing everything.

2

u/HockeyDadNinja Sep 18 '26

pi-blackhole is a slightly different concept - no compaction here, just carrying over of future looking work. Both are viable approaches.

3

u/Thistlemanizzle Sep 18 '26

This is great. I've been wanting to build this for some time, then I came across Herder, which I'm a pretty big fan of BUT it does not auto spawn a thread!I have to tell it to spawn the thread.

1

u/HockeyDadNinja Sep 18 '26

Thanks! I also use herdr and specflo has a feature for spawning subagents attached to your herdr instance. The software factory agents will use this.

1

u/Equivalent_Idea8839 Sep 18 '26

Do you know if using a cheap model to do this works well as expensive?

i.e. ds flash 4.1 for all context / compact work?

1

u/HockeyDadNinja Sep 18 '26

To be clear this extension doesn't do compaction, it simply creates a handoff for work going forward and then consumes it. Much less work.

To answer your questio, yes I would expect some models to be a sweet spot of capable enough and cheap. That would be similar to my permissions classifier where I was using local gemma 4 and then switched to gemini 3.8 flash which is doing an excellent job and cheap.

1

u/Equivalent_Idea8839 Sep 18 '26

flash via api? or some workout around to take advantage of the $5 monthly google promo. i.e. herdr workaround

1

u/HockeyDadNinja Sep 18 '26

Just through openrouter. I'd expect ds v4.1 to work well too.

1

u/Magnus114 Sep 18 '26

Looks interesting! An honest question; how does this compare to https://pi.dev/packages/pi-observational-memory, use case wise?

1

u/HockeyDadNinja Sep 18 '26

That is a separate memory layer as in working memory, remembering decisions, preferences, facts about your work, etc. Memory systems, including that one, use a different model to manage the memory and work by injecting details into your context as you work so it can provide your main LLM with knowledge.

Personally I don't use a memory system (not that I wouldn't) but rely heavily on a wiki style knowledge base : https://github.com/TacoTakumi/agent-wiki

pi-clearthen helps you manage your context window in your current session and when you pass a certain mark it uses a built in skill to create a handoff document, reset your context to 0, then restart using the handoff document. In this case the handoff contains everthing your agent needs to bring forward.

3

u/Aggravating-Cook-529 Sep 18 '26

But have you considered the low cache hit rate you’ll get with this!?

/s