fix(acp): report cache token buckets under the anthropic_bucketed usage contract - #80
Merged
Merged
Conversation
…ge contract The ACP prompt-response usage normalizer dropped cachedReadTokens, cachedWriteTokens, and thoughtTokens, so OpenCode runs always recorded zero cache usage: the cost dashboard showed 0% cache hit and cache read/write spend was never billed. The payload was also stamped openai_total_with_cached_breakdown, but OpenCode reports fresh input tokens with cache buckets broken out separately (Anthropic-style semantics), so billing would have subtracted cache reads from an input count that never contained them. Forward all token buckets from the ACP usage payload and declare the anthropic_bucketed contract so the API prices fresh input, cache reads, and cache writes at their own rates.
Collaborator
Author
|
Clean-context review (delegated): APPROVE-WITH-NOTES, merge recommended.
Non-blocking follow-ups (pre-existing, this PR strictly improves data): (1) ACP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fill what changed. Use N/A for irrelevant or maintainer-only items.
Summary
cachedReadTokens,cachedWriteTokens, andthoughtTokensfrom the ACP prompt-response usage payload innormalizePromptUsage(previously only input/output/total were kept, so cache buckets were silently dropped).anthropic_bucketedinstead ofopenai_total_with_cached_breakdown, matching OpenCode's actual token semantics (fresh input excludes cache read/write buckets).Why
acp-fallback) agents only showed avg tokens: Cache Hit was always 0% and cache read/write spend was never billed, because the driver dropped the cache buckets OpenCode reports over ACP (opencode/src/acp/usage.tsbuildUsagesendscachedReadTokens/cachedWriteTokens/thoughtTokens).openai_total_with_cached_breakdownmakes the API computebillableInput = inputTokens - cacheReadTokens, but OpenCode normalizestokens.inputto exclude cache tokens ("AI SDK v6 … Always subtract cache tokens",opencode/src/session/session.ts). With cache fields forwarded under the old contract, fresh input would be under-billed; without them, cached tokens vanish from billing entirely.anthropic_bucketedis the contract that matches this payload shape (same as the Claude harness).Verification
bun test(1074 pass; 1 pre-existing failure intests/acp-file-system.test.tsalso fails on cleanorigin/main),bun run tc,bun run lint.sst/opencodesource (acp/usage.ts,session/session.ts:getUsage) and the ACP schema (UsagewithcachedReadTokens/cachedWriteTokens); traced the mosoo API pipeline (normalizeUsageTokens+calculateUsageCost) to confirmanthropic_bucketedprices fresh input, cache reads, and cache writes at their own rates and fixes the Cache Hit ratio (cache_read / (fresh + cache_read)).test:live:opencode).Impact
usage_event/session_model_callrows for ACP runs now carry cache token buckets and theanthropic_bucketedcontract; Cost dashboards start showing non-zero Cache Hit and cache-adjusted spend for OpenCode agents. Historical rows are unaffected (they recorded zero cache tokens under the old label).Review
src/runtimes/acp/acp-lifecycle-events.ts(normalizePromptUsage), usage-contract handling in mosooapps/api/src/modules/cost/domain/usage-contract.ts.usage_updateis still superseded by the API's own token-based pricing at turn end (tracked separately).