From fa11ca3fb04b65bde9f63c9c38be4a6ead515c68 Mon Sep 17 00:00:00 2001 From: Ishaan Jaffer Date: Wed, 11 Feb 2026 19:34:23 -0800 Subject: [PATCH] UI fix --- .../spend_management_endpoints.py | 4 ++++ .../src/components/view_logs/index.tsx | 24 ++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/litellm/proxy/spend_tracking/spend_management_endpoints.py b/litellm/proxy/spend_tracking/spend_management_endpoints.py index b46deacc1cc..918eb9cf578 100644 --- a/litellm/proxy/spend_tracking/spend_management_endpoints.py +++ b/litellm/proxy/spend_tracking/spend_management_endpoints.py @@ -3146,6 +3146,10 @@ async def _build_ui_spend_logs_response( {row.session_id for row in data if getattr(row, "session_id", None)} ) if session_ids: + # NOTE: This GROUP BY runs on every v1/UI page load. The IN clause + # is bounded by page_size (typically 25-50 distinct session IDs). + # If performance degrades at scale, consider short-lived caching or + # folding the count into the main query via a window function. counts = await prisma_client.db.litellm_spendlogs.group_by( by=["session_id"], where={"session_id": {"in": session_ids}}, diff --git a/ui/litellm-dashboard/src/components/view_logs/index.tsx b/ui/litellm-dashboard/src/components/view_logs/index.tsx index d7d9e65ed3f..a247a1be4a3 100644 --- a/ui/litellm-dashboard/src/components/view_logs/index.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/index.tsx @@ -331,6 +331,18 @@ export default function SpendLogsTable({ return acc; }, {}); + // Build a single-pass map of session_id → representative request_id. + // Prefers an LLM row over an MCP row as the representative. + const sessionRepresentativeMap = new Map(); + for (const log of searchedLogs) { + if (!log.session_id || (log.session_total_count || 1) <= 1) continue; + const isMcp = MCP_CALL_TYPES.includes(log.call_type); + const existing = sessionRepresentativeMap.get(log.session_id); + if (!existing || (existing.isMcp && !isMcp)) { + sessionRepresentativeMap.set(log.session_id, { requestId: log.request_id, isMcp }); + } + } + const filteredData = searchedLogs .map((log) => { @@ -350,16 +362,10 @@ export default function SpendLogsTable({ }, }; }) - // Deduplicate multi-call sessions: - // Prefer showing an LLM row as the root representative when available. - .filter((log, _index, arr) => { + // Deduplicate multi-call sessions using the pre-built map (O(1) per row). + .filter((log) => { if (!log.session_id || (log.session_total_count || 1) <= 1) return true; - const rowsForSession = arr.filter((l) => l.session_id === log.session_id); - const llmRepresentative = rowsForSession.find( - (l) => !MCP_CALL_TYPES.includes(l.call_type), - ); - const representative = llmRepresentative || rowsForSession[0]; - return representative.request_id === log.request_id; + return sessionRepresentativeMap.get(log.session_id)?.requestId === log.request_id; }) || []; // Add this function to handle manual refresh