@@ -485,6 +485,8 @@ export function buildAnthropicHandler(
485485 let outputContent = '' ;
486486 let ttft = - 1 ;
487487 let responseModelName = modelName ;
488+ let responseCost : number | undefined ;
489+ let usage : Record < string , any > | undefined ;
488490
489491 const body = upstreamResponse . body ;
490492 if ( ! body ) {
@@ -519,13 +521,8 @@ export function buildAnthropicHandler(
519521 } else if ( line . startsWith ( 'data: ' ) && currentEventType ) {
520522 try {
521523 const data = JSON . parse ( line . slice ( 6 ) ) ;
522- if (
523- currentEventType === 'message_start' &&
524- data . message
525- ) {
524+ if ( currentEventType === 'message_start' && data . message ) {
526525 responseModelName = data . message . model || responseModelName ;
527- inputTokens =
528- data . message . usage ?. input_tokens || inputTokens ;
529526 } else if ( currentEventType === 'content_block_delta' ) {
530527 if ( ttft === - 1 ) {
531528 ttft = Date . now ( ) - start ;
@@ -534,8 +531,10 @@ export function buildAnthropicHandler(
534531 outputContent += data . delta . text || '' ;
535532 }
536533 } else if ( currentEventType === 'message_delta' ) {
537- outputTokens =
538- data . usage ?. output_tokens || outputTokens ;
534+ usage = data . usage ;
535+ inputTokens = usage ?. input_tokens || inputTokens ;
536+ outputTokens = usage ?. output_tokens || outputTokens ;
537+ responseCost = usage ?. cost ;
539538 }
540539 } catch {
541540 // Skip non-JSON data lines
@@ -556,19 +555,21 @@ export function buildAnthropicHandler(
556555 const customOutputPrice = gatewayInfo ?. customModelOutputPrice ;
557556
558557 const price =
559- options . isCustomRoute && ( customInputPrice || customOutputPrice )
560- ? getLLMCostDecimalWithCustomPrice (
561- inputTokens ,
562- outputTokens ,
563- customInputPrice ,
564- customOutputPrice
565- )
566- : getLLMCostDecimalV2 (
567- modelProvider ,
568- modelName ,
569- inputTokens ,
570- outputTokens
571- ) ;
558+ responseCost !== undefined
559+ ? new Prisma . Decimal ( responseCost )
560+ : options . isCustomRoute && ( customInputPrice || customOutputPrice )
561+ ? getLLMCostDecimalWithCustomPrice (
562+ inputTokens ,
563+ outputTokens ,
564+ customInputPrice ,
565+ customOutputPrice
566+ )
567+ : getLLMCostDecimalV2 (
568+ modelProvider ,
569+ modelName ,
570+ inputTokens ,
571+ outputTokens
572+ ) ;
572573
573574 await prisma . aIGatewayLogs . update ( {
574575 where : { id : logId } ,
@@ -583,8 +584,7 @@ export function buildAnthropicHandler(
583584 responsePayload : {
584585 content : outputContent ,
585586 usage : {
586- input_tokens : inputTokens ,
587- output_tokens : outputTokens ,
587+ ...usage ,
588588 } ,
589589 } ,
590590 } ,
@@ -604,8 +604,10 @@ export function buildAnthropicHandler(
604604
605605 logP . then ( async ( { id : logId } ) => {
606606 const responseModelName = responseBody . model || modelName ;
607- const inputTokens = responseBody . usage ?. input_tokens || 0 ;
608- const outputTokens = responseBody . usage ?. output_tokens || 0 ;
607+ const usage = responseBody . usage ;
608+ const inputTokens = usage ?. input_tokens || 0 ;
609+ const outputTokens = usage ?. output_tokens || 0 ;
610+ const responseCost = usage ?. cost ;
609611
610612 const contentBlocks = responseBody . content || [ ] ;
611613 const outputContent = contentBlocks
@@ -617,19 +619,21 @@ export function buildAnthropicHandler(
617619 const customOutputPrice = gatewayInfo ?. customModelOutputPrice ;
618620
619621 const price =
620- options . isCustomRoute && ( customInputPrice || customOutputPrice )
621- ? getLLMCostDecimalWithCustomPrice (
622- inputTokens ,
623- outputTokens ,
624- customInputPrice ,
625- customOutputPrice
626- )
627- : getLLMCostDecimalV2 (
628- modelProvider ,
629- modelName ,
630- inputTokens ,
631- outputTokens
632- ) ;
622+ responseCost !== undefined
623+ ? new Prisma . Decimal ( responseCost )
624+ : options . isCustomRoute && ( customInputPrice || customOutputPrice )
625+ ? getLLMCostDecimalWithCustomPrice (
626+ inputTokens ,
627+ outputTokens ,
628+ customInputPrice ,
629+ customOutputPrice
630+ )
631+ : getLLMCostDecimalV2 (
632+ modelProvider ,
633+ modelName ,
634+ inputTokens ,
635+ outputTokens
636+ ) ;
633637
634638 await prisma . aIGatewayLogs . update ( {
635639 where : { id : logId } ,
@@ -642,7 +646,7 @@ export function buildAnthropicHandler(
642646 price,
643647 responsePayload : {
644648 content : outputContent ,
645- usage : responseBody . usage ,
649+ usage,
646650 } ,
647651 } ,
648652 } ) ;
@@ -662,8 +666,7 @@ export function buildAnthropicHandler(
662666 type : 'error' ,
663667 error : {
664668 type : 'server_error' ,
665- message :
666- error instanceof Error ? error . message : 'Unknown error' ,
669+ message : error instanceof Error ? error . message : 'Unknown error' ,
667670 } ,
668671 } ) ;
669672 }
0 commit comments