Skip to content

fix(sdk-openrouter): count the provider cost of byok requests - #92

Merged
ephraimduncan merged 1 commit into
mainfrom
fix/sdk-openrouter/byok-cost
Oct 27, 2026
Merged

ephraimduncan merged 1 commit into
mainfrom
fix/sdk-openrouter/byok-cost

Conversation

@ephraimduncan

Copy link
Copy Markdown
Contributor

A request with the user's own provider key (BYOK) through the OpenRouter SDK integration now gets the cost that the provider bills. Before this change, the span took usage.cost. For a BYOK request, that value is only the OpenRouter fee, which is 0 inside OpenRouter's free BYOK allowance. Thus BYOK calls showed as free.

When is_byok is true, the span cost is cost_details.upstream_inference_cost plus usage.cost. If the upstream cost is not in the response, the span gets no cost, and telemetry.dev calculates an estimate from the token counts. Requests without BYOK keep usage.cost, with the upstream cost as the fallback, as before. The TypeScript and Python integrations change together.

The tests that ran are packages/openrouter/tests/openrouter.test.ts (55 pass) and sdks/python-openrouter/tests/test_openrouter.py (73 pass).

For a request with the user's own provider key (BYOK), the provider
bills the inference, and OpenRouter's usage.cost is only the OpenRouter
fee. The fee is zero inside the free BYOK allowance. Thus the
integrations recorded the fee as the cost of the request.

When usage.is_byok is true, the TypeScript and Python integrations now
record usage.cost plus cost_details.upstream_inference_cost as
gen_ai.usage.cost. Without the upstream inference cost, they record no
cost, and telemetry.dev calculates an estimate from the token counts.
Requests without BYOK keep usage.cost.
@ephraimduncan
ephraimduncan merged commit ac4773d into main Oct 27, 2026
10 checks passed
@aikins01 aikins01 mentioned this pull request Oct 27, 2026
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.

1 participant