From 4cf628c937db7f88a61f063ff1ed90a009e42ed4 Mon Sep 17 00:00:00 2001 From: Narendranath Gogineni Date: Tue, 28 Oct 2025 16:05:50 +0530 Subject: [PATCH 1/2] fix anthropic cache creation token mapping in vertex ai --- .../google-vertex-ai/chatComplete.ts | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/providers/google-vertex-ai/chatComplete.ts b/src/providers/google-vertex-ai/chatComplete.ts index 55b9f48c4..eaf68e67c 100644 --- a/src/providers/google-vertex-ai/chatComplete.ts +++ b/src/providers/google-vertex-ai/chatComplete.ts @@ -772,7 +772,15 @@ export const VertexAnthropicChatCompleteResponseTransform: ( } if ('content' in response) { - const { input_tokens = 0, output_tokens = 0 } = response?.usage ?? {}; + const { + input_tokens = 0, + output_tokens = 0, + cache_creation_input_tokens, + cache_read_input_tokens, + } = response?.usage; + + const shouldSendCacheUsage = + cache_creation_input_tokens || cache_read_input_tokens; let content: AnthropicContentItem[] | string = strictOpenAiCompliance ? '' @@ -827,7 +835,15 @@ export const VertexAnthropicChatCompleteResponseTransform: ( usage: { prompt_tokens: input_tokens, completion_tokens: output_tokens, - total_tokens: input_tokens + output_tokens, + total_tokens: + input_tokens + + output_tokens + + (cache_creation_input_tokens ?? 0) + + (cache_read_input_tokens ?? 0), + ...(shouldSendCacheUsage && { + cache_read_input_tokens: cache_read_input_tokens, + cache_creation_input_tokens: cache_creation_input_tokens, + }), }, }; } @@ -895,11 +911,20 @@ export const VertexAnthropicChatCompleteStreamChunkTransform: ( ); } + const shouldSendCacheUsage = + parsedChunk.message?.usage?.cache_read_input_tokens || + parsedChunk.message?.usage?.cache_creation_input_tokens; + if (parsedChunk.type === 'message_start' && parsedChunk.message?.usage) { streamState.model = parsedChunk?.message?.model ?? ''; - streamState.usage = { - prompt_tokens: parsedChunk.message.usage?.input_tokens, + prompt_tokens: parsedChunk.message?.usage?.input_tokens, + ...(shouldSendCacheUsage && { + cache_read_input_tokens: + parsedChunk.message?.usage?.cache_read_input_tokens, + cache_creation_input_tokens: + parsedChunk.message?.usage?.cache_creation_input_tokens, + }), }; return ( `data: ${JSON.stringify({ @@ -926,6 +951,11 @@ export const VertexAnthropicChatCompleteStreamChunkTransform: ( } if (parsedChunk.type === 'message_delta' && parsedChunk.usage) { + const totalTokens = + (streamState?.usage?.prompt_tokens ?? 0) + + (streamState?.usage?.cache_creation_input_tokens ?? 0) + + (streamState?.usage?.cache_read_input_tokens ?? 0) + + (parsedChunk.usage.output_tokens ?? 0); return ( `data: ${JSON.stringify({ id: fallbackId, @@ -945,10 +975,8 @@ export const VertexAnthropicChatCompleteStreamChunkTransform: ( ], usage: { completion_tokens: parsedChunk.usage?.output_tokens, - prompt_tokens: streamState.usage?.prompt_tokens, - total_tokens: - (streamState.usage?.prompt_tokens || 0) + - (parsedChunk.usage?.output_tokens || 0), + ...streamState.usage, + total_tokens: totalTokens, }, })}` + '\n\n' ); From 973ec2cd7e8f1994d335539e93f2e13f1d84ea9c Mon Sep 17 00:00:00 2001 From: Narendranath Gogineni Date: Tue, 28 Oct 2025 16:25:21 +0530 Subject: [PATCH 2/2] add null check --- src/providers/google-vertex-ai/chatComplete.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/providers/google-vertex-ai/chatComplete.ts b/src/providers/google-vertex-ai/chatComplete.ts index eaf68e67c..a66a2c793 100644 --- a/src/providers/google-vertex-ai/chatComplete.ts +++ b/src/providers/google-vertex-ai/chatComplete.ts @@ -777,7 +777,7 @@ export const VertexAnthropicChatCompleteResponseTransform: ( output_tokens = 0, cache_creation_input_tokens, cache_read_input_tokens, - } = response?.usage; + } = response?.usage ?? {}; const shouldSendCacheUsage = cache_creation_input_tokens || cache_read_input_tokens;