@@ -23,6 +23,7 @@ import {
2323import { createOpenAICompatAssistantHistory } from '@/providers/openai-compat/assistant-history'
2424import { getChatCompletionConversationUsage } from '@/providers/openai-compat/conversation-usage'
2525import { createOpenAICompatibleAgentEventStream } from '@/providers/openai-compat/stream-events'
26+ import { createChatCompletionUsageMeter } from '@/providers/openai-compat/usage-meter'
2627import { executeProviderTool } from '@/providers/runtime-context'
2728import { createSettledAgentEventStream } from '@/providers/stream-events'
2829import { createStreamingExecution } from '@/providers/streaming-execution'
@@ -42,7 +43,6 @@ import type {
4243} from '@/providers/types'
4344import { ProviderError } from '@/providers/types'
4445import {
45- calculateCost ,
4646 enforceStrictSchema ,
4747 isFunctionToolCall ,
4848 prepareToolExecution ,
@@ -253,22 +253,10 @@ export const kimiProvider: ProviderConfig = {
253253 request,
254254 onComplete : ( { content, usage } ) => {
255255 output . content = content
256- output . tokens = {
257- input : usage . prompt_tokens ,
258- output : usage . completion_tokens ,
259- total : usage . total_tokens ,
260- }
261-
262- const costResult = calculateCost (
263- request . model ,
264- usage . prompt_tokens ,
265- usage . completion_tokens
266- )
267- output . cost = {
268- input : costResult . input ,
269- output : costResult . output ,
270- total : costResult . total ,
271- }
256+ const streamUsage = createChatCompletionUsageMeter ( request . model )
257+ streamUsage . record ( usage )
258+ output . tokens = { ...streamUsage . tokens }
259+ output . cost = streamUsage . cost ( )
272260 } ,
273261 }
274262 ) ,
@@ -299,11 +287,8 @@ export const kimiProvider: ProviderConfig = {
299287
300288 let content = currentResponse . choices [ 0 ] ?. message ?. content || ''
301289
302- const tokens = {
303- input : currentResponse . usage ?. prompt_tokens || 0 ,
304- output : currentResponse . usage ?. completion_tokens || 0 ,
305- total : currentResponse . usage ?. total_tokens || 0 ,
306- }
290+ const usageMeter = createChatCompletionUsageMeter ( request . model )
291+ usageMeter . record ( currentResponse . usage )
307292 const toolCalls = [ ]
308293 const toolResults : Record < string , unknown > [ ] = [ ]
309294 const currentMessages = [ ...formattedMessages ]
@@ -599,11 +584,7 @@ export const kimiProvider: ProviderConfig = {
599584 content = currentResponse . choices [ 0 ] . message . content
600585 }
601586
602- if ( currentResponse . usage ) {
603- tokens . input += currentResponse . usage . prompt_tokens || 0
604- tokens . output += currentResponse . usage . completion_tokens || 0
605- tokens . total += currentResponse . usage . total_tokens || 0
606- }
587+ usageMeter . record ( currentResponse . usage )
607588
608589 iterationCount ++
609590 }
@@ -654,11 +635,7 @@ export const kimiProvider: ProviderConfig = {
654635 if ( currentResponse . choices [ 0 ] ?. message ?. content ) {
655636 content = currentResponse . choices [ 0 ] . message . content
656637 }
657- if ( currentResponse . usage ) {
658- tokens . input += currentResponse . usage . prompt_tokens || 0
659- tokens . output += currentResponse . usage . completion_tokens || 0
660- tokens . total += currentResponse . usage . total_tokens || 0
661- }
638+ usageMeter . record ( currentResponse . usage )
662639
663640 enrichLastModelSegmentFromChatCompletions (
664641 timeSegments ,
@@ -677,8 +654,7 @@ export const kimiProvider: ProviderConfig = {
677654 }
678655
679656 if ( request . stream ) {
680- const accumulatedCost = calculateCost ( request . model , tokens . input , tokens . output )
681- const toolCost = sumToolCosts ( toolResults )
657+ const accumulatedCost = usageMeter . cost ( sumToolCosts ( toolResults ) )
682658
683659 const streamingResult = createStreamingExecution ( {
684660 model : request . model ,
@@ -692,17 +668,8 @@ export const kimiProvider: ProviderConfig = {
692668 iterations : iterationCount + 1 ,
693669 timeSegments,
694670 } ,
695- initialTokens : {
696- input : tokens . input ,
697- output : tokens . output ,
698- total : tokens . total ,
699- } ,
700- initialCost : {
701- input : accumulatedCost . input ,
702- output : accumulatedCost . output ,
703- toolCost : toolCost || undefined ,
704- total : accumulatedCost . total + toolCost ,
705- } ,
671+ initialTokens : { ...usageMeter . tokens } ,
672+ initialCost : accumulatedCost ,
706673 toolCalls :
707674 toolCalls . length > 0
708675 ? {
@@ -729,7 +696,8 @@ export const kimiProvider: ProviderConfig = {
729696 return {
730697 content,
731698 model : request . model ,
732- tokens,
699+ tokens : { ...usageMeter . tokens } ,
700+ cost : usageMeter . cost ( ) ,
733701 toolCalls : toolCalls . length > 0 ? toolCalls : undefined ,
734702 toolResults : toolResults . length > 0 ? toolResults : undefined ,
735703 timing : {
0 commit comments