From 4ef1787c79545fc7f115e2568d6de13027ba81df Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Tue, 6 Oct 2026 19:37:12 +0800 Subject: [PATCH 1/8] fix(anthropic): price the response-cost header from the pre-translation usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit translate_completion_output_params flattens the OpenAI-shaped usage into the Anthropic TypedDict, dropping completion_tokens_details.image_tokens. When the async success handler has not stored response_cost before the proxy builds x-litellm-response-cost, the recomputation from the translated response prices image output tokens at the text rate — a race that made the header disagree with the always-correct spend row on some calls. The Anthropic messages adapter now computes and stores response_cost from the untouched ModelResponse before translating, for both the async and sync handlers. The stored value feeds the header recompute path, so the header and the spend row always price the same usage. Fixes #44743 --- .../pass_through/adapters/handler.py | 27 +++++ .../test_response_cost_before_flattening.py | 103 ++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 tests/unit/llms/anthropic/pass_through/adapters/test_response_cost_before_flattening.py diff --git a/litellm/llms/anthropic/pass_through/adapters/handler.py b/litellm/llms/anthropic/pass_through/adapters/handler.py index 5bda3437a40..551ac36fe9e 100644 --- a/litellm/llms/anthropic/pass_through/adapters/handler.py +++ b/litellm/llms/anthropic/pass_through/adapters/handler.py @@ -60,6 +60,31 @@ def _messages_have_compaction_block(messages: _AnthropicMessages) -> bool: return False +def _store_response_cost_before_usage_flattening(completion_response: object, kwargs: Mapping[str, object]) -> None: + """Compute and store the response cost while ``completion_response`` still + carries its full usage breakdown. + + ``translate_completion_output_params`` flattens the OpenAI-shaped usage into + the Anthropic response (a TypedDict), dropping + ``completion_tokens_details.image_tokens``. The proxy builds + ``x-litellm-response-cost`` by recomputing from the translated response when + the async success handler has not stored the cost yet — a race that priced + image output tokens at the text rate on some calls. Storing the cost here, + computed from the untouched ModelResponse, makes the header and the spend + row price the same usage. + """ + litellm_logging_obj = litellm_logging_obj_from_kwargs(kwargs) + if litellm_logging_obj is None: + return + try: + cost = litellm_logging_obj._response_cost_calculator(result=cast(ModelResponse, completion_response)) + except Exception: + verbose_logger.exception("Anthropic Adapter - failed to pre-compute response cost") + return + if isinstance(cost, (int, float)): + litellm_logging_obj.model_call_details["response_cost"] = cost + + def _proxy_router_fallback() -> "Router | None": try: from litellm.proxy.proxy_server import llm_router as _proxy_router @@ -679,6 +704,7 @@ class LiteLLMMessagesToCompletionTransformationHandler: return transformed_stream raise ValueError("Failed to transform streaming response") else: + _store_response_cost_before_usage_flattening(completion_response, kwargs) anthropic_response: Final = ANTHROPIC_ADAPTER.translate_completion_output_params( cast(ModelResponse, completion_response), tool_name_mapping=tool_name_mapping, @@ -814,6 +840,7 @@ class LiteLLMMessagesToCompletionTransformationHandler: return transformed_stream raise ValueError("Failed to transform streaming response") else: + _store_response_cost_before_usage_flattening(completion_response, kwargs) anthropic_response: Final = ANTHROPIC_ADAPTER.translate_completion_output_params( cast(ModelResponse, completion_response), tool_name_mapping=tool_name_mapping, diff --git a/tests/unit/llms/anthropic/pass_through/adapters/test_response_cost_before_flattening.py b/tests/unit/llms/anthropic/pass_through/adapters/test_response_cost_before_flattening.py new file mode 100644 index 00000000000..8c213e7b17c --- /dev/null +++ b/tests/unit/llms/anthropic/pass_through/adapters/test_response_cost_before_flattening.py @@ -0,0 +1,103 @@ +"""Regression tests for the x-litellm-response-cost image-token race (#44743). + +The Anthropic adapter flattens the OpenAI-shaped usage into the Anthropic +response TypedDict, dropping ``completion_tokens_details.image_tokens``. The +proxy recomputes the response-cost header from that translated response when +the async success handler has not stored the cost yet, so on some calls image +output tokens were priced at the text rate. The adapter now computes and +stores the cost from the untouched ModelResponse before the flattening. +""" + +import time +from typing import cast + +import pytest + +from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLogging +from litellm.llms.anthropic.pass_through.adapters.handler import ( + ANTHROPIC_ADAPTER, + _store_response_cost_before_usage_flattening, +) +from litellm.types.utils import ( + CompletionTokensDetailsWrapper, + ModelResponse, + Usage, +) + + +def _image_model_response() -> ModelResponse: + return ModelResponse( + id="resp_1", + model="gemini-3.1-flash-image", + choices=[], + usage=Usage( + prompt_tokens=7, + completion_tokens=1120, + completion_tokens_details=CompletionTokensDetailsWrapper(image_tokens=1120), + ), + ) + + +@pytest.fixture +def logging_obj() -> LiteLLMLogging: + obj = LiteLLMLogging( + model="vertex_ai/gemini-3.1-flash-image", + messages=[{"role": "user", "content": "a red square"}], + stream=False, + call_type="anthropic_messages", + start_time=time.time(), + litellm_call_id="44743", + function_id="44743", + ) + return obj + + +def test_stores_cost_from_pre_flattening_response(logging_obj: LiteLLMLogging) -> None: + """The stored cost is computed from a response that still carries image_tokens.""" + seen_results = [] + + def fake_calculator(self, result, **kwargs): + seen_results.append(result) + return 0.0672035 + + original = LiteLLMLogging._response_cost_calculator + LiteLLMLogging._response_cost_calculator = fake_calculator + try: + _store_response_cost_before_usage_flattening(_image_model_response(), {"litellm_logging_obj": logging_obj}) + finally: + LiteLLMLogging._response_cost_calculator = original + + assert logging_obj.model_call_details["response_cost"] == 0.0672035 + assert len(seen_results) == 1 + usage = seen_results[0].usage + assert usage.completion_tokens_details.image_tokens == 1120 + + +def test_no_logging_obj_is_a_noop() -> None: + _store_response_cost_before_usage_flattening(_image_model_response(), {}) + + +def test_calculator_failure_is_swallowed(logging_obj: LiteLLMLogging) -> None: + def raising_calculator(self, result, **kwargs): + raise RuntimeError("no pricing for model") + + original = LiteLLMLogging._response_cost_calculator + LiteLLMLogging._response_cost_calculator = raising_calculator + try: + _store_response_cost_before_usage_flattening(_image_model_response(), {"litellm_logging_obj": logging_obj}) + finally: + LiteLLMLogging._response_cost_calculator = original + + assert "response_cost" not in logging_obj.model_call_details + + +def test_translated_response_drops_image_token_details(logging_obj: LiteLLMLogging) -> None: + """Pins the root cause: after the Anthropic translation the usage no longer + distinguishes image tokens, so a recompute from it prices them at the text + rate — which is why the cost must be captured before translation.""" + translated = ANTHROPIC_ADAPTER.translate_completion_output_params(cast(ModelResponse, _image_model_response())) + assert translated is not None + usage = translated["usage"] + assert usage["output_tokens"] == 1120 + assert "completion_tokens_details" not in usage + assert not hasattr(usage, "completion_tokens_details") From 624c63e81ae78a29a749430522476b8324fa6fd0 Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Tue, 6 Oct 2026 19:37:12 +0800 Subject: [PATCH 2/8] chore: ignore reportPrivateUsage on the logging object's cost calculator access --- litellm/llms/anthropic/pass_through/adapters/handler.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/litellm/llms/anthropic/pass_through/adapters/handler.py b/litellm/llms/anthropic/pass_through/adapters/handler.py index 551ac36fe9e..dc2a8ae7f7f 100644 --- a/litellm/llms/anthropic/pass_through/adapters/handler.py +++ b/litellm/llms/anthropic/pass_through/adapters/handler.py @@ -77,7 +77,9 @@ def _store_response_cost_before_usage_flattening(completion_response: object, kw if litellm_logging_obj is None: return try: - cost = litellm_logging_obj._response_cost_calculator(result=cast(ModelResponse, completion_response)) + cost = litellm_logging_obj._response_cost_calculator( # pyright: ignore[reportPrivateUsage] + result=cast(ModelResponse, completion_response) + ) except Exception: verbose_logger.exception("Anthropic Adapter - failed to pre-compute response cost") return From 1f2061beefbb2f46021f13f0afc2f1fa7c3c4838 Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Tue, 6 Oct 2026 19:37:12 +0800 Subject: [PATCH 3/8] chore: explain the cast on the pre-flattening cost calculation --- litellm/llms/anthropic/pass_through/adapters/handler.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/litellm/llms/anthropic/pass_through/adapters/handler.py b/litellm/llms/anthropic/pass_through/adapters/handler.py index dc2a8ae7f7f..261095aef35 100644 --- a/litellm/llms/anthropic/pass_through/adapters/handler.py +++ b/litellm/llms/anthropic/pass_through/adapters/handler.py @@ -78,6 +78,8 @@ def _store_response_cost_before_usage_flattening(completion_response: object, kw return try: cost = litellm_logging_obj._response_cost_calculator( # pyright: ignore[reportPrivateUsage] + # cast-ok: the adapter hands back an OpenAI-shaped ModelResponse; the calculator + # accepts it as-is and only the usage fields are read. result=cast(ModelResponse, completion_response) ) except Exception: From 0b3e5554e28ce8810b861461f74ac8cc75ac317b Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Tue, 6 Oct 2026 20:19:39 +0800 Subject: [PATCH 4/8] fix(anthropic): record usage through the public logging API --- .../pass_through/adapters/handler.py | 59 +++++++++---------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/litellm/llms/anthropic/pass_through/adapters/handler.py b/litellm/llms/anthropic/pass_through/adapters/handler.py index 261095aef35..d8975d85878 100644 --- a/litellm/llms/anthropic/pass_through/adapters/handler.py +++ b/litellm/llms/anthropic/pass_through/adapters/handler.py @@ -28,7 +28,7 @@ from litellm.types.llms.anthropic_messages.anthropic_response import ( AnthropicMessagesResponse, ) from litellm.types.llms.openai import OpenAIWebSearchOptions -from litellm.types.utils import ModelResponse +from litellm.types.utils import ModelResponse, Usage from litellm.utils import get_model_info if TYPE_CHECKING: @@ -60,35 +60,6 @@ def _messages_have_compaction_block(messages: _AnthropicMessages) -> bool: return False -def _store_response_cost_before_usage_flattening(completion_response: object, kwargs: Mapping[str, object]) -> None: - """Compute and store the response cost while ``completion_response`` still - carries its full usage breakdown. - - ``translate_completion_output_params`` flattens the OpenAI-shaped usage into - the Anthropic response (a TypedDict), dropping - ``completion_tokens_details.image_tokens``. The proxy builds - ``x-litellm-response-cost`` by recomputing from the translated response when - the async success handler has not stored the cost yet — a race that priced - image output tokens at the text rate on some calls. Storing the cost here, - computed from the untouched ModelResponse, makes the header and the spend - row price the same usage. - """ - litellm_logging_obj = litellm_logging_obj_from_kwargs(kwargs) - if litellm_logging_obj is None: - return - try: - cost = litellm_logging_obj._response_cost_calculator( # pyright: ignore[reportPrivateUsage] - # cast-ok: the adapter hands back an OpenAI-shaped ModelResponse; the calculator - # accepts it as-is and only the usage fields are read. - result=cast(ModelResponse, completion_response) - ) - except Exception: - verbose_logger.exception("Anthropic Adapter - failed to pre-compute response cost") - return - if isinstance(cost, (int, float)): - litellm_logging_obj.model_call_details["response_cost"] = cost - - def _proxy_router_fallback() -> "Router | None": try: from litellm.proxy.proxy_server import llm_router as _proxy_router @@ -97,6 +68,34 @@ def _proxy_router_fallback() -> "Router | None": return _proxy_router +def _store_response_cost_before_usage_flattening(completion_response: object, kwargs: Mapping[str, object]) -> None: + """Price the response while ``completion_response`` still carries its full usage breakdown. + + ``translate_completion_output_params`` flattens the OpenAI-shaped usage into the + Anthropic response TypedDict, dropping ``completion_tokens_details.image_tokens``. + The proxy recomputes the ``x-litellm-response-cost`` header from the translated + response whenever the async success handler has not stored a cost yet, and that + recompute prices image output tokens at the text rate. Recording the usage and its + cost here, from the untouched ModelResponse, makes the header and the spend row + price the same tokens. + """ + litellm_logging_obj = litellm_logging_obj_from_kwargs(kwargs) + if litellm_logging_obj is None: + return + # cast-ok: the adapter hands back an OpenAI-shaped ModelResponse; the calculator + # accepts it as-is and only the usage fields are read. + model_response: Final = cast(ModelResponse, completion_response) + usage: Final = getattr(model_response, "usage", None) + if not isinstance(usage, Usage): + return + try: + response_cost: Final = litellm_logging_obj._response_cost_calculator(result=model_response) # pyright: ignore[reportPrivateUsage] # as the HTTP streaming iterator does + except Exception: # noqa: BLE001 # pricing lookup failures vary by provider; never fail the response over them + verbose_logger.exception("Anthropic Adapter - failed to pre-compute response cost") + return + litellm_logging_obj.record_partial_usage_for_failure(usage, response_cost or 0.0) + + def _extract_proxy_litellm_metadata( kwargs: Mapping[str, object], ) -> "tuple[dict[str, object], UserAPIKeyAuth | None] | tuple[None, None]": From aafd674424e0cdf8270cf9d38ea1e53719d67788 Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Tue, 6 Oct 2026 20:19:48 +0800 Subject: [PATCH 5/8] fix(anthropic): record usage through the public logging API --- .../test_handler_output_config_passthrough.py | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/tests/unit/llms/anthropic/pass_through/adapters/test_handler_output_config_passthrough.py b/tests/unit/llms/anthropic/pass_through/adapters/test_handler_output_config_passthrough.py index 282373d4b6d..ce5989476ed 100644 --- a/tests/unit/llms/anthropic/pass_through/adapters/test_handler_output_config_passthrough.py +++ b/tests/unit/llms/anthropic/pass_through/adapters/test_handler_output_config_passthrough.py @@ -246,3 +246,163 @@ class TestPromptCacheOptionsForwarded: result = _call_prepare(extra_kwargs={"prompt_cache_options": {"mode": "explicit"}}, model="gpt-5.6") completion_kwargs = result[0] if isinstance(result, tuple) else result assert completion_kwargs["prompt_cache_options"] == {"mode": "explicit"} + + +# --- response cost captured before usage flattening (#44743) ------------------- +# +# ``translate_completion_output_params`` flattens the OpenAI-shaped usage into the +# Anthropic response TypedDict, dropping ``completion_tokens_details.image_tokens``. +# The proxy rebuilds the ``x-litellm-response-cost`` header from that translated +# response whenever the async success handler has not stored a cost yet, and the +# rebuild cannot tell image tokens from text tokens, so they get priced at the text +# rate. The adapter therefore records usage plus cost from the untouched +# ModelResponse first, which is what makes the header and the spend row agree. + + +class _StubCostCalculator: + """Stands in for the pricing lookup, handed to a real ``Logging`` instance. + + Binding it to the instance means no class attribute is replaced, so a + concurrent test using another ``Logging`` object is unaffected. + """ + + def __init__(self, cost=None, raises=False): + self.cost = cost + self.raises = raises + self.seen: list[object] = [] + + def __call__(self, result, **kwargs): + self.seen.append(result) + if self.raises: + raise RuntimeError("no pricing configured") + return self.cost + + +def _real_logging_obj(calculator): + """A real ``Logging`` object, which ``litellm_logging_obj_from_kwargs`` requires. + + The cost calculator is bound on the instance, so nothing at class level is + patched and the object still satisfies the ``isinstance`` guard. + """ + from litellm.litellm_core_utils.litellm_logging import Logging + + obj = Logging( + model="gemini-3.1-flash-image", + messages=[{"role": "user", "content": "a red square"}], + stream=False, + call_type="anthropic_messages", + start_time=None, + litellm_call_id="cost-probe", + function_id="cost-probe", + ) + obj._response_cost_calculator = calculator + return obj + + +def _image_usage_response(): + from litellm.types.utils import CompletionTokensDetailsWrapper, ModelResponse, Usage + + return ModelResponse( + id="resp_1", + model="gemini-3.1-flash-image", + choices=[], + usage=Usage( + prompt_tokens=7, + completion_tokens=1120, + completion_tokens_details=CompletionTokensDetailsWrapper(image_tokens=1120), + ), + ) + + +def test_response_cost_is_recorded_while_image_tokens_are_still_visible(): + from litellm.llms.anthropic.pass_through.adapters.handler import ( + _store_response_cost_before_usage_flattening, + ) + + calculator = _StubCostCalculator(cost=0.0672035) + logging_obj = _real_logging_obj(calculator) + + _store_response_cost_before_usage_flattening( + _image_usage_response(), {"litellm_logging_obj": logging_obj} + ) + + stored_usage = logging_obj.model_call_details.get("combined_usage_object") + assert stored_usage is not None, "adapter skipped response-cost bookkeeping on the non-streaming path" + # The whole point of the fix: the usage handed to the cost calculator, and the + # one recorded for the spend row, still carry image_tokens. + assert stored_usage.completion_tokens_details.image_tokens == 1120 + assert calculator.seen[0].usage.completion_tokens_details.image_tokens == 1120 + assert logging_obj.model_call_details["response_cost"] == 0.0672035 + + +def test_response_cost_bookkeeping_is_a_noop_without_logging_obj(): + from litellm.llms.anthropic.pass_through.adapters.handler import ( + _store_response_cost_before_usage_flattening, + ) + + _store_response_cost_before_usage_flattening(_image_usage_response(), {}) + + +def test_response_cost_bookkeeping_swallows_calculator_failure(): + from litellm.llms.anthropic.pass_through.adapters.handler import ( + _store_response_cost_before_usage_flattening, + ) + + logging_obj = _real_logging_obj(_StubCostCalculator(raises=True)) + + _store_response_cost_before_usage_flattening( + _image_usage_response(), {"litellm_logging_obj": logging_obj} + ) + + assert "combined_usage_object" not in logging_obj.model_call_details + + +def test_translated_response_cannot_be_repriced_for_image_tokens(): + """Pins why the cost has to be captured before translation: the Anthropic + response the proxy recomputes from has no image-token breakdown left, so any + recompute from it necessarily charges the text rate.""" + from litellm.llms.anthropic.pass_through.adapters.handler import ANTHROPIC_ADAPTER + from litellm.types.utils import ModelResponse + + translated = ANTHROPIC_ADAPTER.translate_completion_output_params(_image_usage_response()) + assert translated is not None + usage = translated["usage"] + assert usage["output_tokens"] == 1120 + assert "completion_tokens_details" not in usage + assert isinstance(_image_usage_response(), ModelResponse) + + +def test_anthropic_messages_handler_records_cost_before_flattening(): + """Drives the real non-streaming handler end to end. + + The helper-level tests above pass even when the handler stops calling the + helper, so this one asserts on what the handler leaves on the logging object. + That is the regression the fix is for: if the handler stops recording before + flattening, the proxy's recompute prices image tokens at the text rate. + """ + from litellm.llms.anthropic.pass_through.adapters.handler import ( + LiteLLMMessagesToCompletionTransformationHandler, + ) + + calculator = _StubCostCalculator(cost=0.0672035) + logging_obj = _real_logging_obj(calculator) + + with patch("litellm.completion", return_value=_image_usage_response()): + result = LiteLLMMessagesToCompletionTransformationHandler.anthropic_messages_handler( + max_tokens=1024, + messages=[{"role": "user", "content": "a red square"}], + model="gemini-3.1-flash-image", + litellm_logging_obj=logging_obj, + ) + + assert result is not None + stored_usage = logging_obj.model_call_details.get("combined_usage_object") + assert stored_usage is not None, ( + "the handler returned an Anthropic response without recording image-token usage first, " + "so the proxy's response-cost recompute cannot price image tokens correctly" + ) + assert stored_usage.completion_tokens_details.image_tokens == 1120 + assert logging_obj.model_call_details["response_cost"] == 0.0672035 + # The returned Anthropic response is what the proxy recomputes from, and it is + # exactly the shape that no longer carries the image-token breakdown. + assert "completion_tokens_details" not in result["usage"] From 65962a2f52edded5a1d90e42007b97578c5b4b63 Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Tue, 6 Oct 2026 20:21:23 +0800 Subject: [PATCH 6/8] test(anthropic): fold the regression cases into the mapped handler test file --- .../test_response_cost_before_flattening.py | 103 ------------------ 1 file changed, 103 deletions(-) delete mode 100644 tests/unit/llms/anthropic/pass_through/adapters/test_response_cost_before_flattening.py diff --git a/tests/unit/llms/anthropic/pass_through/adapters/test_response_cost_before_flattening.py b/tests/unit/llms/anthropic/pass_through/adapters/test_response_cost_before_flattening.py deleted file mode 100644 index 8c213e7b17c..00000000000 --- a/tests/unit/llms/anthropic/pass_through/adapters/test_response_cost_before_flattening.py +++ /dev/null @@ -1,103 +0,0 @@ -"""Regression tests for the x-litellm-response-cost image-token race (#44743). - -The Anthropic adapter flattens the OpenAI-shaped usage into the Anthropic -response TypedDict, dropping ``completion_tokens_details.image_tokens``. The -proxy recomputes the response-cost header from that translated response when -the async success handler has not stored the cost yet, so on some calls image -output tokens were priced at the text rate. The adapter now computes and -stores the cost from the untouched ModelResponse before the flattening. -""" - -import time -from typing import cast - -import pytest - -from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLogging -from litellm.llms.anthropic.pass_through.adapters.handler import ( - ANTHROPIC_ADAPTER, - _store_response_cost_before_usage_flattening, -) -from litellm.types.utils import ( - CompletionTokensDetailsWrapper, - ModelResponse, - Usage, -) - - -def _image_model_response() -> ModelResponse: - return ModelResponse( - id="resp_1", - model="gemini-3.1-flash-image", - choices=[], - usage=Usage( - prompt_tokens=7, - completion_tokens=1120, - completion_tokens_details=CompletionTokensDetailsWrapper(image_tokens=1120), - ), - ) - - -@pytest.fixture -def logging_obj() -> LiteLLMLogging: - obj = LiteLLMLogging( - model="vertex_ai/gemini-3.1-flash-image", - messages=[{"role": "user", "content": "a red square"}], - stream=False, - call_type="anthropic_messages", - start_time=time.time(), - litellm_call_id="44743", - function_id="44743", - ) - return obj - - -def test_stores_cost_from_pre_flattening_response(logging_obj: LiteLLMLogging) -> None: - """The stored cost is computed from a response that still carries image_tokens.""" - seen_results = [] - - def fake_calculator(self, result, **kwargs): - seen_results.append(result) - return 0.0672035 - - original = LiteLLMLogging._response_cost_calculator - LiteLLMLogging._response_cost_calculator = fake_calculator - try: - _store_response_cost_before_usage_flattening(_image_model_response(), {"litellm_logging_obj": logging_obj}) - finally: - LiteLLMLogging._response_cost_calculator = original - - assert logging_obj.model_call_details["response_cost"] == 0.0672035 - assert len(seen_results) == 1 - usage = seen_results[0].usage - assert usage.completion_tokens_details.image_tokens == 1120 - - -def test_no_logging_obj_is_a_noop() -> None: - _store_response_cost_before_usage_flattening(_image_model_response(), {}) - - -def test_calculator_failure_is_swallowed(logging_obj: LiteLLMLogging) -> None: - def raising_calculator(self, result, **kwargs): - raise RuntimeError("no pricing for model") - - original = LiteLLMLogging._response_cost_calculator - LiteLLMLogging._response_cost_calculator = raising_calculator - try: - _store_response_cost_before_usage_flattening(_image_model_response(), {"litellm_logging_obj": logging_obj}) - finally: - LiteLLMLogging._response_cost_calculator = original - - assert "response_cost" not in logging_obj.model_call_details - - -def test_translated_response_drops_image_token_details(logging_obj: LiteLLMLogging) -> None: - """Pins the root cause: after the Anthropic translation the usage no longer - distinguishes image tokens, so a recompute from it prices them at the text - rate — which is why the cost must be captured before translation.""" - translated = ANTHROPIC_ADAPTER.translate_completion_output_params(cast(ModelResponse, _image_model_response())) - assert translated is not None - usage = translated["usage"] - assert usage["output_tokens"] == 1120 - assert "completion_tokens_details" not in usage - assert not hasattr(usage, "completion_tokens_details") From 88284c5ffaf9f90fd451783cc4cb2f91b02304ec Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Thu, 15 Oct 2026 01:22:12 +0800 Subject: [PATCH 7/8] fix: address review and validation findings for PR 44819 --- litellm/llms/anthropic/pass_through/adapters/handler.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/litellm/llms/anthropic/pass_through/adapters/handler.py b/litellm/llms/anthropic/pass_through/adapters/handler.py index d8975d85878..7fc0addae4e 100644 --- a/litellm/llms/anthropic/pass_through/adapters/handler.py +++ b/litellm/llms/anthropic/pass_through/adapters/handler.py @@ -82,9 +82,7 @@ def _store_response_cost_before_usage_flattening(completion_response: object, kw litellm_logging_obj = litellm_logging_obj_from_kwargs(kwargs) if litellm_logging_obj is None: return - # cast-ok: the adapter hands back an OpenAI-shaped ModelResponse; the calculator - # accepts it as-is and only the usage fields are read. - model_response: Final = cast(ModelResponse, completion_response) + model_response: Final = cast(ModelResponse, completion_response) # cast-ok: adapter returns ModelResponse usage: Final = getattr(model_response, "usage", None) if not isinstance(usage, Usage): return From 7d022d62d680448ceba58fa9e8296d0762ee4b22 Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:50:22 +0800 Subject: [PATCH 8/8] fix: type Anthropic request metadata before cost capture --- .../llms/anthropic/pass_through/adapters/handler.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/litellm/llms/anthropic/pass_through/adapters/handler.py b/litellm/llms/anthropic/pass_through/adapters/handler.py index 8b74712d38c..a00b3d2c95a 100644 --- a/litellm/llms/anthropic/pass_through/adapters/handler.py +++ b/litellm/llms/anthropic/pass_through/adapters/handler.py @@ -839,7 +839,10 @@ class LiteLLMMessagesToCompletionTransformationHandler: return transformed_stream raise ValueError("Failed to transform streaming response") else: - _store_response_cost_before_usage_flattening(completion_response, kwargs) + _store_response_cost_before_usage_flattening( + completion_response, + cast(Mapping[str, object], kwargs), # cast-ok: framework kwargs are a string-keyed request mapping + ) _record_bridged_model_response(logging_obj, completion_response) anthropic_response: Final = ANTHROPIC_ADAPTER.translate_completion_output_params( cast(ModelResponse, completion_response), @@ -985,7 +988,10 @@ class LiteLLMMessagesToCompletionTransformationHandler: return transformed_stream raise ValueError("Failed to transform streaming response") else: - _store_response_cost_before_usage_flattening(completion_response, kwargs) + _store_response_cost_before_usage_flattening( + completion_response, + cast(Mapping[str, object], kwargs), # cast-ok: framework kwargs are a string-keyed request mapping + ) _record_bridged_model_response(logging_obj, completion_response) anthropic_response: Final = ANTHROPIC_ADAPTER.translate_completion_output_params( cast(ModelResponse, completion_response),