From ab45fa43c6a464edf93f9c2033a327fde08f2089 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Mon, 17 Mar 2025 22:22:09 -0700 Subject: [PATCH] Merge pull request #9331 from BerriAI/litellm_patch_disable_spend_updates [Patch] - Allow disabling all spend updates / writes to DB --- litellm/proxy/hooks/proxy_track_cost_callback.py | 6 ++++++ litellm/proxy/proxy_server.py | 3 +++ litellm/proxy/utils.py | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/litellm/proxy/hooks/proxy_track_cost_callback.py b/litellm/proxy/hooks/proxy_track_cost_callback.py index e068a446c52..e8a947329d7 100644 --- a/litellm/proxy/hooks/proxy_track_cost_callback.py +++ b/litellm/proxy/hooks/proxy_track_cost_callback.py @@ -13,6 +13,7 @@ from litellm.litellm_core_utils.core_helpers import ( from litellm.litellm_core_utils.litellm_logging import StandardLoggingPayloadSetup from litellm.proxy._types import UserAPIKeyAuth from litellm.proxy.auth.auth_checks import log_db_metrics +from litellm.proxy.utils import ProxyUpdateSpend from litellm.types.utils import ( StandardLoggingPayload, StandardLoggingUserAPIKeyMetadata, @@ -230,6 +231,11 @@ def _should_track_cost_callback( """ Determine if the cost callback should be tracked based on the kwargs """ + + # don't run track cost callback if user opted into disabling spend + if ProxyUpdateSpend.disable_spend_updates() is True: + return False + if ( user_api_key is not None or user_id is not None diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index b1660f7ad0b..e0e8bef787e 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -249,6 +249,7 @@ from litellm.proxy.ui_crud_endpoints.proxy_setting_endpoints import ( from litellm.proxy.utils import ( PrismaClient, ProxyLogging, + ProxyUpdateSpend, _cache_user_row, _get_docs_url, _get_projected_spend_over_limit, @@ -919,6 +920,8 @@ async def update_database( # noqa: PLR0915 verbose_proxy_logger.debug( f"Enters prisma db call, response_cost: {response_cost}, token: {token}; user_id: {user_id}; team_id: {team_id}" ) + if ProxyUpdateSpend.disable_spend_updates() is True: + return if token is not None and isinstance(token, str) and token.startswith("sk-"): hashed_token = hash_token(token=token) else: diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 08afcf23c17..1fcae3b81c6 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -2439,6 +2439,18 @@ class ProxyUpdateSpend: e=e, start_time=start_time, proxy_logging_obj=proxy_logging_obj ) + @staticmethod + def disable_spend_updates() -> bool: + """ + returns True if should not update spend in db + Skips writing spend logs and updates to key, team, user spend to DB + """ + from litellm.proxy.proxy_server import general_settings + + if general_settings.get("disable_spend_updates") is True: + return True + return False + async def update_spend( # noqa: PLR0915 prisma_client: PrismaClient,