@@ -12,27 +12,7 @@ import type { ChatStats, TokenConsumer } from "@/types/chatStats";
1212import type { LanguageModelV2Usage } from "@ai-sdk/provider" ;
1313import { getTokenizerForModel , countTokensForData , getToolDefinitionTokens } from "./tokenizer" ;
1414import { getModelStats } from "./modelStats" ;
15-
16- export interface ChatUsageComponent {
17- tokens : number ;
18- cost_usd ?: number ; // undefined if model pricing unknown
19- }
20-
21- /**
22- * Enhanced usage type for display that includes provider-specific cache stats
23- */
24- export interface ChatUsageDisplay {
25- // Input is the part of the input that was not cached. So,
26- // totalInput = input + cached (cacheCreate is separate for billing)
27- input : ChatUsageComponent ;
28- cached : ChatUsageComponent ;
29- cacheCreate : ChatUsageComponent ; // Cache creation tokens (separate billing concept)
30-
31- // Output is the part of the output excluding reasoning, so
32- // totalOutput = output + reasoning
33- output : ChatUsageComponent ;
34- reasoning : ChatUsageComponent ;
35- }
15+ import type { ChatUsageDisplay } from "./usageAggregator" ;
3616
3717/**
3818 * Create a display-friendly usage object from AI SDK usage
@@ -109,48 +89,6 @@ export function createDisplayUsage(
10989 } ;
11090}
11191
112- /**
113- * Sum multiple ChatUsageDisplay objects into a single cumulative display
114- * Used for showing total costs across multiple API responses
115- */
116- export function sumUsageHistory ( usageHistory : ChatUsageDisplay [ ] ) : ChatUsageDisplay | undefined {
117- if ( usageHistory . length === 0 ) return undefined ;
118-
119- // Track if any costs are undefined (model pricing unknown)
120- let hasUndefinedCosts = false ;
121-
122- const sum : ChatUsageDisplay = {
123- input : { tokens : 0 , cost_usd : 0 } ,
124- cached : { tokens : 0 , cost_usd : 0 } ,
125- cacheCreate : { tokens : 0 , cost_usd : 0 } ,
126- output : { tokens : 0 , cost_usd : 0 } ,
127- reasoning : { tokens : 0 , cost_usd : 0 } ,
128- } ;
129-
130- for ( const usage of usageHistory ) {
131- // Iterate over each component and sum tokens and costs
132- for ( const key of Object . keys ( sum ) as Array < keyof ChatUsageDisplay > ) {
133- sum [ key ] . tokens += usage [ key ] . tokens ;
134- if ( usage [ key ] . cost_usd === undefined ) {
135- hasUndefinedCosts = true ;
136- } else {
137- sum [ key ] . cost_usd = ( sum [ key ] . cost_usd ?? 0 ) + ( usage [ key ] . cost_usd ?? 0 ) ;
138- }
139- }
140- }
141-
142- // If any costs were undefined, set all to undefined
143- if ( hasUndefinedCosts ) {
144- sum . input . cost_usd = undefined ;
145- sum . cached . cost_usd = undefined ;
146- sum . cacheCreate . cost_usd = undefined ;
147- sum . output . cost_usd = undefined ;
148- sum . reasoning . cost_usd = undefined ;
149- }
150-
151- return sum ;
152- }
153-
15492/**
15593 * Calculate token statistics from raw CmuxMessages
15694 * This is the single source of truth for token counting
0 commit comments