Skip to content

Handle None input_tokens from Anthropic SDK #1674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pydantic_ai_slim/pydantic_ai/models/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down