From 7f345df47762ff3be04e6fde2f13e70019ede4ee Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Fri, 28 Feb 2025 16:31:15 -0800 Subject: [PATCH] _should_store_errors_in_spend_logs --- .../proxy/hooks/proxy_track_cost_callback.py | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/hooks/proxy_track_cost_callback.py b/litellm/proxy/hooks/proxy_track_cost_callback.py index 8adc80e0d529..9200acd92b1f 100644 --- a/litellm/proxy/hooks/proxy_track_cost_callback.py +++ b/litellm/proxy/hooks/proxy_track_cost_callback.py @@ -1,5 +1,12 @@ +""" +_ProxyDBLogger, handles writing per request logs to the database + +- track_cost_callback, stores successful requests to the database +- async_post_call_failure_hook, stores failed requests to the database + - for now this is opt in, we don't track failed requests in the DB by default +""" + import asyncio -import json import traceback from datetime import datetime from typing import Any, Optional, Union, cast @@ -35,6 +42,9 @@ async def async_post_call_failure_hook( ): from litellm.proxy.proxy_server import update_database + if _ProxyDBLogger._should_store_errors_in_spend_logs() is not True: + return + _metadata = dict( StandardLoggingUserAPIKeyMetadata( user_api_key_hash=user_api_key_dict.api_key, @@ -202,6 +212,26 @@ async def _PROXY_track_cost_callback( "Error in tracking cost callback - %s", str(e) ) + @staticmethod + def _should_store_errors_in_spend_logs() -> bool: + """ + Returns True if we should store errors in the spend logs + + Enabled when `store_errors_in_spend_logs` is set to `true` in the `general_settings` section of the config file. + + By Default, this is disabled. + + ```yaml + general_settings: + store_errors_in_spend_logs: true + ``` + """ + from litellm.proxy.proxy_server import general_settings + + if general_settings.get("store_errors_in_spend_logs", False) is True: + return True + return False + def _should_track_cost_callback( user_api_key: Optional[str],