Repository navigation
fix(api): adopt cumulative message_delta output tokens in Anthropic/MiniMax cost - #2242
Conversation
…put-cost - src/api/providers/anthropic.ts: additive both-sides resolution in the message_delta case. Kept main's deltaThinkingTokens decomposition block (reasoningTokens telemetry) and placed the branch's cumulative output_tokens adoption immediately after it, so the accumulator used for the final totalCost is the adopted cumulative count, not the message_start seed. - src/api/providers/__tests__/minimax.spec.ts: import collision union. Kept the branch's calculateApiCostAnthropic import alongside main's MAX_TIMEOUT_MS import, and dropped the duplicate ApiStreamUsageChunk / MiniMaxHandler lines the conflict hunk re-inserted. - src/api/providers/minimax.ts: auto-merged; verified the cumulative output_tokens adoption line survived unchanged.
📝 Summary
Merge Risk:
|
Review statusThanks for contributing. This comment tracks the review sequence and the next action. Current step: The required review sequence passed. Remaining merge requirements apply. Review-state labels are managed by this workflow; do not edit them manually. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Supersedes #1733, which was auto-closed by the stale bot after CodeRabbit APPROVED — the only remaining gate then was 'resolve merge conflicts', now resolved: this branch includes a merge of current main (post #1906/#2034 era). Full context and review history: #1733
Related GitHub Issue
Closes: #1727
Description
For Anthropic and MiniMax models, the task cost shown in the UI omits the output-token portion of the bill, because the streaming loop never adopts the cumulative
message_delta.usage.output_tokensinto the local accumulator used for the finaltotalCostchunk — only themessage_startseed (0/1) is counted.This PR adopts the cumulative value (replace, not add — the final
message_deltacarries the message total, per the pinned SDK's replace semantics) in bothsrc/api/providers/anthropic.tsandsrc/api/providers/minimax.ts, so the end-of-stream cost chunk reflects the real output tokens. Scope note:anthropic-vertex.tsnever emits atotalCostchunk and is unaffected;opencode-go.tsconsumes the same cumulative delta but with pre-existing+=accumulation instreamAnthropicMessage— that file is intentionally not changed here and will be addressed in a separate follow-up.Test Procedure
message_startusage{input:100, output:1}+message_deltausage{output_tokens:500}→ final usage chunk'stotalCostequalscalculateApiCostAnthropic(info, 100, 500), not the starved(info, 100, 1). Verified failing before the fix, passing after.vitest run api/providers/__tests__/anthropic.spec.ts api/providers/__tests__/minimax.spec.ts→ 147/147 passed (2 files, incl. describe-nested cases).pnpm run test:coverage:core→ exit 0 (162 files / 3015 passed).vitest run --coverage --config vitest.api.config.ts→ exit 0; changed lines covered (anthropic.ts:295, minimax.ts:153).pnpm run check-types→ 0 errors. ESLint on changed files → clean, suppression counts unchanged.Pre-Submission Checklist
Visual Snapshots
N/A.
Videos (interaction / animation only)
N/A.
Documentation Updates
Additional Notes
Pre-existing (out of scope): the consumer in
Task.tssums the cumulative delta yield into its own display accumulator, so displayedtokensOutcan read ~1 over — cosmetic; the cost figure this PR fixes is now exact.Get in Touch
GitHub: @myk1yt — please tag me here; I monitor notifications.