Skip to content

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.

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.

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).

Terminal window
pi install npm:context-mode

Benefits

  • 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.

Terminal window
# Adapt the example into ~/.pi/agent/extensions/ and /reload

Benefits

  • 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.

Terminal window
pi install npm:@juicesharp/rpiv-btw

Benefits

  • Prevention beats cure — less to compact later.
  • No summarization quality risk.

Drawbacks

  • Helps at the margins; won’t rescue a genuinely huge task.
  • Big, context-heavy tasks: context-mode.
  • Want domain-tuned summaries / cheaper summarizer: the custom-compaction example.
  • Want to keep context clean to begin with: side-channel tools + tidy AGENTS.md/SYSTEM.md.