File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -153,6 +153,25 @@ describe("normalizeUsage", () => {
153153 } ) ;
154154 } ) ;
155155
156+ it . each ( [
157+ { provider : "Anthropic" , details : { thinking_tokens : 17 } , expected : 17 } ,
158+ { provider : "Anthropic zero" , details : { thinking_tokens : 0 } , expected : 0 } ,
159+ { provider : "OpenAI" , details : { reasoning_tokens : 17 } , expected : 17 } ,
160+ {
161+ provider : "OpenAI precedence" ,
162+ details : { reasoning_tokens : 17 , thinking_tokens : 22 } ,
163+ expected : 17 ,
164+ } ,
165+ ] ) ( "normalizes $provider output reasoning token details" , ( { details, expected } ) => {
166+ expect (
167+ normalizeUsage ( {
168+ input_tokens : 30 ,
169+ output_tokens : 40 ,
170+ output_tokens_details : details ,
171+ } ) ,
172+ ) . toMatchObject ( { input : 30 , output : 40 , reasoningTokens : expected } ) ;
173+ } ) ;
174+
156175 it ( "clamps negative input to zero (pre-subtracted cached_tokens > prompt_tokens)" , ( ) => {
157176 // shared model runtime OpenAI-format providers subtract cached_tokens from prompt_tokens
158177 // upstream. When cached_tokens exceeds prompt_tokens the result is negative.
Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ export type UsageLike = {
3030 reasoningTokens ?: number ;
3131 reasoning_tokens ?: number ;
3232 completion_tokens_details ?: { reasoning_tokens ?: number } ;
33- output_tokens_details ?: { reasoning_tokens ?: number } ;
33+ output_tokens_details ?: { reasoning_tokens ?: number ; thinking_tokens ?: number } ;
3434 // Moonshot/Kimi uses cached_tokens for cache read count (explicit caching API).
3535 cached_tokens ?: number ;
3636 // OpenAI Responses reports cached prompt reuse here.
@@ -201,7 +201,8 @@ export function normalizeUsage(raw?: UsageLike | null): NormalizedUsage | undefi
201201 raw . reasoningTokens ??
202202 raw . reasoning_tokens ??
203203 raw . completion_tokens_details ?. reasoning_tokens ??
204- raw . output_tokens_details ?. reasoning_tokens ,
204+ raw . output_tokens_details ?. reasoning_tokens ??
205+ raw . output_tokens_details ?. thinking_tokens ,
205206 ) ;
206207 const total = normalizeTokenCount ( raw . total ?? raw . totalTokens ?? raw . total_tokens ) ;
207208
You can’t perform that action at this time.
0 commit comments