03. Cost and statistics¶
🎯 Objective¶
Measure and optimize cost and tokens with opencode stats, understanding what most impacts the bill.
📊 The stats command¶
opencode stats # shareable report
opencode stats --days 7 # 7-day window
opencode stats --models # usage by model
opencode stats --cost # cost and tokens
opencode stats --json # for pipelines
💸 What weighs the most¶
- Large context — the biggest cost factor (input + cache)
- Model — switch to
#lowvariants for mechanical tasks - Tool outputs — huge logs/builds inflate the history
- Warming — real keep-alive requests
- Subagents — each child consumes independently
🛠️ Actions that reduce cost¶
{
"compaction": { "auto": true },
"tool_output": { "max_lines": 2000, "max_bytes": 51200 },
"agents": {
"ci-reviewer": { "steps": 8 }
}
}
- Pin models by role: cheap for mechanical review, strong for architecture
- Prefer
--format jsonand process it outside the context - Limit subagent
steps - Turn off
warmingif there is no cache gain
🧮 Estimate before spending¶
- Models have a
cost(input/output/cache per 1M tokens) — check the catalog - Use
opencode run --model provider/model#lowfor repetitive tasks - Monitor automation: record
opencode stats --costas an artifact in CI
🔍 Audit¶
opencode stats --days 30 --models
opencode stats --cost --json | jq
For teams, the Console adds budgets and a leaderboard.
❓ Hands-on challenge¶
- Measure the cost of a long refactor with
--cost - Enable
compaction+tool_outputand repeat — compare the numbers - Run the same task with
#lowand#highand compare cost vs. quality - Export
--jsonand build a small report withjq
📚 Official source¶
Next step: Lesson 04 — Console and enterprise · Back to module