Merge pull request #1443 from adithyaxx/anthropic-client-bug-fixes

Handle missing token counts in AsyncAnthropicBedrock client properly
This commit is contained in:
Mark Backman
2025-05-05 21:13:11 -04:00
committed by GitHub

View File

@@ -250,13 +250,23 @@ class AnthropicLLMService(LLMService):
if hasattr(event.message.usage, "output_tokens") if hasattr(event.message.usage, "output_tokens")
else 0 else 0
) )
if hasattr(event.message.usage, "cache_creation_input_tokens"):
cache_creation_input_tokens += ( cache_creation_input_tokens += (
event.message.usage.cache_creation_input_tokens event.message.usage.cache_creation_input_tokens
if (
hasattr(event.message.usage, "cache_creation_input_tokens")
and event.message.usage.cache_creation_input_tokens is not None
)
else 0
) )
logger.debug(f"Cache creation input tokens: {cache_creation_input_tokens}") logger.debug(f"Cache creation input tokens: {cache_creation_input_tokens}")
if hasattr(event.message.usage, "cache_read_input_tokens"): cache_read_input_tokens += (
cache_read_input_tokens += event.message.usage.cache_read_input_tokens event.message.usage.cache_read_input_tokens
if (
hasattr(event.message.usage, "cache_read_input_tokens")
and event.message.usage.cache_read_input_tokens is not None
)
else 0
)
logger.debug(f"Cache read input tokens: {cache_read_input_tokens}") logger.debug(f"Cache read input tokens: {cache_read_input_tokens}")
total_input_tokens = ( total_input_tokens = (
prompt_tokens + cache_creation_input_tokens + cache_read_input_tokens prompt_tokens + cache_creation_input_tokens + cache_read_input_tokens