Skip to content

Commit 29ebce2

Browse files
fix(session): port upstream malformed model cost guard (anomalyco/opencode#43248)
1 parent 11bfb29 commit 29ebce2

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎packages/opencode/src/session/index.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -932,18 +932,21 @@ export namespace Session {
932932
input.model.cost?.experimentalOver200K && tokens.input + tokens.cache.read > 200_000
933933
? input.model.cost.experimentalOver200K
934934
: input.model.cost
935+
// altimate_change start — upstream_fix: ignore malformed (non-finite) model cost fields (anomalyco/opencode#43248)
936+
const finite = (value: number) => (Number.isFinite(value) ? value : 0)
935937
return {
936938
cost: safe(
937939
new Decimal(0)
938-
.add(new Decimal(tokens.input).mul(costInfo?.input ?? 0).div(1_000_000))
939-
.add(new Decimal(tokens.output).mul(costInfo?.output ?? 0).div(1_000_000))
940-
.add(new Decimal(tokens.cache.read).mul(costInfo?.cache?.read ?? 0).div(1_000_000))
941-
.add(new Decimal(tokens.cache.write).mul(costInfo?.cache?.write ?? 0).div(1_000_000))
940+
.add(new Decimal(tokens.input).mul(finite(costInfo?.input ?? 0)).div(1_000_000))
941+
.add(new Decimal(tokens.output).mul(finite(costInfo?.output ?? 0)).div(1_000_000))
942+
.add(new Decimal(tokens.cache.read).mul(finite(costInfo?.cache?.read ?? 0)).div(1_000_000))
943+
.add(new Decimal(tokens.cache.write).mul(finite(costInfo?.cache?.write ?? 0)).div(1_000_000))
942944
// TODO: update models.dev to have better pricing model, for now:
943945
// charge reasoning tokens at the same rate as output tokens
944-
.add(new Decimal(tokens.reasoning).mul(costInfo?.output ?? 0).div(1_000_000))
946+
.add(new Decimal(tokens.reasoning).mul(finite(costInfo?.output ?? 0)).div(1_000_000))
945947
.toNumber(),
946948
),
949+
// altimate_change end
947950
tokens,
948951
}
949952
},

‎packages/opencode/test/session/session-getusage.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ describe("Session.getUsage — provider edge cases", () => {
216216
})
217217
})
218218

219+
// altimate_change start — upstream_fix: malformed model cost fields (anomalyco/opencode#43248)
220+
describe("Session.getUsage — malformed model cost", () => {
221+
test("ignores non-numeric cost fields instead of throwing", () => {
222+
const model = fakeModel("@ai-sdk/openai")
223+
model.cost = { input: {}, output: 15, cache: { read: Number.NaN, write: "x" } }
224+
const result = Session.getUsage({
225+
model,
226+
usage: { inputTokens: 1_000_000, outputTokens: 100_000, totalTokens: 1_100_000 } as any,
227+
})
228+
expect(result.cost).toBe(1.5)
229+
})
230+
})
231+
// altimate_change end
232+
219233
describe("Session.getUsage — invariant", () => {
220234
test("input + cache.read + cache.write === inputTotal for cache-using calls", () => {
221235
for (const npm of ["@ai-sdk/anthropic", "@ai-sdk/openai", "@ai-sdk/amazon-bedrock"] as const) {

0 commit comments

Comments
 (0)