OpenCode
Token Optimizer for OpenCode is a native TypeScript plugin with full context-quality scoring, smart compaction with mode-aware context injection, session continuity, quality nudges, loop detection, and a built-in dashboard. It works with every model OpenCode supports.
Prerequisites
Section titled “Prerequisites”- OpenCode installed
- npm (for the npm install method) or bun (for the local build method)
Recommended method: npm install
Section titled “Recommended method: npm install”-
Install the plugin:
Terminal window opencode plugin token-optimizer-opencode -
Alternatively, add it directly to
opencode.jsonor.opencode/opencode.jsonc:{"plugin": ["token-optimizer-opencode"]} -
Restart OpenCode. The plugin loads automatically.
Alternative method: local bun build
Section titled “Alternative method: local bun build”Use this method if you do not have npm or want to build from source.
-
Clone the repo:
Terminal window git clone https://github.com/alexgreensh/token-optimizer.git -
Run the bundled installer:
Terminal window token-optimizer/install.sh --opencode
The installer runs bun install --frozen-lockfile, builds via bun run build:bundle, and copies dist-bundle/token-optimizer.js to ~/.config/opencode/plugins/token-optimizer.js. OpenCode auto-loads plugins from that directory at startup.
After any git pull, re-run token-optimizer/install.sh --opencode to update.
Configuration block
Section titled “Configuration block”All options are optional. Add this to .opencode/opencode.jsonc to customize:
{ "plugin": [ ["token-optimizer-opencode", { "qualityWindow": 20, "features": { "qualityNudges": true, "loopDetection": true, "smartCompaction": true, "continuity": true, "activityTracking": true, "trends": true } }] ]}Environment variable overrides
Section titled “Environment variable overrides”| Variable | Default | Description |
|---|---|---|
TOKEN_OPTIMIZER_QUALITY_WINDOW | 20 | Rolling window size for ratio signals |
TOKEN_OPTIMIZER_TOOL_CALL_WARN | auto | Tool call warning threshold (scales with context window) |
TOKEN_OPTIMIZER_TOOL_CALL_CRITICAL | auto | Tool call critical threshold |
TOKEN_OPTIMIZER_CHECKPOINT_RETENTION_DAYS | 7 | Days to keep checkpoints |
TOKEN_OPTIMIZER_CHECKPOINT_RETENTION_MAX | 50 | Max checkpoints to scan for restore |
TOKEN_OPTIMIZER_RELEVANCE_THRESHOLD | 0.3 | Minimum relevance score for checkpoint restore |
TOKEN_OPTIMIZER_NUDGES | true | Enable quality nudges |
TOKEN_OPTIMIZER_LOOP_DETECTION | true | Enable retry loop detection |
TOKEN_OPTIMIZER_SMART_COMPACTION | true | Enable compaction context injection |
TOKEN_OPTIMIZER_CONTINUITY | true | Enable session continuity |
TOKEN_OPTIMIZER_ACTIVITY | true | Enable activity tracking |
TOKEN_OPTIMIZER_TRENDS | true | Enable trends collection |
Tools available inside OpenCode
Section titled “Tools available inside OpenCode”Two tools are available in every OpenCode session after install:
token_status: on-demand quality report for the current sessiontoken_dashboard: generate and open the visual dashboard
Verify the install
Section titled “Verify the install”No separate opencode-doctor command is documented. Use token_status inside OpenCode to confirm the plugin loaded and data is flowing.
Auto-update
Section titled “Auto-update”npm install: update with standard npm tooling (npm update token-optimizer-opencode or equivalent).
Local build: run git pull in the cloned repo, then re-run install.sh --opencode.
Model support
Section titled “Model support”Token Optimizer for OpenCode supports 30+ models: Anthropic, OpenAI, Google, DeepSeek, Qwen, Mistral, xAI, and local models via Ollama. MRCR quality curves are calibrated per model family, so the grade scale stays accurate regardless of which model you use.
See OpenCode capabilities for the full feature list, hook details, and parity notes vs Claude Code.