Update utils.py

This commit is contained in:
Praveen11558 2026-03-05 12:16:24 +05:30 • committed by GitHub
parent 40998b3659
commit 86d9535ab8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -662,7 +662,7 @@ def generic_cost_per_token( # noqa: PLR0915
accounted_tokens = text_tokens + cache_hit + audio_tokens + cache_creation + image_tokens
has_double_counting = cache_hit > 0 and accounted_tokens > usage.prompt_tokens
# Double-counting fix (xAI etc.): recalculate text_tokens from scratch
if has_double_counting:
text_tokens = (
usage.prompt_tokens
@ -673,6 +673,9 @@ def generic_cost_per_token( # noqa: PLR0915
)
prompt_tokens_details["text_tokens"] = text_tokens
elif accounted_tokens < usage.prompt_tokens:
# unaccounted tokens fix: inline documents (PDF, DOCX, etc.) are counted
# in prompt_tokens by the provider but not broken out at input_cost_per_token.
# Add the gap to text_tokens so they are costed at input_cost_per_token.
unaccounted_tokens = usage.prompt_tokens - accounted_tokens
prompt_tokens_details["text_tokens"] += unaccounted_tokens