feat: include upstream cost forwarding #247
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 enforcedupstreamInferenceCost: 0even 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