Skip to content

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.

  • OpenCode installed
  • bun for the local build method
  1. Add it to opencode.json or .opencode/opencode.jsonc in OpenCode V2:

    {
    "plugins": ["token-optimizer-opencode"]
    }
  2. 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.

Use this method if you do not have npm or want to build from source.

  1. Clone the repo:

    Terminal window
    git clone https://github.com/alexgreensh/token-optimizer.git
  2. 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.

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.

VariableDefaultDescription
TOKEN_OPTIMIZER_QUALITY_WINDOW20Rolling window size for ratio signals
TOKEN_OPTIMIZER_TOOL_CALL_WARNautoTool call warning threshold (scales with context window)
TOKEN_OPTIMIZER_TOOL_CALL_CRITICALautoTool call critical threshold
TOKEN_OPTIMIZER_CHECKPOINT_RETENTION_DAYS7Days to keep checkpoints
TOKEN_OPTIMIZER_CHECKPOINT_RETENTION_MAX50Max checkpoints to scan for restore
TOKEN_OPTIMIZER_RELEVANCE_THRESHOLD0.3Minimum relevance score for checkpoint restore
TOKEN_OPTIMIZER_NUDGEStrueEnable quality nudges
TOKEN_OPTIMIZER_LOOP_DETECTIONtrueEnable retry loop detection
TOKEN_OPTIMIZER_SMART_COMPACTIONtrueEnable compaction context injection
TOKEN_OPTIMIZER_CONTINUITYtrueEnable session continuity
TOKEN_OPTIMIZER_ACTIVITYtrueEnable activity tracking
TOKEN_OPTIMIZER_TRENDStrueEnable trends collection

Two tools are available in every OpenCode session after install:

  • token_status: on-demand quality report for the current session
  • token_dashboard: generate and open the visual dashboard

No separate opencode-doctor command is documented. Use token_status inside OpenCode to confirm the plugin loaded and data is flowing.

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.

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.