diff --git a/litellm/proxy/spend_tracking/spend_management_endpoints.py b/litellm/proxy/spend_tracking/spend_management_endpoints.py index a319535f725..93634da7f83 100644 --- a/litellm/proxy/spend_tracking/spend_management_endpoints.py +++ b/litellm/proxy/spend_tracking/spend_management_endpoints.py @@ -2339,7 +2339,7 @@ def _build_spend_log_search_condition( f"(request_id = {raw} OR (" f"\"startTime\" >= ({window_start}::timestamptz AT TIME ZONE 'UTC') " f"AND \"startTime\" <= ({window_end}::timestamptz AT TIME ZONE 'UTC') " - f'AND (api_key = {raw} OR team_id = {raw} OR "user" = {raw} OR end_user = {raw} ' + f'AND (litellm_call_id = {raw} OR api_key = {raw} OR team_id = {raw} OR "user" = {raw} OR end_user = {raw} ' f"OR session_id = {raw} OR model_id = {raw})))" ) return _SpendLogSearchCondition(sql=sql, params=(search, start_date, end_date)) @@ -2448,7 +2448,7 @@ async def ui_view_spend_logs( search: str | None = fastapi.Query( default=None, description=( - "Match a log whose request_id, api_key (hash), team_id, user, end_user, " + "Match a log whose request_id, litellm_call_id, api_key (hash), team_id, user, end_user, " "session_id, or model_id equals this value. request_id matches across all time; the other columns " "match inside start_date/end_date, which stay required" ), diff --git a/tests/e2e/ui/helpers/traffic.ts b/tests/e2e/ui/helpers/traffic.ts index cb68747b364..fbc4488387c 100644 --- a/tests/e2e/ui/helpers/traffic.ts +++ b/tests/e2e/ui/helpers/traffic.ts @@ -51,6 +51,24 @@ export async function sendChatCompletion(request: APIRequestContext, opts: ChatO return body.id as string; } +export interface ServedChat { + /** The completion id, which the Logs table calls Request ID. */ + requestId: string; + /** The proxy's own correlation id, returned as x-litellm-call-id and used for OTEL traces. */ + callId: string; +} + +/** POST /v1/chat/completions and return both the completion id and the proxy's x-litellm-call-id. */ +export async function sendChatCompletionWithCallId(request: APIRequestContext, opts: ChatOptions): Promise { + const res = await postChatCompletion(request, opts); + expect(res.ok(), `chat completion for ${opts.model} failed (${res.status()}): ${await res.text()}`).toBe(true); + const callId = res.headers()["x-litellm-call-id"]; + expect(callId, "proxy did not return an x-litellm-call-id header").toBeTruthy(); + const body = await res.json(); + expect(body.choices?.[0]?.message?.content).toContain(MOCK_RESPONSE_TEXT); + return { requestId: body.id as string, callId }; +} + export interface ChatAttempt { status: number; body: string; diff --git a/tests/e2e/ui/tests/logs/logs.spec.ts b/tests/e2e/ui/tests/logs/logs.spec.ts index 2748c91395f..b7996dce124 100644 --- a/tests/e2e/ui/tests/logs/logs.spec.ts +++ b/tests/e2e/ui/tests/logs/logs.spec.ts @@ -6,6 +6,7 @@ import { CHAT_MODEL_A, MOCK_RESPONSE_TEXT, sendChatCompletion, + sendChatCompletionWithCallId, waitForSpendLog, waitForSpendLogByPrompt, } from "../../helpers/traffic"; @@ -95,6 +96,33 @@ test.describe("Logs page", () => { await expect(drawer.getByText(MOCK_RESPONSE_TEXT, { exact: false }).first()).toBeVisible({ timeout: 20_000 }); }); + test("a served request's Logs row and drawer show its x-litellm-call-id", async ({ page, request }) => { + const prompt = `logs-call-id-prompt-${uniqueSuffix()}`; + const { requestId, callId } = await sendChatCompletionWithCallId(request, { + model: CHAT_MODEL_A, + prompt, + }); + expect(callId, "call id must differ from the provider response id for this check to mean anything").not.toBe(requestId); + await waitForSpendLog(request, requestId); + + await navigateToPage(page, Page.Logs); + await dismissFeedbackPopup(page); + const search = visibleTestId(page, "datatable-search"); + await expect(search).toBeVisible({ timeout: 20_000 }); + await search.fill(callId); + + // Searching by the header value finds the row; the row itself must show that same id so it can be + // correlated with the OTEL trace by eye. + const row = requestLogsRows(page).filter({ hasText: requestId }); + await expect(row, `no logs row for call id ${callId}`).toHaveCount(1, { timeout: 30_000 }); + await expect(row, `Logs row for ${requestId} does not display its x-litellm-call-id ${callId}`).toContainText(callId); + + await row.click(); + const drawer = page.getByRole("dialog").first(); + await expect(drawer.getByText("Request & Response")).toBeVisible({ timeout: 20_000 }); + await expect(drawer.getByText(callId, { exact: false }).first(), `drawer does not show x-litellm-call-id ${callId}`).toBeVisible({ timeout: 10_000 }); + }); + // Split out because only the copy path needs a secure context; folding it in would // take the drawer-rendering coverage down with it. test("the drawer copies the request and the response to the clipboard", async ({ page, request }) => { 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 9de6679472e..60301a9a584 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 @@ -59,7 +59,7 @@ def _filter_logs_by_date_range(logs, where): _SEARCH_CLAUSE_RE = re.compile( r'\(request_id = \$(\d+) OR \("startTime" >= \(\$(\d+)::timestamptz AT TIME ZONE \'UTC\'\) ' r'AND "startTime" <= \(\$(\d+)::timestamptz AT TIME ZONE \'UTC\'\) ' - r'AND \(api_key = \$\1 OR team_id = \$\1 OR "user" = \$\1 OR end_user = \$\1 ' + r'AND \(litellm_call_id = \$\1 OR api_key = \$\1 OR team_id = \$\1 OR "user" = \$\1 OR end_user = \$\1 ' r"OR session_id = \$\1 OR model_id = \$\1\)\)\)" ) @@ -70,7 +70,7 @@ def _matches_spend_log_search(log, search): return True if not _filter_logs_by_date_range([log], {"startTime": {"gte": search["gte"], "lte": search["lte"]}}): return False - columns = ("api_key", "team_id", "user", "end_user", "session_id", "model_id") + columns = ("litellm_call_id", "api_key", "team_id", "user", "end_user", "session_id", "model_id") return any(log.get(col) == search["value"] for col in columns) @@ -3043,7 +3043,7 @@ def test_build_spend_log_search_condition_windows_every_branch_except_request_id assert condition.sql == ( "(request_id = $3 OR (\"startTime\" >= ($4::timestamptz AT TIME ZONE 'UTC') " "AND \"startTime\" <= ($5::timestamptz AT TIME ZONE 'UTC') " - 'AND (api_key = $3 OR team_id = $3 OR "user" = $3 OR end_user = $3 OR session_id = $3 OR model_id = $3)))' + 'AND (litellm_call_id = $3 OR api_key = $3 OR team_id = $3 OR "user" = $3 OR end_user = $3 OR session_id = $3 OR model_id = $3)))' ) assert condition.params == ("key-hash-7", start, end) @@ -3069,6 +3069,8 @@ def _search_fixture_logs(today): {**base, "request_id": "req-user", "user": "user-7", "startTime": recent}, {**base, "request_id": "req-end-user", "end_user": "cust-7", "startTime": recent}, {**base, "request_id": "req-model", "model_id": "mdl-7", "startTime": recent}, + {**base, "request_id": "chatcmpl-x", "litellm_call_id": "call-recent", "startTime": recent}, + {**base, "request_id": "chatcmpl-old", "litellm_call_id": "call-old", "startTime": old}, ] @@ -3103,6 +3105,8 @@ def _five_day_window(today): ("user-7", {"req-user"}), ("cust-7", {"req-end-user"}), ("mdl-7", {"req-model"}), + ("call-recent", {"chatcmpl-x"}), + ("call-old", set()), ("no-such-id", set()), ], ) diff --git a/ui/litellm-dashboard/src/components/view_logs/LogDetailsDrawer/DrawerHeader.test.tsx b/ui/litellm-dashboard/src/components/view_logs/LogDetailsDrawer/DrawerHeader.test.tsx index a8e27504019..f51cbf05e47 100644 --- a/ui/litellm-dashboard/src/components/view_logs/LogDetailsDrawer/DrawerHeader.test.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/LogDetailsDrawer/DrawerHeader.test.tsx @@ -58,6 +58,23 @@ describe("DrawerHeader sidebar toggle", () => { expect(within(row).getByText("gpt-4o")).toBeInTheDocument(); }); + it("shows the x-litellm-call-id with its own copy button when it differs from the request id", () => { + renderHeader(logEntry({ request_id: "chatcmpl-h", litellm_call_id: "call-h" }), false); + + expect(screen.getByText("chatcmpl-h")).toBeInTheDocument(); + expect(screen.getByText("call-h")).toBeInTheDocument(); + expect(screen.getByText("call id")).toBeInTheDocument(); + expect(screen.getByRole("button", { name: "Copy Request ID" })).toBeInTheDocument(); + expect(screen.getByRole("button", { name: "Copy Call ID" })).toBeInTheDocument(); + }); + + it("omits the call id caption and button when the ids match", () => { + renderHeader(logEntry({ request_id: "same-h", litellm_call_id: "same-h" }), false); + + expect(screen.queryByText("call id")).not.toBeInTheDocument(); + expect(screen.queryByRole("button", { name: "Copy Call ID" })).not.toBeInTheDocument(); + }); + it("falls back to the request id row when the log names no model", () => { renderHeader(logEntry({ model: "", custom_llm_provider: "" }), true); diff --git a/ui/litellm-dashboard/src/components/view_logs/LogDetailsDrawer/DrawerHeader.tsx b/ui/litellm-dashboard/src/components/view_logs/LogDetailsDrawer/DrawerHeader.tsx index 65b5801602c..429208fb747 100644 --- a/ui/litellm-dashboard/src/components/view_logs/LogDetailsDrawer/DrawerHeader.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/LogDetailsDrawer/DrawerHeader.tsx @@ -50,6 +50,8 @@ export function DrawerHeader({ const providerInfo = provider ? getProviderLogoAndName(provider) : null; const showToggleWithProvider = isSidebarCollapsed && Boolean(providerInfo || log.model); const showToggleWithRequestId = isSidebarCollapsed && !showToggleWithProvider; + const callId: string | null = + log.litellm_call_id && log.litellm_call_id !== log.request_id ? log.litellm_call_id : null; return (
{showToggleWithRequestId && } - +
+ + {callId && ( +
+ + call id + + +
+ )} +
@@ -143,12 +155,22 @@ function ModelProviderSection({ /** * Request ID display with copy functionality */ -function RequestIdSection({ requestId }: { requestId: string }) { +function CopyableId({ + value, + label, + fontSize, + muted, +}: { + value: string; + label: string; + fontSize: number; + muted?: boolean; +}) { const [copied, setCopied] = useState(false); const handleCopy = async () => { try { - await navigator.clipboard.writeText(requestId); + await navigator.clipboard.writeText(value); setCopied(true); setTimeout(() => setCopied(false), 1200); } catch { @@ -157,38 +179,36 @@ function RequestIdSection({ requestId }: { requestId: string }) { }; return ( -
- - - - } + + + + } + > + {value} + - - {requestId} - - -
+ {copied ? : } + + + {value} + + ); } diff --git a/ui/litellm-dashboard/src/components/view_logs/RequestLogsTableColumns.test.tsx b/ui/litellm-dashboard/src/components/view_logs/RequestLogsTableColumns.test.tsx index 4d451a9c7b9..c9ce833c79d 100644 --- a/ui/litellm-dashboard/src/components/view_logs/RequestLogsTableColumns.test.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/RequestLogsTableColumns.test.tsx @@ -267,10 +267,35 @@ describe("batch rows", () => { }); it("leaves ordinary request ids untouched", () => { - renderRows([logEntry({ request_id: "chatcmpl-42" })]); + renderRows([logEntry({ request_id: "chatcmpl-42", litellm_call_id: "chatcmpl-42" })]); expect(screen.getByText("chatcmpl-42")).toBeInTheDocument(); expect(screen.queryByText("batch cost")).not.toBeInTheDocument(); + expect(screen.queryByText("call id")).not.toBeInTheDocument(); + }); +}); + +describe("Request ID column", () => { + it("shows the x-litellm-call-id under the request id when they differ", () => { + renderRows([logEntry({ request_id: "chatcmpl-9", litellm_call_id: "call-uuid-9" })]); + + expect(screen.getByText("chatcmpl-9")).toBeInTheDocument(); + expect(screen.getByText("call-uuid-9")).toBeInTheDocument(); + expect(screen.getByText("call id")).toBeInTheDocument(); + }); + + it("shows the id once when request id and call id are the same", () => { + renderRows([logEntry({ request_id: "same-id-7", litellm_call_id: "same-id-7" })]); + + expect(screen.getAllByText("same-id-7")).toHaveLength(1); + expect(screen.queryByText("call id")).not.toBeInTheDocument(); + }); + + it("shows only the request id when the row carries no call id", () => { + renderRows([logEntry({ request_id: "chatcmpl-no-call", litellm_call_id: null })]); + + expect(screen.getByText("chatcmpl-no-call")).toBeInTheDocument(); + expect(screen.queryByText("call id")).not.toBeInTheDocument(); }); }); diff --git a/ui/litellm-dashboard/src/components/view_logs/RequestLogsTableColumns.tsx b/ui/litellm-dashboard/src/components/view_logs/RequestLogsTableColumns.tsx index 5705f41f3de..51584257620 100644 --- a/ui/litellm-dashboard/src/components/view_logs/RequestLogsTableColumns.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/RequestLogsTableColumns.tsx @@ -160,6 +160,20 @@ export const getRequestLogsTableColumns = ({ ); } + if (log.litellm_call_id && log.litellm_call_id !== log.request_id) { + return ( +
+ + + call id +
+ ); + } return ; }, },