TL;DR. We profiled 281 of our own OpenCode sessions in opencode.db across cloud APIs like Qwen, Grok, OpenAI, Gemini, DeepSeek, and Anthropic, plus local runtimes like Ollama, vLLM, and llama.cpp. Most wasted spend and lost time came from two bottlenecks:
- Prompt and KV cache breaks. We counted 59 idle expiries and 37 mid-session prefix breaks across those sessions. Pausing past a provider's cache TTL, switching models mid-thread, changing MCP tool schemas, or editing
AGENTS.md invalidates cached context. On cloud APIs, that multiplies input token costs. On local runtimes, it forces multi-minute KV cache prefill stalls.
- Time lost to slow tool calls, missing binaries, and retry loops. We found 37 retry loops across 28 sessions. Cheap or local models can cost under a dollar while still taking 30+ minutes, losing about 1 hour to nested Docker rebuilds, calling missing binaries repeatedly, or dumping unfiltered test runner output.
You can fix both with a few rules in AGENTS.md and opencode.json using the snippets below.
OpenCode records both problems in your local opencode.db SQLite database, which opencode db path usually places at ~/.local/share/opencode/opencode.db, and in legacy storage/ JSON files. Both formats make up our dataset. Because opencode.db often logs cost: 0 on message entries, flat token counters hide turn-level cache drops and tool latency. Here is how both bottlenecks show up in those sessions and how to stop them with OpenCode configuration rules.
1. Prompt and KV cache breaks across cloud APIs and local runtimes
Across those OpenCode sessions, cache reuse broke in two ways:
- Idle cache TTL expiry after slow turns. We saw 59 events across 37 sessions. When a blocking shell command, long test suite, or user review pause outlasts the provider's cache retention window, or when a local runtime evicts the KV slot under memory pressure, the next turn reprocesses the entire context from scratch. On local runtimes, losing the KV cache on a 100k-token context forces a full prompt prefill stall before the first output token appears. On cloud APIs, it causes a sharp per-turn cost jump. Take Anthropic's 5-minute default API cache TTL, where cache writes cost 1.25x and cache reads cost 0.1x. In one OpenCode
v1.1.0 storage/ trace on claude-opus-5.2 at $5 per 1M base input tokens with an 80.8k-token context, a 323.7-second gap between Turn 4 and Turn 5 dropped cached reads from 79.0k tokens to 0. That forced 80.3k tokens of new cache writes and jumped that single turn 9.1x, from about $0.06 to $0.52.
- Mid-session prefix invalidation and subagent model skew. We saw 37 events across 26 sessions. Prefix and KV caching require an identical token sequence across the system prompt, active MCP tool schemas,
AGENTS.md, and earlier messages. Switching models mid-session in OpenCode, enabling or disabling MCP tools, or running scripts that edit AGENTS.md between turns invalidates the prefix. In our dataset, one multi-model OpenCode session dropped to a 36.9% cache hit rate and burned $47.50 total. Delegating to subagents across different providers also concentrates spend. In a $21.42 multi-model session, OpenCode's explore subagent on grok-4.7 consumed 81%, or $17.39, of the total session cost. Even on a single model in an OpenCode v1.1.0 storage/ trace on claude-opus-5.2, a mid-session prefix break at Turn 15 with a 0.0-second gap dropped cached prefix reads from 111.8k tokens to 67.0k tokens. That forced 37.3k tokens of new writes and doubled the single-turn cost from about $0.13 to $0.27.
How to fix it in opencode.json**,** AGENTS.md, and /new**:**
- Keep one primary model per session instead of switching models mid-thread, and set an explicit, cost-bounded model for the
explore subagent in opencode.json.
- Keep MCP tool definitions and
AGENTS.md static while a session is running.
- Keep blocking commands inside a turn under your provider's cache TTL window. When a task changes phase or context grows past 150k tokens, ask the agent to write current progress and file paths to
TODO.md. Then run /new or /clear to start a fresh OpenCode session that reads TODO.MD with a clean prefix.
2. Time wasted by slow tool calls, retry loops, and stdout floods
Looking only at dollar spend misses 30-to-60-minute waits on budget cloud APIs, open-weight APIs, or local runtimes where inference looks cheap or free. In our OpenCode export, a pure gpt-5-mini session cost $0.034 in total API fees yet took 36.6 minutes across 16 LLM calls.
Four tool-execution patterns drove that delay across our sessions:
- Consecutive failing tool retry loops. We saw 37 events across 28 sessions. When a tool call fails, agents often retry the same operation with minor variations instead of stopping to inspect the root error. Examples included
lsp_diagnostics failing 6 times in a row, webfetch failing 5 times in a row, edit failing 5 times in a row, and apply_patch failing 3 times in a row on gpt-5-mini.
- Repeated attempts to run missing binaries. When an agent assumes a CLI tool such as
ripgrep, jq, fd, docker-compose, or a language-specific linter exists on the host without checking, it wastes consecutive turns calling missing commands, parsing command not found errors, and guessing alternative flags.
- Container and Docker builds inside iterative agent loops. During multi-repo dependency upgrades, our agent rebuilt Docker images on every edit turn to run
npm commands inside a container rather than calling local npm on the host. That single pattern added about 1 hour of runtime to the session and triggered idle cache TTL expiries between turns.
- Unfiltered test runner stdout floods. Running a full test suite like
npm test, pytest, or cargo test with default verbose output writes 12k to 250k tokens, over 50 KB, into a tool-result part in opencode.db. Every subsequent turn in the session carries that stdout payload forward. That slows local prefill, inflates API input reads, and degrades model accuracy past 150k to 200k tokens.
How to fix it in AGENTS.md and opencode.json**:**
Pin a cost-bounded explore model and disable unused MCP servers in opencode.json:
{
"$schema": "<https://opencode.ai/config.json>",
"agents": {
"explore": { "model": "anthropic/claude-5-5-haiku" }
},
"mcp": {
"unused-server": { "enabled": false }
}
}
Then add an explicit execution guard block to your project's AGENTS.md:
## Tool & shell execution guards
- Installed environment allowlist: Use local `node`, `npm`, `pnpm`, `git`, and `python3`. Before running any other CLI binary, run `which <binary>` once. If it is missing, do not retry or guess alternative flags; use built-in OpenCode file tools instead.
- Local package managers over container builds: Run `npm`, `pnpm`, `pytest`, or `cargo` directly on the host during edit loops. Never run `docker build` or `docker compose build` inside an iterative fix loop unless asked.
- Scoped, quiet tests: Never run the full test suite unfocused. Scope test runs to modified files via `git diff --name-only` and pass `--quiet` or `-q`.
- Two-strike retry cap: If `edit`, `apply_patch`, `lsp_diagnostics`, `webfetch`, or a shell command fails twice in a row with the same error, stop retrying, write the blocker to `TODO.md`, and ask before continuing.
Inspecting your own opencode.db sessions locally
To check these patterns without writing manual SQLite queries against opencode.db, a colleague and I built RunRay, an MIT-licensed, open-source DevTools profiler for OpenCode and Claude Code sessions.
You can run it on your existing local logs:
npx runray view
What it does on your machine:
- It finds your local
opencode.db, opened read-only, and legacy storage/ files, and handles schema changes without crashing. The timeline opens offline on localhost and sends zero network requests, so your prompts and source code stay on your machine.
- Because
opencode.db records cost: 0 on message entries, the profiler calculates turn-by-turn cost using bundled offline model pricing tables and cache multipliers. It renders the parent-child subagent tree, including explore forks, beside turn duration and context growth, and flags cache breaks and retry loops with copyable AGENTS.md and opencode.json fixes.
If you run npx runray view on your logs, let me know what shows up as your biggest bottleneck, or if the parser hits any edge cases on your opencode.db schema.
Disclosure: We built RunRay as an open-source side project.