[PR #4256] fix: correct cache cost for OpenRouter and other OpenAI-compatible providers #10918

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

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

State: closed
Merged: Yes


fixes #534, #454, #2020

Problem: There's a significant mismatch in costs displayed in OpenCode vs OpenRouter.

OpenRouter provides inputTokens as the total count of input tokens (including cached).
AFAIK other providers (OpenRouter/OpenAI/Gemini etc.) do it the same way e.g. https://github.com/vercel/ai/issues/8794#issuecomment-3320975344

Anthropic does it differently though - inputTokens doesn't include cached tokens.
It looks like OpenCode's cost calculation assumes all providers return inputTokens the same way Anthropic does (I'm guessing getUsage logic was originally implemented with anthropic), so it's causing incorrect cost calculation for OpenRouter and others.

Testing:
Verified against OpenRouter data - costs are now matching almost perfectly.
Why almost - cache.write costs are not included in OpenRouter's metadata, so there may be minor discrepancies for the messages with cache writes. This seems to be an upstream limitation that needs to be addressed by ai-sdk/openrouter.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/4256 **State:** closed **Merged:** Yes --- fixes #534, #454, #2020 Problem: There's a significant mismatch in costs displayed in OpenCode vs OpenRouter. OpenRouter provides `inputTokens` as the total count of input tokens (including cached). AFAIK other providers (OpenRouter/OpenAI/Gemini etc.) do it the same way e.g. https://github.com/vercel/ai/issues/8794#issuecomment-3320975344 Anthropic does it differently though - `inputTokens` doesn't include cached tokens. It looks like OpenCode's cost calculation assumes all providers return `inputTokens` the same way Anthropic does (I'm guessing `getUsage` logic was originally implemented with anthropic), so it's causing incorrect cost calculation for OpenRouter and others. Testing: Verified against OpenRouter data - costs are now matching _almost_ perfectly. Why almost - `cache.write` costs are not included in OpenRouter's metadata, so there may be minor discrepancies for the messages with cache writes. This seems to be an upstream limitation that needs to be addressed by ai-sdk/openrouter.
yindo added the pull-request label 2026-02-16 18:15:40 -05:00
yindo closed this issue 2026-02-16 18:15:40 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#10918