OpenCode
Token Optimizer for OpenCode is a native TypeScript plugin with context-quality scoring, mode-aware compaction guidance, session continuity, quality nudges, loop detection, and a built-in dashboard. Version 1.2 adds an OpenCode V2 entrypoint while retaining V1 support. The offline bundle has been tested with V2 2.0.18 and V1 1.18.33; the npm registry publishes token-optimizer-opencode@1.2.0. Registry availability is not a live-host install test.
Prerequisites
Section titled “Prerequisites”- OpenCode installed
- bun for the local build method
Package configuration
Section titled “Package configuration”-
Add it to
opencode.jsonor.opencode/opencode.jsoncin OpenCode V2:{"plugins": ["token-optimizer-opencode"]} -
Restart OpenCode. It resolves the package from npm. On V1 1.18.29+, use
plugin(singular) with the same package. Do not add the npm entry alongside the offline bundle, or both may load.
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. For OpenCode V2, put a package object in the plugins array:
{ "plugins": [{ "package": "token-optimizer-opencode", "options": { "qualityWindow": 20, "features": { "qualityNudges": true, "loopDetection": true, "smartCompaction": true, "continuity": true, "activityTracking": true, "trends": true } } }]}For V1 use plugin (singular) with the plain package name. The old array-of-tuples form is not a valid OpenCode config.
By default, data stays in a per-user directory. Checkpoints can retain short prompt excerpts and absolute file paths locally. If you set dataDir inside a repo, add the directory to .gitignore to avoid committing private session data.
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.
