Skip to content

Commit b5c32c9

Browse files
authored
Merge pull request BerriAI#9331 from BerriAI/litellm_patch_disable_spend_updates
[Patch] - Allow disabling all spend updates / writes to DB
2 parents d42f84d + 0c1d008 commit b5c32c9

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

litellm/proxy/hooks/proxy_track_cost_callback.py

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from litellm.litellm_core_utils.litellm_logging import StandardLoggingPayloadSetup
1414
from litellm.proxy._types import UserAPIKeyAuth
1515
from litellm.proxy.auth.auth_checks import log_db_metrics
16+
from litellm.proxy.utils import ProxyUpdateSpend
1617
from litellm.types.utils import (
1718
StandardLoggingPayload,
1819
StandardLoggingUserAPIKeyMetadata,
@@ -230,6 +231,11 @@ def _should_track_cost_callback(
230231
"""
231232
Determine if the cost callback should be tracked based on the kwargs
232233
"""
234+
235+
# don't run track cost callback if user opted into disabling spend
236+
if ProxyUpdateSpend.disable_spend_updates() is True:
237+
return False
238+
233239
if (
234240
user_api_key is not None
235241
or user_id is not None

litellm/proxy/proxy_server.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
get_origin,
2424
get_type_hints,
2525
)
26+
2627
from litellm.types.utils import (
2728
ModelResponse,
2829
ModelResponseStream,
@@ -254,6 +255,7 @@ def generate_feedback_box():
254255
from litellm.proxy.utils import (
255256
PrismaClient,
256257
ProxyLogging,
258+
ProxyUpdateSpend,
257259
_cache_user_row,
258260
_get_docs_url,
259261
_get_projected_spend_over_limit,
@@ -924,6 +926,8 @@ async def update_database( # noqa: PLR0915
924926
verbose_proxy_logger.debug(
925927
f"Enters prisma db call, response_cost: {response_cost}, token: {token}; user_id: {user_id}; team_id: {team_id}"
926928
)
929+
if ProxyUpdateSpend.disable_spend_updates() is True:
930+
return
927931
if token is not None and isinstance(token, str) and token.startswith("sk-"):
928932
hashed_token = hash_token(token=token)
929933
else:
@@ -3047,7 +3051,11 @@ async def async_data_generator(
30473051
):
30483052
verbose_proxy_logger.debug("inside generator")
30493053
try:
3050-
async for chunk in proxy_logging_obj.async_post_call_streaming_iterator_hook(user_api_key_dict=user_api_key_dict, response=response, request_data=request_data):
3054+
async for chunk in proxy_logging_obj.async_post_call_streaming_iterator_hook(
3055+
user_api_key_dict=user_api_key_dict,
3056+
response=response,
3057+
request_data=request_data,
3058+
):
30513059
verbose_proxy_logger.debug(
30523060
"async_data_generator: received streaming chunk - {}".format(chunk)
30533061
)

litellm/proxy/utils.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,9 @@ async def post_call_success_hook(
969969

970970
async def async_post_call_streaming_hook(
971971
self,
972-
response: Union[ModelResponse, EmbeddingResponse, ImageResponse],
972+
response: Union[
973+
ModelResponse, EmbeddingResponse, ImageResponse, ModelResponseStream
974+
],
973975
user_api_key_dict: UserAPIKeyAuth,
974976
):
975977
"""
@@ -2474,6 +2476,18 @@ async def update_spend_logs(
24742476
e=e, start_time=start_time, proxy_logging_obj=proxy_logging_obj
24752477
)
24762478

2479+
@staticmethod
2480+
def disable_spend_updates() -> bool:
2481+
"""
2482+
returns True if should not update spend in db
2483+
Skips writing spend logs and updates to key, team, user spend to DB
2484+
"""
2485+
from litellm.proxy.proxy_server import general_settings
2486+
2487+
if general_settings.get("disable_spend_updates") is True:
2488+
return True
2489+
return False
2490+
24772491

24782492
async def update_spend( # noqa: PLR0915
24792493
prisma_client: PrismaClient,

0 commit comments

Comments
 (0)