Skip to content

Commit 11668af

Browse files
chrarnoldusdrakonenEamonNerbonne
authored
Fix OpenRouter cost calculation with BYOK (#4543)
Currently, when you use OpenRouter with your own key for the underlying service, the costs shown by Roo Code are way off what it actually costs. With bring your own key, OpenRouter charges 5% of what it normally would (see https://openrouter.ai/docs/use-cases/byok) so we have to multiply the reported cost by 20 to get an estimate of what it actually costs. Co-authored-by: Johan Otten <[email protected]> Co-authored-by: Eamon Nerbonne <[email protected]>
1 parent 395f55b commit 11668af

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/api/providers/openrouter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ interface CompletionUsage {
4848
}
4949
total_tokens?: number
5050
cost?: number
51+
is_byok?: boolean
5152
}
5253

54+
// with bring your own key, OpenRouter charges 5% of what it normally would: https://openrouter.ai/docs/use-cases/byok
55+
// so we multiply the cost reported by OpenRouter to get an estimate of what the request actually cost
56+
const BYOK_COST_MULTIPLIER = 20
57+
5358
export class OpenRouterHandler extends BaseProvider implements SingleCompletionHandler {
5459
protected options: ApiHandlerOptions
5560
private client: OpenAI
@@ -164,7 +169,7 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
164169
// and how to best support it.
165170
// cacheReadTokens: lastUsage.prompt_tokens_details?.cached_tokens,
166171
reasoningTokens: lastUsage.completion_tokens_details?.reasoning_tokens,
167-
totalCost: lastUsage.cost || 0,
172+
totalCost: (lastUsage.is_byok ? BYOK_COST_MULTIPLIER : 1) * (lastUsage.cost || 0),
168173
}
169174
}
170175
}

0 commit comments

Comments
 (0)