From 86d9535ab8f296772c9245208da7adba82634f5b Mon Sep 17 00:00:00 2001 From: Praveen11558 <44603409+Praveen11558@users.noreply.github.com> Date: Thu, 5 Mar 2026 12:16:24 +0530 Subject: [PATCH] Update utils.py --- litellm/litellm_core_utils/llm_cost_calc/utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/litellm/litellm_core_utils/llm_cost_calc/utils.py b/litellm/litellm_core_utils/llm_cost_calc/utils.py index ccbebbbed5e..e7c09f6ec0c 100644 --- a/litellm/litellm_core_utils/llm_cost_calc/utils.py +++ b/litellm/litellm_core_utils/llm_cost_calc/utils.py @@ -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