🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(ai-gateway): show last used on gateway keys (#19544)
Hassieb Pakzad70b6f908f962
diff --git a/web/src/__tests__/server/ai-gateway-api-key-last-used.servertest.ts b/web/src/__tests__/server/ai-gateway-api-key-last-used.servertest.tsnew file mode 100644index 0000000000..7faad8342a--- /dev/null+++ b/web/src/__tests__/server/ai-gateway-api-key-last-used.servertest.ts@@ -0,0 +1,280 @@+import { randomUUID } from "node:crypto";+import type { Session } from "next-auth";+import { afterEach, describe, expect, it } from "vitest";++import { testFeatureFlags } from "@/src/__tests__/fixtures/feature-flags";+import { env } from "@/src/env.mjs";+import { appRouter } from "@/src/server/api/root";+import { createInnerTRPCContext } from "@/src/server/api/trpc";+import { AI_GATEWAY_SERVICE_NAME } from "@langfuse/shared";+import { prisma, Role } from "@langfuse/shared/src/db";+import { createEvent, createEventsCh } from "@langfuse/shared/src/server";++const HOUR_MS = 60 * 60 * 1000;+const DAY_MS = 24 * HOUR_MS;++const cleanupOrganizations: string[] = [];+const cleanupUsers: string[] = [];+const originalGatewayOrganizationAllowlist = [+ ...env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST,+];+const originalDeploymentEnv = {+ NEXT_PUBLIC_LANGFUSE_CLOUD_REGION: env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION,+ LANGFUSE_AI_GATEWAY_DEPLOYED: env.LANGFUSE_AI_GATEWAY_DEPLOYED,+};++afterEach(async () => {+ await prisma.organization.deleteMany({+ where: { id: { in: cleanupOrganizations.splice(0) } },+ });+ await prisma.user.deleteMany({+ where: { id: { in: cleanupUsers.splice(0) } },+ });+ env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST.splice(+ 0,+ env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST.length,+ ...originalGatewayOrganizationAllowlist,+ );+ Object.assign(env, originalDeploymentEnv);+});++async function prepareOrganization() {+ const suffix = randomUUID();+ const org = await prisma.organization.create({+ data: { name: `Gateway last used ${suffix}` },+ });+ cleanupOrganizations.push(org.id);+ env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST.push(org.id);+ const [ingestionProject, otherProject] = await Promise.all([+ prisma.project.create({ data: { name: "Ingestion", orgId: org.id } }),+ prisma.project.create({ data: { name: "Other", orgId: org.id } }),+ ]);+ return { org, ingestionProject, otherProject };+}++async function callerFor(+ org: { id: string; name: string },+ role: Role,+): Promise<ReturnType<typeof appRouter.createCaller>> {+ const user = await prisma.user.create({+ data: {+ email: `gateway-last-used-${randomUUID()}@example.test`,+ name: `Gateway ${role.toLowerCase()}`,+ },+ });+ cleanupUsers.push(user.id);+ await prisma.organizationMembership.create({+ data: { orgId: org.id, userId: user.id, role },+ });+ const session: Session = {+ expires: "1",+ user: {+ id: user.id,+ name: user.name,+ canCreateOrganizations: true,+ featureFlags: testFeatureFlags({ templateFlag: false }),+ organizations: [+ {+ id: org.id,+ name: org.name,+ role,+ cloudConfig: undefined,+ plan: "oss",+ metadata: {},+ aiFeaturesEnabled: false,+ aiTelemetryEnabled: true,+ projects: [],+ },+ ],+ },+ environment: {} as Session["environment"],+ };+ const ctx = createInnerTRPCContext({ session, headers: {} });+ return appRouter.createCaller({ ...ctx, prisma });+}++function gatewayRequest(params: {+ projectId: string;+ apiKeyId: string;+ at: Date;+ serviceName?: string;+}) {+ const id = randomUUID();+ return createEvent({+ id,+ span_id: id,+ trace_id: randomUUID(),+ project_id: params.projectId,+ type: "GENERATION",+ start_time: params.at.getTime() * 1000,+ ingestion_api_key: params.apiKeyId,+ service_name: params.serviceName ?? AI_GATEWAY_SERVICE_NAME,+ });+}++describe("aiGateway.getApiKeysLastUsed", () => {+ it("returns the latest gateway request per key from the ingestion project in the last 7 days", async () => {+ const { org, ingestionProject, otherProject } = await prepareOrganization();+ const owner = await callerFor(org, Role.OWNER);+ await prisma.gatewayConfig.create({+ data: {+ organizationId: org.id,+ defaultIngestionProjectId: ingestionProject.id,+ },+ });+ const createKey = (name: string) =>+ owner.aiGateway.createApiKey({ orgId: org.id, name, metadata: {} });+ const recentKey = await createKey("Recent");+ const staleKey = await createKey("Stale");+ const sdkOnlyKey = await createKey("SDK only");+ const now = Date.now();+ const latest = new Date(now - HOUR_MS);+ await createEventsCh([+ gatewayRequest({+ projectId: ingestionProject.id,+ apiKeyId: recentKey!.id,+ at: new Date(now - 2 * DAY_MS),+ }),+ gatewayRequest({+ projectId: ingestionProject.id,+ apiKeyId: recentKey!.id,+ at: latest,+ }),+ // Newer traffic outside the ingestion project must not count.+ gatewayRequest({+ projectId: otherProject.id,+ apiKeyId: recentKey!.id,+ at: new Date(now - 60_000),+ }),+ gatewayRequest({+ projectId: ingestionProject.id,+ apiKeyId: staleKey!.id,+ at: new Date(now - 8 * DAY_MS),+ }),+ gatewayRequest({+ projectId: ingestionProject.id,+ apiKeyId: sdkOnlyKey!.id,+ at: latest,+ serviceName: "my-sdk-app",+ }),+ ]);++ const result = await owner.aiGateway.getApiKeysLastUsed({+ orgId: org.id,+ apiKeyIds: [recentKey!.id, staleKey!.id, sdkOnlyKey!.id],+ });++ expect(result).toEqual({+ isTracked: true,+ apiKeys: [{ id: recentKey!.id, lastUsedAt: latest }],+ });+ });++ it("drops key ids the caller cannot list and rejects callers who cannot list keys", async () => {+ const { org, ingestionProject } = await prepareOrganization();+ const owner = await callerFor(org, Role.OWNER);+ const member = await callerFor(org, Role.MEMBER);+ const viewer = await callerFor(org, Role.VIEWER);+ await prisma.gatewayConfig.create({+ data: {+ organizationId: org.id,+ defaultIngestionProjectId: ingestionProject.id,+ },+ });+ const ownerKey = await owner.aiGateway.createApiKey({+ orgId: org.id,+ name: "Owner key",+ metadata: {},+ });+ const memberKey = await member.aiGateway.createApiKey({+ orgId: org.id,+ name: "Member key",+ metadata: {},+ });+ const at = new Date(Date.now() - HOUR_MS);+ await createEventsCh(+ [ownerKey, memberKey].map((key) =>+ gatewayRequest({+ projectId: ingestionProject.id,+ apiKeyId: key.id,+ at,+ }),+ ),+ );+ const apiKeyIds = [ownerKey.id, memberKey.id];++ const memberResult = await member.aiGateway.getApiKeysLastUsed({+ orgId: org.id,+ apiKeyIds,+ });+ expect(memberResult.apiKeys).toEqual([+ { id: memberKey.id, lastUsedAt: at },+ ]);++ const otherOrg = await prepareOrganization();+ const outsider = await callerFor(otherOrg.org, Role.OWNER);+ await expect(+ outsider.aiGateway.getApiKeysLastUsed({ orgId: org.id, apiKeyIds }),+ ).rejects.toMatchObject({ code: "UNAUTHORIZED" });+ await expect(+ viewer.aiGateway.getApiKeysLastUsed({ orgId: org.id, apiKeyIds }),+ ).rejects.toMatchObject({ code: "FORBIDDEN" });+ });++ it("reports usage as untracked in ingestion mode NONE or without an active ingestion project", async () => {+ const { org, ingestionProject } = await prepareOrganization();+ const owner = await callerFor(org, Role.OWNER);+ const key = await owner.aiGateway.createApiKey({+ orgId: org.id,+ name: "Key",+ metadata: {},+ });+ await createEventsCh([+ gatewayRequest({+ projectId: ingestionProject.id,+ apiKeyId: key.id,+ at: new Date(Date.now() - HOUR_MS),+ }),+ ]);+ const input = { orgId: org.id, apiKeyIds: [key.id] };+ const untracked = { isTracked: false, apiKeys: [] };++ await expect(owner.aiGateway.getApiKeysLastUsed(input)).resolves.toEqual(+ untracked,+ );++ // NONE keeps a linked project. Cloud runs a stored NONE as USAGE, so+ // traffic is still recorded there; self-hosted sends none.+ await prisma.gatewayConfig.create({+ data: {+ organizationId: org.id,+ defaultIngestionProjectId: ingestionProject.id,+ ingestionMode: "NONE",+ },+ });+ Object.assign(env, { NEXT_PUBLIC_LANGFUSE_CLOUD_REGION: "DEV" });+ await expect(+ owner.aiGateway.getApiKeysLastUsed(input),+ ).resolves.toMatchObject({ isTracked: true, apiKeys: [{ id: key.id }] });++ Object.assign(env, {+ NEXT_PUBLIC_LANGFUSE_CLOUD_REGION: undefined,+ LANGFUSE_AI_GATEWAY_DEPLOYED: "true",+ });+ await expect(owner.aiGateway.getApiKeysLastUsed(input)).resolves.toEqual(+ untracked,+ );++ await prisma.gatewayConfig.update({+ where: { organizationId: org.id },+ data: { ingestionMode: "USAGE" },+ });+ await prisma.project.update({+ where: { id: ingestionProject.id },+ data: { deletedAt: new Date() },+ });+ await expect(owner.aiGateway.getApiKeysLastUsed(input)).resolves.toEqual(+ untracked,+ );+ });+});diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/ConnectedGatewayApiKeysTable.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/ConnectedGatewayApiKeysTable.tsxindex b3975d4a6d..1d1163ffc9 100644--- a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/ConnectedGatewayApiKeysTable.tsx+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/ConnectedGatewayApiKeysTable.tsx@@ -1,5 +1,4 @@import { useState } from "react";-import { keepPreviousData } from "@tanstack/react-query";import { Alert } from "@/src/components/design-system/Alert/Alert";import { Button } from "@/src/components/ui/button";@@ -8,6 +7,7 @@ import { TeamInviteDialogController } from "@/src/features/ai-gateway/componentsimport { ConfirmationDialogController } from "@/src/components/design-system/ConfirmationDialogController/ConfirmationDialogController";import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";import { usePaginationState } from "@/src/hooks/usePaginationState";+import { useGatewayApiKeysTableData } from "@/src/features/ai-gateway/hooks/useGatewayApiKeysTableData";import { api } from "@/src/utils/api";import { GatewayApiKeysTable } from "./GatewayApiKeysTable";@@ -24,15 +24,12 @@ export function ConnectedGatewayApiKeysTable({limit: "pageSize",});const [searchQuery, setSearchQuery] = useState("");- const apiKeysQuery = api.aiGateway.listApiKeys.useQuery(- {- orgId: organizationId,- searchQuery,- page: paginationState.pageIndex,- limit: paginationState.pageSize,- },- { placeholderData: keepPreviousData },- );+ const { apiKeysQuery, rows, lastUsed } = useGatewayApiKeysTableData({+ organizationId,+ searchQuery,+ pageIndex: paginationState.pageIndex,+ pageSize: paginationState.pageSize,+ });const utils = api.useUtils();const capture = usePostHogClientCapture();const revoke = api.aiGateway.revokeApiKey.useMutation();@@ -95,13 +92,11 @@ export function ConnectedGatewayApiKeysTable({return (<GatewayApiKeysTabledata={- apiKeysQuery.isPending+ rows === undefined? { status: "loading" }- : {- status: "success",- data: apiKeysQuery.data.data,- }+ : { status: "success", data: rows }}+ lastUsed={lastUsed}pagination={{mode: "offset",totalCount: apiKeysQuery.data?.totalCount ?? null,diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.stories.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.stories.tsxindex b6099ab000..cf15fdb902 100644--- a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.stories.tsx+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.stories.tsx@@ -52,6 +52,7 @@ const manyApiKeys = Array.from({ length: 30 }, (_, index) => ({>["data"];const actions = {+ lastUsed: { status: "success", isTracked: true },createAction: onCreate,onRevoke,pagination: {@@ -63,9 +64,72 @@ const actions = {search: { value: "", onChange: fn() },} satisfies Pick<ComponentProps<typeof GatewayApiKeysTable>,- "createAction" | "onRevoke" | "pagination" | "search"+ "lastUsed" | "createAction" | "onRevoke" | "pagination" | "search">;+const lastUsedCell = (canvas: {+ getAllByRole: (role: "row") => HTMLElement[];+}) => canvas.getAllByRole("row")[1]!.querySelectorAll("td")[5]!;++export const LastUsedLoading = meta.story({+ name: "(Test) Last used shows a skeleton while loading",+ args: {+ data: { status: "success", data: apiKeys },+ ...actions,+ lastUsed: { status: "loading" },+ },+ play: async ({ canvas }) => {+ await expect(canvas.getByText("Production application")).toBeVisible();+ await expect(+ lastUsedCell(canvas).querySelector("[data-slot='skeleton']"),+ ).not.toBeNull();+ },+});++export const LastUsedRecently = meta.story({+ name: "(Test) Last used shows relative time with the full timestamp",+ args: {+ data: {+ status: "success",+ data: [+ {+ ...apiKeys[0]!,+ lastUsedAt: new Date(Date.now() - 2 * 60 * 60 * 1000),+ },+ ],+ },+ ...actions,+ },+ play: async ({ canvas }) => {+ const timestamp = lastUsedCell(canvas).querySelector("[title]")!;+ await expect(timestamp).toHaveTextContent(/^2 hours ago$/);+ await expect(timestamp.getAttribute("title")).toMatch(/\d{4}-\d{2}-\d{2}/);+ },+});++export const LastUsedNotInWindow = meta.story({+ name: "(Test) Last used reports keys without recent traffic",+ args: {+ data: { status: "success", data: apiKeys },+ ...actions,+ },+ play: async ({ canvas }) => {+ await expect(lastUsedCell(canvas)).toHaveTextContent("Not used (7d)");+ },+});++export const LastUsedUntracked = meta.story({+ name: "(Test) Last used is unavailable without gateway traffic storage",+ args: {+ data: { status: "success", data: apiKeys },+ ...actions,+ lastUsed: { status: "success", isTracked: false },+ },+ play: async ({ canvas }) => {+ await expect(lastUsedCell(canvas)).toHaveTextContent("Unavailable");+ },+});+export const PopulatedMetadata = meta.story({name: "(Test) Populated metadata excludes key data from recordings",args: {@@ -75,7 +139,7 @@ export const PopulatedMetadata = meta.story({play: async ({ canvas }) => {const cells = canvas.getAllByRole("row")[1]!.querySelectorAll("td");await expect(cells[1]).toHaveClass("ph-no-capture");- await expect(cells[5]).toHaveClass("ph-no-capture");+ await expect(cells[6]).toHaveClass("ph-no-capture");},});@@ -106,7 +170,7 @@ export const OverflowingMetadata = meta.story({play: async ({ canvas }) => {const metadataCell = canvas.getAllByRole("row")[1]!- .querySelectorAll("td")[5]!;+ .querySelectorAll("td")[6]!;await waitFor(() => {expect(metadataCell).toHaveTextContent(/\+\d+/);});diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.tsxindex 46c7a5ec39..8bb41fb154 100644--- a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.tsx+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.tsx@@ -1,4 +1,5 @@import { Plus, Trash, UserPlus } from "@langfuse/icons";+import { formatDistanceToNowStrict } from "date-fns";import { SettingsTable } from "@/src/components/SettingsTable/SettingsTable";import type { PaginationBarProps } from "@/src/components/design-system/PaginationBar/PaginationBar";@@ -6,9 +7,13 @@ import { createDateTableColumn } from "@/src/components/design-system/table/coluimport { createBadgeListTableColumn } from "@/src/components/design-system/table/columns/createBadgeListTableColumn";import { createTextTableColumn } from "@/src/components/design-system/table/columns/createTextTableColumn";import { createUserTableColumn } from "@/src/components/design-system/table/columns/createUserTableColumn";+import { createTableColumn } from "@/src/components/design-system/table/columns/utils/createTableColumn";+import { Skeleton } from "@/src/components/ui/skeleton";+import { buildLocalIsoDatePresentation } from "@/src/utils/dates";import type { AsyncTableData } from "@/src/components/design-system/table/Table";import type { LangfuseColumnDef } from "@/src/components/table/types";import { isApiKeyExpired } from "@/src/features/apiKey";+import { GATEWAY_API_KEY_LAST_USED_WINDOW_DAYS } from "@/src/features/ai-gateway/constants/gatewayApiKeyLastUsed";import { GatewayApiKeysEmptyState } from "@/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/components/GatewayApiKeysEmptyState/GatewayApiKeysEmptyState";type GatewayApiKey = {@@ -26,10 +31,21 @@ type GatewayApiKey = {image: string | null;} | null;};+ /** Absent until the last-used query resolves, and when the key had no gateway traffic in the window. */+ lastUsedAt?: Date;};+const lastUsedSkeleton = <Skeleton className="h-4 w-1/2" />;++/** `isTracked` is false when gateway traffic is not recorded (ingestion mode `NONE` or no active ingestion project). */+type GatewayApiKeysLastUsedState =+ | { status: "loading" }+ | { status: "error" }+ | { status: "success"; isTracked: boolean };+export function GatewayApiKeysTable({data,+ lastUsed,createAction,inviteAction,onRevoke,@@ -37,6 +53,7 @@ export function GatewayApiKeysTable({search,}: {data: AsyncTableData<GatewayApiKey[]>;+ lastUsed: GatewayApiKeysLastUsedState;createAction: () => void;/** Omit for callers who cannot invite people to the organization. */inviteAction?: () => void;@@ -87,6 +104,34 @@ export function GatewayApiKeysTable({getPrefix: (expiresAt) =>isApiKeyExpired(expiresAt) ? "Expired on " : null,}),+ createTableColumn<GatewayApiKey, Date>({+ accessorFn: (row) => row.lastUsedAt,+ id: "lastUsedAt",+ header: "Last used",+ hideBelowMd: true,+ enableResizing: false,+ loadingCell: lastUsedSkeleton,+ renderCell: (lastUsedAt) => {+ if (lastUsed.status === "loading") return lastUsedSkeleton;+ if (!lastUsedAt) {+ return (+ <span className="text-muted-foreground">+ {lastUsed.status === "success" && lastUsed.isTracked+ ? `Not used (${GATEWAY_API_KEY_LAST_USED_WINDOW_DAYS}d)`+ : "Unavailable"}+ </span>+ );+ }+ return (+ <span+ className="block w-full truncate"+ title={buildLocalIsoDatePresentation({ date: lastUsedAt })?.title}+ >+ {formatDistanceToNowStrict(lastUsedAt, { addSuffix: true })}+ </span>+ );+ },+ }),createBadgeListTableColumn<GatewayApiKey>({accessorFn: (row) => getMetadataEntries(row.metadata),id: "metadata",diff --git a/web/src/features/ai-gateway/constants/gatewayApiKeyLastUsed.ts b/web/src/features/ai-gateway/constants/gatewayApiKeyLastUsed.tsnew file mode 100644index 0000000000..2d8db87a01--- /dev/null+++ b/web/src/features/ai-gateway/constants/gatewayApiKeyLastUsed.ts@@ -0,0 +1,2 @@+/** How far back gateway traffic counts towards a gateway key's "Last used". */+export const GATEWAY_API_KEY_LAST_USED_WINDOW_DAYS = 7;diff --git a/web/src/features/ai-gateway/hooks/useGatewayApiKeysTableData.ts b/web/src/features/ai-gateway/hooks/useGatewayApiKeysTableData.tsnew file mode 100644index 0000000000..bb2527cad0--- /dev/null+++ b/web/src/features/ai-gateway/hooks/useGatewayApiKeysTableData.ts@@ -0,0 +1,57 @@+import { keepPreviousData } from "@tanstack/react-query";++import { joinTableCoreAndMetrics } from "@/src/components/table/utils/joinTableCoreAndMetrics";+import { api } from "@/src/utils/api";++/**+ * Gateway keys for one table page, then their last-used times for exactly+ * those keys. The key list never waits for ClickHouse: the last-used query is+ * unbatched and starts once the page's key ids are known.+ */+export function useGatewayApiKeysTableData({+ organizationId,+ searchQuery,+ pageIndex,+ pageSize,+}: {+ organizationId: string;+ searchQuery: string;+ pageIndex: number;+ pageSize: number;+}) {+ const apiKeysQuery = api.aiGateway.listApiKeys.useQuery(+ { orgId: organizationId, searchQuery, page: pageIndex, limit: pageSize },+ { placeholderData: keepPreviousData },+ );+ const lastUsedQuery = api.aiGateway.getApiKeysLastUsed.useQuery(+ {+ orgId: organizationId,+ apiKeyIds: apiKeysQuery.data?.data.map(({ apiKey }) => apiKey.id) ?? [],+ },+ {+ enabled: apiKeysQuery.isSuccess,+ trpc: { context: { skipBatch: true } },+ },+ );++ return {+ apiKeysQuery,+ rows: joinTableCoreAndMetrics(+ apiKeysQuery.data?.data.map((row) => ({ ...row, id: row.apiKey.id })),+ lastUsedQuery.data?.apiKeys,+ ).rows,+ lastUsed: toLastUsedState(lastUsedQuery),+ };+}++function toLastUsedState({+ data,+ isError,+}: {+ data?: { isTracked: boolean };+ isError: boolean;+}) {+ if (data) return { status: "success" as const, isTracked: data.isTracked };+ if (isError) return { status: "error" as const };+ return { status: "loading" as const };+}diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyLastUsedRepository.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyLastUsedRepository.tsnew file mode 100644index 0000000000..fb8e230650--- /dev/null+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyLastUsedRepository.ts@@ -0,0 +1,56 @@+import { AI_GATEWAY_SERVICE_NAME } from "@langfuse/shared";+import {+ convertDateToClickhouseDateTime,+ EventsAggQueryBuilder,+ parseClickhouseUTCDateTimeFormat,+ queryClickhouse,+} from "@langfuse/shared/src/server";++/**+ * Latest gateway request per gateway key in the gateway ingestion project.+ *+ * Gateway ingestion stores the calling gateway key id as `ingestion_api_key`+ * and `langfuse-ai-gateway` as `service_name`. `project_id` and the+ * `start_time` lower bound are the events_core primary key prefix and+ * partition key, so ClickHouse reads only the project's granules since+ * `since`. Keys without traffic in that window are absent from the result.+ */+export async function getGatewayApiKeysLastUsedAt(params: {+ projectId: string;+ apiKeyIds: string[];+ since: Date;+}) {+ if (params.apiKeyIds.length === 0) return [];++ const { query, params: queryParams } = new EventsAggQueryBuilder({+ projectId: params.projectId,+ groupByColumn: "e.ingestion_api_key",+ selectExpression:+ "e.ingestion_api_key as api_key_id, max(e.start_time) as last_used_at",+ })+ .whereRaw("e.start_time >= {since: DateTime64(3)}", {+ since: convertDateToClickhouseDateTime(params.since),+ })+ .whereRaw("e.service_name = {gatewayServiceName: String}", {+ gatewayServiceName: AI_GATEWAY_SERVICE_NAME,+ })+ .whereRaw("e.ingestion_api_key IN {apiKeyIds: Array(String)}", {+ apiKeyIds: params.apiKeyIds,+ })+ .buildWithParams();++ const rows = await queryClickhouse<{+ api_key_id: string;+ last_used_at: string;+ }>({+ query,+ params: queryParams,+ tags: { projectId: params.projectId },+ preferredClickhouseService: "EventsReadOnly",+ });++ return rows.map((row) => ({+ id: row.api_key_id,+ lastUsedAt: parseClickhouseUTCDateTimeFormat(row.last_used_at),+ }));+}diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.tsindex e469f632af..09c7706c2d 100644--- a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts@@ -48,6 +48,23 @@ export class GatewayApiKeyRepository {return { data, totalCount };}+ /** The subset of apiKeyIds that are gateway keys visible under the same scoping as the key list. */+ async filterVisibleGatewayApiKeyIds(params: {+ organizationId: string;+ ownerUserId?: string;+ apiKeyIds: string[];+ }) {+ if (params.apiKeyIds.length === 0) return [];+ const rows = await this.prisma.gatewayApiKeyAssociation.findMany({+ where: {+ apiKeyId: { in: params.apiKeyIds },+ apiKey: gatewayApiKeyWhere(params),+ },+ select: { apiKeyId: true },+ });+ return rows.map((row) => row.apiKeyId);+ }+getGatewayApiKey(params: {organizationId: string;ownerUserId?: string;diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.tsindex 90649b1280..3a091582c4 100644--- a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts@@ -22,18 +22,24 @@ import type { GatewayMetadata } from "@/src/features/ai-gateway/server/provider"import type { OrgAuthedContext } from "@/src/server/api/trpc";import { invalidateGatewayResolveCacheForApiKey } from "@/src/features/ai-gateway/server/resolve/gatewayResolveCache";import { hasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";+import { GATEWAY_API_KEY_LAST_USED_WINDOW_DAYS } from "@/src/features/ai-gateway/constants/gatewayApiKeyLastUsed";+import { getEffectiveGatewayIngestionMode } from "@/src/features/ai-gateway/fns/gatewayIngestionModePolicy";+import { GatewayConfigRepository } from "@/src/features/ai-gateway/server/config/gatewayConfigRepository";import { GatewayApiKeyRepository } from "./gatewayApiKeyRepository";+import { getGatewayApiKeysLastUsedAt } from "./gatewayApiKeyLastUsedRepository";type GatewayApiKeyIssuedAs = "user" | "serviceAccount";export class GatewayApiKeyService {private readonly repository: GatewayApiKeyRepository;+ private readonly configRepository: GatewayConfigRepository;constructor(private readonly prisma: PrismaClient,private readonly redis?: Redis | Cluster | null,) {this.repository = new GatewayApiKeyRepository(prisma);+ this.configRepository = new GatewayConfigRepository(prisma);}/** Without ownerUserId, lists every gateway key in the organization. */@@ -47,6 +53,44 @@ export class GatewayApiKeyService {return this.repository.listGatewayApiKeys(params);}+ /**+ * Latest gateway request per key in the last+ * GATEWAY_API_KEY_LAST_USED_WINDOW_DAYS days, read from the organization's+ * gateway ingestion project. Ids the caller cannot list are dropped.+ * `isTracked` is false when gateway traffic is not recorded: ingestion mode+ * `NONE` (which may keep a linked project) or no active ingestion project.+ */+ async getLastUsed(params: {+ organizationId: string;+ ownerUserId?: string;+ apiKeyIds: string[];+ }) {+ const config = await this.configRepository.getConfig(params.organizationId);+ const project = config?.defaultIngestionProject;+ if (+ !config ||+ getEffectiveGatewayIngestionMode(config.ingestionMode) === "NONE" ||+ !project ||+ project.deletedAt ||+ project.orgId !== params.organizationId+ ) {+ return { isTracked: false, apiKeys: [] };+ }+ const apiKeyIds =+ await this.repository.filterVisibleGatewayApiKeyIds(params);+ const since = new Date(+ Date.now() - GATEWAY_API_KEY_LAST_USED_WINDOW_DAYS * 24 * 60 * 60 * 1000,+ );+ return {+ isTracked: true,+ apiKeys: await getGatewayApiKeysLastUsedAt({+ projectId: project.id,+ apiKeyIds,+ since,+ }),+ };+ }+async create(params: {organizationId: string;name: string;diff --git a/web/src/features/ai-gateway/server/router.ts b/web/src/features/ai-gateway/server/router.tsindex 45717e93b5..26b5a9ac85 100644--- a/web/src/features/ai-gateway/server/router.ts+++ b/web/src/features/ai-gateway/server/router.ts@@ -477,6 +477,25 @@ export const aiGatewayRouter = createTRPCRouter({});}),+ /** Loaded after listApiKeys for the visible page, so ClickHouse never delays the key list. */+ getApiKeysLastUsed: protectedOrganizationProcedure+ .input(+ organizationInput.extend({+ apiKeyIds: z.array(z.string()).max(100),+ }),+ )+ .query(async ({ input, ctx }) => {+ const { ownerUserId } = requireGatewayInvoke({+ session: ctx.session,+ orgId: input.orgId,+ });+ return new GatewayApiKeyService(ctx.prisma, redis).getLastUsed({+ organizationId: input.orgId,+ ownerUserId,+ apiKeyIds: input.apiKeyIds,+ });+ }),+// Returns the plaintext secret key once, so the procedure must not be traced.createApiKey: protectedOrganizationProcedureWithoutTracing.input(