@@ -13,6 +13,7 @@ export interface ResponsesStreamState {
1313 currentResponseId ?: string
1414 currentModel ?: string
1515 initialInputTokens ?: number
16+ initialInputCachedTokens ?: number
1617 functionCallStateByOutputIndex : Map < number , FunctionCallStreamState >
1718 functionCallOutputIndexByItemId : Map < string , number >
1819}
@@ -476,11 +477,10 @@ const ensureMessageStart = (
476477 const id = response ?. id ?? state . currentResponseId ?? "response"
477478 const model = response ?. model ?? state . currentModel ?? ""
478479
479- const inputTokens =
480- response ?. usage ?. input_tokens ?? state . initialInputTokens ?? 0
481-
482480 state . messageStartSent = true
483481
482+ const inputTokens =
483+ ( state . initialInputTokens ?? 0 ) - ( state . initialInputCachedTokens ?? 0 )
484484 return [
485485 {
486486 type : "message_start" ,
@@ -495,6 +495,9 @@ const ensureMessageStart = (
495495 usage : {
496496 input_tokens : inputTokens ,
497497 output_tokens : 0 ,
498+ ...( state . initialInputCachedTokens !== undefined && {
499+ cache_creation_input_tokens : state . initialInputCachedTokens ,
500+ } ) ,
498501 } ,
499502 } ,
500503 } ,
@@ -597,6 +600,8 @@ const cacheResponseMetadata = (
597600 state . currentResponseId = response . id
598601 state . currentModel = response . model
599602 state . initialInputTokens = response . usage ?. input_tokens ?? 0
603+ state . initialInputCachedTokens =
604+ response . usage ?. input_tokens_details ?. cached_tokens
600605}
601606
602607const buildErrorEvent = ( message : string ) : AnthropicStreamEventData => ( {
0 commit comments