anomalyco / anomalyco/opencode
[FEATURE] Client-side token estimate fallback when a provider omits usage stats (Ollama Cloud kimi-k3:cloud shows 0 tokens)
@kitlangton is already working on this.
Since Jul 27, 2026.
- Dominant language
- TypeScript
- Stars
- 209k
- Forks
- 27.5k
- PR merge metrics
- PR metrics pending
Description
Summary
When a provider's response omits the usage block entirely, OpenCode records 0 tokens / 0% / $0.00 for the session and shows it in the TUI — with no client-side fallback estimate. New / minimal providers that don't yet emit usage stats are rendered invisible in the token counter and context-% meter.
OpenCode version
1.18.7 (TUI, Linux)
Reproduction
Provider: Ollama Cloud (ollama provider), model kimi-k3:cloud (released ~1 hour before this report).
- Start a session on
ollama/kimi-k3:cloudand run a few turns (e.g. a/startbootstrap that triggers several model generations). - The TUI Context panel shows
0 tokens,0% used,$0.00 spentregardless of how much was generated. - Inspect the session DB:
sqlite3 -readonly ~/.local/share/opencode/opencode.db \
"SELECT substr(id,1,16), model, tokens_input, tokens_output, cost
FROM session ORDER BY time_created DESC LIMIT 6"
Actual (one kimi-k3 session that completed 19 real assistant turns):
ses_05bc182d5ffe | {"id":"kimi-k3:cloud","providerID":"ollama"} | 0 | 0 | 0.0
ses_05bbb9584ffe | {"id":"glm-5.2:cloud","providerID":"ollama"} | 3271199 | 21691 | 0.0
At the message level the per-turn tokens object is {"total":0,"input":0,"output":0,"reasoning":0,"cache":{"write":0,"read":0}} — i.e. no usage was populated, not even a partial one. The model responded normally (19 assistant messages with content were persisted); only the usage telemetry is missing.
Contrast (same provider, same OpenCode build, different model)
ollama/glm-5.2:cloud over the same ollama provider records full usage correctly (millions of tokens across many sessions). So this is not a provider-adapter parse regression in general — it's specific to a model whose response omits the usage block. kimi-k3:cloud is brand-new; Ollama Cloud appears to ship new models before wiring usage reporting on the server side.
The gap
OpenCode records exactly what the provider returns. When the usage block is absent, every field defaults to 0 and the TUI displays a misleading 0% / 0 tokens, with no indication that the provider simply didn't report usage. (Cost being $0.00 is expected for subscription providers like Ollama Cloud; the token/percent zeros are the problem.)
Proposal
When a provider returns no usage stats for a turn, fall back to a client-side estimate so the context-% meter and token counter keep working:
- Estimate input tokens from the prompt via a tokenizer (or a cheap heuristic — chars/4). Output tokens can be counted from the streamed completion text directly (this is exact, no tokenizer needed).
- Mark the estimate distinctly in the UI (e.g. an
~prefix or a tooltip "estimated — provider did not report usage") so it's not confused with provider-reported numbers. - Alternatively (lower-effort): when usage is absent, show "usage not reported by provider" instead of
0, so users aren't misled into thinking nothing happened.
Related
- #39092 — same symptom (tokens frozen at 0, $0, 0%) but on Desktop with the Synthetic provider; the reporter's own hypothesis is "the provider may not return token usage metadata." Same root gap, different provider — confirms this is a general cross-provider issue, not kimi-k3-specific.
- #34022 — OpenAI-compatible providers report zero
tokens_cache_readdespite upstream returningcached_tokens(a parsing gap on the openai-compatible path). Adjacent: both are about OpenCode's usage-tracking fidelity for openai-compatible/Ollama-style providers.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.