Skip to content

Fix usage & billing for custom model aliases and cached/reasoning tokens#4222

Merged
rekram1-node merged 4 commits intoanomalyco:devfrom
melihmucuk:fix/usage-cost-models
Nov 12, 2025
Merged

Fix usage & billing for custom model aliases and cached/reasoning tokens#4222
rekram1-node merged 4 commits intoanomalyco:devfrom
melihmucuk:fix/usage-cost-models

Conversation

@melihmucuk
Copy link
Copy Markdown
Contributor

@melihmucuk melihmucuk commented Nov 11, 2025

Summary:

  • Ensure custom model aliases inherit the real model metadata so context/limits and cost are computed from the actual model.
  • Correct usage/cost calculation: bill cached/uncached input tokens separately, include cache-write tokens, and bill reasoning tokens at the same rate as output tokens.

Changed files:

  • packages/opencode/src/provider/provider.ts — fallback to lookup parsed.models[model.id] for aliases.
  • packages/opencode/src/session/index.ts — getUsage: compute uncachedInput = inputTokens - cachedInputTokens, bill cached/uncached input separately, include cache.write, and charge reasoning tokens at output rate.

Tests: ran session tests locally (2 passed).

Fixes #4162

@melihmucuk
Copy link
Copy Markdown
Contributor Author

@rekram1-node, we fixed the same bug, but this also fixes the spent calculations.

@rekram1-node
Copy link
Copy Markdown
Collaborator

@melihmucuk can u explain the cached input stuff? Cached input isn't free

@melihmucuk
Copy link
Copy Markdown
Contributor Author

melihmucuk commented Nov 11, 2025

@rekram1-node Yes, cached input isn’t free. The SDK returns total input tokens, but we need to know how many of those were uncached to calculate spent correctly.

For example, suppose the SDK reports 1M input tokens: 100K uncached and 900K cached. We can’t calculate spent as 1M * input_token_price. We have to separate cached and uncached input tokens and then calculate the cost accordingly.

@rekram1-node
Copy link
Copy Markdown
Collaborator

okay cool, I think I misread ur original comment which got me confused so apologies

@rekram1-node
Copy link
Copy Markdown
Collaborator

@melihmucuk can u resolve the conflict, I think ur changes are correct for the input tokens

@melihmucuk
Copy link
Copy Markdown
Contributor Author

@rekram1-node done.

@melihmucuk
Copy link
Copy Markdown
Contributor Author

@rekram1-node ok for revert, but definitely add reasoning token cost to total cost. Almost all providers calculate the reasoning tokens as output tokens.

@rekram1-node
Copy link
Copy Markdown
Collaborator

Yeah for sure ill add in

@rekram1-node
Copy link
Copy Markdown
Collaborator

ill merge this in sorry got distracted

@rekram1-node rekram1-node merged commit b63b6d0 into anomalyco:dev Nov 12, 2025
xywsxp pushed a commit to xywsxp/opencode that referenced this pull request Apr 24, 2026
…ens (anomalyco#4222)

Co-authored-by: Melih Mucuk <melih@monkeysteam.com>
Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
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.

Cost is always displayed as zero ($0.00) for custom models that extend built-in ones

2 participants