From 212139ff95bb725d698bfe657d6fda37cbbfa01b Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Thu, 20 Aug 2026 22:27:16 -0500 Subject: [PATCH] fix(ai): accept nullable Anthropic input usage (#43761) --- packages/ai/src/protocols/anthropic-messages.ts | 4 ++-- packages/ai/test/provider/anthropic-messages.test.ts | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/ai/src/protocols/anthropic-messages.ts b/packages/ai/src/protocols/anthropic-messages.ts index a0a41b16a9b..2196e8033a7 100644 --- a/packages/ai/src/protocols/anthropic-messages.ts +++ b/packages/ai/src/protocols/anthropic-messages.ts @@ -234,7 +234,7 @@ export type AnthropicMessagesBody = Schema.Schema.Type { // expose that subset through `output_tokens_details.thinking_tokens`. const mapUsage = (usage: AnthropicUsage | undefined): Usage | undefined => { if (!usage) return undefined - const nonCached = usage.input_tokens + const nonCached = usage.input_tokens ?? undefined const cacheRead = usage.cache_read_input_tokens ?? undefined const cacheWrite = usage.cache_creation_input_tokens ?? undefined const inputTokens = ProviderShared.sumTokens(nonCached, cacheRead, cacheWrite) diff --git a/packages/ai/test/provider/anthropic-messages.test.ts b/packages/ai/test/provider/anthropic-messages.test.ts index cd7dee52ec0..ae7f80e4bab 100644 --- a/packages/ai/test/provider/anthropic-messages.test.ts +++ b/packages/ai/test/provider/anthropic-messages.test.ts @@ -640,7 +640,7 @@ describe("Anthropic Messages route", () => { }), ) - it.effect("maps thinking tokens and preserves unknown Anthropic usage fields", () => + it.effect("maps nullable input tokens and preserves unknown Anthropic usage fields", () => Effect.gen(function* () { const response = yield* LLMClient.generate(request).pipe( Effect.provide( @@ -663,6 +663,7 @@ describe("Anthropic Messages route", () => { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { + input_tokens: null, output_tokens: 8, server_tool_use: { web_search_requests: 2, terminal_counter: 3 }, output_tokens_details: { terminal_detail: "preserved" }, @@ -682,7 +683,7 @@ describe("Anthropic Messages route", () => { totalTokens: 15, providerMetadata: { anthropic: { - input_tokens: 5, + input_tokens: null, cache_read_input_tokens: 2, service_tier: "standard", cache_creation: { ephemeral_5m_input_tokens: 1 },