Skip to content

Commit 9b0dd36

Browse files
authored
fix(session): ignore malformed model costs (anomalyco#43248)
1 parent 4e81a0b commit 9b0dd36

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,8 @@ export function plan(input: { slug: string; time: { created: number } }, instanc
336336
}
337337

338338
export const getUsage = (input: { model: Provider.Model; usage: Usage; metadata?: ProviderMetadata }) => {
339-
const safe = (value: number) => {
340-
if (!Number.isFinite(value)) return 0
341-
return Math.max(0, value)
342-
}
339+
const finite = (value: number) => (Number.isFinite(value) ? value : 0)
340+
const safe = (value: number) => Math.max(0, finite(value))
343341
const inputTokens = safe(input.usage.inputTokens ?? 0)
344342
const outputTokens = safe(input.usage.outputTokens ?? 0)
345343
const reasoningTokens = safe(input.usage.reasoningTokens ?? 0)
@@ -393,13 +391,13 @@ export const getUsage = (input: { model: Provider.Model; usage: Usage; metadata?
393391
? new Decimal(totalNanoAiu).div(100_000_000_000).toNumber()
394392
: safe(
395393
new Decimal(0)
396-
.add(new Decimal(tokens.input).mul(costInfo?.input ?? 0).div(1_000_000))
397-
.add(new Decimal(tokens.output).mul(costInfo?.output ?? 0).div(1_000_000))
398-
.add(new Decimal(tokens.cache.read).mul(costInfo?.cache?.read ?? 0).div(1_000_000))
399-
.add(new Decimal(tokens.cache.write).mul(costInfo?.cache?.write ?? 0).div(1_000_000))
394+
.add(new Decimal(tokens.input).mul(finite(costInfo?.input ?? 0)).div(1_000_000))
395+
.add(new Decimal(tokens.output).mul(finite(costInfo?.output ?? 0)).div(1_000_000))
396+
.add(new Decimal(tokens.cache.read).mul(finite(costInfo?.cache?.read ?? 0)).div(1_000_000))
397+
.add(new Decimal(tokens.cache.write).mul(finite(costInfo?.cache?.write ?? 0)).div(1_000_000))
400398
// TODO: update models.dev to have better pricing model, for now:
401399
// charge reasoning tokens at the same rate as output tokens
402-
.add(new Decimal(tokens.reasoning).mul(costInfo?.output ?? 0).div(1_000_000))
400+
.add(new Decimal(tokens.reasoning).mul(finite(costInfo?.output ?? 0)).div(1_000_000))
403401
.toNumber(),
404402
),
405403
tokens,

‎packages/opencode/test/session/compaction.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,22 @@ describe("SessionNs.getUsage", () => {
17821782
expect(Number.isNaN(result.cost)).toBe(false)
17831783
})
17841784

1785+
test("ignores malformed cost fields", () => {
1786+
const model = createModel({
1787+
context: 100_000,
1788+
output: 32_000,
1789+
cost: { input: 3, output: 15, cache: { read: 0.3, write: 3.75 } },
1790+
})
1791+
Object.assign(model.cost, { input: {} })
1792+
1793+
const result = SessionNs.getUsage({
1794+
model,
1795+
usage: usage({ inputTokens: 1_000_000, outputTokens: 100_000, totalTokens: 1_100_000 }),
1796+
})
1797+
1798+
expect(result.cost).toBe(1.5)
1799+
})
1800+
17851801
test("calculates cost correctly", () => {
17861802
const model = createModel({
17871803
context: 100_000,

0 commit comments

Comments
 (0)