diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py index 8a059c5077..670cefcf2f 100644 --- a/litellm/proxy/_types.py +++ b/litellm/proxy/_types.py @@ -343,8 +343,7 @@ class LiteLLM_SpendLogs(LiteLLMBase): endTime: Union[str, datetime, None] user: Optional[str] = "" modelParameters: Optional[Json] = {} - messages: Optional[Json] = [] - response: Optional[Json] = {} usage: Optional[Json] = {} metadata: Optional[Json] = {} cache_hit: Optional[str] = "False" + cache_key: Optional[str] = None diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 512e956b04..e1ca25e130 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -593,6 +593,12 @@ async def track_cost_callback( "user_api_key_user_id", None ) + if kwargs.get("cache_hit", False) == True: + response_cost = 0.0 + verbose_proxy_logger.info( + f"Cache Hit: response_cost {response_cost}, for user_id {user_id}" + ) + verbose_proxy_logger.info( f"response_cost {response_cost}, for user_id {user_id}" ) @@ -1382,8 +1388,6 @@ async def initialize( verbose_proxy_logger.setLevel( level=logging.DEBUG ) # set proxy logs to debug - litellm.set_verbose = True - dynamic_config = {"general": {}, user_model: {}} if config: ( diff --git a/litellm/proxy/schema.prisma b/litellm/proxy/schema.prisma index 441c3515fb..f06d42ba5b 100644 --- a/litellm/proxy/schema.prisma +++ b/litellm/proxy/schema.prisma @@ -58,4 +58,5 @@ model LiteLLM_SpendLogs { usage Json @default("{}") metadata Json @default("{}") cache_hit String @default("") + cache_key String @default("") } \ No newline at end of file diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 15f230a6a4..25c5c82ce5 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -995,11 +995,18 @@ def get_logging_payload(kwargs, response_obj, start_time, end_time): if api_key is not None and isinstance(api_key, str) and api_key.startswith("sk-"): # hash the api_key api_key = hash_token(api_key) - if "headers" in metadata and "authorization" in metadata["headers"]: metadata["headers"].pop( "authorization" ) # do not store the original `sk-..` api key in the db + if litellm.cache is not None: + cache_key = litellm.cache.get_cache_key(**kwargs) + else: + cache_key = "Cache OFF" + if cache_hit == True: + import time + + id = f"{id}_cache_hit{time.time()}" # SpendLogs does not allow duplicate request_id payload = { "request_id": id, @@ -1013,6 +1020,7 @@ def get_logging_payload(kwargs, response_obj, start_time, end_time): "modelParameters": optional_params, "usage": usage, "metadata": metadata, + "cache_key": cache_key, } json_fields = [ diff --git a/litellm/tests/test_key_generate_prisma.py b/litellm/tests/test_key_generate_prisma.py index 49f091cd6f..78fb756b25 100644 --- a/litellm/tests/test_key_generate_prisma.py +++ b/litellm/tests/test_key_generate_prisma.py @@ -716,6 +716,9 @@ def test_call_with_key_over_budget(prisma_client): # update spend using track_cost callback, make 2nd request, it should fail from litellm.proxy.proxy_server import track_cost_callback from litellm import ModelResponse, Choices, Message, Usage + from litellm.caching import Cache + + litellm.cache = Cache() import time request_id = f"chatcmpl-e41836bb-bb8b-4df2-8e70-8f3e160155ac{time.time()}" @@ -763,6 +766,10 @@ def test_call_with_key_over_budget(prisma_client): assert spend_log.request_id == request_id assert spend_log.spend == float("2e-05") assert spend_log.model == "chatgpt-v-2" + assert ( + spend_log.cache_key + == "a61ae14fe4a8b8014a61e6ae01a100c8bc6770ac37c293242afed954bc69207d" + ) # use generated key to auth in result = await user_api_key_auth(request=request, api_key=bearer_token) diff --git a/schema.prisma b/schema.prisma index 0dd11eb644..72d14e13bd 100644 --- a/schema.prisma +++ b/schema.prisma @@ -61,4 +61,5 @@ model LiteLLM_SpendLogs { usage Json @default("{}") metadata Json @default("{}") cache_hit String @default("") + cache_key String @default("") } \ No newline at end of file