From 1ca7194c5dc3286f8d9141b9dc5d36c864fcc5af Mon Sep 17 00:00:00 2001 From: David Golightly Date: Tue, 23 Jun 2026 10:13:29 -0700 Subject: [PATCH 1/2] fix(ai-anthropic): populate cachedInputTokens in streaming responses The streaming response builder set inputTokens/outputTokens/totalTokens but never copied cache_read_input_tokens, so cachedInputTokens was always undefined for streamText/streamObject even when prompt caching was active. The non-streaming path already maps it; mirror that at message_start, where the Anthropic API reports the cache-read count. Co-Authored-By: Claude Opus 4.8 (1M context) --- .changeset/anthropic-streaming-cached-input-tokens.md | 7 +++++++ packages/ai/anthropic/src/AnthropicLanguageModel.ts | 1 + 2 files changed, 8 insertions(+) create mode 100644 .changeset/anthropic-streaming-cached-input-tokens.md diff --git a/.changeset/anthropic-streaming-cached-input-tokens.md b/.changeset/anthropic-streaming-cached-input-tokens.md new file mode 100644 index 00000000000..e3ae201ecc0 --- /dev/null +++ b/.changeset/anthropic-streaming-cached-input-tokens.md @@ -0,0 +1,7 @@ +--- +"@effect/ai-anthropic": patch +--- + +fix(ai-anthropic): populate `cachedInputTokens` in streaming responses from `cache_read_input_tokens` + +The streaming response builder never copied `cache_read_input_tokens` into the normalized `usage`, so `cachedInputTokens` was always `undefined`/`0` for `streamText`/`streamObject` even when prompt caching was active. The non-streaming path already maps it; this mirrors that mapping at `message_start`, where the Anthropic API reports the cache-read count. diff --git a/packages/ai/anthropic/src/AnthropicLanguageModel.ts b/packages/ai/anthropic/src/AnthropicLanguageModel.ts index c9ee13ba11d..db0025eb391 100644 --- a/packages/ai/anthropic/src/AnthropicLanguageModel.ts +++ b/packages/ai/anthropic/src/AnthropicLanguageModel.ts @@ -971,6 +971,7 @@ const makeStreamResponse: ( case "message_start": { // Track usage metadata usage.inputTokens = event.message.usage.input_tokens + usage.cachedInputTokens = event.message.usage.cache_read_input_tokens ?? undefined metaUsage = event.message.usage // Track response metadata From 512af66c8284b40ab3cb5e6b7779a9831a92db25 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 24 Jun 2026 13:30:54 +1200 Subject: [PATCH 2/2] Apply suggestion from @tim-smart --- .changeset/anthropic-streaming-cached-input-tokens.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.changeset/anthropic-streaming-cached-input-tokens.md b/.changeset/anthropic-streaming-cached-input-tokens.md index e3ae201ecc0..66387941d84 100644 --- a/.changeset/anthropic-streaming-cached-input-tokens.md +++ b/.changeset/anthropic-streaming-cached-input-tokens.md @@ -2,6 +2,4 @@ "@effect/ai-anthropic": patch --- -fix(ai-anthropic): populate `cachedInputTokens` in streaming responses from `cache_read_input_tokens` - -The streaming response builder never copied `cache_read_input_tokens` into the normalized `usage`, so `cachedInputTokens` was always `undefined`/`0` for `streamText`/`streamObject` even when prompt caching was active. The non-streaming path already maps it; this mirrors that mapping at `message_start`, where the Anthropic API reports the cache-read count. +populate `cachedInputTokens` in streaming responses from `cache_read_input_tokens`