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(passthrough): keep prompt/completion token split for streamed OpenAI rows
mateo-berri08a73740ec3d
diff --git a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/openai_passthrough_logging_handler.py b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/openai_passthrough_logging_handler.pyindex 1614784a0dc..b40de351847 100644--- a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/openai_passthrough_logging_handler.py+++ b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/openai_passthrough_logging_handler.py@@ -583,6 +583,8 @@ class OpenAIPassthroughLoggingHandler(BasePassthroughLoggingHandler):"response_cost": response_cost,"model": model,"custom_llm_provider": custom_llm_provider,+ "call_type": litellm_logging_obj.call_type,+ "messages": litellm_logging_obj.model_call_details.get("messages"),"litellm_params": existing_litellm_params.copy(),}@@ -599,8 +601,11 @@ class OpenAIPassthroughLoggingHandler(BasePassthroughLoggingHandler):user)- # Create standard logging object- get_standard_logging_object_payload(+ # Attach the payload to kwargs so the success handler adopts it;+ # its later rebuild runs on a copy whose Responses usage was+ # coerced to chat shape and serializes as total_tokens only,+ # zeroing the prompt/completion split in spend logs.+ standard_logging_object: Final = get_standard_logging_object_payload(kwargs=kwargs,init_response_obj=complete_response,start_time=start_time,@@ -608,6 +613,8 @@ class OpenAIPassthroughLoggingHandler(BasePassthroughLoggingHandler):logging_obj=litellm_logging_obj,status="success",)+ if standard_logging_object is not None:+ kwargs["standard_logging_object"] = standard_logging_object# Update logging object with cost informationlitellm_logging_obj.model_call_details["model"] = modeldiff --git a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.pyindex 1735308bda0..05051ab3745 100644--- a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py+++ b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py@@ -586,6 +586,7 @@ class TestOpenAIPassthroughLoggingHandler:assert response.usage.input_tokens == 14assert response.usage.output_tokens == 2assert result["kwargs"]["response_cost"] == 3.3e-06+ assert result["kwargs"]["standard_logging_object"] is mock_get_standard_logging.return_valuemock_completion_cost.assert_called_once_with(completion_response=response,model="gpt-4o-mini",@@ -657,6 +658,7 @@ class TestOpenAIPassthroughLoggingHandler:assert response.status == "incomplete"assert response.usage.output_tokens == 32assert result["kwargs"]["response_cost"] == 2.1e-06+ assert result["kwargs"]["standard_logging_object"] is mock_get_standard_logging.return_valuemock_completion_cost.assert_called_once_with(completion_response=response,model="gpt-4o-mini",@@ -714,6 +716,7 @@ class TestOpenAIPassthroughLoggingHandler:assert response.status == "failed"assert response.usage.total_tokens == 21assert result["kwargs"]["response_cost"] == 1.4e-06+ assert result["kwargs"]["standard_logging_object"] is mock_get_standard_logging.return_valuemock_completion_cost.assert_called_once_with(completion_response=response,model="gpt-4o-mini",@@ -721,6 +724,53 @@ class TestOpenAIPassthroughLoggingHandler:call_type="responses",)+ @patch(f"{OpenAIPassthroughLoggingHandler.__module__}.get_standard_logging_object_payload", return_value=None)+ @patch("litellm.completion_cost", return_value=3.3e-06)+ def test_streaming_responses_none_payload_is_not_attached(self, mock_completion_cost, mock_get_standard_logging):+ completed_event = {+ "type": "response.completed",+ "sequence_number": 8,+ "response": {+ "id": "resp_NONEPAYLOADSENTINEL0123456789",+ "object": "response",+ "created_at": 1786374786,+ "status": "completed",+ "model": "gpt-4o-mini-2024-07-18",+ "output": [],+ "usage": {+ "input_tokens": 14,+ "input_tokens_details": {"cached_tokens": 0},+ "output_tokens": 2,+ "output_tokens_details": {"reasoning_tokens": 0},+ "total_tokens": 16,+ },+ "error": None,+ "incomplete_details": None,+ "instructions": None,+ "metadata": {},+ "parallel_tool_calls": True,+ "temperature": 1.0,+ "tool_choice": "auto",+ "tools": [],+ "top_p": 1.0,+ },+ }+ logging_obj = self._create_mock_logging_obj()++ result = OpenAIPassthroughLoggingHandler._handle_logging_openai_collected_chunks(+ litellm_logging_obj=logging_obj,+ passthrough_success_handler_obj=MagicMock(),+ url_route="https://api.openai.com/v1/responses",+ request_body={"model": "gpt-4o-mini", "stream": True},+ endpoint_type=MagicMock(),+ start_time=self.start_time,+ all_chunks=[f"data: {json.dumps(completed_event)}", "data: [DONE]"],+ end_time=self.end_time,+ )++ assert "standard_logging_object" not in result["kwargs"]+ assert result["kwargs"]["response_cost"] == 3.3e-06+@patch(f"{OpenAIPassthroughLoggingHandler.__module__}.get_standard_logging_object_payload")@patch("litellm.completion_cost")def test_streaming_responses_without_completed_event_returns_none(