Skip to content

Commit 85f2ef2

Browse files
committed
fix(sdk): use max instead of sum for OpenRouter cost fields
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 #1164
1 parent bc4ed90 commit 85f2ef2

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

‎sdk/src/impl/__tests__/usage-receipts.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ describe('stream usage receipts', () => {
103103
},
104104
])
105105
expect(costs).toHaveLength(1)
106-
expect(costs[0]).toBeGreaterThan(0)
106+
// max(0.01, 0.02) * 1.055 * 100 = 2.11, rounded to 2
107+
expect(costs[0]).toBe(2)
107108
expect(callbackOrder).toEqual(['usage', 'cost'])
108109
})
109110

‎sdk/src/impl/llm.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,10 @@ export async function* promptAiSdkStream(
392392
| OpenRouterUsageAccounting
393393
| undefined
394394
const costOverrideDollars = openrouterUsage
395-
? (openrouterUsage.cost ?? 0) +
396-
(openrouterUsage.costDetails?.upstreamInferenceCost ?? 0)
395+
? Math.max(
396+
openrouterUsage.cost ?? 0,
397+
openrouterUsage.costDetails?.upstreamInferenceCost ?? 0,
398+
)
397399
: undefined
398400
if (!params.onCostCalculated || !costOverrideDollars) return
399401
costReported = true
@@ -732,9 +734,10 @@ export async function promptAiSdk(
732734
const openrouterUsage = providerMetadata.codebuff
733735
.usage as OpenRouterUsageAccounting
734736

735-
costOverrideDollars =
736-
(openrouterUsage.cost ?? 0) +
737-
(openrouterUsage.costDetails?.upstreamInferenceCost ?? 0)
737+
costOverrideDollars = Math.max(
738+
openrouterUsage.cost ?? 0,
739+
openrouterUsage.costDetails?.upstreamInferenceCost ?? 0,
740+
)
738741
}
739742
}
740743

@@ -803,9 +806,10 @@ export async function promptAiSdkStructured<T>(
803806
const openrouterUsage = providerMetadata.codebuff
804807
.usage as OpenRouterUsageAccounting
805808

806-
costOverrideDollars =
807-
(openrouterUsage.cost ?? 0) +
808-
(openrouterUsage.costDetails?.upstreamInferenceCost ?? 0)
809+
costOverrideDollars = Math.max(
810+
openrouterUsage.cost ?? 0,
811+
openrouterUsage.costDetails?.upstreamInferenceCost ?? 0,
812+
)
809813
}
810814
}
811815

0 commit comments

Comments
 (0)