File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -111,22 +111,36 @@ export const parseUsageDetails = (
111111 prompt_tokens_details,
112112 } = completionUsage ;
113113
114+ const flatPromptTokensDetails = Object . fromEntries (
115+ Object . entries ( prompt_tokens_details ?? { } ) . map ( ( [ key , value ] ) => [
116+ `input_${ key } ` ,
117+ value as number ,
118+ ] ) ,
119+ ) ;
120+
121+ const flatCompletionTokensDetails = Object . fromEntries (
122+ Object . entries ( completion_tokens_details ?? { } ) . map ( ( [ key , value ] ) => [
123+ `output_${ key } ` ,
124+ value as number ,
125+ ] ) ,
126+ ) ;
127+
128+ let finalInputTokens = prompt_tokens as number ;
129+ Object . values ( flatPromptTokensDetails ) . forEach ( ( value ) => {
130+ finalInputTokens = Math . max ( finalInputTokens - value , 0 ) ;
131+ } ) ;
132+
133+ let finalOutputTokens = completion_tokens as number ;
134+ Object . values ( flatCompletionTokensDetails ) . forEach ( ( value ) => {
135+ finalOutputTokens = Math . max ( finalOutputTokens - value , 0 ) ;
136+ } ) ;
137+
114138 return {
115- input : prompt_tokens ,
116- output : completion_tokens ,
139+ input : finalInputTokens ,
140+ output : finalOutputTokens ,
117141 total : total_tokens ,
118- ...Object . fromEntries (
119- Object . entries ( prompt_tokens_details ?? { } ) . map ( ( [ key , value ] ) => [
120- `input_${ key } ` ,
121- value as number ,
122- ] ) ,
123- ) ,
124- ...Object . fromEntries (
125- Object . entries ( completion_tokens_details ?? { } ) . map ( ( [ key , value ] ) => [
126- `output_${ key } ` ,
127- value as number ,
128- ] ) ,
129- ) ,
142+ ...flatPromptTokensDetails ,
143+ ...flatCompletionTokensDetails ,
130144 } ;
131145 } else if ( "input_tokens" in completionUsage ) {
132146 const {
You can’t perform that action at this time.
0 commit comments