Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sdk/src/impl/__tests__/usage-receipts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ describe('stream usage receipts', () => {
},
])
expect(costs).toHaveLength(1)
expect(costs[0]).toBeGreaterThan(0)
// max(0.01, 0.02) * 1.055 * 100 = 2.11, rounded to 2
expect(costs[0]).toBe(2)
expect(callbackOrder).toEqual(['usage', 'cost'])
})

Expand Down
20 changes: 12 additions & 8 deletions sdk/src/impl/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,10 @@ export async function* promptAiSdkStream(
| OpenRouterUsageAccounting
| undefined
const costOverrideDollars = openrouterUsage
? (openrouterUsage.cost ?? 0) +
(openrouterUsage.costDetails?.upstreamInferenceCost ?? 0)
? Math.max(
openrouterUsage.cost ?? 0,
openrouterUsage.costDetails?.upstreamInferenceCost ?? 0,
)
: undefined
if (!params.onCostCalculated || !costOverrideDollars) return
costReported = true
Expand Down Expand Up @@ -732,9 +734,10 @@ export async function promptAiSdk(
const openrouterUsage = providerMetadata.codebuff
.usage as OpenRouterUsageAccounting

costOverrideDollars =
(openrouterUsage.cost ?? 0) +
(openrouterUsage.costDetails?.upstreamInferenceCost ?? 0)
costOverrideDollars = Math.max(
openrouterUsage.cost ?? 0,
openrouterUsage.costDetails?.upstreamInferenceCost ?? 0,
)
}
}

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

costOverrideDollars =
(openrouterUsage.cost ?? 0) +
(openrouterUsage.costDetails?.upstreamInferenceCost ?? 0)
costOverrideDollars = Math.max(
openrouterUsage.cost ?? 0,
openrouterUsage.costDetails?.upstreamInferenceCost ?? 0,
)
}
}

Expand Down
Loading