MUL-5652: fix(agent): accumulate CacheWriteTokens in ACP backend promptDone handlers - #6301
Open
liuguiyuan3 wants to merge 3 commits into
Open
Conversation
|
@liuguiyuan3 is attempting to deploy a commit to the IndexLabs Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
Author
|
Hi @Bohan-J 👋 I've submitted a fix for missing CacheWriteTokens accumulation in the promptDone handler across 5 ACP backends. If you have a moment, could you please review this MR? Thanks! |
liuguiyuan3
force-pushed
the
fix/agent-cache-write-tokens-accumulation
branch
from
August 3, 2026 12:17
bee6e1a to
618c762
Compare
…P backends The promptDone handler in hermes, grok, kimi, qoder, and traecli backends omitted CacheWriteTokens (and kimi additionally omitted CacheReadTokens) when accumulating per-turn usage from the session/prompt response. When a runtime does not emit usage_update streaming notifications — e.g. Grok Build reports metering only in result._meta — the promptDone path is the sole source of token usage data. The missing field caused CacheWriteTokens to report as zero in Result.Usage, which in turn made the downstream cost calculation (tokenCostUSD(0, CacheWritePerM)) produce $0 for cache writes. The kiro backend already accumulated all four standard fields correctly; align the other five backends to match.
Co-authored-by: multica-agent <github@multica.ai>
Co-authored-by: multica-agent <github@multica.ai>
liuguiyuan3
force-pushed
the
fix/agent-cache-write-tokens-accumulation
branch
from
August 3, 2026 12:17
618c762 to
d5ea73d
Compare
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.
Summary
CacheWriteTokensaccumulation in thepromptDoneusage handler for 5 ACP backends (hermes, grok, kimi, qoder, traecli)CacheReadTokensaccumulation in kimi'spromptDonehandlerTestHermesBackendAccumulatesCacheWriteTokensthat exercises the promptDone-only path and asserts all cache token fields survive toResult.UsagecacheWriteTokensand add assertionsProblem
parseACPTokenUsagecorrectly parsescacheWriteTokensfrom the ACP wire format, but the per-backendpromptDonehandler only accumulated a subset of the parsed fields intoc.usage. When the runtime does not sendusage_updatenotifications (e.g. Grok Build reports usage only inresult._meta), this is the sole data source — causingCacheWriteTokensto be permanently zero inResult.Usage.The downstream pricing layer (
metrics/business.go) computestokenCostUSD(cacheWriteTokens, CacheWritePerM)— with zero tokens in, the cost is $0 regardless of the per-million rate ($6.25 for Anthropic Opus, $2.00 for xAI Grok, etc.).Fix
Add the missing
c.usage.CacheWriteTokens += pr.usage.CacheWriteTokensline in each backend's promptDone select branch, aligning with the kiro backend which was already correct.Test plan
go build ./...passesgo test ./pkg/agent/ -run "TestHermesBackendAccumulatesCacheWriteTokens"— new test passesgo test ./pkg/agent/ -run "TestGrokPropagatesMCPAndUsage|TestGrokAttributesUsageOnResumeWithoutConfiguredModel"— updated assertions passgo test ./pkg/agent/ -run "TestHermes|TestGrok|TestKimi|TestKiro|TestQoder|TestTraecli"— all ACP backend tests passCacheWriteTokens = 0, want 15)