Skip to content

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

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

rekram1-node merged 4 commits into
anomalyco:devfrom
melihmucuk:fix/usage-cost-models

Conversation

@melihmucuk

@melihmucuk melihmucuk commented Nov 11, 2025 •

Copy link
Copy Markdown
Contributor

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

melihmucuk commented Nov 11, 2025 •

Copy link
Copy Markdown
Contributor Author

@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
Rwanbt pushed a commit to Rwanbt/unifia that referenced this pull request May 5, 2026
AIALRA-0 pushed a commit to AIALRA-0/opencode-turn-engine that referenced this pull request Jun 10, 2026
Rwanbt pushed a commit to Rwanbt/unifia that referenced this pull request Jun 14, 2026
meSingh pushed a commit to meSingh/opencode that referenced this pull request Sep 6, 2026
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