anomalyco / anomalyco/opencode

ACP PromptResponse.usage reports only the turn's final assistant message

Open
#41,660 0 comments 2 reactions 1 assignee View on GitHub

@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
  1. Drive opencode over ACP and send a prompt that makes at least one tool call.
  2. Read PromptResponse.usage from the session/prompt response.
  3. Run opencode export <sessionID> and sum tokens across the turn's assistant messages.
  4. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.