From 7cfab05960feb666a02e912550c0fba9812932d6 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Sat, 17 Oct 2026 22:42:34 -0700 Subject: [PATCH] fix(spend-logs): keep main's stored tail for a negative prompt length limit At a negative MAX_STRING_LENGTH_PROMPT_IN_DB the shared truncation helper stored one more character of each value than main, since it dropped main's clamp of the end slice to the limit minus the start --- litellm/proxy/spend_tracking/spend_tracking_utils.py | 2 +- .../proxy/spend_tracking/test_spend_tracking_utils.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.py index 9e6c383766c..536950aff45 100644 --- a/litellm/proxy/spend_tracking/spend_tracking_utils.py +++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py @@ -1205,7 +1205,7 @@ def _truncated_for_db(value: str, max_length: int) -> str: f"{value[:start_chars]}" f"... ({LITELLM_TRUNCATED_PAYLOAD_FIELD} skipped {len(value) - start_chars - end_chars} chars. " f"{LITELLM_TRUNCATION_DB_SAFEGUARD_NOTE}) ..." - f"{value[-end_chars:]}" + f"{value[-min(end_chars, max_length - start_chars) :]}" ) diff --git a/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py b/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py index edfb1005ec7..2201150c74c 100644 --- a/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py +++ b/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py @@ -668,14 +668,17 @@ def test_large_request_no_truncation_threshold(): assert "skipped" in truncated_content -@pytest.mark.parametrize("max_string_length_prompt_in_db", [0, 1]) -def test_a_limit_under_two_still_stores_the_whole_value_after_the_marker(max_string_length_prompt_in_db: int): +@pytest.mark.parametrize( + ("max_string_length_prompt_in_db", "stored_tail"), + [(0, "claude-sonnet-5-5"), (1, "claude-sonnet-5-5"), (-1, "laude-sonnet-5-5"), (-2, "aude-sonnet-5-5")], +) +def test_a_limit_under_two_keeps_mains_tail_after_the_marker(max_string_length_prompt_in_db: int, stored_tail: str): sanitized: Final = _sanitize_request_body_for_spend_logs_payload( {"model": "claude-sonnet-5-5"}, max_string_length_prompt_in_db=max_string_length_prompt_in_db ) assert sanitized["model"].startswith("...") - assert sanitized["model"].endswith("claude-sonnet-5-5") + assert sanitized["model"].endswith(f") ...{stored_tail}") def test_small_request_no_truncation():