anomalyco / anomalyco/opencode
ACP PromptResponse.usage reports only the turn's final assistant message
@kitlangton is already working on this.
Since Aug 11, 2026.
- Dominant language
- TypeScript
- Stars
- 209k
- Forks
- 27.5k
- PR merge metrics
- PR metrics pending
Description
Description
ACP documents PromptResponse.usage as "Token usage for this turn", but OpenCode fills it
from the turn's final assistant message only. Each tool-call step gets its own assistant
message (session/prompt.ts:1188), so a turn with N tool calls reports 1 of N+1 model
requests.
Measured on a 3-tool-call turn (4 requests) — reported vs summed from opencode export:
reported: input=1 output=15 cache_read=11439 cache_write=140
actual: input=6 output=232 cache_read=33894 cache_write=11579
The reported row is byte-identical to the final request's. The skew is uneven — the first
request pays the large cache write — so any hit rate derived from it reads high, 98.8% vs
74.6% here. Single-request turns are exact.
buildUsage (packages/opencode/src/acp/usage.ts:90) takes a single message;
totalSessionCost just below it already reduces over the list, and the cost path accumulates
where the token path assigns (session/processor.ts:444-445).
Happy to PR this. Should the sum be bounded to the initiating user message, and do subtask
and compaction messages (same session, same parentID) count toward the turn?
Plugins
none
OpenCode version
1.18.16
Steps to reproduce
- Drive opencode over ACP and send a prompt that makes at least one tool call.
- Read PromptResponse.usage from the session/prompt response.
- Run
opencode export <sessionID>and sumtokensacross the turn's assistant messages. - The value from step 2 equals the final message's row, not the sum.
The aggregation half also reproduces without a model:
UsageService.buildUsage(UsageService.latestAssistantMessage(messages)) returns the last
message's tokens by construction.
Screenshot and/or share link
No response
Operating System
macOS 26.5 (Darwin 25.5.0)
Terminal
n/a — driven over ACP, not the TUI
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.