@@ -95,6 +95,7 @@ export function buildOpenAIHandler(
9595 stream,
9696 inputToken : 0 ,
9797 outputToken : 0 ,
98+ cacheInputToken : 0 ,
9899 duration : 0 ,
99100 ttft : 0 ,
100101 requestPayload : payload ,
@@ -180,6 +181,9 @@ export function buildOpenAIHandler(
180181 calcOpenAIToken ( outputContent , modelName ) ,
181182 ] ) ;
182183
184+ const cacheInputToken =
185+ get ( usage , [ 'prompt_tokens_details' , 'cached_tokens' ] ) ?? 0 ;
186+
183187 const customInputPrice = gatewayInfo ?. customModelInputPrice ;
184188 const customOutputPrice = gatewayInfo ?. customModelOutputPrice ;
185189
@@ -219,6 +223,7 @@ export function buildOpenAIHandler(
219223 modelName : responseModelName ,
220224 inputToken,
221225 outputToken,
226+ cacheInputToken,
222227 duration,
223228 ttft,
224229 price,
@@ -262,6 +267,9 @@ export function buildOpenAIHandler(
262267 : Promise . resolve ( 0 ) ) ,
263268 ] ) ;
264269
270+ const cacheInputToken =
271+ response . usage ?. prompt_tokens_details ?. cached_tokens ?? 0 ;
272+
265273 const customInputPrice = gatewayInfo ?. customModelInputPrice ;
266274 const customOutputPrice = gatewayInfo ?. customModelOutputPrice ;
267275
@@ -300,6 +308,7 @@ export function buildOpenAIHandler(
300308 status : AIGatewayLogsStatus . Success ,
301309 inputToken,
302310 outputToken,
311+ cacheInputToken,
303312 duration,
304313 modelName : responseModelName ,
305314 price,
@@ -409,6 +418,7 @@ export function buildAnthropicHandler(
409418 stream,
410419 inputToken : 0 ,
411420 outputToken : 0 ,
421+ cacheInputToken : 0 ,
412422 duration : 0 ,
413423 ttft : 0 ,
414424 requestPayload : payload ,
@@ -482,6 +492,7 @@ export function buildAnthropicHandler(
482492
483493 let inputTokens = 0 ;
484494 let outputTokens = 0 ;
495+ let cacheInputTokens = 0 ;
485496 let outputContent = '' ;
486497 let ttft = - 1 ;
487498 let responseModelName = modelName ;
@@ -523,6 +534,9 @@ export function buildAnthropicHandler(
523534 const data = JSON . parse ( line . slice ( 6 ) ) ;
524535 if ( currentEventType === 'message_start' && data . message ) {
525536 responseModelName = data . message . model || responseModelName ;
537+ cacheInputTokens =
538+ data . message . usage ?. cache_read_input_tokens ||
539+ cacheInputTokens ;
526540 } else if ( currentEventType === 'content_block_delta' ) {
527541 if ( ttft === - 1 ) {
528542 ttft = Date . now ( ) - start ;
@@ -578,6 +592,7 @@ export function buildAnthropicHandler(
578592 modelName : responseModelName ,
579593 inputToken : inputTokens ,
580594 outputToken : outputTokens ,
595+ cacheInputToken : cacheInputTokens ,
581596 duration,
582597 ttft,
583598 price,
@@ -607,6 +622,7 @@ export function buildAnthropicHandler(
607622 const usage = responseBody . usage ;
608623 const inputTokens = usage ?. input_tokens || 0 ;
609624 const outputTokens = usage ?. output_tokens || 0 ;
625+ const cacheInputTokens = usage ?. cache_read_input_tokens || 0 ;
610626 const responseCost = usage ?. cost ;
611627
612628 const contentBlocks = responseBody . content || [ ] ;
@@ -642,6 +658,7 @@ export function buildAnthropicHandler(
642658 modelName : responseModelName ,
643659 inputToken : inputTokens ,
644660 outputToken : outputTokens ,
661+ cacheInputToken : cacheInputTokens ,
645662 duration,
646663 price,
647664 responsePayload : {
0 commit comments