From 84042736c3e59321c8bfb849e6742c0040aee1fe Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 30 Oct 2026 00:24:36 +0000 Subject: [PATCH] refactor(cost): name the providers billed from their own usage fields for what they are --- litellm/litellm_core_utils/core_helpers.py | 9 +++++---- .../litellm_core_utils/streaming_chunk_builder_utils.py | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index 31230d4e8d1..5280d2cd145 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -1017,7 +1017,7 @@ def chunk_usage(chunk: object) -> object: return getattr(chunk, "usage", None) -OWN_REPORTED_COST_READER_PROVIDERS: Final = frozenset({"xai", "perplexity", "opencode_go"}) +PROVIDERS_BILLED_FROM_THEIR_OWN_USAGE_FIELDS: Final = frozenset({"xai", "perplexity", "opencode_go"}) LITELLM_BILLED_USAGE_COST: Final = "litellm_billed_usage_cost" @@ -1040,9 +1040,10 @@ def _usage_cost_is_litellm_bill(response: object) -> bool: def billed_usage_cost(response: object, custom_llm_provider: str | None) -> float | None: - """The charge an OpenAI-compatible upstream reports in ``usage.cost``, skipped for providers with their own reader - and once litellm rewrote ``usage.cost`` to its own bill (the stream builder and the Responses terminal event do).""" - if custom_llm_provider in OWN_REPORTED_COST_READER_PROVIDERS or _usage_cost_is_litellm_bill(response): + """The charge an OpenAI-compatible upstream reports in ``usage.cost``, skipped for providers billed from their own + usage fields and once litellm rewrote ``usage.cost`` to its own bill (the stream builder and the Responses terminal + event do).""" + if custom_llm_provider in PROVIDERS_BILLED_FROM_THEIR_OWN_USAGE_FIELDS or _usage_cost_is_litellm_bill(response): return None cost: Final = provider_reported_usage_cost(_usage_field(response, "usage"), custom_llm_provider) return cost if cost is not None and math.isfinite(cost) and cost >= 0 else None diff --git a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py index 9aa7302b165..02dcf09ba95 100644 --- a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py +++ b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py @@ -13,7 +13,7 @@ from typing_extensions import ReadOnly, Required import litellm from litellm._logging import verbose_logger from litellm.litellm_core_utils.core_helpers import ( - OWN_REPORTED_COST_READER_PROVIDERS, + PROVIDERS_BILLED_FROM_THEIR_OWN_USAGE_FIELDS, chunk_usage, mark_usage_cost_as_litellm_bill, provider_reported_usage_cost, @@ -1420,7 +1420,7 @@ def _set_stream_builder_response_cost( def _stamp_streaming_usage_cost(usage: Usage, response: ModelResponse, logging_obj: Optional["Logging"]) -> None: if logging_obj is None: return - if _logged_provider(logging_obj) in OWN_REPORTED_COST_READER_PROVIDERS and isinstance( + if _logged_provider(logging_obj) in PROVIDERS_BILLED_FROM_THEIR_OWN_USAGE_FIELDS and isinstance( getattr(usage, "cost", None), (int, float) ): return