Tokenminning in OpenCode
OpenCode is a terminal-first AI coding agent. Every turn resends your prompt, AGENTS.md, instruction files, MCP tool schemas, and the full session history—including large tool outputs from read, grep, and bash. Most spend comes from long sessions, heavy MCP, and frontier models on routine work—not from one verbose reply.
This page is the hub for the OpenCode cluster. Use the guides below for the query you actually have (opencode stats, disable MCP, context percentage, reasoning effort, custom providers). For the general technique stack, see Where to start.
Quick checklist
- Run
opencode stats --models --days 7— add--toolsto see tool overhead. - Watch context fill in the TUI sidebar — OpenCode context usage.
- Use Plan for analysis; switch to Build only when ready to edit — agents guide.
- Keep
AGENTS.mdshort — rules guide. Disable MCP servers you are not using. - Set
agent.*.stepson looping agents. Change reasoning effort in OpenCode on Plan.
Typical impact: 40–70% savings routing Plan/Explore to cheaper models; 20–50% on input trimming rules and MCP; 30–60% less context growth from compaction and new sessions per task. Benchmark on your own opencode stats output.
How OpenCode bills a request
OpenCode talks to LLM providers through your API keys (or OpenCode Zen ). Many teams use OpenRouter for one key across tools. There is no bundled subscription pool like Cursor’s Auto allowance—you pay provider rates directly.
Each agent turn sends:
- Your prompt and
@file references - Rules from
AGENTS.md,CLAUDE.md, andinstructionsinopencode.json - Skill metadata (full skill body loads only when the
skilltool runs) - MCP tool schemas for every enabled server
- The full session transcript, including prior tool outputs
Unlike Cursor’s cache-read tiers, OpenCode does not discount re-sent context unless your provider offers prefix caching (Providers). Hidden agents (compaction, summary, title) also call the model when triggered.
Guides
| Guide | What you’ll learn |
|---|---|
| Token usage | opencode stats, input vs output diagnosis, reconcile with provider bills |
| Agents | Plan/Build, subagents, steps, default_agent, permission.task |
| Rules | AGENTS.md, /init, instructions, skills metadata |
| OpenCode context usage | Context percentage in the TUI, compaction, prune, /compact, new sessions |
| MCP | Disable MCP, schema overhead, per-agent tool toggles |
| Change reasoning effort in OpenCode | reasoningEffort, budgetTokens, variants, per-agent overrides |
| Providers | Custom providers, context limits, OpenCode Zen vs BYOK |
Model routing (Plan vs Build)
Default cheap, escalate only on failure — OpenCode’s version of Model routing:
| Agent | Typical model tier |
|---|---|
| Plan | Haiku / mid-tier — analysis without full edit loop |
| Explore | Read-only search (@explore) |
| Build | Sonnet-class — implementation and refactors |
{
"$schema": "https://opencode.ai/config.json",
"default_agent": "plan",
"model": "anthropic/claude-sonnet-4-5",
"small_model": "anthropic/claude-haiku-4-5",
"agent": {
"plan": { "model": "anthropic/claude-haiku-4-20250514" },
"build": { "model": "anthropic/claude-sonnet-4-5", "steps": 25 }
}
}Use Tab to cycle Plan ↔ Build. Details: Agents, change reasoning effort in OpenCode.
Trim configuration bloat
| Source | Fix |
|---|---|
AGENTS.md / instructions | Short rules; lazy read — see Rules |
| Skills | Metadata only until invoked; disable on Plan if unused |
| Custom agents | Short prompts; set steps; restrict permission.task |
@ mentions | One file, not whole trees — Context hygiene |
Anti-patterns
| Anti-pattern | Why it fails |
|---|---|
| Frontier model on Plan and Build for every question | Paying Build’s tool loop on analysis |
| Five MCP servers enabled by default | Schemas on every turn — MCP |
| One session for a week’s work | Input tokens compound; compaction is not free |
| Reasoning model as global default | Thinking tokens on every grep — Reasoning |
@general subagents for simple search | Child sessions with fresh context — use @explore |