Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

[Refactor - Filtering Spend Logs] Add `status` to root of SpendLogs table (#10661)

Ishaan Jaffd9f2415f653d
diff --git a/litellm-proxy-extras/litellm_proxy_extras/migrations/20250508072103_add_status_to_spendlogs/migration.sql b/litellm-proxy-extras/litellm_proxy_extras/migrations/20250508072103_add_status_to_spendlogs/migration.sql
new file mode 100644
index 00000000000..8f6c68aa67e
--- /dev/null
+++ b/litellm-proxy-extras/litellm_proxy_extras/migrations/20250508072103_add_status_to_spendlogs/migration.sql
@@ -0,0 +1,3 @@
+-- AlterTable
+ALTER TABLE "LiteLLM_SpendLogs" ADD COLUMN "status" TEXT;
+
diff --git a/litellm-proxy-extras/litellm_proxy_extras/schema.prisma b/litellm-proxy-extras/litellm_proxy_extras/schema.prisma
index 145253da512..3c20b9a3cda 100644
--- a/litellm-proxy-extras/litellm_proxy_extras/schema.prisma
+++ b/litellm-proxy-extras/litellm_proxy_extras/schema.prisma
@@ -260,6 +260,7 @@ model LiteLLM_SpendLogs {
messages Json? @default("{}")
response Json? @default("{}")
session_id String?
+ status String?
proxy_server_request Json? @default("{}")
@@index([startTime])
@@index([end_user])
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py
index ce2639b3b35..938a2c88a73 100644
--- a/litellm/proxy/_types.py
+++ b/litellm/proxy/_types.py
@@ -2035,6 +2035,7 @@ class SpendLogsPayload(TypedDict):
response: Optional[Union[str, list, dict]]
proxy_server_request: Optional[str]
session_id: Optional[str]
+ status: Literal["success", "failure"]
class SpanAttributes(str, enum.Enum):
diff --git a/litellm/proxy/schema.prisma b/litellm/proxy/schema.prisma
index 145253da512..3c20b9a3cda 100644
--- a/litellm/proxy/schema.prisma
+++ b/litellm/proxy/schema.prisma
@@ -260,6 +260,7 @@ model LiteLLM_SpendLogs {
messages Json? @default("{}")
response Json? @default("{}")
session_id String?
+ status String?
proxy_server_request Json? @default("{}")
@@index([startTime])
@@index([end_user])
diff --git a/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.py
index 431c40b89c4..65656a9ff86 100644
--- a/litellm/proxy/spend_tracking/spend_tracking_utils.py
+++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py
@@ -4,7 +4,7 @@ import secrets
from datetime import datetime
from datetime import datetime as dt
from datetime import timezone
-from typing import Any, List, Optional, cast
+from typing import Any, List, Literal, Optional, cast
from pydantic import BaseModel
@@ -88,9 +88,9 @@ def _get_spend_logs_metadata(
clean_metadata["applied_guardrails"] = applied_guardrails
clean_metadata["batch_models"] = batch_models
clean_metadata["mcp_tool_call_metadata"] = mcp_tool_call_metadata
- clean_metadata["vector_store_request_metadata"] = (
- _get_vector_store_request_for_spend_logs_payload(vector_store_request_metadata)
- )
+ clean_metadata[
+ "vector_store_request_metadata"
+ ] = _get_vector_store_request_for_spend_logs_payload(vector_store_request_metadata)
clean_metadata["usage_object"] = usage_object
clean_metadata["model_map_information"] = model_map_information
return clean_metadata
@@ -310,6 +310,9 @@ def get_logging_payload( # noqa: PLR0915
kwargs=kwargs,
standard_logging_payload=standard_logging_payload,
),
+ status=_get_status_for_spend_log(
+ metadata=metadata,
+ ),
)
verbose_proxy_logger.debug(
@@ -530,3 +533,17 @@ def _should_store_prompts_and_responses_in_spend_logs() -> bool:
from litellm.proxy.proxy_server import general_settings
return general_settings.get("store_prompts_in_spend_logs") is True
+
+
+def _get_status_for_spend_log(
+ metadata: dict,
+) -> Literal["success", "failure"]:
+ """
+ Get the status for the spend log.
+
+ It's only a failure if metadata.get("status") is "failure"
+ """
+ _status: Optional[str] = metadata.get("status", None)
+ if _status == "failure":
+ return "failure"
+ return "success"
diff --git a/schema.prisma b/schema.prisma
index 145253da512..3c20b9a3cda 100644
--- a/schema.prisma
+++ b/schema.prisma
@@ -260,6 +260,7 @@ model LiteLLM_SpendLogs {
messages Json? @default("{}")
response Json? @default("{}")
session_id String?
+ status String?
proxy_server_request Json? @default("{}")
@@index([startTime])
@@index([end_user])
diff --git a/tests/litellm/proxy/spend_tracking/test_spend_management_endpoints.py b/tests/litellm/proxy/spend_tracking/test_spend_management_endpoints.py
index 3f04bfe154d..9b86aca98b0 100644
--- a/tests/litellm/proxy/spend_tracking/test_spend_management_endpoints.py
+++ b/tests/litellm/proxy/spend_tracking/test_spend_management_endpoints.py
@@ -485,6 +485,7 @@ class TestSpendLogsPayload:
"messages": "{}",
"response": "{}",
"proxy_server_request": "{}",
+ "status": "success",
}
)
@@ -576,6 +577,7 @@ class TestSpendLogsPayload:
"messages": "{}",
"response": "{}",
"proxy_server_request": "{}",
+ "status": "success",
}
)
@@ -665,6 +667,7 @@ class TestSpendLogsPayload:
"messages": "{}",
"response": "{}",
"proxy_server_request": "{}",
+ "status": "success",
}
)
diff --git a/tests/logging_callback_tests/gcs_pub_sub_body/spend_logs_payload.json b/tests/logging_callback_tests/gcs_pub_sub_body/spend_logs_payload.json
index bdb5b219789..7ea23de3b63 100644
--- a/tests/logging_callback_tests/gcs_pub_sub_body/spend_logs_payload.json
+++ b/tests/logging_callback_tests/gcs_pub_sub_body/spend_logs_payload.json
@@ -25,5 +25,6 @@
"custom_llm_provider": "openai",
"messages": "{}",
"response": "{}",
- "proxy_server_request": "{}"
+ "proxy_server_request": "{}",
+ "status": "success"
}
\ No newline at end of file