MCPcopy Create free account
hub / github.com/BerriAI/litellm / _update_end_user_cache

Function _update_end_user_cache

litellm/proxy/proxy_server.py:2760–2799  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2758
2759 ### UPDATE END-USER SPEND ###
2760 async def _update_end_user_cache():
2761 if end_user_id is None or response_cost is None:
2762 return
2763
2764 _id = "end_user_id:{}".format(end_user_id)
2765 try:
2766 # Fetch the existing cost for the given user
2767 cached_end_user = await user_api_key_cache.async_get_cache(key=_id)
2768 if cached_end_user is None:
2769 # if user does not exist in LiteLLM_UserTable, create a new user
2770 # do nothing if end-user not in api key cache
2771 return
2772 existing_spend_obj = CacheCodec.deserialize(cached_end_user, LiteLLM_EndUserTable)
2773 if existing_spend_obj is None:
2774 return
2775 verbose_proxy_logger.debug(
2776 f"_update_end_user_db: existing spend: {existing_spend_obj}; response_cost: {response_cost}"
2777 )
2778
2779 existing_spend = existing_spend_obj.spend or 0.0
2780 # Calculate the new cost by adding the existing cost and response_cost
2781 new_spend = existing_spend + response_cost
2782
2783 existing_spend_obj.spend = new_spend
2784 values_to_update_in_cache.append(
2785 (
2786 _id,
2787 CacheCodec.serialize(existing_spend_obj, model_type=LiteLLM_EndUserTable),
2788 )
2789 )
2790 except Exception as e:
2791 verbose_proxy_logger.warning(
2792 "Spend tracking - failed to update end user spend in cache. "
2793 "Budget enforcement may use stale spend values. "
2794 "end_user_id=%s, response_cost=%s - %s\n%s",
2795 end_user_id,
2796 response_cost,
2797 str(e),
2798 traceback.format_exc(),
2799 )
2800
2801 ### UPDATE TEAM SPEND ###
2802 async def _update_team_cache():

Callers 1

update_cacheFunction · 0.85

Calls 6

deserializeMethod · 0.80
debugMethod · 0.80
formatMethod · 0.45
async_get_cacheMethod · 0.45
appendMethod · 0.45
serializeMethod · 0.45

Tested by

no test coverage detected