Context & compaction
Long sessions exhaust the context window. Pi compacts older messages automatically, but the strategy is fully customizable via extensions — and several packages go further with knowledge bases and context-saving execution models.
Background
Section titled “Background”Pi’s built-in compaction summarizes older messages while keeping recent ones. It
runs automatically (on overflow or when approaching the limit) and can be
triggered manually with /compact [instructions]. Compaction is lossy, but the
full history stays in the session file — use /tree to revisit. You customize
how compaction works through extensions.
Alternatives
Section titled “Alternatives”1. context-mode
Section titled “1. context-mode”An MCP-based plugin that advertises saving most of your context window via sandboxed code execution, an FTS5 knowledge base, and intent-driven search. Cross-agent (Claude Code, Gemini CLI, Copilot, OpenCode, Codex).
pi install npm:context-modeBenefits
- Aggressively reduces context usage on large tasks.
- Searchable knowledge base instead of stuffing everything into the window.
- Portable across multiple agents.
Drawbacks
- Imposes its own workflow; not a drop-in.
- Benefits depend on adopting its patterns.
2. Custom compaction via the built-in example extension
Section titled “2. Custom compaction via the built-in example extension”Pi ships a custom-compaction example extension
showing how to implement topic-based compaction, code-aware summaries, or a
different summarization model.
# Adapt the example into ~/.pi/agent/extensions/ and /reloadBenefits
- Tailor summarization to your domain (e.g. keep code, drop chatter).
- Use a cheaper/faster model just for summarization.
- Official, transparent starting point.
Drawbacks
- Requires writing TypeScript.
- You own the maintenance.
3. Reduce context pressure with side-channel tools
Section titled “3. Reduce context pressure with side-channel tools”Instead of compacting harder, avoid polluting context in the first place.
@juicesharp/rpiv-btw keeps one-off asides out
of the main thread, and good AGENTS.md/SYSTEM.md hygiene keeps the baseline
prompt lean.
pi install npm:@juicesharp/rpiv-btwBenefits
- Prevention beats cure — less to compact later.
- No summarization quality risk.
Drawbacks
- Helps at the margins; won’t rescue a genuinely huge task.
Which should I pick?
Section titled “Which should I pick?”- Big, context-heavy tasks:
context-mode. - Want domain-tuned summaries / cheaper summarizer: the
custom-compactionexample. - Want to keep context clean to begin with: side-channel tools + tidy
AGENTS.md/SYSTEM.md.