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
perf: skip throwaway Usage() construction in ModelResponse.__init__
Ryan Crabbe7414c09277b6
diff --git a/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen2_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen2_transformation.pyindex c532d8ea27c..2abcc679eef 100644--- a/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen2_transformation.py+++ b/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen2_transformation.py@@ -11,6 +11,7 @@ from typing import Any, List, Optionalimport httpx+from litellm.types.utils import Usagefrom litellm.llms.bedrock.chat.invoke_transformations.amazon_qwen3_transformation import (AmazonQwen3Config,)@@ -79,10 +80,11 @@ class AmazonQwen2Config(AmazonQwen3Config):# Set usage information if available in responseif "usage" in response_data:usage_data = response_data["usage"]- if hasattr(model_response, 'usage'):- model_response.usage.prompt_tokens = usage_data.get("prompt_tokens", 0)- model_response.usage.completion_tokens = usage_data.get("completion_tokens", 0)- model_response.usage.total_tokens = usage_data.get("total_tokens", 0)+ model_response.usage = Usage(+ prompt_tokens=usage_data.get("prompt_tokens", 0),+ completion_tokens=usage_data.get("completion_tokens", 0),+ total_tokens=usage_data.get("total_tokens", 0),+ )return model_responsediff --git a/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen3_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen3_transformation.pyindex b3a957ce0f8..12333623f51 100644--- a/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen3_transformation.py+++ b/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen3_transformation.py@@ -10,6 +10,7 @@ from typing import Any, List, Optionalimport httpx+from litellm.types.utils import Usagefrom litellm.llms.base_llm.chat.transformation import BaseConfigfrom litellm.llms.bedrock.chat.invoke_transformations.base_invoke_transformation import (AmazonInvokeConfig,@@ -201,10 +202,11 @@ class AmazonQwen3Config(AmazonInvokeConfig, BaseConfig):# Set usage information if available in responseif "usage" in response_data:usage_data = response_data["usage"]- if hasattr(model_response, 'usage'):- model_response.usage.prompt_tokens = usage_data.get("prompt_tokens", 0)- model_response.usage.completion_tokens = usage_data.get("completion_tokens", 0)- model_response.usage.total_tokens = usage_data.get("total_tokens", 0)+ model_response.usage = Usage(+ prompt_tokens=usage_data.get("prompt_tokens", 0),+ completion_tokens=usage_data.get("completion_tokens", 0),+ total_tokens=usage_data.get("total_tokens", 0),+ )return model_responsediff --git a/litellm/types/utils.py b/litellm/types/utils.pyindex 9228b25b03e..9d8d421a91a 100644--- a/litellm/types/utils.py+++ b/litellm/types/utils.py@@ -1825,7 +1825,7 @@ class ModelResponse(ModelResponseBase):else:usage = usageelif stream is None or stream is False:- usage = Usage()+ usage = None # avoid constructing throwaway Usage; set by convert_to_model_response_objectif hidden_params:self._hidden_params = hidden_paramsdiff --git a/tests/llm_translation/test_llm_response_utils/test_convert_dict_to_chat_completion.py b/tests/llm_translation/test_llm_response_utils/test_convert_dict_to_chat_completion.pyindex 3b2087d25e9..cb580c63e0d 100644--- a/tests/llm_translation/test_llm_response_utils/test_convert_dict_to_chat_completion.py+++ b/tests/llm_translation/test_llm_response_utils/test_convert_dict_to_chat_completion.py@@ -1246,3 +1246,47 @@ def test_convert_to_model_response_object_with_error_code_only():_response_headers=None,convert_tool_call_to_json_mode=False,)+++def test_convert_to_model_response_object_default_usage_overwritten():+ """+ Regression test: convert_to_model_response_object must properly set Usage+ on a ModelResponse that only has the default Usage from ModelResponse.__init__()+ (i.e. no extra litellm.Usage() set via setattr beforehand).++ This validates the optimization of removing the redundant+ `setattr(model_response, "usage", litellm.Usage())` in completion().+ """+ mr = ModelResponse()+ # usage is not set by default (optimization: avoid constructing throwaway Usage)+ assert not hasattr(mr, "usage")++ response_object = {+ "id": "chatcmpl-usage-test",+ "choices": [+ {+ "index": 0,+ "message": {"role": "assistant", "content": "Hello"},+ "finish_reason": "stop",+ }+ ],+ "usage": {+ "prompt_tokens": 15,+ "completion_tokens": 7,+ "total_tokens": 22,+ },+ "model": "gpt-4o",+ }++ result = convert_to_model_response_object(+ model_response_object=mr,+ response_object=response_object,+ stream=False,+ start_time=datetime.now(),+ end_time=datetime.now(),+ )++ assert isinstance(result, ModelResponse)+ assert result.usage.prompt_tokens == 15+ assert result.usage.completion_tokens == 7+ assert result.usage.total_tokens == 22