@@ -18,6 +18,7 @@ import { getProviderDefaultModel, getProviderModels } from '@/providers/models'
1818import { createOpenAICompatAssistantHistory } from '@/providers/openai-compat/assistant-history'
1919import { getChatCompletionConversationUsage } from '@/providers/openai-compat/conversation-usage'
2020import { createOpenAICompatibleAgentEventStream } from '@/providers/openai-compat/stream-events'
21+ import { createChatCompletionUsageMeter } from '@/providers/openai-compat/usage-meter'
2122import { buildJsonSchemaResponseFormat } from '@/providers/response-format'
2223import { executeProviderTool } from '@/providers/runtime-context'
2324import { createSettledAgentEventStream } from '@/providers/stream-events'
@@ -38,7 +39,6 @@ import type {
3839} from '@/providers/types'
3940import { ProviderError } from '@/providers/types'
4041import {
41- calculateCost ,
4242 isFunctionToolCall ,
4343 prepareToolExecution ,
4444 prepareToolsWithUsageControl ,
@@ -160,22 +160,10 @@ export const cerebrasProvider: ProviderConfig = {
160160 request,
161161 onComplete : ( { content, usage } ) => {
162162 output . content = content
163- output . tokens = {
164- input : usage . prompt_tokens ,
165- output : usage . completion_tokens ,
166- total : usage . total_tokens ,
167- }
168-
169- const costResult = calculateCost (
170- request . model ,
171- usage . prompt_tokens ,
172- usage . completion_tokens
173- )
174- output . cost = {
175- input : costResult . input ,
176- output : costResult . output ,
177- total : costResult . total ,
178- }
163+ const streamUsage = createChatCompletionUsageMeter ( request . model )
164+ streamUsage . record ( usage )
165+ output . tokens = { ...streamUsage . tokens }
166+ output . cost = streamUsage . cost ( )
179167 } ,
180168 } ) ,
181169 } )
@@ -199,11 +187,8 @@ export const cerebrasProvider: ProviderConfig = {
199187 const firstResponseTime = Date . now ( ) - initialCallTime
200188
201189 let content = currentResponse . choices [ 0 ] ?. message ?. content || ''
202- const tokens = {
203- input : currentResponse . usage ?. prompt_tokens || 0 ,
204- output : currentResponse . usage ?. completion_tokens || 0 ,
205- total : currentResponse . usage ?. total_tokens || 0 ,
206- }
190+ const usageMeter = createChatCompletionUsageMeter ( request . model )
191+ usageMeter . record ( currentResponse . usage )
207192 const toolCalls = [ ]
208193 const toolResults : Record < string , unknown > [ ] = [ ]
209194 const currentMessages = [ ...formattedMessages ]
@@ -476,11 +461,7 @@ export const cerebrasProvider: ProviderConfig = {
476461 if ( currentResponse . choices [ 0 ] ?. message ?. content ) {
477462 content = currentResponse . choices [ 0 ] . message . content
478463 }
479- if ( currentResponse . usage ) {
480- tokens . input += currentResponse . usage . prompt_tokens || 0
481- tokens . output += currentResponse . usage . completion_tokens || 0
482- tokens . total += currentResponse . usage . total_tokens || 0
483- }
464+ usageMeter . record ( currentResponse . usage )
484465
485466 enrichLastModelSegmentFromChatCompletions (
486467 timeSegments ,
@@ -525,11 +506,7 @@ export const cerebrasProvider: ProviderConfig = {
525506 } )
526507
527508 modelTime += thisModelTime
528- if ( currentResponse . usage ) {
529- tokens . input += currentResponse . usage . prompt_tokens || 0
530- tokens . output += currentResponse . usage . completion_tokens || 0
531- tokens . total += currentResponse . usage . total_tokens || 0
532- }
509+ usageMeter . record ( currentResponse . usage )
533510
534511 iterationCount ++
535512 }
@@ -577,11 +554,7 @@ export const cerebrasProvider: ProviderConfig = {
577554 if ( currentResponse . choices [ 0 ] ?. message ?. content ) {
578555 content = currentResponse . choices [ 0 ] . message . content
579556 }
580- if ( currentResponse . usage ) {
581- tokens . input += currentResponse . usage . prompt_tokens || 0
582- tokens . output += currentResponse . usage . completion_tokens || 0
583- tokens . total += currentResponse . usage . total_tokens || 0
584- }
557+ usageMeter . record ( currentResponse . usage )
585558
586559 enrichLastModelSegmentFromChatCompletions (
587560 timeSegments ,
@@ -604,8 +577,7 @@ export const cerebrasProvider: ProviderConfig = {
604577 const totalDuration = providerEndTime - providerStartTime
605578
606579 if ( request . stream ) {
607- const accumulatedCost = calculateCost ( request . model , tokens . input , tokens . output )
608- const toolCost = sumToolCosts ( toolResults )
580+ const accumulatedCost = usageMeter . cost ( sumToolCosts ( toolResults ) )
609581
610582 const streamingResult = createStreamingExecution ( {
611583 model : request . model ,
@@ -619,17 +591,8 @@ export const cerebrasProvider: ProviderConfig = {
619591 iterations : iterationCount + 1 ,
620592 timeSegments,
621593 } ,
622- initialTokens : {
623- input : tokens . input ,
624- output : tokens . output ,
625- total : tokens . total ,
626- } ,
627- initialCost : {
628- input : accumulatedCost . input ,
629- output : accumulatedCost . output ,
630- toolCost : toolCost || undefined ,
631- total : accumulatedCost . total + toolCost ,
632- } ,
594+ initialTokens : { ...usageMeter . tokens } ,
595+ initialCost : accumulatedCost ,
633596 toolCalls :
634597 toolCalls . length > 0
635598 ? {
@@ -652,7 +615,8 @@ export const cerebrasProvider: ProviderConfig = {
652615 return {
653616 content,
654617 model : request . model ,
655- tokens,
618+ tokens : { ...usageMeter . tokens } ,
619+ cost : usageMeter . cost ( ) ,
656620 toolCalls : toolCalls . length > 0 ? toolCalls : undefined ,
657621 toolResults : toolResults . length > 0 ? toolResults : undefined ,
658622 timing : {
0 commit comments