Fix cached tokens (#7679)

Co-authored-by: OpenHands Bot <openhands@all-hands.dev>
This commit is contained in:
Engel Nyst 2025-04-03 03:11:18 +02:00 committed by GitHub
parent 5b3921586f
commit d7f651a06c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -551,14 +551,16 @@ class LLM(RetryMixin, DebugMixin):
'prompt_tokens_details'
)
cache_hit_tokens = (
prompt_tokens_details.cached_tokens if prompt_tokens_details else 0
prompt_tokens_details.cached_tokens
if prompt_tokens_details and prompt_tokens_details.cached_tokens
else 0
)
if cache_hit_tokens:
stats += 'Input tokens (cache hit): ' + str(cache_hit_tokens) + '\n'
# For Anthropic, the cache writes have a different cost than regular input tokens
# but litellm doesn't separate them in the usage stats
# so we can read it from the provider-specific extra field
# we can read it from the provider-specific extra field
model_extra = usage.get('model_extra', {})
cache_write_tokens = model_extra.get('cache_creation_input_tokens', 0)
if cache_write_tokens: