Dynamic Context Pruning
2,975 GitHub stars — Optimize token usage by intelligently pruning stale and obsolete tool outputs from conversation context.
Overview
As OpenCode sessions grow, tool outputs, file reads, search results, and verbose command output accumulate in the context window. This plugin monitors the conversation and automatically removes or summarizes content that is no longer relevant, keeping the context focused and reducing token consumption.
When to Use
| Scenario | Benefit |
|---|---|
| Long-running sessions | Prevent context overflow in multi-hour sessions |
| Token-sensitive billing | Reduce API costs by minimizing context size |
| Large codebase exploration | Prune stale search results and file reads |
| Multi-step workflows | Keep only the current step's context visible |
How It Works
- Monitoring — The plugin tracks every tool call and its output.
- Classification — Each output is tagged by type (file read, search result, command output, error, etc.) and relevance.
- Pruning — When the context approaches the token limit, older or less relevant outputs are pruned or summarized.
- Preservation — Recent turns, active file contents, and error messages are kept verbatim.
Installation
- npm
- Local
npm install opencode-dynamic-context-pruning
{
"plugin": ["opencode-dynamic-context-pruning"]
}
Download the plugin and place it in your plugins directory:
git clone https://github.com/Tarquinen/opencode-dynamic-context-pruning.git ~/.config/opencode/plugins/opencode-dynamic-context-pruning
Configuration
Default Settings
{
"plugin": ["opencode-dynamic-context-pruning"],
"compaction": {
"auto": true,
"tail_turns": 3,
"reserved": 4096
}
}
Aggressive Pruning
For token-sensitive environments:
{
"compaction": {
"auto": true,
"tail_turns": 1,
"prune": true
}
}
Conservative Pruning
For sessions where context retention is critical:
{
"compaction": {
"auto": true,
"tail_turns": 5,
"prune": true,
"preserve_recent_tokens": 8000
}
}
Usage
The plugin operates automatically once installed and enabled. You can also trigger manual pruning:
/compact
This forces an immediate context compaction.
Monitoring Token Usage
{
"experimental": {
"openTelemetry": true
}
}
With OpenTelemetry enabled, you can monitor token usage and compaction events.
Best Practices
Balance Pruning and Context
- Aggressive pruning saves tokens but may cause the agent to lose context.
- Conservative pruning keeps more context but increases costs.
- Start with defaults and adjust based on your workflow patterns.
Pair with Other Plugins
- Combine with Supermemory to preserve cross-session context.
- The pruner removes what is no longer needed; Supermemory persists what is important.
Monitor Compaction Events
- Check that compaction is not removing critical information.
- Review session summaries to verify context quality after compaction.
GitHub
- Repo: Tarquinen/opencode-dynamic-context-pruning
- npm:
opencode-dynamic-context-pruning - Topics:
token-optimization,context-pruning,performance - Category: Context / Memory