🪢 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): let members create their own gateway API keys (#19160)
Tobias Wochingerfdff585ad908
diff --git a/ai-gateway/README.md b/ai-gateway/README.mdindex 2ebaa7d09a..26c90c10f0 100644--- a/ai-gateway/README.md+++ b/ai-gateway/README.md@@ -499,6 +499,7 @@ each field describes:| ------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- || `organization.id`, `project.id` | Resolved Langfuse organization and ingestion project || `api_key.id`, `connection.id` | Authenticated gateway key and selected Langfuse connection |+| `api_key.user.id` | Langfuse user the gateway key belongs to; null for keys created before keys were bound to users || `ingestion.mode` | Effective capture mode || `request.api_format` | Native API contract: `openai.responses`, `openai.chat-completions` or `anthropic.messages` || `request.metadata`, `request.prompt_cache_key`, `request.safety_identifier`, `request.user` | Native caller-supplied fields, captured only in full mode |diff --git a/ai-gateway/src/capture/mod.rs b/ai-gateway/src/capture/mod.rsindex d375dce16c..d98f0b2254 100644--- a/ai-gateway/src/capture/mod.rs+++ b/ai-gateway/src/capture/mod.rs@@ -170,6 +170,7 @@ impl ExecutionCapture {"organization_id": attribution.organization_id().as_str(),"project_id": attribution.project_id().map(ProjectId::as_str),"key_id": attribution.key_id(),+ "user_id": attribution.user_id(),"provider_connection_id": attribution.provider_connection_id(),"key_metadata": key_metadata,"ingestion_mode": context.ingestion_mode().as_str(),diff --git a/ai-gateway/src/resolution/contracts.rs b/ai-gateway/src/resolution/contracts.rsindex 4557eac60e..bc3ae3ccb4 100644--- a/ai-gateway/src/resolution/contracts.rs+++ b/ai-gateway/src/resolution/contracts.rs@@ -244,6 +244,10 @@ pub struct RequestAttribution {/// Absent only in [`IngestionMode::None`], which needs no ingestion project.project_id: Option<ProjectId>,key_id: String,+ /// The user the key is bound to. Absent for keys created before keys were+ /// bound to users, and from Web versions that predate the field.+ #[serde(default)]+ user_id: Option<String>,key_metadata: BTreeMap<String, MetadataValue>,provider_connection_id: String,}@@ -258,6 +262,9 @@ impl RequestAttribution {pub fn key_id(&self) -> &str {&self.key_id}+ pub fn user_id(&self) -> Option<&str> {+ self.user_id.as_deref()+ }pub fn key_metadata(&self) -> &BTreeMap<String, MetadataValue> {&self.key_metadata}@@ -404,6 +411,7 @@ pub(super) fn decode(].into_iter().chain(attribution.project_id.as_ref().map(ProjectId::as_str))+ .chain(attribution.user_id.as_deref()).any(|value| value.trim().is_empty())|| connection.provider_model.trim().is_empty(){diff --git a/ai-gateway/src/resolution/tests.rs b/ai-gateway/src/resolution/tests.rsindex 994cf62fae..643ceba985 100644--- a/ai-gateway/src/resolution/tests.rs+++ b/ai-gateway/src/resolution/tests.rs@@ -106,6 +106,7 @@ fn success(project: &str, provider_token: &str) -> Value {"organization_id": "org-1","project_id": project,"key_id": "key-1",+ "user_id": "user-1","key_metadata": {"customer": "private-customer-label", "enabled": true, "number": 42},"provider_connection_id": "connection-1"},@@ -157,6 +158,7 @@ async fn signs_the_exact_key_and_sends_api_format_and_model() {context.attribution().provider_connection_id(),"connection-1");+ assert_eq!(context.attribution().user_id(), Some("user-1"));assert_eq!(context.ingestion().unwrap().access_token().expose_secret(),ingestion_token("org-1", "project-1")@@ -735,6 +737,8 @@ async fn rejects_incompatible_or_incomplete_execution_contexts() {("/connection/provider_model", json!("")),("/attribution/project_id", json!("")),("/attribution/provider_connection_id", json!("")),+ ("/attribution/user_id", json!("")),+ ("/attribution/user_id", json!(42)),("/attribution/key_metadata",json!({"nested": {"key": "value"}}),@@ -843,6 +847,38 @@ async fn resolves_none_mode_without_an_ingestion_grant_or_project() {assert!(context.attribution().project_id().is_none());}+#[tokio::test]+async fn resolves_keys_that_are_not_bound_to_a_user() {+ let unbound: fn(&mut Value) = |body| body["attribution"]["user_id"] = Value::Null;+ let predates_field: fn(&mut Value) = |body| {+ body["attribution"]+ .as_object_mut()+ .unwrap()+ .remove("user_id");+ };+ for strip in [unbound, predates_field] {+ let mut body = success("project-1", "provider-token");+ strip(&mut body);+ let web = FakeWeb::start(move |_| {+ let body = body.clone();+ async move { response(body) }+ })+ .await;+ let context = web+ .control_plane()+ .resolve_at(+ GATEWAY_KEY,+ ApiFormat::OpenAiResponses,+ "primary:gpt-4.1",+ NOW_TIME,+ REQUEST_ID,+ )+ .await+ .unwrap();+ assert!(context.attribution().user_id().is_none());+ }+}+#[tokio::test]async fn delivering_modes_require_an_ingestion_grant_and_project() {for mode in ["usage", "full"] {diff --git a/ai-gateway/src/telemetry/mapping.rs b/ai-gateway/src/telemetry/mapping.rsindex 2aee0a05f4..dcb2a41d5f 100644--- a/ai-gateway/src/telemetry/mapping.rs+++ b/ai-gateway/src/telemetry/mapping.rs@@ -158,6 +158,7 @@ fn generation_metadata(facts: &InferenceFacts) -> Map<String, Value> {("organization_id", "organization.id"),("ingestion_mode", "ingestion.mode"),("key_id", "api_key.id"),+ ("user_id", "api_key.user.id"),("provider_connection_id", "connection.id"),] {if let Some(value) = facts.metadata.get(source) {@@ -473,10 +474,12 @@ mod tests {let mut facts = facts();facts.metadata["organization_id"] = json!("org");facts.metadata["provider_connection_id"] = json!("connection");+ facts.metadata["user_id"] = json!("user");facts.metadata["key_metadata"] = json!({"team": "search", "enabled": true, "cost_center": 42,"langfuse.gateway.response.status_code": 500,"langfuse.gateway.api_key.id": "spoofed-key",+ "langfuse.gateway.api_key.user.id": "spoofed-user","langfuse.gateway.upstream.request.id": "spoofed-request","langfuse.gateway.future_field": "spoofed-future","langfuse.gateway.api_key.metadata.team": "spoofed-team",@@ -502,6 +505,7 @@ mod tests {);assert_eq!(metadata["langfuse.gateway.response.status_code"], 200);assert_eq!(metadata["langfuse.gateway.api_key.id"], "key");+ assert_eq!(metadata["langfuse.gateway.api_key.user.id"], "user");assert_eq!(metadata["langfuse.gateway.connection.id"], "connection");assert_eq!(metadata["langfuse.gateway.response.id"], "response");assert_eq!(metadata["langfuse.gateway.organization.id"], "org");diff --git a/web/src/__e2e__/ai-gateway.gatewaye2e.ts b/web/src/__e2e__/ai-gateway.gatewaye2e.tsindex 8bc63ebe76..5e5c7c270a 100644--- a/web/src/__e2e__/ai-gateway.gatewaye2e.ts+++ b/web/src/__e2e__/ai-gateway.gatewaye2e.ts@@ -607,6 +607,7 @@ describe("AI gateway live end-to-end", () => {organization_id: ORGANIZATION_ID,project_id: expect.stringMatching(/\S/),key_id: keyId,+ user_id: expect.stringMatching(/\S/),key_metadata: {test: true,provider,diff --git a/web/src/__tests__/organization-settings-pages.clienttest.tsx b/web/src/__tests__/organization-settings-pages.clienttest.tsxindex 89fc9134db..5a57d145cf 100644--- a/web/src/__tests__/organization-settings-pages.clienttest.tsx+++ b/web/src/__tests__/organization-settings-pages.clienttest.tsx@@ -245,7 +245,10 @@ describe("useOrganizationSettingsPages", () => {it("hides all AI Gateway settings when the internal flag is disabled", () => {vi.mocked(useHasOrganizationAccess).mockImplementation(- ({ scope }) => scope === "gateway:manage" || scope === "gateway:read",+ ({ scope }) =>+ scope === "gateway:manage" ||+ scope === "gateway:read" ||+ scope === "gateway:invoke",);const { result } = renderHook(() => useOrganizationSettingsPages());@@ -258,7 +261,10 @@ describe("useOrganizationSettingsPages", () => {it("shows all AI Gateway settings when the internal flag and gateway management access are enabled", () => {vi.mocked(useHasOrganizationAccess).mockImplementation(- ({ scope }) => scope === "gateway:manage" || scope === "gateway:read",+ ({ scope }) =>+ scope === "gateway:manage" ||+ scope === "gateway:read" ||+ scope === "gateway:invoke",);vi.mocked(useIsFeatureEnabled).mockReturnValue(true);@@ -279,7 +285,10 @@ describe("useOrganizationSettingsPages", () => {it("disables every AI Gateway page except Configuration for managers until ingestion is set up", () => {vi.mocked(useHasOrganizationAccess).mockImplementation(- ({ scope }) => scope === "gateway:manage" || scope === "gateway:read",+ ({ scope }) =>+ scope === "gateway:manage" ||+ scope === "gateway:read" ||+ scope === "gateway:invoke",);vi.mocked(useIsFeatureEnabled).mockReturnValue(true);getSetupStatusQuery.mockReturnValue({@@ -320,6 +329,28 @@ describe("useOrganizationSettingsPages", () => {expect(models?.disabledReason).toBeUndefined();});+ it("shows members their AI Gateway keys alongside models, even before ingestion is set up", () => {+ vi.mocked(useHasOrganizationAccess).mockImplementation(+ ({ scope }) => scope === "gateway:read" || scope === "gateway:invoke",+ );+ vi.mocked(useIsFeatureEnabled).mockReturnValue(true);+ getSetupStatusQuery.mockReturnValue({+ data: { isConfigured: false },+ isPending: false,+ });++ const { result } = renderHook(() => useOrganizationSettingsPages());++ expect(+ result.current+ .filter((page) => page.slug.startsWith("ai-gateway") && page.show)+ .map((page) => [page.slug, !!page.disabledReason]),+ ).toEqual([+ ["ai-gateway-models", false],+ ["ai-gateway-api-keys", false],+ ]);+ });+it("shows the usage page instead of the billing page without cloud billing", () => {vi.mocked(useHasOrganizationAccess).mockImplementation(({ scope }) => scope === "organizationUsage:read",diff --git a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts b/web/src/__tests__/server/ai-gateway-control-plane.servertest.tsindex ac44fa55e9..37381c6f3d 100644--- a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts+++ b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts@@ -697,6 +697,102 @@ describe("AI gateway control plane", () => {).rejects.toThrow("Gateway API key not found");});+ it("scopes gateway API keys to their creator for members", async () => {+ const owner = await prepare();+ const member = await createCallerForOrganizationRole(owner, Role.MEMBER);+ const viewer = await createCallerForOrganizationRole(owner, Role.VIEWER);++ const ownerKey = await owner.caller.aiGateway.createApiKey({+ orgId: owner.org.id,+ name: "Owner key",+ metadata: {},+ });+ const memberKey = await member.aiGateway.createApiKey({+ orgId: owner.org.id,+ name: "Member key",+ metadata: {},+ });++ const memberList = await member.aiGateway.listApiKeys({+ orgId: owner.org.id,+ });+ expect(memberList.data.map(({ apiKey }) => apiKey.id)).toEqual([+ memberKey.id,+ ]);+ expect(memberList.data[0]?.apiKey.user).toMatchObject({+ name: "Gateway member",+ });++ const ownerList = await owner.caller.aiGateway.listApiKeys({+ orgId: owner.org.id,+ });+ expect(+ ownerList.data.map(({ apiKey }) => [apiKey.id, apiKey.user?.name]),+ ).toEqual(+ expect.arrayContaining([+ [ownerKey.id, "Gateway tester"],+ [memberKey.id, "Gateway member"],+ ]),+ );++ const search = async (+ caller: typeof member,+ searchQuery: string,+ ): Promise<string[]> =>+ (+ await caller.aiGateway.listApiKeys({ orgId: owner.org.id, searchQuery })+ ).data.map(({ apiKey }) => apiKey.id);+ expect(await search(owner.caller, "owner KEY")).toEqual([ownerKey.id]);+ expect(await search(owner.caller, "gateway member")).toEqual([+ memberKey.id,+ ]);+ expect(await search(owner.caller, "%")).toEqual([]);+ expect(await search(member, "owner key")).toEqual([]);++ await expect(+ member.aiGateway.revokeApiKey({ orgId: owner.org.id, id: ownerKey.id }),+ ).rejects.toThrow("Gateway API key not found");+ await expect(+ member.aiGateway.revokeApiKey({ orgId: owner.org.id, id: memberKey.id }),+ ).resolves.toEqual({ success: true });++ await expect(+ viewer.aiGateway.listApiKeys({ orgId: owner.org.id }),+ ).rejects.toMatchObject({ code: "FORBIDDEN" });+ await expect(+ viewer.aiGateway.createApiKey({ orgId: owner.org.id, metadata: {} }),+ ).rejects.toMatchObject({ code: "FORBIDDEN" });+ });++ it.each(["legacy", "shadow"] as const)(+ "keeps gateway API key creation admin-only when API_AUTH_MIGRATION is %s",+ async (migration) => {+ const originalMigration = env.API_AUTH_MIGRATION;+ Object.assign(env, { API_AUTH_MIGRATION: migration });+ try {+ const owner = await prepare();+ const member = await createCallerForOrganizationRole(+ owner,+ Role.MEMBER,+ );++ await expect(+ member.aiGateway.createApiKey({ orgId: owner.org.id, metadata: {} }),+ ).rejects.toMatchObject({ code: "FORBIDDEN" });+ await expect(+ owner.caller.aiGateway.createApiKey({+ orgId: owner.org.id,+ metadata: {},+ }),+ ).resolves.toMatchObject({+ secretKey: expect.stringMatching(/^sk-lf-/),+ });+ } finally {+ Object.assign(env, { API_AUTH_MIGRATION: originalMigration });+ }+ },+ );+it("rejects gateway API key expiration dates in the past", async () => {const { caller, org } = await prepare();diff --git a/web/src/__tests__/server/organizations-api-key-trpc.servertest.ts b/web/src/__tests__/server/organizations-api-key-trpc.servertest.tsindex 299ebc4425..5c89d78710 100644--- a/web/src/__tests__/server/organizations-api-key-trpc.servertest.ts+++ b/web/src/__tests__/server/organizations-api-key-trpc.servertest.ts@@ -553,6 +553,7 @@ describe("organization API keys trpc", () => {context: {organizationId,apiKeyId: gatewayKey.id,+ userId: null,keyMetadata: {},ingestionProjectId: null,ingestionMode: null,diff --git a/web/src/__tests__/server/unit/gatewayResolveCache.servertest.ts b/web/src/__tests__/server/unit/gatewayResolveCache.servertest.tsindex 52222bf74b..340bcc31ce 100644--- a/web/src/__tests__/server/unit/gatewayResolveCache.servertest.ts+++ b/web/src/__tests__/server/unit/gatewayResolveCache.servertest.ts@@ -16,6 +16,7 @@ vi.mock("@/src/env.mjs", () => ({const context: CachedResolveContext = {organizationId: "org-1",apiKeyId: "key-1",+ userId: "user-1",keyMetadata: {},ingestionProjectId: null,ingestionMode: null,diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/GatewayApiKeysPage.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/GatewayApiKeysPage.tsxindex 0499b88cee..033378eb5c 100644--- a/web/src/features/ai-gateway/components/GatewayApiKeysPage/GatewayApiKeysPage.tsx+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/GatewayApiKeysPage.tsx@@ -1,4 +1,8 @@import Header from "@/src/components/layouts/header";+import { InfoTooltip } from "@/src/components/ui/InfoTooltip/InfoTooltip";+import { Skeleton } from "@/src/components/ui/skeleton";+import { GatewayNotSetUpNotice } from "@/src/features/ai-gateway/components/GatewayNotSetUpNotice/GatewayNotSetUpNotice";+import { useGatewaySettingsAccess } from "@/src/features/ai-gateway/hooks/useGatewaySettingsAccess";import { ConnectedGatewayApiKeysTable } from "./components/GatewayApiKeysTable/ConnectedGatewayApiKeysTable";export function GatewayApiKeysPage({@@ -6,12 +10,26 @@ export function GatewayApiKeysPage({}: {organizationId: string;}) {+ const { canManageGateway, isIngestionConfigured, isSetupStatusPending } =+ useGatewaySettingsAccess(organizationId);++ if (isSetupStatusPending) {+ return <ApiKeysSkeleton />;+ }+ /** Managers are redirected to Configuration by the settings route. */+ if (!canManageGateway && isIngestionConfigured === false) {+ return <GatewayNotSetUpNotice title="Gateway API keys" />;+ }return (<div className="flex flex-col gap-4">- <Header title="Gateway API keys" />- <p className="text-muted-foreground text-sm">- These organization keys authenticate requests to the AI Gateway only.- </p>+ <div className="flex items-center gap-1">+ <Header title="Gateway API keys" className="mb-0!" />+ <InfoTooltip label="About gateway API keys">+ {canManageGateway+ ? "These organization keys authenticate requests to the AI Gateway only. Each key belongs to the member who created it and is revoked when they leave the organization."+ : "Your keys authenticate requests to the AI Gateway only. They are revoked when you leave the organization or lose gateway access."}+ </InfoTooltip>+ </div><ConnectedGatewayApiKeysTablekey={organizationId}organizationId={organizationId}@@ -19,3 +37,12 @@ export function GatewayApiKeysPage({</div>);}++function ApiKeysSkeleton() {+ return (+ <div className="flex flex-col gap-4">+ <Skeleton className="h-7 w-48" />+ <Skeleton className="h-48 w-full" />+ </div>+ );+}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 5b5fe69852..a690648a0b 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,3 +1,6 @@+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";import { CreateGatewayApiKeyDialogController } from "@/src/features/ai-gateway/components/GatewayApiKeysPage/components/CreateGatewayApiKeyDialogController/CreateGatewayApiKeyDialogController";@@ -15,14 +18,24 @@ export function ConnectedGatewayApiKeysTable({page: "pageIndex",limit: "pageSize",});- const apiKeysQuery = api.aiGateway.listApiKeys.useQuery({- orgId: organizationId,- page: paginationState.pageIndex,- limit: paginationState.pageSize,- });+ const [searchQuery, setSearchQuery] = useState("");+ const apiKeysQuery = api.aiGateway.listApiKeys.useQuery(+ {+ orgId: organizationId,+ searchQuery,+ page: paginationState.pageIndex,+ limit: paginationState.pageSize,+ },+ { placeholderData: keepPreviousData },+ );const utils = api.useUtils();const revoke = api.aiGateway.revokeApiKey.useMutation();+ const handleSearchChange = (value: string) => {+ setSearchQuery(value);+ setPaginationState({ ...paginationState, pageIndex: 0 });+ };+if (apiKeysQuery.isError) {return (<div className="flex flex-col gap-4">@@ -84,6 +97,7 @@ export function ConnectedGatewayApiKeysTable({state: paginationState,onChange: setPaginationState,}}+ search={{ value: searchQuery, onChange: handleSearchChange }}createAction={openDialog}onRevoke={openRevokeDialog}/>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 51165325ab..115cb77712 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@@ -21,6 +21,7 @@ const apiKeys = [note: "Production application",createdAt: new Date("2026-09-04T12:00:00.000Z"),expiresAt: new Date("2027-09-04T23:59:59.999Z"),+ user: { name: "Marc Klingen", email: "[email protected]", image: null },},},] satisfies Extract<@@ -40,6 +41,10 @@ const manyApiKeys = Array.from({ length: 30 }, (_, index) => ({note: `Application ${index + 1}`,createdAt: new Date(Date.UTC(2026, 8, 4 - (index % 28))),expiresAt: null,+ user:+ index % 5 === 0+ ? null+ : { name: `Member ${index + 1}`, email: null, image: null },},})) satisfies Extract<ComponentProps<typeof GatewayApiKeysTable>["data"],@@ -55,9 +60,10 @@ const actions = {state: { pageIndex: 0, pageSize: 10 },onChange: onPageChange,},+ search: { value: "", onChange: fn() },} satisfies Pick<ComponentProps<typeof GatewayApiKeysTable>,- "createAction" | "onRevoke" | "pagination"+ "createAction" | "onRevoke" | "pagination" | "search">;export const PopulatedMetadata = meta.story({@@ -69,7 +75,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[4]).toHaveClass("ph-no-capture");+ await expect(cells[5]).toHaveClass("ph-no-capture");},});@@ -100,7 +106,7 @@ export const OverflowingMetadata = meta.story({play: async ({ canvas }) => {const metadataCell = canvas.getAllByRole("row")[1]!- .querySelectorAll("td")[4]!;+ .querySelectorAll("td")[5]!;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 5be038298f..f13f47cf4c 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@@ -5,6 +5,7 @@ import type { PaginationBarProps } from "@/src/components/design-system/Paginatiimport { createDateTableColumn } from "@/src/components/design-system/table/columns/createDateTableColumn";import { 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 type { AsyncTableData } from "@/src/components/design-system/table/Table";import type { LangfuseColumnDef } from "@/src/components/table/types";import { isApiKeyExpired } from "@/src/features/apiKey";@@ -18,6 +19,11 @@ type GatewayApiKey = {note: string | null;createdAt: Date;expiresAt: Date | null;+ user: {+ name: string | null;+ email: string | null;+ image: string | null;+ } | null;};};@@ -26,11 +32,13 @@ export function GatewayApiKeysTable({createAction,onRevoke,pagination,+ search,}: {data: AsyncTableData<GatewayApiKey[]>;createAction: () => void;onRevoke: (apiKeyId: string) => void;pagination: PaginationBarProps;+ search: { value: string; onChange: (value: string) => void };}) {const columns: LangfuseColumnDef<GatewayApiKey>[] = [createDateTableColumn<GatewayApiKey>({@@ -53,6 +61,15 @@ export function GatewayApiKeysTable({nullValue: "-",enableResizing: false,}),+ createUserTableColumn<GatewayApiKey>({+ accessorFn: (row) => row.apiKey.user,+ id: "user",+ header: "User",+ variant: "avatar",+ nullValue: "-",+ hideBelowMd: true,+ enableResizing: false,+ }),createDateTableColumn<GatewayApiKey>({accessorFn: (row) => row.apiKey.expiresAt,id: "expiresAt",@@ -88,6 +105,7 @@ export function GatewayApiKeysTable({},]}pagination={pagination}+ search={{ ...search, placeholder: "Search by name or user" }}toolbarActions={[{id: "create-key",@@ -96,7 +114,11 @@ export function GatewayApiKeysTable({onClick: createAction,},]}- noResultsMessage="No gateway API keys created."+ noResultsMessage={+ search.value.trim().length > 0+ ? "No gateway API keys match the current search."+ : "No gateway API keys created."+ }/>);}diff --git a/web/src/features/ai-gateway/hooks/useGatewaySettingsAccess.ts b/web/src/features/ai-gateway/hooks/useGatewaySettingsAccess.tsindex df09f0de0a..c5e7eae08e 100644--- a/web/src/features/ai-gateway/hooks/useGatewaySettingsAccess.ts+++ b/web/src/features/ai-gateway/hooks/useGatewaySettingsAccess.ts@@ -18,12 +18,18 @@ export function useGatewaySettingsAccess(organizationId: string | undefined) {organizationId,scope: "gateway:read",});+ const canInvokeGateway = useHasOrganizationAccess({+ organizationId,+ scope: "gateway:invoke",+ });const isAiGatewayEnabled = useIsFeatureEnabled("aiGateway", {enableForAdmins: false,organizationId,});const showManagement = isAiGatewayEnabled && canManageGateway;+ /** Members manage their own gateway keys; managers see every key. */+ const showApiKeys = isAiGatewayEnabled && canInvokeGateway;const showModels =isAiGatewayEnabled &&organizationId !== undefined &&@@ -36,13 +42,18 @@ export function useGatewaySettingsAccess(organizationId: string | undefined) {const setupStatus = api.aiGateway.getSetupStatus.useQuery({ orgId: organizationId ?? "" },- { enabled: organizationId !== undefined && (showManagement || showModels) },+ {+ enabled:+ organizationId !== undefined &&+ (showManagement || showModels || showApiKeys),+ },);return {canManageGateway,showManagement,showModels,+ showApiKeys,isIngestionConfigured: setupStatus.data?.isConfigured,isSetupStatusPending: setupStatus.isPending,};diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.tsindex 28e965960e..e469f632af 100644--- a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts@@ -1,36 +1,42 @@import type { PrismaClient } from "@langfuse/shared/src/db";+import { escapeSqlLikePattern } from "@langfuse/shared/src/server";++const GATEWAY_API_KEY_SELECT = {+ metadata: true,+ apiKey: {+ select: {+ id: true,+ publicKey: true,+ displaySecretKey: true,+ note: true,+ createdAt: true,+ expiresAt: true,+ lastUsedAt: true,+ createdByUserId: true,+ user: { select: { name: true, email: true, image: true } },+ },+ },+} as const;export class GatewayApiKeyRepository {constructor(private readonly prisma: PrismaClient) {}async listGatewayApiKeys(params: {organizationId: string;+ ownerUserId?: string;+ searchQuery?: string;page: number;limit: number;}) {const where = {apiKey: {- orgId: params.organizationId,- scope: "ORGANIZATION",+ ...gatewayApiKeyWhere(params),+ ...(params.searchQuery && gatewayApiKeySearch(params.searchQuery)),},- } as const;+ };const findPage = this.prisma.gatewayApiKeyAssociation.findMany({where,- select: {- metadata: true,- apiKey: {- select: {- id: true,- publicKey: true,- displaySecretKey: true,- note: true,- createdAt: true,- expiresAt: true,- lastUsedAt: true,- createdByUserId: true,- },- },- },+ select: GATEWAY_API_KEY_SELECT,orderBy: [{ apiKey: { createdAt: "asc" } }, { apiKeyId: "asc" }],take: params.limit,skip: params.page * params.limit,@@ -42,30 +48,41 @@ export class GatewayApiKeyRepository {return { data, totalCount };}- getGatewayApiKey(params: { organizationId: string; apiKeyId: string }) {+ getGatewayApiKey(params: {+ organizationId: string;+ ownerUserId?: string;+ apiKeyId: string;+ }) {return this.prisma.gatewayApiKeyAssociation.findFirst({- where: {- apiKeyId: params.apiKeyId,- apiKey: {- orgId: params.organizationId,- scope: "ORGANIZATION",- },- },- select: {- metadata: true,- apiKey: {- select: {- id: true,- publicKey: true,- displaySecretKey: true,- note: true,- createdAt: true,- expiresAt: true,- lastUsedAt: true,- createdByUserId: true,- },- },- },+ where: { apiKeyId: params.apiKeyId, apiKey: gatewayApiKeyWhere(params) },+ select: GATEWAY_API_KEY_SELECT,});}}++/** gatewayApiKeyWhere scopes gateway keys to the organization and, when set, to the user the keys are bound to. */+function gatewayApiKeyWhere(params: {+ organizationId: string;+ ownerUserId?: string;+}) {+ return {+ orgId: params.organizationId,+ scope: "ORGANIZATION",+ ...(params.ownerUserId !== undefined && { userId: params.ownerUserId }),+ } as const;+}++/** gatewayApiKeySearch matches the key name or the name or email of the user the key is bound to. */+function gatewayApiKeySearch(searchQuery: string) {+ const contains = {+ contains: escapeSqlLikePattern(searchQuery),+ mode: "insensitive",+ } as const;+ return {+ OR: [+ { note: contains },+ { user: { name: contains } },+ { user: { email: contains } },+ ],+ };+}diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.tsindex 02c1ed55c1..ad0f22e67f 100644--- a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts@@ -28,7 +28,14 @@ export class GatewayApiKeyService {this.repository = new GatewayApiKeyRepository(prisma);}- list(params: { organizationId: string; page: number; limit: number }) {+ /** Without ownerUserId, lists every gateway key in the organization. */+ list(params: {+ organizationId: string;+ ownerUserId?: string;+ searchQuery?: string;+ page: number;+ limit: number;+ }) {return this.repository.listGatewayApiKeys(params);}@@ -79,8 +86,10 @@ export class GatewayApiKeyService {return key;}+ /** With ownerUserId, another user's key is reported as not found. */async revoke(params: {organizationId: string;+ ownerUserId?: string;apiKeyId: string;session: OrgAuthedContext["session"];}): Promise<void> {diff --git a/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts b/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.tsindex ae5804ddbd..3180e04103 100644--- a/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts+++ b/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts@@ -148,6 +148,7 @@ export class GatewayApiKeyAuthenticator {const context: CachedResolveContext = {organizationId,apiKeyId: row.id,+ userId: row.userId,keyMetadata: toKeyMetadata(row.gatewayAssociation?.metadata),ingestionProjectId: projectIsUsable? (gatewayConfig?.defaultIngestionProjectId ?? null)diff --git a/web/src/features/ai-gateway/server/gatewaySetupRedirect.ts b/web/src/features/ai-gateway/server/gatewaySetupRedirect.tsindex 0f8d2a079c..a6243dfbd2 100644--- a/web/src/features/ai-gateway/server/gatewaySetupRedirect.ts+++ b/web/src/features/ai-gateway/server/gatewaySetupRedirect.ts@@ -14,7 +14,8 @@ const SETUP_GATED_SETTINGS_SLUGS = new Set([/*** Gateway managers cannot open provider, model or API key settings before an* ingestion project is configured; they are sent to the configuration page.- * Readers are not redirected because their Models page explains the state.+ * Other members are not redirected because their Models and API key pages+ * explain the state.*/export async function getGatewaySetupRedirect(params: {prisma: PrismaClient;diff --git a/web/src/features/ai-gateway/server/provider/registry.ts b/web/src/features/ai-gateway/server/provider/registry.tsindex cd124c63ae..48794b22e7 100644--- a/web/src/features/ai-gateway/server/provider/registry.ts+++ b/web/src/features/ai-gateway/server/provider/registry.ts@@ -48,6 +48,8 @@ export const GatewayResolveResponseSchema = z// Null only in "none" mode, which needs no ingestion project.project_id: z.string().nullable(),key_id: z.string(),+ // Null for keys created before keys were bound to users.+ user_id: z.string().nullable(),key_metadata: GatewayMetadataSchema,provider_connection_id: z.string(),})diff --git a/web/src/features/ai-gateway/server/resolve/gatewayResolveCache.ts b/web/src/features/ai-gateway/server/resolve/gatewayResolveCache.tsindex 6c81b3e4a7..bc7e81bb06 100644--- a/web/src/features/ai-gateway/server/resolve/gatewayResolveCache.ts+++ b/web/src/features/ai-gateway/server/resolve/gatewayResolveCache.ts@@ -26,6 +26,7 @@ export const GATEWAY_RESOLVE_KEY_NON_EXISTENT = "gateway-key-non-existent";export const CachedResolveContext = z.object({organizationId: z.string(),apiKeyId: z.string(),+ userId: z.string().nullable(),keyMetadata: GatewayMetadataSchema,ingestionProjectId: z.string().nullable(),ingestionMode: z.enum(["NONE", "USAGE", "FULL"]).nullable(),diff --git a/web/src/features/ai-gateway/server/resolve/gatewayResolveRepository.ts b/web/src/features/ai-gateway/server/resolve/gatewayResolveRepository.tsindex 4b6c091a44..fdac1a33dd 100644--- a/web/src/features/ai-gateway/server/resolve/gatewayResolveRepository.ts+++ b/web/src/features/ai-gateway/server/resolve/gatewayResolveRepository.ts@@ -19,6 +19,7 @@ export class GatewayResolveRepository {select: {id: true,orgId: true,+ userId: true,expiresAt: true,gatewayAssociation: { select: { metadata: true } },organization: {diff --git a/web/src/features/ai-gateway/server/resolve/resolveService.servertest.ts b/web/src/features/ai-gateway/server/resolve/resolveService.servertest.tsindex e9b1d6e77c..378cc4183e 100644--- a/web/src/features/ai-gateway/server/resolve/resolveService.servertest.ts+++ b/web/src/features/ai-gateway/server/resolve/resolveService.servertest.ts@@ -62,9 +62,11 @@ const row = (overrides: {projectOrgId?: string;withoutProject?: boolean;metadata?: unknown;+ userId?: string | null;}) => ({id: "key-1",orgId: "org-1",+ userId: overrides.userId === undefined ? "user-1" : overrides.userId,expiresAt: null,gatewayAssociation:overrides.metadata === undefined ? null : { metadata: overrides.metadata },@@ -122,6 +124,7 @@ describe("GatewayResolveService", () => {organization_id: "org-1",project_id: "project-1",key_id: "key-1",+ user_id: "user-1",key_metadata: { team: "platform", project_id: "spoofed-project" },provider_connection_id: "connection-1",},@@ -168,6 +171,19 @@ describe("GatewayResolveService", () => {});});+ it("attributes a key that is not bound to a user to no user", async () => {+ const service = serviceWith(() => Promise.resolve(row({ userId: null })));++ await expect(+ service.resolve({+ fastHashedSecretKey: "hashed-secret-key",+ apiFormat: "openai.responses",+ }),+ ).resolves.toMatchObject({+ attribution: { key_id: "key-1", user_id: null },+ });+ });+it("refuses to resolve when the ingestion project belongs to another organization", async () => {const service = serviceWith(() =>Promise.resolve(row({ projectOrgId: "org-other" })),diff --git a/web/src/features/ai-gateway/server/resolve/resolveService.ts b/web/src/features/ai-gateway/server/resolve/resolveService.tsindex d5ed9e78a9..f8e8dca50c 100644--- a/web/src/features/ai-gateway/server/resolve/resolveService.ts+++ b/web/src/features/ai-gateway/server/resolve/resolveService.ts@@ -115,6 +115,7 @@ export class GatewayResolveService {organization_id: context.organizationId,project_id: ingestionProjectId,key_id: context.apiKeyId,+ user_id: context.userId,key_metadata: context.keyMetadata,provider_connection_id: context.connection.id,},diff --git a/web/src/features/ai-gateway/server/router.ts b/web/src/features/ai-gateway/server/router.tsindex 63e078e3e0..49d2bcc1fb 100644--- a/web/src/features/ai-gateway/server/router.ts+++ b/web/src/features/ai-gateway/server/router.ts@@ -9,6 +9,7 @@ import {createTRPCRouter,protectedOrganizationProcedure,protectedOrganizationProcedureWithoutTracing,+ type AuthedSession,} from "@/src/server/api/trpc";import { env } from "@/src/env.mjs";import { canReadGatewayModels } from "@/src/features/ai-gateway/fns/canReadGatewayModels";@@ -58,6 +59,25 @@ function requireGatewayAdmin(params: { session: Session; orgId: string }) {});}+/** Admits gateway key owners; the returned `ownerUserId` limits non-managers to their own keys. */+function requireGatewayInvoke(params: {+ session: AuthedSession;+ orgId: string;+}) {+ requireGatewayEnabledForOrganization(params.orgId);+ throwIfNoOrganizationAccess({+ session: params.session,+ organizationId: params.orgId,+ scope: "gateway:invoke",+ });+ const canManageGateway = hasOrganizationAccess({+ session: params.session,+ organizationId: params.orgId,+ scope: "gateway:manage",+ });+ return { ownerUserId: canManageGateway ? undefined : params.session.user.id };+}+function requireGatewayRead(params: { session: Session; orgId: string }) {requireGatewayEnabledForOrganization(params.orgId);const canManageGateway = hasOrganizationAccess({@@ -328,11 +348,20 @@ export const aiGatewayRouter = createTRPCRouter({}),listApiKeys: protectedOrganizationProcedure- .input(offsetPaginatedOrganizationInput)+ .input(+ offsetPaginatedOrganizationInput.extend({+ searchQuery: z.string().trim().optional(),+ }),+ ).query(async ({ input, ctx }) => {- requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });+ const { ownerUserId } = requireGatewayInvoke({+ session: ctx.session,+ orgId: input.orgId,+ });return new GatewayApiKeyService(ctx.prisma, redis).list({organizationId: input.orgId,+ ownerUserId,+ searchQuery: input.searchQuery,page: input.page,limit: input.limit,});@@ -353,7 +382,13 @@ export const aiGatewayRouter = createTRPCRouter({}),).mutation(async ({ input, ctx }) => {- requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });+ // Legacy public API auth ignores key roles, so a gateway key passes as an+ // organization admin key there; only managers may mint one outside enforce.+ if (env.API_AUTH_MIGRATION === "enforce") {+ requireGatewayInvoke({ session: ctx.session, orgId: input.orgId });+ } else {+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });+ }const key = await new GatewayApiKeyService(ctx.prisma, redis).create({organizationId: input.orgId,name: input.name,@@ -367,9 +402,13 @@ export const aiGatewayRouter = createTRPCRouter({revokeApiKey: protectedOrganizationProcedure.input(connectionInput).mutation(async ({ input, ctx }) => {- requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });+ const { ownerUserId } = requireGatewayInvoke({+ session: ctx.session,+ orgId: input.orgId,+ });await new GatewayApiKeyService(ctx.prisma, redis).revoke({organizationId: input.orgId,+ ownerUserId,apiKeyId: input.id,session: ctx.session,});diff --git a/web/src/features/organizations/OrganizationSettingsPage.tsx b/web/src/features/organizations/OrganizationSettingsPage.tsxindex 1413fba294..53d81899bc 100644--- a/web/src/features/organizations/OrganizationSettingsPage.tsx+++ b/web/src/features/organizations/OrganizationSettingsPage.tsx@@ -91,7 +91,8 @@ export function useOrganizationSettingsPages(): OrganizationSettingsPage[] {showV4Migration,showAiGatewayManagement: gatewayAccess.showManagement,showAiGatewayModels: gatewayAccess.showModels,- /** Only managers get disabled entries; readers see a notice on Models. */+ showAiGatewayApiKeys: gatewayAccess.showApiKeys,+ /** Only managers get disabled entries; others see a notice on the page. */gatewaySetupDisabledReason:gatewayAccess.showManagement &&gatewayAccess.isIngestionConfigured === false@@ -113,6 +114,7 @@ const getOrganizationSettingsPages = ({showV4Migration,showAiGatewayManagement,showAiGatewayModels,+ showAiGatewayApiKeys,gatewaySetupDisabledReason,showOrganizationLlmConnections,showFeaturePreviews,@@ -136,6 +138,7 @@ const getOrganizationSettingsPages = ({showV4Migration: boolean;showAiGatewayManagement: boolean;showAiGatewayModels: boolean;+ showAiGatewayApiKeys: boolean;gatewaySetupDisabledReason: string | undefined;showOrganizationLlmConnections: boolean;showFeaturePreviews: boolean;@@ -358,7 +361,7 @@ const getOrganizationSettingsPages = ({section: "AI Gateway",cmdKKeywords: ["gateway", "api", "keys", "metadata", "credentials"],content: <GatewayApiKeysPage organizationId={organization.id} />,- show: showAiGatewayManagement,+ show: showAiGatewayApiKeys,disabledReason: gatewaySetupDisabledReason,},];