fix(spend): keep caller-set call ids from splitting the non-finite cost warning

The overflow warning strips newlines from litellm_call_id, and neither
non-finite cost warning prints the raw value, which said nothing beyond
"non-finite"
This commit is contained in:
mateo-berri 2026-10-28 12:14:48 -07:00
parent 9829e4b929
commit f149dea9de
4 changed files with 19 additions and 12 deletions

View file

@ -944,6 +944,10 @@ def finite_cost(value: object) -> float | None:
return float(value)
def single_log_line(value: object) -> str:
return str(value).replace("\r", "").replace("\n", "")
def finite_reported_cost(value: object) -> float | None:
cost: Final = finite_cost(value)
if cost is None and isinstance(value, float):

View file

@ -84,6 +84,7 @@ from litellm.litellm_core_utils.core_helpers import (
proxy_stamped_used_client_oauth_token,
reconstruct_model_name,
set_response_cost_in_hidden_params,
single_log_line,
)
from litellm.litellm_core_utils.error_normalization import normalize_error
from litellm.litellm_core_utils.get_litellm_params import get_litellm_params
@ -2195,7 +2196,7 @@ class Logging(LiteLLMLoggingBaseClass):
if cost is None or finite_cost(cost) is not None:
return cost
verbose_logger.warning(
"Recording a non-finite response_cost=%s as 0 for litellm_call_id=%s", cost, self.litellm_call_id
"Recording a non-finite response_cost as 0 for litellm_call_id=%s", single_log_line(self.litellm_call_id)
)
return 0.0

View file

@ -2062,9 +2062,7 @@ class Usage(SafeAttributeModel, CompletionUsage):
if cost is None:
del self.cost
elif isinstance(cost, float) and not math.isfinite(cost):
verbose_logger.warning(
"Dropping a non-finite provider usage cost %s, the cost map prices the request", cost
)
verbose_logger.warning("Dropping a non-finite provider usage cost, the cost map prices the request")
del self.cost
else:
self.cost = cost

View file

@ -6134,16 +6134,20 @@ def test_get_standard_logging_object_payload_logs_a_non_finite_response_cost_as_
assert payload["response_cost"] == 0
def test_an_overflowing_custom_price_records_a_zero_response_cost() -> None:
response: Final = litellm.completion(
model="gpt-4o",
mock_response="hi there",
messages=[{"role": "user", "content": "hello world"}],
input_cost_per_token=1e308,
output_cost_per_token=1e308,
)
def test_an_overflowing_custom_price_records_a_zero_response_cost(caplog: pytest.LogCaptureFixture) -> None:
with caplog.at_level(logging.WARNING, logger="LiteLLM"):
response: Final = litellm.completion(
model="gpt-4o",
mock_response="hi there",
messages=[{"role": "user", "content": "hello world"}],
input_cost_per_token=1e308,
output_cost_per_token=1e308,
litellm_call_id="overflow\nforged log line",
)
assert response._hidden_params["response_cost"] == 0.0
warnings: Final = [r.getMessage() for r in caplog.records if "non-finite response_cost" in r.getMessage()]
assert warnings == ["Recording a non-finite response_cost as 0 for litellm_call_id=overflowforged log line"]
def test_get_standard_logging_object_payload_carries_matched_access_groups(logging_obj):