From d427ff6705cd3a600e0470ebc00503048e105595 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Sat, 21 Feb 2026 14:57:03 -0800 Subject: [PATCH] fix(logging): zero out response_cost for cache hits in _process_hidden_params_and_response_cost (#21841) When a cached response had `response_cost` pre-calculated in its `_hidden_params` (from the original response), `_process_hidden_params_and_response_cost` was using it directly without checking `cache_hit`, so cached responses were reported with non-zero cost instead of 0. Fixes test_cost_tracking_with_caching. --- litellm/litellm_core_utils/litellm_logging.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index 258df1bb900..64f1f9c72c2 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -1632,7 +1632,9 @@ class Logging(LiteLLMLoggingBaseClass): self.model_call_details["litellm_params"]["metadata"] = {} self.model_call_details["litellm_params"]["metadata"]["hidden_params"] = getattr(logging_result, "_hidden_params", {}) # type: ignore - if "response_cost" in hidden_params: + if self.model_call_details.get("cache_hit") is True: + self.model_call_details["response_cost"] = 0.0 + elif "response_cost" in hidden_params: self.model_call_details["response_cost"] = hidden_params["response_cost"] else: self.model_call_details["response_cost"] = self._response_cost_calculator(