Skip to content

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

Closed

Description

@myk1yt

Summary

For Anthropic and MiniMax models, the task cost reported in the UI omits the output-token portion of the bill. Every task on the default Anthropic provider shows only input + cache-read/write cost — typically 20–40% of the real total.

Mechanism

In src/api/providers/anthropic.ts the streaming loop accumulates outputTokens only from message_start.usage.output_tokens, which Anthropic documents (and the pinned SDK @anthropic-ai/[email protected] MessageStream confirms, lib/MessageStream.js:454 — replace semantics) as a seed value (0 or 1). The real total arrives cumulatively in message_delta.usage.output_tokens, whose final event carries the full count. The message_delta branch yields the value to the consumer but never adopts it into the local accumulator, so the end-of-stream totalCost chunk is computed from the seed.

Task.ts then assigns totalCost = chunk.totalCost (overriding the consumer-side costResult which is summed correctly), so the wrong value wins. Same defect in src/api/providers/minimax.ts. In-repo proof this is a known trap: opencode-go.ts:558-565 fixes exactly this with an explanatory comment.

Repro

  1. Select any Anthropic (or MiniMax) model, run any task.
  2. Open the task cost/token summary.
  3. Compare reported cost against calculateApiCostAnthropic(modelInfo, inputTokens, outputTokens) with the real output count — the output-token term (60–80% of the bill) is missing. Token counts look right because the consumer sums the deltas itself.

Suggested fix

Adopt (not add) the cumulative value in the message_delta case: outputTokens = chunk.usage.output_tokens || outputTokens, mirrored in minimax.ts. Regression tests: stream with message_start usage {input:100, output:1} + message_delta usage {output_tokens:500} → final usage chunk's totalCost must equal cost(info, 100, 500).

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions