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(cost-tracking): support base_model lookup in litellm_metadata for Responses API (#16778)
Cesar Garcia5e70c78b9411
diff --git a/litellm/utils.py b/litellm/utils.pyindex 2249599dbc5..47fd06a9f27 100644--- a/litellm/utils.py+++ b/litellm/utils.py@@ -6680,7 +6680,13 @@ def _get_base_model_from_metadata(model_call_details=None):return _base_modelmetadata = litellm_params.get("metadata", {})- return _get_base_model_from_litellm_call_metadata(metadata=metadata)+ base_model_from_metadata = _get_base_model_from_litellm_call_metadata(metadata=metadata)+ if base_model_from_metadata is not None:+ return base_model_from_metadata++ # Also check litellm_metadata (used by Responses API and other generic API calls)+ litellm_metadata = litellm_params.get("litellm_metadata", {})+ return _get_base_model_from_litellm_call_metadata(metadata=litellm_metadata)return Nonediff --git a/tests/litellm_utils_tests/test_utils.py b/tests/litellm_utils_tests/test_utils.pyindex 0a650843824..bffcc91a7a1 100644--- a/tests/litellm_utils_tests/test_utils.py+++ b/tests/litellm_utils_tests/test_utils.py@@ -2421,4 +2421,80 @@ def test_completion_with_no_model():# test on emptywith pytest.raises(TypeError):response = litellm.completion(messages=[{"role": "user", "content": "Hello, how are you?"}])-+++def test_get_base_model_from_metadata():+ """+ Test _get_base_model_from_metadata function with both metadata and litellm_metadata.+ This ensures cost tracking works for both Chat Completions API and Responses API.++ Related issue: https://github.com/BerriAI/litellm/issues/16772+ """+ from litellm.utils import _get_base_model_from_metadata++ # Test 1: base_model in metadata (Chat Completions API pattern)+ model_call_details_with_metadata = {+ "litellm_params": {+ "metadata": {+ "model_info": {+ "base_model": "azure/gpt-4"+ }+ }+ }+ }+ result = _get_base_model_from_metadata(model_call_details_with_metadata)+ assert result == "azure/gpt-4", f"Expected 'azure/gpt-4', got {result}"++ # Test 2: base_model in litellm_metadata (Responses API and generic API calls pattern)+ model_call_details_with_litellm_metadata = {+ "litellm_params": {+ "litellm_metadata": {+ "model_info": {+ "base_model": "azure/gpt-5-mini"+ }+ }+ }+ }+ result = _get_base_model_from_metadata(model_call_details_with_litellm_metadata)+ assert result == "azure/gpt-5-mini", f"Expected 'azure/gpt-5-mini', got {result}"++ # Test 3: base_model in litellm_params (direct base_model)+ model_call_details_with_direct_base_model = {+ "litellm_params": {+ "base_model": "azure/gpt-3.5-turbo"+ }+ }+ result = _get_base_model_from_metadata(model_call_details_with_direct_base_model)+ assert result == "azure/gpt-3.5-turbo", f"Expected 'azure/gpt-3.5-turbo', got {result}"++ # Test 4: metadata takes precedence over litellm_metadata+ model_call_details_with_both = {+ "litellm_params": {+ "metadata": {+ "model_info": {+ "base_model": "azure/gpt-4-from-metadata"+ }+ },+ "litellm_metadata": {+ "model_info": {+ "base_model": "azure/gpt-4-from-litellm-metadata"+ }+ }+ }+ }+ result = _get_base_model_from_metadata(model_call_details_with_both)+ assert result == "azure/gpt-4-from-metadata", f"Expected metadata to take precedence, got {result}"++ # Test 5: No base_model present+ model_call_details_without_base_model = {+ "litellm_params": {+ "metadata": {}+ }+ }+ result = _get_base_model_from_metadata(model_call_details_without_base_model)+ assert result is None, f"Expected None when no base_model present, got {result}"++ # Test 6: None input+ result = _get_base_model_from_metadata(None)+ assert result is None, f"Expected None for None input, got {result}"+