From a3bf78f2301e3ca694abe8ccaad7d6307d564cd7 Mon Sep 17 00:00:00 2001 From: Milan Date: Mon, 27 Apr 2026 18:42:46 +0300 Subject: [PATCH] refactor(logging): simplify hidden response cost backfill Clean up metadata initialization and reuse the raw response cost when deciding whether to backfill hidden params. Made-with: Cursor --- litellm/litellm_core_utils/litellm_logging.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index 9a2cea3fa2c..fea61f04ce9 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -1712,10 +1712,8 @@ class Logging(LiteLLMLoggingBaseClass): metadata_hidden_params["response_cost"] = response_cost litellm_params = self.model_call_details["litellm_params"] - metadata = litellm_params.setdefault("metadata", {}) - if metadata is None: - metadata = {} - litellm_params["metadata"] = metadata + metadata = litellm_params.get("metadata") or {} + litellm_params["metadata"] = metadata metadata["hidden_params"] = metadata_hidden_params def _process_hidden_params_and_response_cost( @@ -5433,7 +5431,8 @@ def get_standard_logging_object_payload( ## Get model cost information ## base_model = _get_base_model_from_metadata(model_call_details=kwargs) custom_pricing = use_custom_pricing_for_model(litellm_params=litellm_params) - response_cost: float = kwargs.get("response_cost", 0) or 0.0 + raw_response_cost = kwargs.get("response_cost") + response_cost: float = raw_response_cost or 0.0 # clean up litellm hidden params clean_hidden_params = StandardLoggingPayloadSetup.get_hidden_params( @@ -5441,7 +5440,7 @@ def get_standard_logging_object_payload( ) if ( clean_hidden_params["response_cost"] is None - and kwargs.get("response_cost") is not None + and raw_response_cost is not None ): clean_hidden_params["response_cost"] = response_cost