Skip to main content

Dynamic Context Pruning

tip

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

ScenarioBenefit
Long-running sessionsPrevent context overflow in multi-hour sessions
Token-sensitive billingReduce API costs by minimizing context size
Large codebase explorationPrune stale search results and file reads
Multi-step workflowsKeep only the current step's context visible

How It Works

  1. Monitoring — The plugin tracks every tool call and its output.
  2. Classification — Each output is tagged by type (file read, search result, command output, error, etc.) and relevance.
  3. Pruning — When the context approaches the token limit, older or less relevant outputs are pruned or summarized.
  4. Preservation — Recent turns, active file contents, and error messages are kept verbatim.

Installation

npm install opencode-dynamic-context-pruning
{
"plugin": ["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