Browse Source

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]>
Christiaan Arnoldus 7 months ago
parent
commit
11668af83d
1 changed files with 6 additions and 1 deletions
  1. 6 1
      src/api/providers/openrouter.ts

+ 6 - 1
src/api/providers/openrouter.ts

@@ -48,8 +48,13 @@ interface CompletionUsage {
 	}
 	}
 	total_tokens?: number
 	total_tokens?: number
 	cost?: number
 	cost?: number
+	is_byok?: boolean
 }
 }
 
 
+// with bring your own key, OpenRouter charges 5% of what it normally would: https://openrouter.ai/docs/use-cases/byok
+// so we multiply the cost reported by OpenRouter to get an estimate of what the request actually cost
+const BYOK_COST_MULTIPLIER = 20
+
 export class OpenRouterHandler extends BaseProvider implements SingleCompletionHandler {
 export class OpenRouterHandler extends BaseProvider implements SingleCompletionHandler {
 	protected options: ApiHandlerOptions
 	protected options: ApiHandlerOptions
 	private client: OpenAI
 	private client: OpenAI
@@ -164,7 +169,7 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
 				// and how to best support it.
 				// and how to best support it.
 				// cacheReadTokens: lastUsage.prompt_tokens_details?.cached_tokens,
 				// cacheReadTokens: lastUsage.prompt_tokens_details?.cached_tokens,
 				reasoningTokens: lastUsage.completion_tokens_details?.reasoning_tokens,
 				reasoningTokens: lastUsage.completion_tokens_details?.reasoning_tokens,
-				totalCost: lastUsage.cost || 0,
+				totalCost: (lastUsage.is_byok ? BYOK_COST_MULTIPLIER : 1) * (lastUsage.cost || 0),
 			}
 			}
 		}
 		}
 	}
 	}