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 image tokens spend logging for /images/generations
Dima-Mediator7c61933bc52d
diff --git a/litellm/responses/utils.py b/litellm/responses/utils.pyindex 49b8e123fe2..469eeb3da06 100644--- a/litellm/responses/utils.py+++ b/litellm/responses/utils.py@@ -472,7 +472,9 @@ class ResponseAPILoggingUtils:output_tokens_details = getattr(response_api_usage, "output_tokens_details", None)if output_tokens_details:completion_tokens_details = CompletionTokensDetailsWrapper(- reasoning_tokens=getattr(output_tokens_details, "reasoning_tokens", None)+ reasoning_tokens=getattr(output_tokens_details, "reasoning_tokens", None),+ image_tokens=getattr(output_tokens_details, "image_tokens", None),+ text_tokens=getattr(output_tokens_details, "text_tokens", None),)chat_usage = Usage(diff --git a/tests/logging_callback_tests/test_standard_logging_payload.py b/tests/logging_callback_tests/test_standard_logging_payload.pyindex 1c01ab91938..4ead642c462 100644--- a/tests/logging_callback_tests/test_standard_logging_payload.py+++ b/tests/logging_callback_tests/test_standard_logging_payload.py@@ -80,6 +80,53 @@ def test_get_usage(response_obj, expected_values):assert usage.total_tokens == expected_values[2]+def test_get_usage_from_image_generation_response():+ """+ Test that image generation usage (with input_tokens/output_tokens format)+ is correctly transformed to standard usage format with image_tokens preserved.++ Note: get_usage_from_response_obj() is used by multiple endpoints including+ /images/generations and Response API (/responses), both of which use the+ input_tokens/output_tokens format instead of prompt_tokens/completion_tokens.++ This tests the fix for the bug where image_tokens were being lost during+ spend log creation for /images/generations endpoint.+ """+ # Simulating image generation response usage from OpenAI+ response_obj = {+ "usage": {+ "input_tokens": 13,+ "output_tokens": 372,+ "total_tokens": 385,+ "input_tokens_details": {+ "image_tokens": 0,+ "text_tokens": 13,+ },+ "output_tokens_details": {+ "image_tokens": 272,+ "text_tokens": 100,+ },+ }+ }++ usage = StandardLoggingPayloadSetup.get_usage_from_response_obj(response_obj)++ # Check basic token counts are mapped correctly+ assert usage.prompt_tokens == 13+ assert usage.completion_tokens == 372+ assert usage.total_tokens == 385++ # Check that prompt_tokens_details contains image_tokens and text_tokens+ assert usage.prompt_tokens_details is not None+ assert usage.prompt_tokens_details.image_tokens == 0+ assert usage.prompt_tokens_details.text_tokens == 13++ # Check that completion_tokens_details contains image_tokens and text_tokens+ assert usage.completion_tokens_details is not None+ assert usage.completion_tokens_details.image_tokens == 272+ assert usage.completion_tokens_details.text_tokens == 100++def test_get_additional_headers():additional_headers = {"x-ratelimit-limit-requests": "2000",diff --git a/tests/test_litellm/responses/test_responses_utils.py b/tests/test_litellm/responses/test_responses_utils.pyindex 09628cd4a76..3eb9c63e1be 100644--- a/tests/test_litellm/responses/test_responses_utils.py+++ b/tests/test_litellm/responses/test_responses_utils.py@@ -222,3 +222,90 @@ class TestResponseAPILoggingUtils:assert result.prompt_tokens == 15assert result.completion_tokens == 25assert result.total_tokens == 40 # 15 + 25++ def test_transform_response_api_usage_with_image_tokens(self):+ """Test transformation handles image_tokens from image generation responses.++ Note: _transform_response_api_usage_to_chat_usage() is used by multiple+ endpoints including /images/generations and Response API (/responses),+ both of which use the input_tokens/output_tokens format.++ This tests the fix for image generation responses that include image_tokens+ in both input_tokens_details and output_tokens_details.++ Example from gpt-image-1.5:+ - input: text prompt with 13 tokens+ - output: generated image with 272 image tokens + 100 text tokens+ """+ # Setup - simulating image generation usage from OpenAI+ usage = {+ "input_tokens": 13,+ "output_tokens": 372,+ "total_tokens": 385,+ "input_tokens_details": {+ "image_tokens": 0,+ "text_tokens": 13,+ },+ "output_tokens_details": {+ "image_tokens": 272,+ "text_tokens": 100,+ },+ }++ # Execute+ result = ResponseAPILoggingUtils._transform_response_api_usage_to_chat_usage(+ usage+ )++ # Assert - verify basic token counts+ assert isinstance(result, Usage)+ assert result.prompt_tokens == 13+ assert result.completion_tokens == 372+ assert result.total_tokens == 385++ # Assert - verify prompt_tokens_details includes image_tokens and text_tokens+ assert result.prompt_tokens_details is not None+ assert result.prompt_tokens_details.image_tokens == 0+ assert result.prompt_tokens_details.text_tokens == 13++ # Assert - verify completion_tokens_details includes image_tokens and text_tokens+ assert result.completion_tokens_details is not None+ assert result.completion_tokens_details.image_tokens == 272+ assert result.completion_tokens_details.text_tokens == 100++ def test_transform_response_api_usage_mixed_details(self):+ """Test transformation handles mixed token details (cached + image + audio)."""+ # Setup - hypothetical usage with mixed token types+ usage = {+ "input_tokens": 100,+ "output_tokens": 200,+ "total_tokens": 300,+ "input_tokens_details": {+ "cached_tokens": 50,+ "audio_tokens": 10,+ "image_tokens": 20,+ "text_tokens": 20,+ },+ "output_tokens_details": {+ "reasoning_tokens": 30,+ "image_tokens": 100,+ "text_tokens": 70,+ },+ }++ # Execute+ result = ResponseAPILoggingUtils._transform_response_api_usage_to_chat_usage(+ usage+ )++ # Assert - all token detail types should be preserved+ assert result.prompt_tokens_details is not None+ assert result.prompt_tokens_details.cached_tokens == 50+ assert result.prompt_tokens_details.audio_tokens == 10+ assert result.prompt_tokens_details.image_tokens == 20+ assert result.prompt_tokens_details.text_tokens == 20++ assert result.completion_tokens_details is not None+ assert result.completion_tokens_details.reasoning_tokens == 30+ assert result.completion_tokens_details.image_tokens == 100+ assert result.completion_tokens_details.text_tokens == 70