@@ -561,37 +561,93 @@ export const anthropicProvider: ProviderConfig = {
561561 throw error
562562 }
563563
564- const providerEndTime = Date . now ( )
565- const providerEndTimeISO = new Date ( providerEndTime ) . toISOString ( )
566- const totalDuration = providerEndTime - providerStartTime
564+ const accumulatedCost = calculateCost ( request . model , tokens . prompt , tokens . completion )
567565
568- return {
569- content,
570- model : request . model || 'claude-3-7-sonnet-20250219' ,
571- tokens,
572- toolCalls :
573- toolCalls . length > 0
574- ? toolCalls . map ( ( tc ) => ( {
575- name : tc . name ,
576- arguments : tc . arguments as Record < string , any > ,
577- startTime : tc . startTime ,
578- endTime : tc . endTime ,
579- duration : tc . duration ,
580- result : tc . result ,
581- } ) )
582- : undefined ,
583- toolResults : toolResults . length > 0 ? toolResults : undefined ,
584- timing : {
585- startTime : providerStartTimeISO ,
586- endTime : providerEndTimeISO ,
587- duration : totalDuration ,
588- modelTime : modelTime ,
589- toolsTime : toolsTime ,
590- firstResponseTime : firstResponseTime ,
591- iterations : iterationCount + 1 ,
592- timeSegments : timeSegments ,
566+ const streamingPayload = {
567+ ...payload ,
568+ messages : currentMessages ,
569+ stream : true ,
570+ tool_choice : undefined ,
571+ }
572+
573+ const streamResponse : any = await anthropic . messages . create ( streamingPayload )
574+
575+ const streamingResult = {
576+ stream : createReadableStreamFromAnthropicStream (
577+ streamResponse ,
578+ ( streamContent , usage ) => {
579+ streamingResult . execution . output . content = streamContent
580+ streamingResult . execution . output . tokens = {
581+ prompt : tokens . prompt + usage . input_tokens ,
582+ completion : tokens . completion + usage . output_tokens ,
583+ total : tokens . total + usage . input_tokens + usage . output_tokens ,
584+ }
585+
586+ const streamCost = calculateCost (
587+ request . model ,
588+ usage . input_tokens ,
589+ usage . output_tokens
590+ )
591+ streamingResult . execution . output . cost = {
592+ input : accumulatedCost . input + streamCost . input ,
593+ output : accumulatedCost . output + streamCost . output ,
594+ total : accumulatedCost . total + streamCost . total ,
595+ }
596+
597+ const streamEndTime = Date . now ( )
598+ const streamEndTimeISO = new Date ( streamEndTime ) . toISOString ( )
599+
600+ if ( streamingResult . execution . output . providerTiming ) {
601+ streamingResult . execution . output . providerTiming . endTime = streamEndTimeISO
602+ streamingResult . execution . output . providerTiming . duration =
603+ streamEndTime - providerStartTime
604+ }
605+ }
606+ ) ,
607+ execution : {
608+ success : true ,
609+ output : {
610+ content : '' ,
611+ model : request . model || 'claude-3-7-sonnet-20250219' ,
612+ tokens : {
613+ prompt : tokens . prompt ,
614+ completion : tokens . completion ,
615+ total : tokens . total ,
616+ } ,
617+ toolCalls :
618+ toolCalls . length > 0
619+ ? {
620+ list : toolCalls ,
621+ count : toolCalls . length ,
622+ }
623+ : undefined ,
624+ providerTiming : {
625+ startTime : providerStartTimeISO ,
626+ endTime : new Date ( ) . toISOString ( ) ,
627+ duration : Date . now ( ) - providerStartTime ,
628+ modelTime : modelTime ,
629+ toolsTime : toolsTime ,
630+ firstResponseTime : firstResponseTime ,
631+ iterations : iterationCount + 1 ,
632+ timeSegments : timeSegments ,
633+ } ,
634+ cost : {
635+ input : accumulatedCost . input ,
636+ output : accumulatedCost . output ,
637+ total : accumulatedCost . total ,
638+ } ,
639+ } ,
640+ logs : [ ] ,
641+ metadata : {
642+ startTime : providerStartTimeISO ,
643+ endTime : new Date ( ) . toISOString ( ) ,
644+ duration : Date . now ( ) - providerStartTime ,
645+ } ,
646+ isStreaming : true ,
593647 } ,
594648 }
649+
650+ return streamingResult as StreamingExecution
595651 } catch ( error ) {
596652 const providerEndTime = Date . now ( )
597653 const providerEndTimeISO = new Date ( providerEndTime ) . toISOString ( )
0 commit comments