The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(anthropic): bill an uncostable partial pass-through stream at zero cost instead of dropping its usage
mateo-berri6c27754455b3
diff --git a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.pyindex dae52bab956..30b75a7b482 100644--- a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py+++ b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py@@ -240,18 +240,28 @@ class AnthropicPassthroughLoggingHandler:usage: Final = cast(Usage | None, getattr(partial_response, "usage", None))if partial_response is None or usage is None:return+ litellm_logging_obj.record_partial_usage_for_failure(+ usage=usage,+ response_cost=AnthropicPassthroughLoggingHandler._cost_partial_stream_or_zero(+ partial_response=partial_response, model=model, logging_obj=litellm_logging_obj+ ),+ )++ @staticmethod+ def _cost_partial_stream_or_zero(+ partial_response: ModelResponse | TextCompletionResponse, model: str, logging_obj: LiteLLMLoggingObj+ ) -> float:try:- response_cost: Final = AnthropicPassthroughLoggingHandler._compute_response_cost(+ return AnthropicPassthroughLoggingHandler._compute_response_cost(litellm_model_response=partial_response,- model=AnthropicPassthroughLoggingHandler._resolve_costing_model(model, litellm_logging_obj),- logging_obj=litellm_logging_obj,+ model=AnthropicPassthroughLoggingHandler._resolve_costing_model(model, logging_obj),+ logging_obj=logging_obj,)- except Exception as e: # noqa: BLE001 # an uncostable partial stream must still log as a failure+ except Exception as e: # noqa: BLE001 # an uncostable partial stream still bills its tokens, at zero costverbose_proxy_logger.warning("Anthropic passthrough: could not cost the partial usage of a failed stream (model=%s): %s", model, e)- return- litellm_logging_obj.record_partial_usage_for_failure(usage=usage, response_cost=response_cost)+ return 0.0@staticmethoddef _compute_response_cost(diff --git a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.pyindex 480da1d040e..d721be62efe 100644--- a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py+++ b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py@@ -2505,6 +2505,19 @@ class TestRecordPartialUsageForFailure:assert usage.prompt_tokens == 52assert logging_obj.model_call_details["response_cost"] > 0+ def test_stashes_partial_usage_at_zero_cost_when_model_is_unpriced(self):+ logging_obj = self._make_logging_obj()++ AnthropicPassthroughLoggingHandler.record_partial_usage_for_failure(+ litellm_logging_obj=logging_obj,+ request_body={"model": "claude-unpriced-test-model", "stream": True},+ all_chunks=self._interrupted_chunks(),+ )++ usage = logging_obj.model_call_details["combined_usage_object"]+ assert usage.prompt_tokens == 52+ assert logging_obj.model_call_details["response_cost"] == 0.0+def test_leaves_logging_obj_untouched_when_nothing_streamed(self):logging_obj = self._make_logging_obj()