Skip to content

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.

  • OpenCode installed
  • npm (for the npm install method) or bun (for the local build method)
  1. Install the plugin:

    Terminal window
    opencode plugin token-optimizer-opencode
  2. Alternatively, add it directly to opencode.json or .opencode/opencode.jsonc:

    {
    "plugin": ["token-optimizer-opencode"]
    }
  3. Restart OpenCode. The plugin loads automatically.

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. 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
}
}]
]
}
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.