Skip to content

fix(api): adopt cumulative message_delta output tokens in Anthropic/MiniMax cost - #1733

Closed
myk1yt wants to merge 2 commits into
Zoo-Code-Org:mainfrom
myk1yt:fix/anthropic-minimax-output-cost
Closed

myk1yt wants to merge 2 commits into
Zoo-Code-Org:mainfrom
myk1yt:fix/anthropic-minimax-output-cost

Conversation

@myk1yt

@myk1yt myk1yt commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

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_tokens into the local accumulator used for the final totalCost chunk — only the message_start seed (0/1) is counted.

This PR adopts the cumulative value (replace, not add — the final message_delta carries the message total, per the pinned SDK's replace semantics) in both src/api/providers/anthropic.ts and src/api/providers/minimax.ts, so the end-of-stream cost chunk reflects the real output tokens. (opencode-go.ts already documents this exact trap; anthropic-vertex.ts never emits a totalCost chunk and is unaffected.)

Test Procedure

  • New regression test in each provider spec: stream message_start usage {input:100, output:1} + message_delta usage {output_tokens:500} → final usage chunk's totalCost equals calculateApiCostAnthropic(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 → 99/99.
  • 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

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Visual Snapshot (UI changes only): N/A.
  • Documentation Impact: No documentation updates are required.
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Visual Snapshots

N/A.

Videos (interaction / animation only)

N/A.

Documentation Updates

  • No documentation updates are required.

Additional Notes

Pre-existing (out of scope): the consumer in Task.ts sums the cumulative delta yield into its own display accumulator, so displayed tokensOut can 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.

@coderabbitai

coderabbitai Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Understand this PR’s impact

Explore downstream dependencies and potential security impact with Blast Radius.

View blast radius →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: Zoo-Code-Org/Zoo-Code/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Advanced

Run ID: de38da69-ca1a-493f-bed7-c49b65b735ea

📥 Commits

Reviewing files that changed from the base of the PR and between 99442c3 and cee37b6.

📒 Files selected for processing (2)
  • src/api/providers/__tests__/anthropic.spec.ts
  • src/api/providers/__tests__/minimax.spec.ts

Included review availability: Your plan provides up to 4 included reviews per hour; 0 remain after this review.

📜 Recent review details
🧰 Additional context used
📓 Path-based instructions (5)
Treat model, provider, MCP, path, command, and tool data as untrusted.

⚙️ CodeRabbit configuration file

Files:

  • src/api/providers/__tests__/minimax.spec.ts
  • src/api/providers/__tests__/anthropic.spec.ts
Require regression coverage at the lowest valid harness with behavior-focused assertions, including relevant negative, error, false/unset, and boundary cases.

⚙️ CodeRabbit configuration file

Files:

  • src/api/providers/__tests__/minimax.spec.ts
  • src/api/providers/__tests__/anthropic.spec.ts
Check strict typing and exhaustive behavior across normal, boundary, error, cancellation, retry, and compatibility paths.

⚙️ CodeRabbit configuration file

Files:

  • src/api/providers/__tests__/minimax.spec.ts
  • src/api/providers/__tests__/anthropic.spec.ts
Verify extension/webview contracts, cancellation and error propagation, VS Code lifecycle correctness, and behavior under retries and partial failure.

⚙️ CodeRabbit configuration file

Files:

  • src/api/providers/__tests__/minimax.spec.ts
  • src/api/providers/__tests__/anthropic.spec.ts
Act as an adversarial second-opinion reviewer.

⚙️ CodeRabbit configuration file

Files:

  • src/api/providers/__tests__/minimax.spec.ts
  • src/api/providers/__tests__/anthropic.spec.ts
🔇 Additional comments (2)
src/api/providers/__tests__/anthropic.spec.ts (1)

209-215: LGTM!

src/api/providers/__tests__/minimax.spec.ts (1)

303-309: LGTM!

📝 Summary

Summary by CodeRabbit

  • Bug Fixes

    • Corrected streamed usage tracking so final output-token totals are calculated accurately.
    • Improved cost reporting for Anthropic and MiniMax responses by reflecting cumulative output tokens instead of an initial partial count.
    • Ensured later usage updates that report zero tokens do not overwrite the previously recorded total.
  • Tests

    • Added regression coverage for streamed token usage and final cost calculations across both providers.

Anthropic and MiniMax streaming handlers now use cumulative message_delta output-token usage for final cost calculations. Regression tests verify the corrected output-token totals.

Changes: Streaming cost correction

Layer / File(s) Summary
Provider usage adoption
src/api/providers/anthropic.ts, src/api/providers/minimax.ts
The handlers now replace the initial output-token seed with the cumulative message_delta value.
Streaming cost regression coverage
src/api/providers/__tests__/anthropic.spec.ts, src/api/providers/__tests__/minimax.spec.ts
Tests verify final costs use cumulative output counts of 500 and 20, including retention after a subsequent zero-count delta.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Bug fix · Severity of issue fixed: Medium

Pre-merge checks: Passed 8
Check name Status Explanation
Linked Issues check Passed Issue #1727 requires both providers to adopt the cumulative message_delta.usage.output_tokens value for final task cost calculations. anthropic.ts and minimax.ts now replace the seed with that v…
Out of Scope Changes check Passed The changes are limited to the Anthropic and MiniMax output-token accumulators and focused provider regression tests. These changes directly support issue #1727. No unrelated behavior or scope expansi…
Regression Evidence Passed Focused provider-level regression coverage is present for both changed behaviors. anthropic.spec.ts and minimax.spec.ts stream a message_start seed of 1, a cumulative message_delta value, and …
Security Boundaries Passed No changed path crosses a security boundary. The only production changes in src/api/providers/anthropic.ts:295 and src/api/providers/minimax.ts:153 assign the provider-reported numeric `message_de…
Persistence Integrity Passed PASS. The pull request changes only in-memory output-token accounting in AnthropicHandler.createMessage and MiniMaxHandler.createMessage, plus tests. The changed assignment adopts `message_delta.u…
Lifecycle Resource Cleanup Passed The pull request changes only local output-token assignment inside the existing for await stream loops in AnthropicHandler.createMessage and MiniMaxHandler.createMessage, plus regression tests. …
Title check Passed The title clearly and concisely describes the main fix: using cumulative message_delta output tokens for Anthropic and MiniMax cost calculations.
Description check Passed The description includes the linked issue, implementation details, test procedure and results, completed checklist, documentation status, and additional notes. It is focused and sufficiently detailed …
Full details: Linked Issues check

Explanation

Issue #1727 requires both providers to adopt the cumulative message_delta.usage.output_tokens value for final task cost calculations. anthropic.ts and minimax.ts now replace the seed with that value and preserve the prior value when the delta value is zero or absent. Regression tests verify final costs with cumulative output counts instead of seed counts. The changes meet the coding requirements.

Full details: Out of Scope Changes check

Explanation

The changes are limited to the Anthropic and MiniMax output-token accumulators and focused provider regression tests. These changes directly support issue #1727. No unrelated behavior or scope expansion is shown.

Full details: Regression Evidence

Explanation

Focused provider-level regression coverage is present for both changed behaviors. anthropic.spec.ts and minimax.spec.ts stream a message_start seed of 1, a cumulative message_delta value, and a later zero-token delta; each asserts the final totalCost uses the cumulative value and not the seed. This covers replacement semantics and the falsy fallback branch. The tests exercise createMessage through the final cost calculation, so they do not merely assert the local assignment. The PR changes no visible UI component, so no Playwright snapshot applies.

Full details: Security Boundaries

Explanation

No changed path crosses a security boundary. The only production changes in src/api/providers/anthropic.ts:295 and src/api/providers/minimax.ts:153 assign the provider-reported numeric message_delta.usage.output_tokens value to the local counter used by calculateApiCostAnthropic. The value is used for cost reporting only; the diff does not expose secrets or PII, execute input, alter requests or credentials, or bypass approval and allowlist controls. The test-only changes add stream fixtures and cost assertions.

Full details: Persistence Integrity

Explanation

PASS. The pull request changes only in-memory output-token accounting in AnthropicHandler.createMessage and MiniMaxHandler.createMessage, plus tests. The changed assignment adopts message_delta.usage.output_tokens before calculating the final cost. No changed persistence path, write, transaction, rollback, or persisted-default propagation exists.

Full details: Lifecycle Resource Cleanup

Explanation

The pull request changes only local output-token assignment inside the existing for await stream loops in AnthropicHandler.createMessage and MiniMaxHandler.createMessage, plus regression tests. The changed paths do not add or remove listeners, watchers, timers, tasks, providers, or other resources, and they do not alter cancellation, disposal, restart, or stream termination behavior. No changed lifecycle path can leak a resource or duplicate work.

Full details: Description check

Explanation

The description includes the linked issue, implementation details, test procedure and results, completed checklist, documentation status, and additional notes. It is focused and sufficiently detailed for review.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Review status

Thanks for contributing. This comment tracks the review sequence and the next action.

Current step: Resolve the merge conflicts. The review sequence resumes after the branch is mergeable.

Review-state labels are managed by this workflow; do not edit them manually. community-approved is managed the same way — do not add or remove it manually. It signals a fresh community code approval for the current head as an advisory priority only; maintainer review is still required.

@github-actions github-actions Bot added coderabbit-review-active Required CI passed; CodeRabbit review is active awaiting-coderabbit Waiting for CodeRabbit to approve the latest commit labels Sep 20, 2026
@codecov

codecov Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

coderabbitai[bot]
coderabbitai Bot previously approved these changes Sep 20, 2026
@github-actions github-actions Bot added awaiting-maintainer CodeRabbit approved; waiting for a human maintainer and removed coderabbit-review-active Required CI passed; CodeRabbit review is active awaiting-coderabbit Waiting for CodeRabbit to approve the latest commit labels Sep 20, 2026
@github-actions github-actions Bot added coderabbit-review-active Required CI passed; CodeRabbit review is active awaiting-coderabbit Waiting for CodeRabbit to approve the latest commit and removed awaiting-maintainer CodeRabbit approved; waiting for a human maintainer labels Sep 21, 2026
@github-actions github-actions Bot added awaiting-maintainer CodeRabbit approved; waiting for a human maintainer and removed coderabbit-review-active Required CI passed; CodeRabbit review is active awaiting-coderabbit Waiting for CodeRabbit to approve the latest commit labels Sep 21, 2026
@github-actions github-actions Bot added awaiting-maintainer CodeRabbit approved; waiting for a human maintainer and removed awaiting-maintainer CodeRabbit approved; waiting for a human maintainer labels Oct 3, 2026
@github-actions github-actions Bot added has-conflicts PR has merge conflicts with the base branch and removed awaiting-maintainer CodeRabbit approved; waiting for a human maintainer labels Oct 13, 2026
@github-actions github-actions Bot added the awaiting-author PR is waiting for the author to address requested changes label Oct 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been awaiting author changes for 14 days and will be automatically closed in 7 days. Please address the review comments or leave a comment if you need more time.

@github-actions github-actions Bot added the stale-awaiting-author PR is stale while waiting for requested author changes label Oct 21, 2026
@github-actions github-actions Bot closed this Oct 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Closing due to author inactivity after requested changes. Feel free to reopen once the requested changes have been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-author PR is waiting for the author to address requested changes has-conflicts PR has merge conflicts with the base branch stale-awaiting-author PR is stale while waiting for requested author changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic/MiniMax streaming: reported task cost omits output-token cost

1 participant