The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Allow configuration to on what threshold to try truncating request content in db
Wilson Britten771052d67d19
diff --git a/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.pyindex 653426e8bd2..f14949e12af 100644--- a/litellm/proxy/spend_tracking/spend_tracking_utils.py+++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py@@ -53,7 +53,7 @@ def _get_spend_logs_metadata(guardrail_information: Optional[StandardLoggingGuardrailInformation] = None,usage_object: Optional[dict] = None,model_map_information: Optional[StandardLoggingModelInformation] = None,- cold_storage_object_key: Optional[str] = None+ cold_storage_object_key: Optional[str] = None,) -> SpendLogsMetadata:if metadata is None:return SpendLogsMetadata(@@ -101,7 +101,7 @@ def _get_spend_logs_metadata(clean_metadata["usage_object"] = usage_objectclean_metadata["model_map_information"] = model_map_informationclean_metadata["cold_storage_object_key"] = cold_storage_object_key-+return clean_metadata@@ -484,6 +484,7 @@ def _sanitize_request_body_for_spend_logs_payload(Truncates strings longer than 1000 characters and handles nested dictionaries."""from litellm.constants import LITELLM_TRUNCATED_PAYLOAD_FIELD+MAX_STRING_LENGTH = 1000if visited is None:@@ -522,8 +523,21 @@ def _get_proxy_server_request_for_spend_logs_payload()if _proxy_server_request is not None:_request_body = _proxy_server_request.get("body", {}) or {}- _request_body = _sanitize_request_body_for_spend_logs_payload(_request_body)_request_body_json_str = json.dumps(_request_body, default=str)++ # Check if request body size exceeds truncation threshold+ max_body_size_before_trunc = litellm_params.get(+ "max_request_size_before_trunc"+ )+ if (+ max_body_size_before_trunc is None+ or len(_request_body_json_str) > max_body_size_before_trunc+ ):+ _request_body = _sanitize_request_body_for_spend_logs_payload(+ _request_body+ )+ _request_body_json_str = json.dumps(_request_body, default=str)+return _request_body_json_strreturn "{}"diff --git a/tests/test_spend_logs.py b/tests/test_spend_logs.pyindex 80dd8c9bcca..583709c6705 100644--- a/tests/test_spend_logs.py+++ b/tests/test_spend_logs.py@@ -336,3 +336,100 @@ async def test_spend_report_endpoint():print(f"Total Spend: {total_spend}")print("Metadata: ", metadata)print()+++# Helper function to generate test request bodies+def _generate_test_request_bodies():+ large_content = "x" * 2000 # 2000 characters+ small_content = "x" * 50 # 50 characters++ large_request_body = {+ "model": "gpt-3.5-turbo",+ "messages": [{"role": "user", "content": large_content}]+ }++ small_request_body = {+ "model": "gpt-3.5-turbo",+ "messages": [{"role": "user", "content": small_content}]+ }++ return large_request_body, small_request_body, large_content, small_content+++def _setup_spend_logs_settings():+ """Helper to mock the settings for storing prompts in spend logs"""+ import litellm.proxy.proxy_server+ litellm.proxy.proxy_server.general_settings = {"store_prompts_in_spend_logs": True}+++def test_large_request_no_truncation_threshold():+ """Test that large request with no threshold gets truncated"""+ from litellm.proxy.spend_tracking.spend_tracking_utils import _get_proxy_server_request_for_spend_logs_payload++ large_request_body, _, _, _ = _generate_test_request_bodies()+ _setup_spend_logs_settings()++ litellm_params = {+ "proxy_server_request": {"body": large_request_body}+ }++ result = _get_proxy_server_request_for_spend_logs_payload({}, litellm_params)++ # Should be truncated (contains truncation indicator)+ assert "LITELLM_TRUNCATED_PAYLOAD_FIELD" in result+++def test_large_request_high_truncation_threshold():+ """Test that large request with high threshold does not get truncated"""+ from litellm.proxy.spend_tracking.spend_tracking_utils import _get_proxy_server_request_for_spend_logs_payload++ large_request_body, _, large_content, _ = _generate_test_request_bodies()+ _setup_spend_logs_settings()++ litellm_params = {+ "proxy_server_request": {"body": large_request_body},+ "max_request_size_before_trunc": 10000 # Higher than request size+ }++ result = _get_proxy_server_request_for_spend_logs_payload({}, litellm_params)++ # Should not be truncated (full content preserved)+ assert "LITELLM_TRUNCATED_PAYLOAD_FIELD" not in result+ assert large_content in result+++def test_large_request_low_truncation_threshold():+ """Test that large request with low threshold gets truncated"""+ from litellm.proxy.spend_tracking.spend_tracking_utils import _get_proxy_server_request_for_spend_logs_payload++ large_request_body, _, _, _ = _generate_test_request_bodies()+ _setup_spend_logs_settings()++ litellm_params = {+ "proxy_server_request": {"body": large_request_body},+ "max_request_size_before_trunc": 100 # Lower than request size+ }++ result = _get_proxy_server_request_for_spend_logs_payload({}, litellm_params)++ # Should be truncated+ assert "LITELLM_TRUNCATED_PAYLOAD_FIELD" in result+++def test_small_request_low_truncation_threshold():+ """Test that small request with low threshold does not get truncated"""+ from litellm.proxy.spend_tracking.spend_tracking_utils import _get_proxy_server_request_for_spend_logs_payload++ _, small_request_body, _, small_content = _generate_test_request_bodies()+ _setup_spend_logs_settings()++ litellm_params = {+ "proxy_server_request": {"body": small_request_body},+ "max_request_size_before_trunc": 100 # Higher than small request size+ }++ result = _get_proxy_server_request_for_spend_logs_payload({}, litellm_params)++ # Should not be truncated (content is small enough)+ assert "LITELLM_TRUNCATED_PAYLOAD_FIELD" not in result+ assert small_content in result