Skip to content

fix(sdk): use max instead of sum for OpenRouter cost fields - #1198

Open
mturac wants to merge 1 commit into
CodebuffAI:mainfrom
mturac:fix/issue-1164
Open

fix(sdk): use max instead of sum for OpenRouter cost fields#1198
mturac wants to merge 1 commit into
CodebuffAI:mainfrom
mturac:fix/issue-1164

Conversation

@mturac

@mturac mturac commented Sep 2, 2026

Copy link
Copy Markdown

All three cost-calculation paths in the SDK (promptAiSdkStream, promptAiSdk, promptAiSdkStructured) sum usage.cost and cost_details.upstream_inference_cost to derive the dollar amount charged as credits. Per OpenRouter's accounting, usage.cost is the total charge and upstream_inference_cost is the upstream component already included in that total -- so summing them roughly doubles the true spend on non-BYOK routes.

Changed the aggregation from cost + upstream to Math.max(cost, upstream), which returns the correct value in both shapes:

Normal route: cost is the authoritative total (includes upstream + margin); upstream < cost; max picks cost.
BYOK route: cost is 0; upstream carries the real spend; max picks upstream.

This matches the server-side extractUsageAndCost semantics documented in common/src/constants/freebuff-models.ts.

Existing usage-receipts.test.ts passes (strengthened the assertion from > 0 to exact expected credits). TypeScript compiles clean.

Fixes #1164

usage.cost already includes upstream_inference_cost on non-BYOK
routes, so summing them double-counts the upstream component.
Use Math.max to pick the correct value for both BYOK (cost=0,
upstream>0) and normal (cost>upstream) routes.

Fixes CodebuffAI#1164
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 2, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-02T08:05:14.701479Z 85f2ef2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch and a clean, minimal fix. The reasoning matches how OpenRouter's usage.cost and cost_details.upstream_inference_cost are documented to relate (total vs. upstream-only component), and you applied the same fix consistently across promptAiSdkStream, promptAiSdk, and promptAiSdkStructured in sdk/src/impl/llm.ts, which is exactly the kind of consistency this repo would want.

A few things a maintainer will want to check before porting:

  1. Please link or quote the actual OpenRouter docs/API response confirming cost is always the total inclusive of upstream_inference_cost, and not the reverse in some provider configurations. The PR body asserts this but the diff doesn't add a comment or reference in the code itself — worth adding a one-line comment above the Math.max(...) calls citing the semantics, since this is non-obvious and someone will change it back to + later.
  2. The test change in usage-receipts.test.ts only exercises the streaming path (promptAiSdkStream). Since you're touching two more call sites with the same logic, it would strengthen the PR to add or update tests for promptAiSdk and promptAiSdkStructured too, particularly the BYOK case where cost is 0 and upstream carries the real spend — that's the case most likely to regress silently.
  3. Confirm this matches extractUsageAndCost in common/src/constants/freebuff-models.ts byte-for-byte in the private repo, since that's your comparison point but it's outside this diff.

The PR is marked "unstable" mergeability — worth checking whether that's just CI flake or a real conflict before it lands.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SDK: credits double-count OpenRouter's upstream_inference_cost — usage.cost already includes it

2 participants