Skip to content

Conversation

@abromberg
Copy link
Contributor

@abromberg abromberg commented Nov 22, 2025

OpenRouter supports BYOK, where the user adds a provider API key to their OpenRouter account, and usage is then charged directly to that key. In that case, OpenRouter's usage accounting stores the upstream costs in cost_details.upstream_inference_cost. This field was not being passed through this provider in all cases, and so users could not access these costs in their applications.

This PR adds the upstream inference cost to the chat & completions outputs when it is present, and is backwards-compatible. It also adds tests for the same, including evaluating 1) only normal cost, 2) only upstream inference cost, and 3) both normal and upstream inference cost.

PR #107 added some of this behavior (for doGenerate) but left it out of the streaming and completion endpoints. It also enforced upstreamInferenceCost: 0 even when OpenRouter never actually reported that field, which I'm not sure is the correct behavior (although I'm happy to revert that if it is). This PR also adds more robust testing.

Note: written on top of #246 to fix the underlying build error

@abromberg abromberg changed the title Include upstream cost forwarding feat: include upstream cost forwarding Nov 22, 2025
subtleGradient and others added 3 commits December 1, 2025 17:03
Renamed models.Sort to models.ProviderSort to resolve type mismatch.
Adds support for OpenRouter's BYOK usage accounting. If you have a provider's own API key in your OpenRouter account, cost details are now accessible via usage.costDetails.upstreamInferenceCost.
@subtleGradient subtleGradient changed the base branch from main to tom/fix-optional-token-details December 1, 2025 22:21
@subtleGradient subtleGradient deleted the branch OpenRouterTeam:tom/fix-optional-token-details December 1, 2025 22:26
@subtleGradient
Copy link
Contributor

closed on accident; will fix; 1sec

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.

2 participants