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.
This commit is contained in:
Ishaan Jaff 2026-02-21 14:57:03 -08:00 committed by GitHub
parent 8c7f667df2
commit d427ff6705
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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(