Skip to content

MUL-5652: fix(agent): accumulate CacheWriteTokens in ACP backend promptDone handlers - #6301

Open
liuguiyuan3 wants to merge 3 commits into
multica-ai:mainfrom
liuguiyuan3:fix/agent-cache-write-tokens-accumulation
Open

MUL-5652: fix(agent): accumulate CacheWriteTokens in ACP backend promptDone handlers#6301
liuguiyuan3 wants to merge 3 commits into
multica-ai:mainfrom
liuguiyuan3:fix/agent-cache-write-tokens-accumulation

Conversation

@liuguiyuan3

Copy link
Copy Markdown
Contributor

Summary

  • Fix missing CacheWriteTokens accumulation in the promptDone usage handler for 5 ACP backends (hermes, grok, kimi, qoder, traecli)
  • Fix missing CacheReadTokens accumulation in kimi's promptDone handler
  • Add unit test TestHermesBackendAccumulatesCacheWriteTokens that exercises the promptDone-only path and asserts all cache token fields survive to Result.Usage
  • Extend grok test fixture to include cacheWriteTokens and add assertions

Problem

parseACPTokenUsage correctly parses cacheWriteTokens from the ACP wire format, but the per-backend promptDone handler only accumulated a subset of the parsed fields into c.usage. When the runtime does not send usage_update notifications (e.g. Grok Build reports usage only in result._meta), this is the sole data source — causing CacheWriteTokens to be permanently zero in Result.Usage.

The downstream pricing layer (metrics/business.go) computes tokenCostUSD(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.CacheWriteTokens line in each backend's promptDone select branch, aligning with the kiro backend which was already correct.

Test plan

  • go build ./... passes
  • go test ./pkg/agent/ -run "TestHermesBackendAccumulatesCacheWriteTokens" — new test passes
  • go test ./pkg/agent/ -run "TestGrokPropagatesMCPAndUsage|TestGrokAttributesUsageOnResumeWithoutConfiguredModel" — updated assertions pass
  • go test ./pkg/agent/ -run "TestHermes|TestGrok|TestKimi|TestKiro|TestQoder|TestTraecli" — all ACP backend tests pass
  • Verified the new test FAILS without the fix (CacheWriteTokens = 0, want 15)

@vercel

vercel Bot commented Aug 3, 2026

Copy link
Copy Markdown

@liuguiyuan3 is attempting to deploy a commit to the IndexLabs Team on Vercel.

A member of the Team first needs to authorize it.

@liuguiyuan3

liuguiyuan3 commented Aug 3, 2026

Copy link
Copy Markdown
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!

@multica-eve multica-eve changed the title fix(agent): accumulate CacheWriteTokens in ACP backend promptDone handlers MUL-5652: fix(agent): accumulate CacheWriteTokens in ACP backend promptDone handlers Aug 3, 2026
@liuguiyuan3
liuguiyuan3 force-pushed the fix/agent-cache-write-tokens-accumulation branch from bee6e1a to 618c762 Compare August 3, 2026 12:17
liuguiyuan3 and others added 3 commits August 3, 2026 20:17
…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
liuguiyuan3 force-pushed the fix/agent-cache-write-tokens-accumulation branch from 618c762 to d5ea73d Compare August 3, 2026 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants