@@ -336,10 +336,8 @@ export function plan(input: { slug: string; time: { created: number } }, instanc
336336}
337337
338338export 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,
0 commit comments