-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: calculate OpenRouter costs locally to avoid API miscalculation #8651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Calculate costs using model pricing info instead of relying on OpenRouter API response - Fixes issue where OpenRouter returns incorrect cost values (e.g., $0.46 instead of $1.50+ for 527k tokens) - Falls back to API cost if model pricing info is unavailable - Added test case to verify fix for reported issue #8650
// Use locally calculated cost, but fall back to API response if our calculation fails | ||
// or if model pricing info is not available | ||
const apiCost = (lastUsage.cost_details?.upstream_inference_cost || 0) + (lastUsage.cost || 0) | ||
const totalCost = modelInfo.inputPrice && modelInfo.outputPrice ? localCost : apiCost |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using modelInfo.inputPrice && modelInfo.outputPrice
to decide whether to use locally calculated cost may misinterpret valid zero prices as missing. Consider checking explicitly for undefined/null (e.g. modelInfo.inputPrice != null && modelInfo.outputPrice != null
) to ensure that a price of 0 isn’t treated as false.
const totalCost = modelInfo.inputPrice && modelInfo.outputPrice ? localCost : apiCost | |
const totalCost = modelInfo.inputPrice != null && modelInfo.outputPrice != null ? localCost : apiCost |
// Use locally calculated cost, but fall back to API response if our calculation fails | ||
// or if model pricing info is not available | ||
const apiCost = (lastUsage.cost_details?.upstream_inference_cost || 0) + (lastUsage.cost || 0) | ||
const totalCost = modelInfo.inputPrice && modelInfo.outputPrice ? localCost : apiCost |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The truthy check here will incorrectly fall back to API cost when model prices are 0 (e.g., free tier models). If inputPrice
or outputPrice
is 0, the condition evaluates to false even though 0 is a valid price.
const totalCost = modelInfo.inputPrice && modelInfo.outputPrice ? localCost : apiCost | |
const totalCost = modelInfo.inputPrice !== undefined && modelInfo.outputPrice !== undefined ? localCost : apiCost |
Related GitHub Issue
Closes: #8650
Roo Code Task Context (Optional)
This PR was created with assistance from Roo Code.
Description
This PR fixes the cost miscalculation issue when using Claude 3.5 Sonnet (and other models) through OpenRouter. The issue occurred because OpenRouter's API was returning incorrect cost values in the usage response.
Key implementation details:
calculateApiCostOpenAI
utility function for consistent cost calculation across the codebaseWhat reviewers should pay attention to:
src/api/providers/openrouter.ts
lines 199-226Test Procedure
How I tested:
How reviewers can verify:
cd src && npx vitest run api/providers/__tests__/openrouter.spec.ts
Pre-Submission Checklist
Screenshots / Videos
Not applicable - this is a backend cost calculation fix with no UI changes.
Documentation Updates
The fix is transparent to users and doesn't change any APIs or user-facing behavior.
Additional Notes
This fix ensures that users see accurate cost calculations when using OpenRouter, particularly for high token count sessions. The fallback mechanism ensures backward compatibility if model pricing information is not available.
Get in Touch
Automated PR created via GitHub API
Important
Fix cost miscalculation in
OpenRouterHandler
by calculating costs locally using model pricing info and adding fallback to API costs.OpenRouterHandler
inopenrouter.ts
to calculate costs locally using model pricing info.calculateApiCostOpenAI
for consistent cost calculation.openrouter.spec.ts
to validate local cost calculation for 527k input tokens.createMessage()
inopenrouter.ts
lines 199-226.This description was created by
for 1eecf1f. You can customize this summary. It will automatically update as commits are pushed.