[PR #11175] fix: correct billing calculation for per-million token pricing #13676

Closed
opened 2026-02-16 18:18:30 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/11175

State: closed
Merged: No


Summary

Fixed a critical billing calculation bug that caused users to be overcharged by approximately 15x.

Root Cause

The cost calculation in trackUsage() was missing division by 1,000,000 to convert from per-million token pricing to actual token count.

Pricing is defined per 1M tokens (e.g., Qwen3 Coder 480B: $0.45 per 1M input tokens), but the calculation was treating the price as per-token.

Example

For Qwen3 Coder 480B at $0.45 per 1M input tokens with 983,087 input tokens:

Before (buggy):

inputCost = 0.45 × 983,087 × 100 = $44,238.91

After (fixed):

inputCost = (0.45 / 1,000,000) × 983,087 × 100 = $0.44

Impact

This bug affected all billing calculations:

  • Input token costs
  • Output token costs
  • Reasoning token costs
  • Cache read costs
  • Cache write costs (5m and 1h)

Files Changed

  • packages/console/app/src/routes/zen/util/handler.ts - Fixed cost calculation

Related Issues

Fixes #11170 (Billing Issue - No Support Channel - Overcharged by a factor of 10)
Also related to #8945 (Mismatch between token spent and billing)

🤖 Generated with Claude Code

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/11175 **State:** closed **Merged:** No --- ## Summary Fixed a critical billing calculation bug that caused users to be overcharged by approximately 15x. ## Root Cause The cost calculation in `trackUsage()` was missing division by 1,000,000 to convert from per-million token pricing to actual token count. Pricing is defined per 1M tokens (e.g., Qwen3 Coder 480B: $0.45 per 1M input tokens), but the calculation was treating the price as per-token. ## Example For Qwen3 Coder 480B at $0.45 per 1M input tokens with 983,087 input tokens: **Before (buggy):** ``` inputCost = 0.45 × 983,087 × 100 = $44,238.91 ``` **After (fixed):** ``` inputCost = (0.45 / 1,000,000) × 983,087 × 100 = $0.44 ``` ## Impact This bug affected all billing calculations: - Input token costs - Output token costs - Reasoning token costs - Cache read costs - Cache write costs (5m and 1h) ## Files Changed - `packages/console/app/src/routes/zen/util/handler.ts` - Fixed cost calculation ## Related Issues Fixes #11170 (Billing Issue - No Support Channel - Overcharged by a factor of 10) Also related to #8945 (Mismatch between token spent and billing) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
yindo added the pull-request label 2026-02-16 18:18:30 -05:00
yindo closed this issue 2026-02-16 18:18:30 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13676