From 8a8f21dabbfad4bb934014ab86436dafb43902a4 Mon Sep 17 00:00:00 2001 From: Tim Esler Date: Thu, 8 May 2025 18:57:51 -0700 Subject: [PATCH] Handle None input_tokens from Anthropic SDK --- pydantic_ai_slim/pydantic_ai/models/anthropic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydantic_ai_slim/pydantic_ai/models/anthropic.py b/pydantic_ai_slim/pydantic_ai/models/anthropic.py index e176b01cd..7213b4376 100644 --- a/pydantic_ai_slim/pydantic_ai/models/anthropic.py +++ b/pydantic_ai_slim/pydantic_ai/models/anthropic.py @@ -418,7 +418,7 @@ def _map_usage(message: AnthropicMessage | RawMessageStreamEvent) -> usage.Usage # Tokens are only counted once between input_tokens, cache_creation_input_tokens, and cache_read_input_tokens # This approach maintains request_tokens as the count of all input tokens, with cached counts as details request_tokens = ( - getattr(response_usage, 'input_tokens', 0) + (getattr(response_usage, 'input_tokens', 0) or 0) + (getattr(response_usage, 'cache_creation_input_tokens', 0) or 0) # These can be missing, None, or int + (getattr(response_usage, 'cache_read_input_tokens', 0) or 0) )