33import { AnthropicHandler } from "../anthropic"
44import { ApiHandlerOptions } from "../../../shared/api"
55import { asyncStreamFrom , collectStream } from "../../../test-utils/stream"
6+ import type { ApiStreamChunk } from "../../../api/transform/stream"
67import { clearAllMocks } from "../../../test-utils/reset"
78
89// Mock TelemetryService
@@ -247,7 +248,7 @@ describe("AnthropicHandler", () => {
247248 await collectStream ( stream )
248249
249250 const requestBody = mockCreate . mock . calls [ mockCreate . mock . calls . length - 1 ] ?. [ 0 ]
250- expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" } )
251+ expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" , display : "summarized" } )
251252 expect ( requestBody ?. max_tokens ) . toBe ( 16384 )
252253 } )
253254
@@ -290,7 +291,7 @@ describe("AnthropicHandler", () => {
290291 await collectStream ( stream )
291292
292293 const requestBody = mockCreate . mock . calls [ mockCreate . mock . calls . length - 1 ] ?. [ 0 ]
293- expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" } )
294+ expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" , display : "summarized" } )
294295 expect ( requestBody ?. max_tokens ) . toBe ( 32768 )
295296 } )
296297
@@ -334,7 +335,7 @@ describe("AnthropicHandler", () => {
334335 await collectStream ( stream )
335336
336337 const requestBody = mockCreate . mock . calls [ mockCreate . mock . calls . length - 1 ] ?. [ 0 ]
337- expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" } )
338+ expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" , display : "summarized" } )
338339 expect ( requestBody ?. max_tokens ) . toBe ( 16384 )
339340 } )
340341
@@ -377,7 +378,7 @@ describe("AnthropicHandler", () => {
377378 await collectStream ( stream )
378379
379380 const requestBody = mockCreate . mock . calls [ mockCreate . mock . calls . length - 1 ] ?. [ 0 ]
380- expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" } )
381+ expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" , display : "summarized" } )
381382 expect ( requestBody ?. max_tokens ) . toBe ( 32768 )
382383 } )
383384
@@ -400,7 +401,7 @@ describe("AnthropicHandler", () => {
400401
401402 const requestBody = mockCreate . mock . calls [ mockCreate . mock . calls . length - 1 ] ?. [ 0 ]
402403 const requestOptions = mockCreate . mock . calls [ mockCreate . mock . calls . length - 1 ] ?. [ 1 ]
403- expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" } )
404+ expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" , display : "summarized" } )
404405 expect ( requestBody ?. temperature ) . toBeUndefined ( )
405406 expect ( requestBody ?. max_tokens ) . toBe ( 32768 )
406407 expect ( requestOptions ?. headers ?. [ "anthropic-beta" ] ) . toContain ( "prompt-caching-2024-07-31" )
@@ -425,7 +426,7 @@ describe("AnthropicHandler", () => {
425426
426427 const requestBody = mockCreate . mock . calls [ mockCreate . mock . calls . length - 1 ] ?. [ 0 ]
427428 const requestOptions = mockCreate . mock . calls [ mockCreate . mock . calls . length - 1 ] ?. [ 1 ]
428- expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" } )
429+ expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" , display : "summarized" } )
429430 expect ( requestBody ?. temperature ) . toBeUndefined ( )
430431 expect ( requestBody ?. max_tokens ) . toBe ( 32768 )
431432 expect ( requestOptions ?. headers ?. [ "anthropic-beta" ] ) . toContain ( "prompt-caching-2024-07-31" )
@@ -450,7 +451,7 @@ describe("AnthropicHandler", () => {
450451
451452 const requestBody = mockCreate . mock . calls [ mockCreate . mock . calls . length - 1 ] ?. [ 0 ]
452453 const requestOptions = mockCreate . mock . calls [ mockCreate . mock . calls . length - 1 ] ?. [ 1 ]
453- expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" } )
454+ expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" , display : "summarized" } )
454455 expect ( requestBody ?. temperature ) . toBeUndefined ( )
455456 expect ( requestBody ?. max_tokens ) . toBe ( 32768 )
456457 expect ( requestOptions ?. headers ?. [ "anthropic-beta" ] ) . toContain ( "prompt-caching-2024-07-31" )
@@ -474,7 +475,84 @@ describe("AnthropicHandler", () => {
474475
475476 const requestBody = mockCreate . mock . calls [ mockCreate . mock . calls . length - 1 ] ?. [ 0 ]
476477 expect ( requestBody ?. model ) . toBe ( "claude-sonnet-5-bf" )
477- expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" } )
478+ expect ( requestBody ?. thinking ) . toEqual ( { type : "adaptive" , display : "summarized" } )
479+ } )
480+
481+ it ( "should surface thinking_tokens from output_tokens_details in usage chunks" , async ( ) => {
482+ // Adaptive models report reasoning tokens inside
483+ // `output_tokens_details.thinking_tokens` (message_start snapshot is
484+ // typically absent; message_delta carries the final decomposition).
485+ mockCreate . mockImplementationOnce ( async ( ) =>
486+ asyncStreamFrom ( [
487+ {
488+ type : "message_start" ,
489+ message : {
490+ usage : {
491+ input_tokens : 100 ,
492+ output_tokens : 50 ,
493+ } ,
494+ } ,
495+ } ,
496+ {
497+ type : "content_block_start" ,
498+ index : 0 ,
499+ content_block : {
500+ type : "text" ,
501+ text : "Hello" ,
502+ } ,
503+ } ,
504+ {
505+ type : "content_block_delta" ,
506+ index : 0 ,
507+ delta : {
508+ type : "text_delta" ,
509+ text : " world" ,
510+ } ,
511+ } ,
512+ {
513+ type : "message_delta" ,
514+ usage : {
515+ output_tokens : 200 ,
516+ output_tokens_details : {
517+ thinking_tokens : 150 ,
518+ } ,
519+ } ,
520+ delta : {
521+ stop_reason : "end_turn" ,
522+ stop_sequence : null ,
523+ } ,
524+ } ,
525+ ] ) ,
526+ )
527+
528+ const adaptiveHandler = new AnthropicHandler ( {
529+ apiKey : "test-api-key" ,
530+ apiModelId : "claude-opus-4-7" ,
531+ enableReasoningEffort : true ,
532+ } )
533+
534+ const stream = adaptiveHandler . createMessage ( "prompt" , [
535+ {
536+ role : "user" ,
537+ content : [ { type : "text" , text : "Hi" } ] ,
538+ } ,
539+ ] )
540+
541+ const chunks : ApiStreamChunk [ ] = await collectStream ( stream )
542+
543+ // message_start snapshot carries no thinking decomposition
544+ const usageChunks = chunks . filter (
545+ ( chunk ) : chunk is Extract < ApiStreamChunk , { type : "usage" } > => chunk . type === "usage" ,
546+ )
547+ const startUsage = usageChunks . find ( ( chunk ) => chunk . inputTokens > 0 )
548+ expect ( startUsage ) . toBeDefined ( )
549+ expect ( startUsage ?. reasoningTokens ) . toBeUndefined ( )
550+
551+ // message_delta surfaces the final reasoning token count
552+ const deltaUsage = usageChunks . find ( ( chunk ) => chunk . inputTokens === 0 )
553+ expect ( deltaUsage ) . toBeDefined ( )
554+ expect ( deltaUsage ?. outputTokens ) . toBe ( 200 )
555+ expect ( deltaUsage ?. reasoningTokens ) . toBe ( 150 )
478556 } )
479557 } )
480558
0 commit comments