From 9813c4bf41b5b9b3af56c8f1dd4aa748bd98713b Mon Sep 17 00:00:00 2001 From: Thibault Serot Date: Thu, 9 Jul 2026 11:58:29 +1000 Subject: [PATCH 1/4] feat(ui): add session id filter to request logs --- .../spend_management_endpoints.py | 8 ++ .../test_spend_management_endpoints.py | 84 +++++++++++++++++++ .../src/components/networking.tsx | 1 + .../components/view_logs/filter_options.ts | 5 ++ .../view_logs/log_filter_logic.test.tsx | 1 + .../components/view_logs/log_filter_logic.tsx | 4 + ui/litellm-dashboard/src/lib/http/schema.d.ts | 4 + 7 files changed, 107 insertions(+) diff --git a/litellm/proxy/spend_tracking/spend_management_endpoints.py b/litellm/proxy/spend_tracking/spend_management_endpoints.py index 8f530e3b8ce..cf7bedfdc71 100644 --- a/litellm/proxy/spend_tracking/spend_management_endpoints.py +++ b/litellm/proxy/spend_tracking/spend_management_endpoints.py @@ -1622,6 +1622,10 @@ async def ui_view_spend_logs( default=None, description="request_id to get spend logs for specific request_id", ), + session_id: str | None = fastapi.Query( + default=None, + description="Filter spend logs by session_id", + ), team_id: str | None = fastapi.Query( default=None, description="Filter spend logs by team_id", @@ -1772,6 +1776,9 @@ async def ui_view_spend_logs( if request_id is not None: where_conditions["request_id"] = request_id + if session_id is not None: + where_conditions["session_id"] = session_id + if model is not None: where_conditions["model"] = model @@ -1887,6 +1894,7 @@ async def ui_view_spend_logs( ('"user"', "user"), ("api_key", "api_key"), ("request_id", "request_id"), + ("session_id", "session_id"), ("model", "model"), ("model_id", "model_id"), ("model_group", "model_group"), diff --git a/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py b/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py index 1e9818534c9..69ffc2275ff 100644 --- a/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py +++ b/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py @@ -1,4 +1,5 @@ import asyncio +import collections import datetime import json import os @@ -85,6 +86,7 @@ def _reconstruct_ui_where_from_sql(sql_query, params): '"user"': "user", "api_key": "api_key", "request_id": "request_id", + "session_id": "session_id", "model": "model", "model_id": "model_id", "model_group": "model_group", @@ -162,7 +164,21 @@ def make_ui_spend_logs_mock_prisma(mock_spend_logs, filter_fn, team_lookup_fn=No async def count(self, *args, **kwargs): return len(filter_fn(kwargs.get("where", {}))) + async def group_by(self, by, where, count): + allowed = set(where["session_id"]["in"]) + tallied = collections.Counter( + log["session_id"] + for log in mock_spend_logs + if log.get("session_id") in allowed + ) + return [ + {"session_id": sid, "_count": {"session_id": n}} + for sid, n in tallied.items() + ] + async def query_raw(self, sql_query, *params): + if "mcp_tool_call_count" in sql_query: + return [] filtered = filter_fn(_reconstruct_ui_where_from_sql(sql_query, params)) total = len(filtered) if "COUNT(*)" in sql_query: @@ -597,6 +613,74 @@ async def test_ui_view_spend_logs_with_user_id(client, monkeypatch): assert data["data"][0]["user"] == "test_user_1" +@pytest.mark.asyncio +async def test_ui_view_spend_logs_with_session_id(client, monkeypatch): + mock_spend_logs = [ + { + "id": "log1", + "request_id": "req1", + "api_key": "sk-test-key", + "user": "test_user_1", + "session_id": "session-abc", + "spend": 0.05, + "startTime": datetime.datetime.now(timezone.utc).isoformat(), + "model": "gpt-4", + }, + { + "id": "log2", + "request_id": "req2", + "api_key": "sk-test-key", + "user": "test_user_1", + "session_id": "session-abc", + "spend": 0.10, + "startTime": datetime.datetime.now(timezone.utc).isoformat(), + "model": "gpt-4", + }, + { + "id": "log3", + "request_id": "req3", + "api_key": "sk-test-key", + "user": "test_user_1", + "session_id": "session-other", + "spend": 0.02, + "startTime": datetime.datetime.now(timezone.utc).isoformat(), + "model": "gpt-4", + }, + ] + + def filter_by_session(where): + if "session_id" in where: + return [ + log + for log in mock_spend_logs + if log["session_id"] == where["session_id"] + ] + return mock_spend_logs + + monkeypatch.setattr( + "litellm.proxy.proxy_server.prisma_client", + make_ui_spend_logs_mock_prisma(mock_spend_logs, filter_by_session), + ) + + start_date, end_date = _default_date_range() + + response = client.get( + "/spend/logs/ui", + params={ + "session_id": "session-abc", + "start_date": start_date, + "end_date": end_date, + }, + headers={"Authorization": "Bearer sk-test"}, + ) + + assert response.status_code == 200 + data = response.json() + assert data["total"] == 2 + assert {log["request_id"] for log in data["data"]} == {"req1", "req2"} + assert all(log["session_id"] == "session-abc" for log in data["data"]) + + # Mock spend logs with distinct values for sorting tests. # req_a: spend=0.10, tokens=500, start/end earliest # req_b: spend=0.05, tokens=200, start/end 2nd diff --git a/ui/litellm-dashboard/src/components/networking.tsx b/ui/litellm-dashboard/src/components/networking.tsx index 403647106d4..5ec2765c621 100644 --- a/ui/litellm-dashboard/src/components/networking.tsx +++ b/ui/litellm-dashboard/src/components/networking.tsx @@ -1929,6 +1929,7 @@ interface UiSpendLogsParams { api_key?: string; team_id?: string; request_id?: string; + session_id?: string; user_id?: string; end_user?: string; status_filter?: string; diff --git a/ui/litellm-dashboard/src/components/view_logs/filter_options.ts b/ui/litellm-dashboard/src/components/view_logs/filter_options.ts index 52632ea5861..90e27b6144d 100644 --- a/ui/litellm-dashboard/src/components/view_logs/filter_options.ts +++ b/ui/litellm-dashboard/src/components/view_logs/filter_options.ts @@ -63,6 +63,11 @@ export function getLogFilterOptions(accessToken: string): FilterOption[] { label: "Key Hash", isSearchable: false, }, + { + name: FILTER_KEYS.SESSION_ID, + label: "Session ID", + isSearchable: false, + }, { name: "Model", label: "Model", diff --git a/ui/litellm-dashboard/src/components/view_logs/log_filter_logic.test.tsx b/ui/litellm-dashboard/src/components/view_logs/log_filter_logic.test.tsx index cbe37e0b70f..ef550baea91 100644 --- a/ui/litellm-dashboard/src/components/view_logs/log_filter_logic.test.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/log_filter_logic.test.tsx @@ -218,6 +218,7 @@ describe("useLogFilterLogic", () => { { filterKey: "Team ID", paramName: "team_id", value: "team-a" }, { filterKey: "Key Hash", paramName: "api_key", value: "key-x" }, { filterKey: "Request ID", paramName: "request_id", value: "req-xyz" }, + { filterKey: "Session ID", paramName: "session_id", value: "sess-42" }, { filterKey: "User ID", paramName: "user_id", value: "user-123" }, { filterKey: "End User", paramName: "end_user", value: "user-a" }, { filterKey: "Status", paramName: "status_filter", value: "error" }, diff --git a/ui/litellm-dashboard/src/components/view_logs/log_filter_logic.tsx b/ui/litellm-dashboard/src/components/view_logs/log_filter_logic.tsx index c45e03905d0..1d699042f05 100644 --- a/ui/litellm-dashboard/src/components/view_logs/log_filter_logic.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/log_filter_logic.tsx @@ -30,6 +30,7 @@ export const FILTER_KEYS = { TEAM_ID: "Team ID", KEY_HASH: "Key Hash", REQUEST_ID: "Request ID", + SESSION_ID: "Session ID", MODEL: "Model", /** Exact match on LiteLLM_SpendLogs.model — use for search tools and public model names. */ PUBLIC_MODEL_OR_SEARCH_TOOL: "Public model / search tool", @@ -49,6 +50,7 @@ const TEXT_FILTER_KEYS: readonly (keyof LogFilterState)[] = [ FILTER_KEYS.KEY_HASH, FILTER_KEYS.ERROR_MESSAGE, FILTER_KEYS.REQUEST_ID, + FILTER_KEYS.SESSION_ID, FILTER_KEYS.USER_ID, FILTER_KEYS.PUBLIC_MODEL_OR_SEARCH_TOOL, ]; @@ -62,6 +64,7 @@ export const defaultFilters: LogFilterState = { [FILTER_KEYS.TEAM_ID]: "", [FILTER_KEYS.KEY_HASH]: "", [FILTER_KEYS.REQUEST_ID]: "", + [FILTER_KEYS.SESSION_ID]: "", [FILTER_KEYS.MODEL]: "", [FILTER_KEYS.PUBLIC_MODEL_OR_SEARCH_TOOL]: "", [FILTER_KEYS.USER_ID]: "", @@ -160,6 +163,7 @@ export function useLogFilterLogic({ api_key: effectiveFilters[FILTER_KEYS.KEY_HASH] || undefined, team_id: effectiveFilters[FILTER_KEYS.TEAM_ID] || undefined, request_id: effectiveFilters[FILTER_KEYS.REQUEST_ID] || undefined, + session_id: effectiveFilters[FILTER_KEYS.SESSION_ID] || undefined, user_id: effectiveFilters[FILTER_KEYS.USER_ID] || (filterByCurrentUser ? userID ?? undefined : undefined), end_user: effectiveFilters[FILTER_KEYS.END_USER] || undefined, status_filter: effectiveFilters[FILTER_KEYS.STATUS] || undefined, diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts index 852f8388ec2..8e26729aa18 100644 --- a/ui/litellm-dashboard/src/lib/http/schema.d.ts +++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts @@ -48633,6 +48633,8 @@ export interface operations { user_id?: string | null; /** @description request_id to get spend logs for specific request_id */ request_id?: string | null; + /** @description Filter spend logs by session_id */ + session_id?: string | null; /** @description Filter spend logs by team_id */ team_id?: string | null; /** @description Filter logs with spend greater than or equal to this value */ @@ -48739,6 +48741,8 @@ export interface operations { user_id?: string | null; /** @description request_id to get spend logs for specific request_id */ request_id?: string | null; + /** @description Filter spend logs by session_id */ + session_id?: string | null; /** @description Filter spend logs by team_id */ team_id?: string | null; /** @description Filter logs with spend greater than or equal to this value */ From f33403cb4b3b4122eadb2bb628ff7b99d8f5be02 Mon Sep 17 00:00:00 2001 From: Thibault Serot Date: Thu, 9 Jul 2026 12:39:13 +1000 Subject: [PATCH 2/4] feat(ui): support partial match on session id filter --- .../spend_management_endpoints.py | 12 ++- .../test_spend_management_endpoints.py | 91 +++++++++---------- ui/litellm-dashboard/src/lib/http/schema.d.ts | 4 +- 3 files changed, 54 insertions(+), 53 deletions(-) diff --git a/litellm/proxy/spend_tracking/spend_management_endpoints.py b/litellm/proxy/spend_tracking/spend_management_endpoints.py index cf7bedfdc71..0bf35352d5f 100644 --- a/litellm/proxy/spend_tracking/spend_management_endpoints.py +++ b/litellm/proxy/spend_tracking/spend_management_endpoints.py @@ -1624,7 +1624,7 @@ async def ui_view_spend_logs( ), session_id: str | None = fastapi.Query( default=None, - description="Filter spend logs by session_id", + description="Filter spend logs by session_id (partial string match)", ), team_id: str | None = fastapi.Query( default=None, @@ -1776,9 +1776,6 @@ async def ui_view_spend_logs( if request_id is not None: where_conditions["request_id"] = request_id - if session_id is not None: - where_conditions["session_id"] = session_id - if model is not None: where_conditions["model"] = model @@ -1894,7 +1891,6 @@ async def ui_view_spend_logs( ('"user"', "user"), ("api_key", "api_key"), ("request_id", "request_id"), - ("session_id", "session_id"), ("model", "model"), ("model_id", "model_id"), ("model_group", "model_group"), @@ -1914,6 +1910,12 @@ async def ui_view_spend_logs( p += 2 sql_conditions.append(or_clause) + if session_id is not None: + like_escaped_session_id = session_id.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") + sql_conditions.append(f"session_id LIKE ${p}") + sql_params.append(f"%{like_escaped_session_id}%") + p += 1 + # Status filter if status_filter is not None: if status_filter == "success": diff --git a/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py b/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py index 69ffc2275ff..c3899883d58 100644 --- a/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py +++ b/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py @@ -86,7 +86,6 @@ def _reconstruct_ui_where_from_sql(sql_query, params): '"user"': "user", "api_key": "api_key", "request_id": "request_id", - "session_id": "session_id", "model": "model", "model_id": "model_id", "model_group": "model_group", @@ -100,6 +99,7 @@ def _reconstruct_ui_where_from_sql(sql_query, params): alias = re.search(r"user_api_key_alias' LIKE \$(\d+)", cond) code = re.search(r"error_code' = \$(\d+)", cond) msg = re.search(r"error_message' LIKE \$(\d+)", cond) + sess = re.fullmatch(r"session_id LIKE \$(\d+)", cond) status = re.fullmatch(r"status = \$(\d+)", cond) if gte: date_bounds["gte"] = _iso(params[int(gte.group(1)) - 1]) @@ -109,6 +109,8 @@ def _reconstruct_ui_where_from_sql(sql_query, params): where["OR"] = where.get("OR", []) + [{"multi_team": True}] elif "status = 'success'" in cond: where["OR"] = where.get("OR", []) + [{"status": "success"}] + elif sess: + where["session_id"] = {"contains": str(params[int(sess.group(1)) - 1]).strip("%")} elif status: where["status"] = {"equals": params[int(status.group(1)) - 1]} elif alias: @@ -165,16 +167,14 @@ def make_ui_spend_logs_mock_prisma(mock_spend_logs, filter_fn, team_lookup_fn=No return len(filter_fn(kwargs.get("where", {}))) async def group_by(self, by, where, count): - allowed = set(where["session_id"]["in"]) + col = by[0] + allowed = where.get(col, {}).get("in") tallied = collections.Counter( - log["session_id"] + log[col] for log in mock_spend_logs - if log.get("session_id") in allowed + if log.get(col) is not None and (allowed is None or log[col] in allowed) ) - return [ - {"session_id": sid, "_count": {"session_id": n}} - for sid, n in tallied.items() - ] + return [{col: value, "_count": {col: n}} for value, n in tallied.items()] async def query_raw(self, sql_query, *params): if "mcp_tool_call_count" in sql_query: @@ -614,48 +614,47 @@ async def test_ui_view_spend_logs_with_user_id(client, monkeypatch): @pytest.mark.asyncio -async def test_ui_view_spend_logs_with_session_id(client, monkeypatch): - mock_spend_logs = [ - { - "id": "log1", - "request_id": "req1", +@pytest.mark.parametrize( + "session_id_query,expected_request_ids", + [ + ("session-filter-demo-1", {"req1", "req2"}), + ("session-filter-demo-2", {"req3"}), + ("session-filter", {"req1", "req2", "req3"}), + ("demo", {"req1", "req2", "req3"}), + ("no-such-session", set()), + ], +) +async def test_ui_view_spend_logs_with_session_id( + client, monkeypatch, session_id_query, expected_request_ids +): + def make_log(request_id, session_id): + return { + "id": f"log-{request_id}", + "request_id": request_id, "api_key": "sk-test-key", "user": "test_user_1", - "session_id": "session-abc", + "session_id": session_id, "spend": 0.05, "startTime": datetime.datetime.now(timezone.utc).isoformat(), "model": "gpt-4", - }, - { - "id": "log2", - "request_id": "req2", - "api_key": "sk-test-key", - "user": "test_user_1", - "session_id": "session-abc", - "spend": 0.10, - "startTime": datetime.datetime.now(timezone.utc).isoformat(), - "model": "gpt-4", - }, - { - "id": "log3", - "request_id": "req3", - "api_key": "sk-test-key", - "user": "test_user_1", - "session_id": "session-other", - "spend": 0.02, - "startTime": datetime.datetime.now(timezone.utc).isoformat(), - "model": "gpt-4", - }, + } + + mock_spend_logs = [ + make_log("req1", "session-filter-demo-1"), + make_log("req2", "session-filter-demo-1"), + make_log("req3", "session-filter-demo-2"), + make_log("req4", "unrelated-abc"), ] def filter_by_session(where): - if "session_id" in where: - return [ - log - for log in mock_spend_logs - if log["session_id"] == where["session_id"] - ] - return mock_spend_logs + session_filter = where.get("session_id") + if session_filter is None: + return mock_spend_logs + return [ + log + for log in mock_spend_logs + if session_filter["contains"] in log["session_id"] + ] monkeypatch.setattr( "litellm.proxy.proxy_server.prisma_client", @@ -667,7 +666,7 @@ async def test_ui_view_spend_logs_with_session_id(client, monkeypatch): response = client.get( "/spend/logs/ui", params={ - "session_id": "session-abc", + "session_id": session_id_query, "start_date": start_date, "end_date": end_date, }, @@ -676,9 +675,9 @@ async def test_ui_view_spend_logs_with_session_id(client, monkeypatch): assert response.status_code == 200 data = response.json() - assert data["total"] == 2 - assert {log["request_id"] for log in data["data"]} == {"req1", "req2"} - assert all(log["session_id"] == "session-abc" for log in data["data"]) + assert data["total"] == len(expected_request_ids) + assert {log["request_id"] for log in data["data"]} == expected_request_ids + assert all(session_id_query in log["session_id"] for log in data["data"]) # Mock spend logs with distinct values for sorting tests. diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts index 8e26729aa18..4ca2f85be2b 100644 --- a/ui/litellm-dashboard/src/lib/http/schema.d.ts +++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts @@ -48633,7 +48633,7 @@ export interface operations { user_id?: string | null; /** @description request_id to get spend logs for specific request_id */ request_id?: string | null; - /** @description Filter spend logs by session_id */ + /** @description Filter spend logs by session_id (partial string match) */ session_id?: string | null; /** @description Filter spend logs by team_id */ team_id?: string | null; @@ -48741,7 +48741,7 @@ export interface operations { user_id?: string | null; /** @description request_id to get spend logs for specific request_id */ request_id?: string | null; - /** @description Filter spend logs by session_id */ + /** @description Filter spend logs by session_id (partial string match) */ session_id?: string | null; /** @description Filter spend logs by team_id */ team_id?: string | null; From 7801324ab0c06b1997010c14aed18b6a2d8fdce4 Mon Sep 17 00:00:00 2001 From: Thibault Serot Date: Thu, 9 Jul 2026 15:33:11 +1000 Subject: [PATCH 3/4] fix(spend): guard session_id filter against non-str query default --- litellm/proxy/spend_tracking/spend_management_endpoints.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/litellm/proxy/spend_tracking/spend_management_endpoints.py b/litellm/proxy/spend_tracking/spend_management_endpoints.py index 0bf35352d5f..bce2e3581b5 100644 --- a/litellm/proxy/spend_tracking/spend_management_endpoints.py +++ b/litellm/proxy/spend_tracking/spend_management_endpoints.py @@ -1910,7 +1910,7 @@ async def ui_view_spend_logs( p += 2 sql_conditions.append(or_clause) - if session_id is not None: + if session_id is not None and isinstance(session_id, str): like_escaped_session_id = session_id.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") sql_conditions.append(f"session_id LIKE ${p}") sql_params.append(f"%{like_escaped_session_id}%") From 8a44fdd66321f26d87223112487e72e0a8c24e27 Mon Sep 17 00:00:00 2001 From: Thibault Serot Date: Thu, 9 Jul 2026 15:53:13 +1000 Subject: [PATCH 4/4] chore(ui): refresh eslint metrics for rebased base --- ui/litellm-dashboard/eslint-metrics.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/litellm-dashboard/eslint-metrics.json b/ui/litellm-dashboard/eslint-metrics.json index ded6ab97e1e..37bad071081 100644 --- a/ui/litellm-dashboard/eslint-metrics.json +++ b/ui/litellm-dashboard/eslint-metrics.json @@ -1,7 +1,7 @@ { "@typescript-eslint/no-explicit-any": 1982, "complexity": 128, - "local/no-large-inline-object-arg": 512, + "local/no-large-inline-object-arg": 519, "local/no-long-condition-chain": 233, "max-depth": 59, "no-console": 15