Skip to content

fix(sdk): take max of OpenRouter cost and upstream_inference_cost - #1165

Closed
nordicnode wants to merge 9049 commits into
CodebuffAI:mainfrom
nordicnode:oss/credits-upstream-max-1164
Closed

fix(sdk): take max of OpenRouter cost and upstream_inference_cost#1165
nordicnode wants to merge 9049 commits into
CodebuffAI:mainfrom
nordicnode:oss/credits-upstream-max-1164

Conversation

@nordicnode

Copy link
Copy Markdown

Summary

  • user-facing credits were computed as usage.cost + cost_details.upstream_inference_cost at all three cost sites (sdk/src/impl/llm.ts streaming reportCost, generateText, and structured paths)
  • usage.cost is the total amount charged and already includes the upstream portion reported separately in cost_details, per OpenRouter's usage accounting docs — summing them roughly doubles credits on normal (non-BYOK) OpenRouter routes, which lines up with the "extreme inflation" reports on Discord
  • take the max instead, matching the server-side ledger's documented semantics (the Solar Pro 4 BYOK note in common/src/constants/freebuff-models.ts:313-320: cost = 0 there and upstream carries the real spend, so max is correct for both shapes)
  • add a regression test asserting exact credits for cost=0.01, upstream=0.02 (fails under the sum: 3 vs 2)

Fixes #1164

Validation

  • bun test sdk/src/impl/__tests__/usage-receipts.test.ts (5 pass, 1 new — verified the new test fails with the sum by stashing the fix)
  • bun test sdk (489 pass; the 100 failures are network/env-dependent and pre-exist on main)
  • bunx tsc --noEmit -p sdk/tsconfig.json clean

Loading
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.

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

2 participants