Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(gateway): add organization control plane (#17085)

Tobias Wochinger8ee053bb105c
diff --git a/.env.dev.example b/.env.dev.example
index 1384e1a3b9..fdfed9a79f 100644
--- a/.env.dev.example
+++ b/.env.dev.example
@@ -148,6 +148,25 @@ REDIS_AUTH="myredissecret"
# openssl rand -hex 32 used only here
ENCRYPTION_KEY=0000000000000000000000000000000000000000000000000000000000000000
+# AI gateway control-plane key rotation. Generate service keys with
+# `openssl rand -hex 32` and ES256 PEM keys with
+# `openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256`.
+# LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST=
+# LANGFUSE_AI_GATEWAY_SERVICE_KEY=
+# LANGFUSE_AI_GATEWAY_SERVICE_KEY_PREVIOUS=
+# LANGFUSE_AI_GATEWAY_JWT_KEY_ID=
+# LANGFUSE_AI_GATEWAY_JWT_PRIVATE_KEY=
+# LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY=
+# LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_KEY_ID=
+# LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_PUBLIC_KEY=
+# LANGFUSE_AI_GATEWAY_JWT_ISSUER=langfuse-control-plane
+# LANGFUSE_AI_GATEWAY_JWT_AUDIENCE=langfuse-ingestion
+# Caching of the /resolve lookup, which the gateway calls on every LLM request.
+# The TTL bounds how long a revoked key or a disabled connection can still be
+# used if explicit invalidation is missed.
+# LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED=true
+# LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_TTL_SECONDS=60
+
# speeds up local development by not executing init scripts on server startup
NEXT_PUBLIC_LANGFUSE_RUN_NEXT_INIT="false"
diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml
index a636d826c2..dded16e063 100644
--- a/.github/workflows/pipeline.yml
+++ b/.github/workflows/pipeline.yml
@@ -150,6 +150,35 @@ jobs:
- 'packages/shared/src/server/llm/**'
- 'worker/src/__tests__/llmConnections.test.ts'
+ ai-gateway-filter:
+ runs-on: blacksmith-4vcpu-ubuntu-2404
+ outputs:
+ changed: ${{ steps.filter.outputs.ai_gateway }}
+ timeout-minutes: 15
+ permissions:
+ contents: read
+ pull-requests: read
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ fetch-depth: 20
+ persist-credentials: false
+ - uses: dorny/paths-filter@7b450fff21473bca461d4b92ce414b9d0420d706 # v4.0.2
+ id: filter
+ with:
+ filters: |
+ ai_gateway:
+ - 'packages/shared/prisma/migrations/**/*gateway*/**'
+ - 'web/src/__e2e__/**/*.gatewaye2e.{ts,tsx}'
+ - 'web/src/features/ai-gateway/**'
+ - 'web/src/features/public-api/server/createAuthedProjectAPIRoute.ts'
+ - 'web/src/pages/api/internal/ai-gateway/**'
+ - 'web/src/pages/api/public/ingestion.ts'
+ - 'web/src/pages/api/public/media/**'
+ - 'web/src/pages/api/public/otel/v1/traces/**'
+ - 'web/src/server/utils/hmac.ts'
+ - 'web/src/server/utils/jwt.ts'
+
tests-ai-gateway:
timeout-minutes: 20
runs-on: blacksmith-4vcpu-ubuntu-2404
@@ -1291,6 +1320,94 @@ jobs:
- name: Run e2e tests
run: pnpm --filter=web run test:e2e:server
+ test-ai-gateway-e2e:
+ timeout-minutes: 25
+ runs-on: blacksmith-4vcpu-ubuntu-2404
+ needs:
+ - pre-job
+ - ai-gateway-filter
+ if: ${{ !cancelled() && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) && (startsWith(github.ref, 'refs/tags/') || ((github.event_name != 'push' || needs.pre-job.outputs.should_skip != 'true') && (needs.ai-gateway-filter.outputs.changed == 'true' || github.event_name == 'workflow_dispatch'))) }}
+ name: test-ai-gateway-e2e (node24, pg15)
+ env:
+ NODE_ENV: test
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ persist-credentials: false
+ - uses: pnpm/setup@703c52620218391530e48b9e8870d5c0082e1b9b # v2.1.0
+ with:
+ install: false
+ cache: false
+ - name: Login to Docker Hub
+ if: github.repository == 'langfuse/langfuse'
+ uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
+ with:
+ username: ${{ secrets.DOCKERHUB_USERNAME_READ }}
+ password: ${{ secrets.DOCKERHUB_TOKEN_READ }}
+ # Keep this secret-bearing job cache-cold to avoid exposing provider
+ # credentials to potentially poisoned shared package-manager state.
+ - name: Use Node.js ${{ env.NODE_VERSION }}
+ uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
+ with:
+ node-version: ${{ env.NODE_VERSION }}
+ package-manager-cache: false
+ - name: Install dependencies
+ run: pnpm install
+ - name: Load default env
+ run: cp .env.dev.example .env
+ - name: Configure gateway signing
+ run: |
+ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out /tmp/gateway-jwt-private.pem
+ openssl pkey -in /tmp/gateway-jwt-private.pem -pubout -out /tmp/gateway-jwt-public.pem
+ delimiter="GATEWAY_KEY_$(openssl rand -hex 12)"
+ {
+ echo "LANGFUSE_AI_GATEWAY_SERVICE_KEY=$(openssl rand -hex 32)"
+ echo "LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST=seed-org-id"
+ echo "LANGFUSE_AI_GATEWAY_JWT_KEY_ID=ci"
+ echo "LANGFUSE_AI_GATEWAY_JWT_ISSUER=langfuse-gateway-ci"
+ echo "LANGFUSE_AI_GATEWAY_JWT_AUDIENCE=langfuse-ingestion-ci"
+ echo "LANGFUSE_AI_GATEWAY_JWT_PRIVATE_KEY<<$delimiter"
+ cat /tmp/gateway-jwt-private.pem
+ echo "$delimiter"
+ echo "LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY<<$delimiter"
+ cat /tmp/gateway-jwt-public.pem
+ echo "$delimiter"
+ } >> "$GITHUB_ENV"
+ - name: Install golang-migrate
+ run: |
+ curl --fail --location --retry 5 --retry-delay 2 --retry-all-errors \
+ --output migrate.linux-amd64.tar.gz \
+ https://github.com/golang-migrate/migrate/releases/download/v4.19.1/migrate.linux-amd64.tar.gz
+ tar xzf migrate.linux-amd64.tar.gz
+ sudo mv migrate /usr/bin/migrate
+ - name: Start dev containers
+ run: |
+ docker compose -f docker-compose.dev.yml up -d --wait --wait-timeout 180
+ docker compose ps
+ env:
+ POSTGRES_VERSION: 15
+ - name: Migrate and seed
+ run: |
+ pnpm run db:migrate
+ pnpm --filter=shared run ch:up
+ pnpm --filter=shared run db:seed
+ - name: Build
+ run: pnpm run build
+ env:
+ NODE_OPTIONS: --max_old_space_size=8192
+ NEXT_IGNORE_BUILD_ERRORS: "true"
+ - name: Run server
+ run: (pnpm run start&)
+ - name: Check worker health
+ run: timeout 30 bash -c 'until curl -f http://localhost:3030/api/health; do sleep 2; done'
+ - name: Check server health
+ run: timeout 30 bash -c 'until curl -f http://localhost:3000/api/public/health; do sleep 2; done'
+ - name: Run AI Gateway end-to-end tests
+ run: pnpm --filter=web run test:e2e:ai-gateway
+ env:
+ LANGFUSE_LLM_CONNECTION_OPENAI_KEY: ${{ secrets.LANGFUSE_LLM_CONNECTION_OPENAI_KEY }}
+ LANGFUSE_LLM_CONNECTION_ANTHROPIC_KEY: ${{ secrets.LANGFUSE_LLM_CONNECTION_ANTHROPIC_KEY }}
+
all-ci-passed:
# This allows us to have a branch protection rule for tests and deploys with matrix
runs-on: blacksmith-4vcpu-ubuntu-2404
@@ -1308,6 +1425,7 @@ jobs:
tests-web-client,
tests-worker,
test-worker-llm-connections,
+ test-ai-gateway-e2e,
e2e-tests,
test-docker-build,
e2e-server-tests,
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 90c53d9836..f08ab5c0e5 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -130,7 +130,7 @@ We built a monorepo using [pnpm](https://pnpm.io/motivation) and [turbo](https:/
Requirements
- Node.js 24 as specified in the [.nvmrc](.nvmrc)
-- [Rust via rustup](https://rust-lang.org/tools/install/) and a native compiler/linker for the AI gateway, which starts with `pnpm dev`. See [gateway setup](ai-gateway/README.md#run-locally).
+- [Rust via rustup](https://rust-lang.org/tools/install/) and a native compiler/linker for the AI gateway, which starts with `pnpm dev`. See [gateway setup](ai-gateway/README.md#run-locally).
- pnpm 12.3.1 as specified in `package.json`
- Docker to run the database locally
- Clickhouse client
diff --git a/packages/shared/prisma/migrations/20260904151000_add_ai_gateway_control_plane/migration.sql b/packages/shared/prisma/migrations/20260904151000_add_ai_gateway_control_plane/migration.sql
new file mode 100644
index 0000000000..7b28878458
--- /dev/null
+++ b/packages/shared/prisma/migrations/20260904151000_add_ai_gateway_control_plane/migration.sql
@@ -0,0 +1,87 @@
+-- Prisma does not automatically wrap PostgreSQL migrations in a transaction.
+BEGIN;
+
+-- Adding a foreign key takes SHARE ROW EXCLUSIVE on the *referenced* table, which conflicts with
+-- the ROW EXCLUSIVE that every INSERT/UPDATE/DELETE takes. The FKs below reference `organizations`,
+-- `projects`, `users` and `api_keys`, so for as long as this transaction is open, all writes to
+-- those tables block -- including hot paths such as the `api_keys.last_used_at` update on every
+-- authenticated API request. The window is milliseconds because every referencing table is created
+-- empty a few lines above its FK.
+--
+-- The timeouts make that bound explicit: if an in-flight writer delays us, we fail fast instead of
+-- parking a SHARE ROW EXCLUSIVE request at the head of the lock queue, where every subsequent
+-- writer would pile up behind it.
+SET LOCAL lock_timeout = '5s';
+SET LOCAL statement_timeout = '30s';
+
+CREATE TYPE "GatewayIngestionMode" AS ENUM ('usage', 'full');
+
+CREATE TYPE "GatewayProvider" AS ENUM ('openai', 'anthropic');
+
+CREATE TYPE "GatewayConnectionStatus" AS ENUM ('enabled', 'disabled', 'error');
+
+CREATE TABLE "gateway_configs" (
+ "organization_id" TEXT NOT NULL,
+ "default_ingestion_project_id" TEXT,
+ "ingestion_mode" "GatewayIngestionMode" NOT NULL DEFAULT 'usage',
+ "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+
+ CONSTRAINT "gateway_configs_pkey" PRIMARY KEY ("organization_id")
+);
+
+ALTER TABLE "gateway_configs"
+ADD CONSTRAINT "gateway_configs_organization_id_fkey"
+FOREIGN KEY ("organization_id") REFERENCES "organizations"("id")
+ON DELETE CASCADE ON UPDATE CASCADE;
+
+ALTER TABLE "gateway_configs"
+ADD CONSTRAINT "gateway_configs_default_ingestion_project_id_fkey"
+FOREIGN KEY ("default_ingestion_project_id") REFERENCES "projects"("id")
+ON DELETE SET NULL ON UPDATE CASCADE;
+
+CREATE TABLE "gateway_ai_connections" (
+ "id" TEXT NOT NULL,
+ "organization_id" TEXT NOT NULL,
+ "name" TEXT NOT NULL,
+ "provider" "GatewayProvider" NOT NULL,
+ "encrypted_credentials" TEXT NOT NULL,
+ "display_secret_key" TEXT NOT NULL,
+ "created_by_id" TEXT,
+ "routing_priority" INTEGER NOT NULL,
+ "status" "GatewayConnectionStatus" NOT NULL DEFAULT 'enabled',
+ "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+
+ CONSTRAINT "gateway_ai_connections_pkey" PRIMARY KEY ("id")
+);
+
+CREATE UNIQUE INDEX "gateway_ai_connections_organization_id_routing_priority_key"
+ON "gateway_ai_connections"("organization_id", "routing_priority");
+
+CREATE INDEX "gateway_ai_connections_organization_id_status_routing_prior_idx"
+ON "gateway_ai_connections"("organization_id", "status", "routing_priority");
+
+ALTER TABLE "gateway_ai_connections"
+ADD CONSTRAINT "gateway_ai_connections_organization_id_fkey"
+FOREIGN KEY ("organization_id") REFERENCES "organizations"("id")
+ON DELETE CASCADE ON UPDATE CASCADE;
+
+ALTER TABLE "gateway_ai_connections"
+ADD CONSTRAINT "gateway_ai_connections_created_by_id_fkey"
+FOREIGN KEY ("created_by_id") REFERENCES "users"("id")
+ON DELETE SET NULL ON UPDATE CASCADE;
+
+CREATE TABLE "gateway_api_key_associations" (
+ "api_key_id" TEXT NOT NULL,
+ "metadata" JSONB NOT NULL DEFAULT '{}',
+
+ CONSTRAINT "gateway_api_key_associations_pkey" PRIMARY KEY ("api_key_id")
+);
+
+ALTER TABLE "gateway_api_key_associations"
+ADD CONSTRAINT "gateway_api_key_associations_api_key_id_fkey"
+FOREIGN KEY ("api_key_id") REFERENCES "api_keys"("id")
+ON DELETE CASCADE ON UPDATE CASCADE;
+
+COMMIT;
diff --git a/packages/shared/prisma/schema.prisma b/packages/shared/prisma/schema.prisma
index 3fae3c439b..d4d3c57387 100644
--- a/packages/shared/prisma/schema.prisma
+++ b/packages/shared/prisma/schema.prisma
@@ -123,6 +123,8 @@ model Organization {
surveys Survey[]
cloudSpendAlerts CloudSpendAlert[]
verifiedDomains VerifiedDomain[]
+ gatewayConfig GatewayConfig?
+ gatewayAiConnections GatewayAiConnection[]
@@map("organizations")
}
@@ -191,6 +193,7 @@ model Project {
InAppAgentEvent InAppAgentEvent[]
InAppAgentRun InAppAgentRun[]
InAppAgentPendingToolApproval InAppAgentPendingToolApproval[]
+ gatewayConfigs GatewayConfig[] @relation("GatewayDefaultIngestionProject")
@@index([orgId])
@@map("projects")
@@ -202,26 +205,27 @@ enum ApiKeyScope {
}
model ApiKey {
- id String @id @unique @default(cuid())
- createdAt DateTime @default(now()) @map("created_at")
+ id String @id @unique @default(cuid())
+ createdAt DateTime @default(now()) @map("created_at")
note String?
- publicKey String @unique @map("public_key")
- hashedSecretKey String @unique @map("hashed_secret_key")
- fastHashedSecretKey String? @unique @map("fast_hashed_secret_key")
- displaySecretKey String @map("display_secret_key")
- lastUsedAt DateTime? @map("last_used_at")
- expiresAt DateTime? @map("expires_at")
- isInAppAgentKey Boolean @default(false) @map("is_in_app_agent_key")
- projectId String? @map("project_id")
- project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)
- orgId String? @map("organization_id")
- organization Organization? @relation(fields: [orgId], references: [id], onDelete: Cascade)
- scope ApiKeyScope @default(PROJECT) @map("scope")
- createdByUserId String? @map("created_by_user_id")
- createdByUser User? @relation("ApiKeyCreatedByUser", fields: [createdByUserId], references: [id], onDelete: SetNull)
- createdByApiKeyId String? @map("created_by_api_key_id")
- createdByApiKey ApiKey? @relation("ApiKeyCreatedByApiKey", fields: [createdByApiKeyId], references: [id], onDelete: SetNull)
- apiKeysCreated ApiKey[] @relation("ApiKeyCreatedByApiKey")
+ publicKey String @unique @map("public_key")
+ hashedSecretKey String @unique @map("hashed_secret_key")
+ fastHashedSecretKey String? @unique @map("fast_hashed_secret_key")
+ displaySecretKey String @map("display_secret_key")
+ lastUsedAt DateTime? @map("last_used_at")
+ expiresAt DateTime? @map("expires_at")
+ isInAppAgentKey Boolean @default(false) @map("is_in_app_agent_key")
+ projectId String? @map("project_id")
+ project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)
+ orgId String? @map("organization_id")
+ organization Organization? @relation(fields: [orgId], references: [id], onDelete: Cascade)
+ scope ApiKeyScope @default(PROJECT) @map("scope")
+ createdByUserId String? @map("created_by_user_id")
+ createdByUser User? @relation("ApiKeyCreatedByUser", fields: [createdByUserId], references: [id], onDelete: SetNull)
+ createdByApiKeyId String? @map("created_by_api_key_id")
+ createdByApiKey ApiKey? @relation("ApiKeyCreatedByApiKey", fields: [createdByApiKeyId], references: [id], onDelete: SetNull)
+ apiKeysCreated ApiKey[] @relation("ApiKeyCreatedByApiKey")
+ gatewayAssociation GatewayApiKeyAssociation?
@@index(orgId)
@@index(projectId)
@@ -232,26 +236,81 @@ model ApiKey {
@@map("api_keys")
}
+enum GatewayIngestionMode {
+ USAGE @map("usage")
+ FULL @map("full")
+}
+
+enum GatewayProvider {
+ OPENAI @map("openai")
+ ANTHROPIC @map("anthropic")
+}
+
+enum GatewayConnectionStatus {
+ ENABLED @map("enabled")
+ DISABLED @map("disabled")
+ ERROR @map("error")
+}
+
+model GatewayConfig {
+ organizationId String @id @map("organization_id")
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
+ defaultIngestionProjectId String? @map("default_ingestion_project_id")
+ defaultIngestionProject Project? @relation("GatewayDefaultIngestionProject", fields: [defaultIngestionProjectId], references: [id], onDelete: SetNull)
+ ingestionMode GatewayIngestionMode @default(USAGE) @map("ingestion_mode")
+ createdAt DateTime @default(now()) @map("created_at")
+ updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
+
+ @@map("gateway_configs")
+}
+
+model GatewayAiConnection {
+ id String @id @default(cuid())
+ organizationId String @map("organization_id")
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
+ name String
+ provider GatewayProvider
+ encryptedCredential String @map("encrypted_credentials")
+ displaySecret String @map("display_secret_key")
+ createdById String? @map("created_by_id")
+ routingPriority Int @map("routing_priority")
+ status GatewayConnectionStatus @default(ENABLED)
+ createdAt DateTime @default(now()) @map("created_at")
+ updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
+
+ @@unique([organizationId, routingPriority])
+ @@index([organizationId, status, routingPriority])
+ @@map("gateway_ai_connections")
+}
+
+model GatewayApiKeyAssociation {
+ apiKeyId String @id @map("api_key_id")
+ apiKey ApiKey @relation(fields: [apiKeyId], references: [id], onDelete: Cascade)
+ metadata Json @default("{}") @db.JsonB
+
+ @@map("gateway_api_key_associations")
+}
+
enum InAppAgentConversationVisibilityScope {
PERSONAL
PROJECT
}
model InAppAgentConversation {
- id String
- projectId String @map("project_id")
- project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
- createdByUserId String? @map("created_by_user_id")
- createdByUser User? @relation("InAppAgentConversationCreatedByUser", fields: [createdByUserId], references: [id], onDelete: SetNull)
- title String?
- renamedByUserAt DateTime? @map("renamed_by_user_at")
- visibilityScope InAppAgentConversationVisibilityScope @default(PERSONAL) @map("visibility_scope")
- providerSessionId String? @map("provider_session_id")
+ id String
+ projectId String @map("project_id")
+ project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
+ createdByUserId String? @map("created_by_user_id")
+ createdByUser User? @relation("InAppAgentConversationCreatedByUser", fields: [createdByUserId], references: [id], onDelete: SetNull)
+ title String?
+ renamedByUserAt DateTime? @map("renamed_by_user_at")
+ visibilityScope InAppAgentConversationVisibilityScope @default(PERSONAL) @map("visibility_scope")
+ providerSessionId String? @map("provider_session_id")
// Prefixed tool grants are revalidated against the registry and owner's role each run.
- alwaysAllowedTools String[] @default([]) @map("always_allowed_tools")
- deletedAt DateTime? @map("deleted_at")
- createdAt DateTime @default(now()) @map("created_at")
- updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
+ alwaysAllowedTools String[] @default([]) @map("always_allowed_tools")
+ deletedAt DateTime? @map("deleted_at")
+ createdAt DateTime @default(now()) @map("created_at")
+ updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
events InAppAgentEvent[]
runs InAppAgentRun[]
@@ -1016,7 +1075,7 @@ model JobConfiguration {
variableMapping Json @map("variable_mapping")
sampling Decimal // ratio of jobs that are executed for sampling (0..1)
delay Int // delay in milliseconds
- timeScope String[] @default(["NEW"]) @map("time_scope")
+ timeScope String[] @default(["NEW"]) @map("time_scope")
@@index([projectId, id])
@@map("job_configurations")
diff --git a/packages/shared/scripts/seeder/seed-postgres.ts b/packages/shared/scripts/seeder/seed-postgres.ts
index 8e7baa9f9d..d9a78bb796 100644
--- a/packages/shared/scripts/seeder/seed-postgres.ts
+++ b/packages/shared/scripts/seeder/seed-postgres.ts
@@ -16,7 +16,12 @@ import {
ScoreDataTypeEnum,
type ScoreDataTypeType,
} from "../../src/index";
-import { getDisplaySecretKey, hashSecretKey, logger } from "../../src/server";
+import {
+ createAndAddApiKeysToDb,
+ getDisplaySecretKey,
+ hashSecretKey,
+ logger,
+} from "../../src/server";
import { redis } from "../../src/server/redis/redis";
import {
DEFAULT_SEED_API_KEY,
@@ -130,7 +135,7 @@ async function main() {
// Realistic support chat scenario
await createSupportChatSession(project1);
- await prisma.organizationMembership.upsert({
+ const orgMembership = await prisma.organizationMembership.upsert({
where: {
orgId_userId: {
userId: user.id,
@@ -178,6 +183,12 @@ async function main() {
},
});
+ await seedAiGateway({
+ organizationId: seedOrgId,
+ userId: user.id,
+ orgMembershipId: orgMembership.id,
+ });
+
const summaryPrompt = await prisma.prompt.upsert({
where: {
projectId_name_version: {
@@ -505,6 +516,115 @@ main()
process.exit(1);
});
+async function seedAiGateway(params: {
+ organizationId: string;
+ userId: string;
+ orgMembershipId: string;
+}) {
+ // The gateway gets its own project rather than the shared seed project.
+ // Organization members do not inherit access to the ingestion project (see
+ // resolveProjectRole), so pointing the config at the seed project would take
+ // that project away from every seeded user.
+ const ingestionProjectId = "c2ba9dcb-1f39-4f2f-a4e6-1f0b6a8f5c11";
+ await prisma.project.upsert({
+ where: { id: ingestionProjectId },
+ create: {
+ id: ingestionProjectId,
+ name: "AI Gateway Ingestion",
+ orgId: params.organizationId,
+ },
+ update: { orgId: params.organizationId },
+ });
+ // Mirrors what the control plane writes when it creates the project: the
+ // creator keeps an explicit role so the project stays reachable in the UI.
+ await prisma.projectMembership.upsert({
+ where: {
+ projectId_userId: {
+ projectId: ingestionProjectId,
+ userId: params.userId,
+ },
+ },
+ create: {
+ projectId: ingestionProjectId,
+ userId: params.userId,
+ orgMembershipId: params.orgMembershipId,
+ role: "OWNER",
+ },
+ update: { orgMembershipId: params.orgMembershipId },
+ });
+ await prisma.gatewayConfig.upsert({
+ where: { organizationId: params.organizationId },
+ create: {
+ organizationId: params.organizationId,
+ defaultIngestionProjectId: ingestionProjectId,
+ ingestionMode: "USAGE",
+ },
+ update: {
+ defaultIngestionProjectId: ingestionProjectId,
+ },
+ });
+
+ if (
+ (await prisma.gatewayAiConnection.count({
+ where: { organizationId: params.organizationId },
+ })) === 0
+ ) {
+ await prisma.gatewayAiConnection.createMany({
+ data: [
+ {
+ id: "seed-gateway-openai",
+ organizationId: params.organizationId,
+ name: "OpenAI production",
+ provider: "OPENAI",
+ encryptedCredential: encrypt("sk-seed-openai-not-valid"),
+ displaySecret: getDisplaySecretKey("sk-seed-openai-not-valid"),
+ createdById: params.userId,
+ routingPriority: 0,
+ status: "ENABLED",
+ },
+ {
+ id: "seed-gateway-anthropic",
+ organizationId: params.organizationId,
+ name: "Anthropic production",
+ provider: "ANTHROPIC",
+ encryptedCredential: encrypt("sk-ant-seed-not-valid"),
+ displaySecret: getDisplaySecretKey("sk-ant-seed-not-valid"),
+ createdById: params.userId,
+ routingPriority: 1,
+ status: "ENABLED",
+ },
+ ],
+ });
+ }
+
+ const publicKey = "pk-lf-gateway-seed";
+ const existingKey = await prisma.apiKey.findUnique({
+ where: { publicKey },
+ select: { id: true },
+ });
+ const key =
+ existingKey ??
+ (await createAndAddApiKeysToDb({
+ prisma,
+ entityId: params.organizationId,
+ scope: "ORGANIZATION",
+ note: "Seeded gateway key",
+ createdByUserId: params.userId,
+ predefinedKeys: {
+ publicKey,
+ secretKey: "sk-lf-gateway-seed",
+ },
+ }));
+ await prisma.gatewayApiKeyAssociation.upsert({
+ where: { apiKeyId: key.id },
+ create: {
+ apiKeyId: key.id,
+ metadata: { environment: "development", team: "platform" },
+ },
+ update: {},
+ });
+}
+
async function createDashboardsAndWidgets(projects: Project[]) {
logger.info("Creating dashboards and widgets");
diff --git a/packages/shared/src/db.ts b/packages/shared/src/db.ts
index 716d2ee272..b7c0dd4c9b 100644
--- a/packages/shared/src/db.ts
+++ b/packages/shared/src/db.ts
@@ -36,6 +36,9 @@ const createPrismaInstance = () => {
remoteExperimentSecretKey: true,
remoteExperimentRequestHeaders: true,
},
+ gatewayAiConnection: {
+ encryptedCredential: true,
+ },
},
});
diff --git a/packages/shared/src/interfaces/rate-limits.ts b/packages/shared/src/interfaces/rate-limits.ts
index fc859742a2..72a5d3b001 100644
--- a/packages/shared/src/interfaces/rate-limits.ts
+++ b/packages/shared/src/interfaces/rate-limits.ts
@@ -1,5 +1,5 @@
import z from "zod";
-import { ApiAccessScope } from "../server";
+import { ApiAccessScopeWithOptionalApiKeyId } from "../server";
export const RateLimitResource = z.enum([
"ingestion",
@@ -23,7 +23,7 @@ export const RateLimitResource = z.enum([
export type RateLimitResult = {
resource: z.infer<typeof RateLimitResource>;
points: number;
- scope: ApiAccessScope;
+ scope: ApiAccessScopeWithOptionalApiKeyId;
// from rate-limiter-flexible
remainingPoints: number;
diff --git a/packages/shared/src/server/auth/types.ts b/packages/shared/src/server/auth/types.ts
index ca32736b35..bd8ba25cdc 100644
--- a/packages/shared/src/server/auth/types.ts
+++ b/packages/shared/src/server/auth/types.ts
@@ -80,3 +80,12 @@ export type ApiAccessScopeIngestion = BaseApiAccessScope &
MakeOptional<ApiAccessScopeMetadata>;
export type ApiAccessScope = BaseApiAccessScope & ApiAccessScopeMetadata;
+
+// Gateway ingestion token don't specify the originating API key as they might be
+// batched across users
+export type ApiAccessScopeWithOptionalApiKeyId = Omit<
+ ApiAccessScope,
+ "apiKeyId"
+> & {
+ apiKeyId?: string;
+};
diff --git a/packages/shared/src/server/auth/userProjectRoleAuth.ts b/packages/shared/src/server/auth/userProjectRoleAuth.ts
index 49bfb7f213..ad371119a7 100644
--- a/packages/shared/src/server/auth/userProjectRoleAuth.ts
+++ b/packages/shared/src/server/auth/userProjectRoleAuth.ts
@@ -71,7 +71,7 @@ function generateUserProjectRolesQuery({
WHERE om.org_id = ${orgId}
AND om.role != 'NONE'
AND NOT EXISTS (
- SELECT 1 FROM project_memberships pm
+ SELECT 1 FROM project_memberships pm
WHERE pm.org_membership_id = om.id
AND pm.project_id = ${projectId}
)
diff --git a/web/.storybook/main.ts b/web/.storybook/main.ts
index e6a644bce9..81be8aa3e6 100644
--- a/web/.storybook/main.ts
+++ b/web/.storybook/main.ts
@@ -26,6 +26,10 @@ const STORY_TITLE_GROUPS: StoryTitleGroup[] = [
directory: "src/components/design-system/table/components",
titlePrefix: "Design/Components/Table/Cells",
},
+ {
+ directory: "src/features/ai-gateway/components",
+ titlePrefix: "Features/AI Gateway",
+ },
{
directory: "src/features/evals/v2/components",
titlePrefix: "Features/Evaluations",
diff --git a/web/AGENTS.md b/web/AGENTS.md
index 71e459435c..d28f08b92a 100644
--- a/web/AGENTS.md
+++ b/web/AGENTS.md
@@ -241,6 +241,7 @@ Sentry instrumentation skill first and decide whether it should capture at all
- In-source tests: `pnpm --filter web run test:in-source <args>`
- Client tests: `pnpm --filter web run test-client <args>`
- E2E tests: `pnpm --filter web run test:e2e`
+- AI Gateway E2E tests against the local configured stack (also run in CI for AI Gateway changes): `pnpm --filter web run test:e2e:ai-gateway`
- Agent browser install to the default user-level Playwright cache: `pnpm run playwright:install`
- Build: `pnpm --filter web run build`
- Structure-RFC violation counts: `pnpm --filter web run structure:stats`
diff --git a/web/package.json b/web/package.json
index b2ee0e3c23..18b5e449f4 100644
--- a/web/package.json
+++ b/web/package.json
@@ -25,6 +25,7 @@
"test-storybook": "vitest run --project storybook",
"test:e2e": "dotenv -e ../.env.test -e ../.env -- playwright test --reporter=line",
"test:e2e:server": "vitest run --project e2e-server",
+ "test:e2e:ai-gateway": "dotenv -e ../.env -- vitest run --project ai-gateway-e2e-server",
"build-trace": "dotenv -e ../.env -- next internal trace .next/trace-turbopack",
"storybook": "dotenv -e ../.env -- storybook dev -p 6006",
"structure:stats": "node scripts/structure/stats.mjs",
diff --git a/web/src/__e2e__/ai-gateway.gatewaye2e.ts b/web/src/__e2e__/ai-gateway.gatewaye2e.ts
new file mode 100644
index 0000000000..58b66fb954
--- /dev/null
+++ b/web/src/__e2e__/ai-gateway.gatewaye2e.ts
@@ -0,0 +1,711 @@
+import { createHash, randomBytes } from "node:crypto";
+
+import type { Session } from "next-auth";
+import { afterAll, beforeAll, describe, expect, it } from "vitest";
+import waitForExpect from "wait-for-expect";
+
+import { env } from "@/src/env.mjs";
+import { $root } from "@/src/pages/api/public/otel/otlp-proto/generated/root";
+import {
+ type GatewayApiFormat,
+ GatewayModelsResponseSchema,
+ GatewayResolveResponseSchema,
+} from "@/src/features/ai-gateway/server/provider";
+import type { GatewayProviderName } from "@/src/features/ai-gateway/server/provider/registry";
+import { appRouter } from "@/src/server/api/root";
+import { createInnerTRPCContext } from "@/src/server/api/trpc";
+import { signHmacSha256 } from "@/src/server/utils/hmac";
+import { encrypt } from "@langfuse/shared/encryption";
+import {
+ GatewayConnectionStatus,
+ GatewayIngestionMode,
+ GatewayProvider,
+ prisma,
+ Role,
+} from "@langfuse/shared/src/db";
+import {
+ getObservationByIdFromEventsTable,
+ getTraceByIdFromEventsTable,
+ redis,
+} from "@langfuse/shared/src/server";
+import { getDisplaySecretKey } from "@langfuse/shared/src/server/auth/apiKeys";
+
+const BASE_URL = (
+ process.env.LANGFUSE_AI_GATEWAY_E2E_BASE_URL ?? "http://localhost:3000"
+).replace(/\/$/, "");
+const ORGANIZATION_ID =
+ process.env.LANGFUSE_AI_GATEWAY_E2E_ORGANIZATION_ID ?? "seed-org-id";
+const PROJECT_ID_OVERRIDE = process.env.LANGFUSE_AI_GATEWAY_E2E_PROJECT_ID;
+const DEFAULT_PROJECT_ID = "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a";
+const MODELS_PATH = "/api/internal/ai-gateway/v1/models";
+const RESOLVE_PATH = "/api/internal/ai-gateway/v1/resolve";
+const TEST_TIMEOUT_MS = 120_000;
+const PROVIDER_CREDENTIALS: Partial<Record<GatewayProviderName, string>> = {
+ OPENAI: process.env.LANGFUSE_LLM_CONNECTION_OPENAI_KEY,
+ ANTHROPIC: process.env.LANGFUSE_LLM_CONNECTION_ANTHROPIC_KEY,
+};
+const ENABLED_PROVIDERS = Object.values(GatewayProvider);
+
+const PROVIDER_FORMATS = [
+ {
+ provider: GatewayProvider.OPENAI,
+ apiFormat: "openai.responses",
+ baseUrl: "https://api.openai.com/v1",
+ authType: "Bearer",
+ },
+ {
+ provider: GatewayProvider.OPENAI,
+ apiFormat: "openai.chat-completions",
+ baseUrl: "https://api.openai.com/v1",
+ authType: "Bearer",
+ },
+ {
+ provider: GatewayProvider.ANTHROPIC,
+ apiFormat: "anthropic.messages",
+ baseUrl: "https://api.anthropic.com/v1",
+ authType: "x-api-key",
+ },
+] as const satisfies ReadonlyArray<{
+ provider: GatewayProviderName;
+ apiFormat: GatewayApiFormat;
+ baseUrl: string;
+ authType: "Bearer" | "x-api-key";
+}>;
+
+type ConnectionSnapshot = {
+ id: string;
+ status: GatewayConnectionStatus;
+ encryptedCredential: string;
+ displaySecret: string;
+ updatedAt: Date;
+};
+
+type GatewayConfigSnapshot = {
+ defaultIngestionProjectId: string | null;
+ ingestionMode: GatewayIngestionMode;
+ updatedAt: Date;
+};
+
+type Admin = Awaited<ReturnType<typeof createGatewayAdmin>>;
+
+let admin: Admin;
+let defaultProjectId: string;
+let gatewayConfigSnapshot: GatewayConfigSnapshot | null = null;
+let gatewayConfigPrepared = false;
+let connectionSnapshots: ConnectionSnapshot[] = [];
+const createdConnectionIds = new Set<string>();
+const connectionIdsByProvider = new Map<GatewayProviderName, string>();
+const apiKeyIds = new Set<string>();
+const originalAllowlist = [
+ ...env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST,
+];
+
+describe("AI gateway live end-to-end", () => {
+ beforeAll(async () => {
+ assertLocalTestTarget();
+
+ if (!env.LANGFUSE_AI_GATEWAY_SERVICE_KEY) {
+ throw new Error(
+ "Set LANGFUSE_AI_GATEWAY_SERVICE_KEY in .env and restart the web server before running the gateway E2E suite.",
+ );
+ }
+ if (process.env.CI) {
+ const missingProviders = ENABLED_PROVIDERS.filter(
+ (provider) => !PROVIDER_CREDENTIALS[provider],
+ );
+ if (missingProviders.length > 0) {
+ throw new Error(
+ `Missing CI gateway provider credentials for: ${missingProviders.join(", ")}`,
+ );
+ }
+ }
+
+ const health = await fetch(`${BASE_URL}/api/public/health`);
+ if (!health.ok) {
+ throw new Error(
+ `Langfuse is not healthy at ${BASE_URL}. Start the local web and worker stack before running the gateway E2E suite.`,
+ );
+ }
+
+ const config = await prisma.gatewayConfig.findUnique({
+ where: { organizationId: ORGANIZATION_ID },
+ });
+ gatewayConfigSnapshot = config
+ ? {
+ defaultIngestionProjectId: config.defaultIngestionProjectId,
+ ingestionMode: config.ingestionMode,
+ updatedAt: config.updatedAt,
+ }
+ : null;
+ const projectId =
+ PROJECT_ID_OVERRIDE ??
+ config?.defaultIngestionProjectId ??
+ DEFAULT_PROJECT_ID;
+ const project = await prisma.project.findFirst({
+ where: {
+ id: projectId,
+ orgId: ORGANIZATION_ID,
+ deletedAt: null,
+ },
+ select: { id: true },
+ });
+ if (!project) {
+ throw new Error(
+ `Set LANGFUSE_AI_GATEWAY_E2E_PROJECT_ID to an active project in organization ${ORGANIZATION_ID}.`,
+ );
+ }
+ defaultProjectId = project.id;
+ await prisma.gatewayConfig.upsert({
+ where: { organizationId: ORGANIZATION_ID },
+ create: {
+ organizationId: ORGANIZATION_ID,
+ defaultIngestionProjectId: defaultProjectId,
+ ingestionMode: GatewayIngestionMode.FULL,
+ },
+ update: {
+ defaultIngestionProjectId: defaultProjectId,
+ ingestionMode: GatewayIngestionMode.FULL,
+ },
+ });
+ gatewayConfigPrepared = true;
+
+ const connections = await prisma.gatewayAiConnection.findMany({
+ where: { organizationId: ORGANIZATION_ID },
+ orderBy: { routingPriority: "asc" },
+ select: {
+ id: true,
+ provider: true,
+ status: true,
+ encryptedCredential: true,
+ displaySecret: true,
+ routingPriority: true,
+ updatedAt: true,
+ },
+ });
+ connectionSnapshots = connections.map(
+ ({ id, status, encryptedCredential, displaySecret, updatedAt }) => ({
+ id,
+ status,
+ encryptedCredential,
+ displaySecret,
+ updatedAt,
+ }),
+ );
+
+ let nextRoutingPriority =
+ Math.max(
+ -1,
+ ...connections.map(({ routingPriority }) => routingPriority),
+ ) + 1;
+ for (const provider of ENABLED_PROVIDERS) {
+ const credential = PROVIDER_CREDENTIALS[provider];
+ const existingConnection = connections.find(
+ (candidate) => candidate.provider === provider,
+ );
+ if (credential && existingConnection) {
+ await prisma.gatewayAiConnection.update({
+ where: { id: existingConnection.id },
+ data: {
+ encryptedCredential: encrypt(credential),
+ displaySecret: getDisplaySecretKey(credential),
+ status: GatewayConnectionStatus.ENABLED,
+ },
+ });
+ connectionIdsByProvider.set(provider, existingConnection.id);
+ continue;
+ }
+ if (credential) {
+ const connection = await prisma.gatewayAiConnection.create({
+ data: {
+ organizationId: ORGANIZATION_ID,
+ name: `Gateway E2E ${provider}`,
+ provider,
+ encryptedCredential: encrypt(credential),
+ displaySecret: getDisplaySecretKey(credential),
+ routingPriority: nextRoutingPriority,
+ status: GatewayConnectionStatus.ENABLED,
+ },
+ });
+ nextRoutingPriority += 1;
+ createdConnectionIds.add(connection.id);
+ connectionIdsByProvider.set(provider, connection.id);
+ continue;
+ }
+ const enabledConnection = connections.find(
+ (candidate) =>
+ candidate.provider === provider &&
+ candidate.status === GatewayConnectionStatus.ENABLED,
+ );
+ if (enabledConnection) {
+ connectionIdsByProvider.set(provider, enabledConnection.id);
+ }
+ }
+
+ if (
+ !env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST.includes(
+ ORGANIZATION_ID,
+ )
+ ) {
+ env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST.push(ORGANIZATION_ID);
+ }
+ admin = await createGatewayAdmin();
+ }, 30_000);
+
+ afterAll(async () => {
+ try {
+ await cleanUpTestArtifacts();
+ } finally {
+ try {
+ await restoreProviderConnections();
+ } finally {
+ try {
+ await restoreGatewayConfig();
+ } finally {
+ env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST.splice(
+ 0,
+ env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST.length,
+ ...originalAllowlist,
+ );
+ }
+ }
+ }
+ }, 30_000);
+
+ describe("model discovery", () => {
+ it.each(PROVIDER_FORMATS)(
+ "$provider / $apiFormat returns canonical models",
+ async ({ provider, apiFormat }) => {
+ await withGatewayApiKey(
+ { provider, apiFormat },
+ async ({ secretKey }) => {
+ const modelsBody = await gatewayControlPlaneRequest({
+ path: MODELS_PATH,
+ apiFormat,
+ virtualSecretKey: secretKey,
+ });
+ const models = GatewayModelsResponseSchema.parse(modelsBody);
+ expect(models.data.length).toBeGreaterThan(0);
+ expect(models.data).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ id: expect.stringMatching(/\S/),
+ provider,
+ canonicalSlug: expect.stringMatching(/\S/),
+ displayName: expect.stringMatching(/\S/),
+ createdAt: expect.stringMatching(
+ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
+ ),
+ }),
+ ]),
+ );
+ },
+ );
+ },
+ TEST_TIMEOUT_MS,
+ );
+ });
+
+ describe("gateway roundtrip", () => {
+ it.each(PROVIDER_FORMATS)(
+ "$provider / $apiFormat resolves credentials and ingests telemetry",
+ async ({ provider, apiFormat, baseUrl, authType }) => {
+ await withGatewayApiKey(
+ { provider, apiFormat },
+ async ({ secretKey, id: keyId }) => {
+ const resolveBody = await gatewayControlPlaneRequest({
+ path: RESOLVE_PATH,
+ apiFormat,
+ virtualSecretKey: secretKey,
+ });
+ const resolved = GatewayResolveResponseSchema.parse(resolveBody);
+
+ expect(resolved.connection.api_format).toBe(apiFormat);
+ expect(resolved.connection.base_url).toBe(baseUrl);
+ if (authType === "x-api-key") {
+ expect(resolved.connection.auth).toMatchObject({
+ type: "x-api-key",
+ header: "x-api-key",
+ value: expect.stringMatching(/\S/),
+ });
+ } else {
+ expect(resolved.connection.auth).toMatchObject({
+ type: "Bearer",
+ token: expect.stringMatching(/\S/),
+ });
+ }
+ expect(resolved.attribution).toEqual({
+ organization_id: ORGANIZATION_ID,
+ project_id: expect.stringMatching(/\S/),
+ key_id: keyId,
+ key_metadata: {
+ test: true,
+ provider,
+ apiFormat,
+ },
+ });
+ expect(resolved.ingestion).toMatchObject({
+ access_token: expect.stringMatching(/\S/),
+ token_type: "Bearer",
+ expires_at: expect.any(Number),
+ });
+
+ const ingestionToken = resolved.ingestion?.access_token;
+ if (!ingestionToken)
+ throw new Error("Resolve did not return an ingestion token");
+
+ await ingestTrace(ingestionToken, {
+ provider,
+ apiFormat,
+ });
+ },
+ );
+ },
+ TEST_TIMEOUT_MS,
+ );
+ });
+});
+
+async function withGatewayApiKey(
+ input: {
+ provider: GatewayProviderName;
+ apiFormat: GatewayApiFormat;
+ },
+ run: (key: { secretKey: string; id: string }) => Promise<void>,
+) {
+ const targetConnectionId = connectionIdsByProvider.get(input.provider);
+ if (!targetConnectionId) {
+ throw new Error(
+ `Add and enable a valid ${input.provider} connection for organization ${ORGANIZATION_ID} before running this test.`,
+ );
+ }
+ await selectOnlyConnection(targetConnectionId);
+
+ const gatewayKey = await admin.caller.aiGateway.createApiKey({
+ orgId: ORGANIZATION_ID,
+ note: `Gateway E2E ${input.provider} ${input.apiFormat}`,
+ metadata: {
+ test: true,
+ provider: input.provider,
+ apiFormat: input.apiFormat,
+ },
+ });
+ apiKeyIds.add(gatewayKey.id);
+
+ try {
+ const listedKeys = await admin.caller.aiGateway.listApiKeys({
+ orgId: ORGANIZATION_ID,
+ });
+ expect(
+ listedKeys.data.some(({ apiKey }) => apiKey.id === gatewayKey.id),
+ ).toBe(true);
+
+ await run({ secretKey: gatewayKey.secretKey, id: gatewayKey.id });
+ } finally {
+ await admin.caller.aiGateway.revokeApiKey({
+ orgId: ORGANIZATION_ID,
+ id: gatewayKey.id,
+ });
+ apiKeyIds.delete(gatewayKey.id);
+ }
+}
+
+async function cleanUpTestArtifacts() {
+ if (apiKeyIds.size > 0) {
+ await prisma.apiKey.deleteMany({
+ where: { id: { in: [...apiKeyIds] } },
+ });
+ }
+}
+
+async function restoreProviderConnections() {
+ if (createdConnectionIds.size > 0) {
+ await prisma.gatewayAiConnection.deleteMany({
+ where: { id: { in: [...createdConnectionIds] } },
+ });
+ }
+ if (connectionSnapshots.length === 0) return;
+
+ await prisma.$transaction(
+ connectionSnapshots.map((connection) =>
+ prisma.gatewayAiConnection.update({
+ where: { id: connection.id },
+ data: {
+ status: connection.status,
+ encryptedCredential: connection.encryptedCredential,
+ displaySecret: connection.displaySecret,
+ updatedAt: connection.updatedAt,
+ },
+ }),
+ ),
+ );
+ await clearModelCaches([
+ ...connectionSnapshots.map(({ id }) => id),
+ ...createdConnectionIds,
+ ]);
+}
+
+async function restoreGatewayConfig() {
+ if (!gatewayConfigPrepared) return;
+
+ if (gatewayConfigSnapshot) {
+ await prisma.gatewayConfig.update({
+ where: { organizationId: ORGANIZATION_ID },
+ data: gatewayConfigSnapshot,
+ });
+ } else {
+ await prisma.gatewayConfig.deleteMany({
+ where: { organizationId: ORGANIZATION_ID },
+ });
+ }
+}
+
+async function createGatewayAdmin() {
+ const membership = await prisma.organizationMembership.findFirst({
+ where: { orgId: ORGANIZATION_ID, role: Role.OWNER },
+ select: { userId: true },
+ });
+ if (!membership) {
+ throw new Error(
+ `Organization ${ORGANIZATION_ID} needs an owner before running this suite.`,
+ );
+ }
+ const [organization, user] = await Promise.all([
+ prisma.organization.findUniqueOrThrow({
+ where: { id: ORGANIZATION_ID },
+ select: { id: true, name: true },
+ }),
+ prisma.user.findUniqueOrThrow({
+ where: { id: membership.userId },
+ select: { id: true, name: true },
+ }),
+ ]);
+ const session: Session = {
+ expires: "1",
+ user: {
+ id: user.id,
+ name: user.name,
+ canCreateOrganizations: true,
+ featureFlags: {} as NonNullable<Session["user"]>["featureFlags"],
+ organizations: [
+ {
+ id: organization.id,
+ name: organization.name,
+ role: Role.OWNER,
+ cloudConfig: undefined,
+ plan: "oss",
+ metadata: {},
+ aiFeaturesEnabled: false,
+ aiTelemetryEnabled: true,
+ projects: [],
+ },
+ ],
+ },
+ environment: {} as Session["environment"],
+ };
+ const context = createInnerTRPCContext({ session, headers: {} });
+ return { caller: appRouter.createCaller({ ...context, prisma }) };
+}
+
+async function selectOnlyConnection(targetConnectionId: string) {
+ await prisma.$transaction([
+ prisma.gatewayAiConnection.updateMany({
+ where: {
+ organizationId: ORGANIZATION_ID,
+ id: { not: targetConnectionId },
+ },
+ data: { status: GatewayConnectionStatus.DISABLED },
+ }),
+ prisma.gatewayAiConnection.update({
+ where: { id: targetConnectionId },
+ data: { status: GatewayConnectionStatus.ENABLED },
+ }),
+ ]);
+ await clearModelCaches(connectionSnapshots.map(({ id }) => id));
+}
+
+async function clearModelCaches(connectionIds: string[]) {
+ if (!redis || connectionIds.length === 0) return;
+ await redis.del(
+ ...connectionIds.map(
+ (connectionId) => `ai-gateway:models:${ORGANIZATION_ID}:${connectionId}`,
+ ),
+ );
+}
+
+async function gatewayControlPlaneRequest(input: {
+ path: string;
+ apiFormat: GatewayApiFormat;
+ virtualSecretKey: string;
+}): Promise<unknown> {
+ const timestamp = Math.floor(Date.now() / 1000);
+ const canonicalMessage = [
+ "gateway-web-v1",
+ timestamp.toString(),
+ sha256Hex(input.virtualSecretKey),
+ ].join("\n");
+ const gatewayAuthorization = `HMAC timestamp=${timestamp},signature=${signHmacSha256(
+ canonicalMessage,
+ env.LANGFUSE_AI_GATEWAY_SERVICE_KEY!,
+ )}`;
+ const isModelsRequest = input.path === MODELS_PATH;
+ const url = new URL(`${BASE_URL}${input.path}`);
+ if (isModelsRequest) url.searchParams.set("api_format", input.apiFormat);
+
+ const response = await fetch(url, {
+ method: isModelsRequest ? "GET" : "POST",
+ headers: {
+ Authorization: `Bearer ${input.virtualSecretKey}`,
+ ...(!isModelsRequest ? { "Content-Type": "application/json" } : {}),
+ "Langfuse-Gateway-Authorization": gatewayAuthorization,
+ },
+ body: isModelsRequest
+ ? undefined
+ : JSON.stringify({ apiFormat: input.apiFormat }),
+ });
+ return readJsonResponse(response, 200, input.path);
+}
+
+async function ingestTrace(
+ ingestionToken: string,
+ source: { provider: GatewayProviderName; apiFormat: GatewayApiFormat },
+) {
+ const traceIdBytes = randomBytes(16);
+ const observationIdBytes = randomBytes(8);
+ const traceId = traceIdBytes.toString("hex");
+ const observationId = observationIdBytes.toString("hex");
+ const startTimeUnixNano = BigInt(Date.now()) * 1_000_000n;
+ const ExportTraceServiceRequest =
+ $root.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
+ const requestBody = ExportTraceServiceRequest.encode(
+ ExportTraceServiceRequest.fromObject({
+ resourceSpans: [
+ {
+ scopeSpans: [
+ {
+ scope: { name: "langfuse-ai-gateway-e2e", version: "1.0.0" },
+ spans: [
+ {
+ traceId: traceIdBytes,
+ spanId: observationIdBytes,
+ name: `Gateway E2E ${source.provider} ${source.apiFormat}`,
+ kind: 1,
+ startTimeUnixNano: startTimeUnixNano.toString(),
+ endTimeUnixNano: (startTimeUnixNano + 1_000_000n).toString(),
+ attributes: [
+ {
+ key: "langfuse.observation.type",
+ value: { stringValue: "generation" },
+ },
+ {
+ key: "gen_ai.request.model",
+ value: { stringValue: "ai-gateway-e2e" },
+ },
+ ],
+ status: { code: 1 },
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ }),
+ ).finish();
+ const response = await fetch(`${BASE_URL}/api/public/otel/v1/traces`, {
+ method: "POST",
+ headers: {
+ Authorization: `Bearer ${ingestionToken}`,
+ "Langfuse-Gateway-Authorization":
+ gatewayIngestionAuthorization(ingestionToken),
+ "Content-Type": "application/x-protobuf",
+ "x-langfuse-ingestion-version": "4",
+ "x-langfuse-sdk-name": "ai-gateway-e2e",
+ "x-langfuse-sdk-version": "1.0.0",
+ },
+ body: requestBody,
+ });
+ await expectResponseStatus(response, 200, "/api/public/otel/v1/traces");
+
+ await waitForExpect(
+ async () => {
+ const [trace, observation] = await Promise.all([
+ getTraceByIdFromEventsTable({
+ traceId,
+ projectId: defaultProjectId,
+ }),
+ getObservationByIdFromEventsTable({
+ id: observationId,
+ projectId: defaultProjectId,
+ }),
+ ]);
+ expect(trace?.id).toBe(traceId);
+ expect(observation?.id).toBe(observationId);
+ },
+ 40_000,
+ 1_000,
+ );
+
+ return { traceId, observationId };
+}
+
+function gatewayIngestionAuthorization(token: string) {
+ const timestamp = Math.floor(Date.now() / 1000);
+ const canonicalMessage = [
+ "gateway-web-v1",
+ timestamp.toString(),
+ sha256Hex(token),
+ ].join("\n");
+ return `HMAC timestamp=${timestamp},signature=${signHmacSha256(
+ canonicalMessage,
+ env.LANGFUSE_AI_GATEWAY_SERVICE_KEY!,
+ )}`;
+}
+
+async function readJsonResponse<T = unknown>(
+ response: Response,
+ expectedStatus: number,
+ label: string,
+): Promise<T> {
+ const text = await response.text();
+ if (response.status !== expectedStatus) {
+ throw new Error(
+ `${label} returned ${response.status}; expected ${expectedStatus}. Response: ${text}`,
+ );
+ }
+ try {
+ return JSON.parse(text) as T;
+ } catch {
+ throw new Error(`${label} returned invalid JSON`);
+ }
+}
+
+async function expectResponseStatus(
+ response: Response,
+ expectedStatus: number,
+ label: string,
+) {
+ if (response.status === expectedStatus) return;
+ throw new Error(
+ `${label} returned ${response.status}; expected ${expectedStatus}. Response: ${await response.text()}`,
+ );
+}
+
+function sha256Hex(value: string) {
+ return createHash("sha256").update(value, "utf8").digest("hex");
+}
+
+function assertLocalTestTarget() {
+ const allowedHosts = new Set([
+ "localhost",
+ "127.0.0.1",
+ "::1",
+ "postgres",
+ "host.docker.internal",
+ ]);
+ const apiHost = new URL(BASE_URL).hostname;
+ const databaseHost = new URL(env.DATABASE_URL).hostname;
+ if (!allowedHosts.has(apiHost) || !allowedHosts.has(databaseHost)) {
+ throw new Error(
+ "Gateway E2E changes provider connection state and may only run against a local API and database.",
+ );
+ }
+}
diff --git a/web/src/__tests__/organization-settings-pages.clienttest.tsx b/web/src/__tests__/organization-settings-pages.clienttest.tsx
index 5b8292cf2d..d57ef3369d 100644
--- a/web/src/__tests__/organization-settings-pages.clienttest.tsx
+++ b/web/src/__tests__/organization-settings-pages.clienttest.tsx
@@ -1,6 +1,7 @@
import { renderHook } from "@testing-library/react";
import { useHasEntitlement, usePlan } from "@/src/features/entitlements/hooks";
+import useIsFeatureEnabled from "@/src/features/feature-flags/hooks/useIsFeatureEnabled";
import { useQueryProjectOrOrganization } from "@/src/features/projects/hooks";
import { useHasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
import { useIsCloudBillingAvailable } from "@/src/ee/features/billing/utils/isCloudBilling";
@@ -91,17 +92,30 @@ vi.mock("@/src/features/v4-migration/useV4UpgradeUiEnabled", () => ({
useV4UpgradeUiFlag: vi.fn(),
}));
+vi.mock("@/src/features/feature-flags/hooks/useIsFeatureEnabled", () => ({
+ default: vi.fn(),
+}));
+
+vi.mock("@/src/features/ai-gateway", () => ({
+ GatewayApiKeysPage: () => null,
+ GatewayConfigurationPage: () => null,
+ GatewayModelsPage: () => null,
+ GatewayProvidersPage: () => null,
+}));
+
const organization = {
id: "org-1",
name: "Org 1",
metadata: {},
+ projects: [],
};
describe("useOrganizationSettingsPages", () => {
beforeEach(() => {
vi.mocked(useQueryProjectOrOrganization).mockReturnValue({
organization,
- } as ReturnType<typeof useQueryProjectOrOrganization>);
+ project: null,
+ } as unknown as ReturnType<typeof useQueryProjectOrOrganization>);
vi.mocked(useHasEntitlement).mockImplementation(
(entitlement) => entitlement === "admin-api",
);
@@ -109,6 +123,7 @@ describe("useOrganizationSettingsPages", () => {
vi.mocked(usePlan).mockReturnValue("oss");
vi.mocked(useIsCloudBillingAvailable).mockReturnValue(false);
vi.mocked(useV4UpgradeUiFlag).mockReturnValue(false);
+ vi.mocked(useIsFeatureEnabled).mockReturnValue(false);
});
it("hides organization API key settings without organization api key access", () => {
@@ -161,4 +176,51 @@ describe("useOrganizationSettingsPages", () => {
enabled.current.find((page) => page.slug === "v4-migration")?.show,
).toBe(true);
});
+
+ it("hides all AI Gateway settings without gateway management access", () => {
+ const { result } = renderHook(() => useOrganizationSettingsPages());
+
+ expect(useHasOrganizationAccess).toHaveBeenCalledWith({
+ organizationId: "org-1",
+ scope: "gateway:manage",
+ });
+ expect(
+ result.current
+ .filter((page) => page.slug.startsWith("ai-gateway"))
+ .every((page) => page.show === false),
+ ).toBe(true);
+ });
+
+ it("hides all AI Gateway settings when the internal flag is disabled", () => {
+ vi.mocked(useHasOrganizationAccess).mockImplementation(
+ ({ scope }) => scope === "gateway:manage",
+ );
+
+ const { result } = renderHook(() => useOrganizationSettingsPages());
+ const gatewayPages = result.current.filter((page) =>
+ page.slug.startsWith("ai-gateway"),
+ );
+
+ expect(gatewayPages.every((page) => page.show === false)).toBe(true);
+ });
+
+ it("shows all AI Gateway settings when the internal flag and gateway management access are enabled", () => {
+ vi.mocked(useHasOrganizationAccess).mockImplementation(
+ ({ scope }) => scope === "gateway:manage",
+ );
+ vi.mocked(useIsFeatureEnabled).mockReturnValue(true);
+
+ const { result } = renderHook(() => useOrganizationSettingsPages());
+ const gatewayPages = result.current.filter((page) =>
+ page.slug.startsWith("ai-gateway"),
+ );
+
+ expect(gatewayPages.map((page) => page.slug)).toEqual([
+ "ai-gateway",
+ "ai-gateway-providers",
+ "ai-gateway-models",
+ "ai-gateway-api-keys",
+ ]);
+ expect(gatewayPages.every((page) => page.show === true)).toBe(true);
+ });
});
diff --git a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
new file mode 100644
index 0000000000..0e6bb9c28b
--- /dev/null
+++ b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
@@ -0,0 +1,1007 @@
+import { generateKeyPairSync, randomUUID } from "node:crypto";
+
+import type { Session } from "next-auth";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import * as SharedServer from "@langfuse/shared/src/server";
+import { z } from "zod/v4";
+
+vi.mock("@langfuse/shared/src/server", async (importOriginal) => {
+ const actual = await importOriginal<typeof SharedServer>();
+ return {
+ ...actual,
+ testModelCall: vi.fn().mockResolvedValue(undefined),
+ };
+});
+
+import { env } from "@/src/env.mjs";
+import { appRouter } from "@/src/server/api/root";
+import {
+ createInnerTRPCContext,
+ type OrgAuthedContext,
+} from "@/src/server/api/trpc";
+import { GatewayApiKeyAuthenticator } from "@/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator";
+import {
+ type GatewayResolveError,
+ GatewayResolveService,
+} from "@/src/features/ai-gateway/server/resolve/resolveService";
+import { GatewayModelsService } from "@/src/features/ai-gateway/server/provider/models/gatewayModelsService";
+import { GatewayApiKeyService } from "@/src/features/ai-gateway/server/apiKey/gatewayApiKeyService";
+import { GatewayConfigService } from "@/src/features/ai-gateway/server/config/gatewayConfigService";
+import {
+ GatewayModelCatalogService,
+ GatewayProviderService,
+} from "@/src/features/ai-gateway/server/provider";
+import { createEs256JwtVerifier } from "@/src/server/utils/jwt";
+import { prisma, Role } from "@langfuse/shared/src/db";
+import { decrypt } from "@langfuse/shared/encryption";
+import { createShaHash } from "@langfuse/shared/src/server/auth/apiKeys";
+
+const cleanupOrganizations: string[] = [];
+const cleanupUsers: string[] = [];
+const originalGatewayOrganizationAllowlist = [
+ ...env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST,
+];
+const originalGatewayJwtConfig = {
+ privateKey: env.LANGFUSE_AI_GATEWAY_JWT_PRIVATE_KEY,
+ publicKey: env.LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY,
+ keyId: env.LANGFUSE_AI_GATEWAY_JWT_KEY_ID,
+ issuer: env.LANGFUSE_AI_GATEWAY_JWT_ISSUER,
+ audience: env.LANGFUSE_AI_GATEWAY_JWT_AUDIENCE,
+};
+const gatewaySigningKeys = generateKeyPairSync("ec", { namedCurve: "P-256" });
+const gatewayPrivateKey = gatewaySigningKeys.privateKey
+ .export({ format: "pem", type: "pkcs8" })
+ .toString();
+const gatewayPublicKey = gatewaySigningKeys.publicKey
+ .export({ format: "pem", type: "spki" })
+ .toString();
+
+function configureGatewayJwtSigning() {
+ Object.assign(env, {
+ LANGFUSE_AI_GATEWAY_JWT_PRIVATE_KEY: gatewayPrivateKey,
+ LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY: gatewayPublicKey,
+ LANGFUSE_AI_GATEWAY_JWT_KEY_ID: "current",
+ LANGFUSE_AI_GATEWAY_JWT_ISSUER: "test-issuer",
+ LANGFUSE_AI_GATEWAY_JWT_AUDIENCE: "test-audience",
+ });
+}
+
+const GatewayIngestionClaimsSchema = z.object({
+ version: z.literal(1),
+ organization_id: z.string(),
+ project_id: z.string(),
+ scope: z.literal("gateway-ingest"),
+ exp: z.number().int(),
+ iss: z.string(),
+ aud: z.string(),
+ iat: z.number().int(),
+ jti: z.string(),
+});
+
+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, {
+ LANGFUSE_AI_GATEWAY_JWT_PRIVATE_KEY: originalGatewayJwtConfig.privateKey,
+ LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY: originalGatewayJwtConfig.publicKey,
+ LANGFUSE_AI_GATEWAY_JWT_KEY_ID: originalGatewayJwtConfig.keyId,
+ LANGFUSE_AI_GATEWAY_JWT_ISSUER: originalGatewayJwtConfig.issuer,
+ LANGFUSE_AI_GATEWAY_JWT_AUDIENCE: originalGatewayJwtConfig.audience,
+ });
+ vi.clearAllMocks();
+});
+
+async function authenticateRequest(
+ params: Parameters<GatewayApiKeyAuthenticator["authenticateRequest"]>[0],
+) {
+ const context = await new GatewayApiKeyAuthenticator(
+ prisma,
+ ).authenticateRequest(params);
+ return new GatewayResolveService().resolve({
+ context,
+ apiFormat: params.apiFormat,
+ });
+}
+
+async function prepare(role: Role = Role.OWNER) {
+ const suffix = randomUUID();
+ const user = await prisma.user.create({
+ data: { email: `gateway-${suffix}@example.test`, name: "Gateway tester" },
+ });
+ const org = await prisma.organization.create({
+ data: { name: `Gateway org ${suffix}` },
+ });
+ const project = await prisma.project.create({
+ data: { name: "Gateway ingestion", orgId: org.id },
+ });
+ await prisma.organizationMembership.create({
+ data: { orgId: org.id, userId: user.id, role },
+ });
+ cleanupOrganizations.push(org.id);
+ cleanupUsers.push(user.id);
+ env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST.push(org.id);
+
+ const session: Session = {
+ expires: "1",
+ user: {
+ id: user.id,
+ name: user.name,
+ canCreateOrganizations: true,
+ featureFlags: {} as NonNullable<Session["user"]>["featureFlags"],
+ organizations: [
+ {
+ id: org.id,
+ name: org.name,
+ role,
+ cloudConfig: undefined,
+ plan: "oss",
+ metadata: {},
+ aiFeaturesEnabled: false,
+ aiTelemetryEnabled: true,
+ projects: [
+ {
+ id: project.id,
+ role,
+ retentionDays: 0,
+ deletedAt: null,
+ hasTraces: false,
+ name: project.name,
+ metadata: {},
+ createdAt: project.createdAt.toISOString(),
+ },
+ ],
+ },
+ ],
+ },
+ environment: {} as Session["environment"],
+ };
+ const ctx = createInnerTRPCContext({ session, headers: {} });
+ const orgSession = {
+ ...session,
+ user: session.user!,
+ orgId: org.id,
+ orgRole: role,
+ } satisfies OrgAuthedContext["session"];
+ return {
+ org,
+ project,
+ user,
+ session: orgSession,
+ caller: appRouter.createCaller({ ...ctx, prisma }),
+ };
+}
+
+describe("AI gateway control plane", () => {
+ it("applies the environment-specific organization allowlist", async () => {
+ const { caller, org } = await prepare();
+ env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST.splice(
+ env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST.indexOf(org.id),
+ 1,
+ );
+
+ const request = caller.aiGateway.getConfig({ orgId: org.id });
+ if (env.NODE_ENV === "development") {
+ await expect(request).resolves.toBeNull();
+ } else {
+ await expect(request).rejects.toMatchObject({ code: "FORBIDDEN" });
+ }
+ });
+
+ it("enforces admin scope and validates the ingestion project organization", async () => {
+ const owner = await prepare();
+ const member = await prepare(Role.MEMBER);
+
+ await expect(
+ member.caller.aiGateway.updateConfig({
+ orgId: member.org.id,
+ defaultIngestionProjectId: member.project.id,
+ ingestionMode: "USAGE",
+ }),
+ ).rejects.toMatchObject({ code: "FORBIDDEN" });
+
+ await expect(
+ owner.caller.aiGateway.updateConfig({
+ orgId: owner.org.id,
+ defaultIngestionProjectId: member.project.id,
+ ingestionMode: "USAGE",
+ }),
+ ).rejects.toThrow("active project in the organization");
+ });
+
+ it("stores provider credentials encrypted and never returns them", async () => {
+ const { caller, org } = await prepare();
+ const credential = "sk-test-gateway-redaction";
+ const created = await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name: "OpenAI primary",
+ provider: "OPENAI",
+ credential,
+ });
+
+ expect(SharedServer.testModelCall).toHaveBeenCalledWith(
+ expect.objectContaining({
+ provider: "openai",
+ model: "gpt-4o-mini",
+ apiKey: expect.objectContaining({
+ baseURL: "https://api.openai.com/v1",
+ secretKey: expect.not.stringContaining(credential),
+ }),
+ }),
+ );
+ expect(created).not.toHaveProperty("encryptedCredential");
+ expect(created).not.toHaveProperty("credential");
+ const listed = await caller.aiGateway.listConnections({ orgId: org.id });
+ expect(listed.data[0]).not.toHaveProperty("encryptedCredential");
+ expect(JSON.stringify(listed)).not.toContain(credential);
+
+ const stored = await prisma.gatewayAiConnection.findUniqueOrThrow({
+ where: { id: created.id },
+ select: { encryptedCredential: true },
+ });
+ expect(stored.encryptedCredential).not.toBe(credential);
+ expect(decrypt(stored.encryptedCredential)).toBe(credential);
+ });
+
+ it("tests provider credentials without saving a connection", async () => {
+ const { caller, org } = await prepare();
+ const credential = "sk-test-only";
+
+ await caller.aiGateway.testConnection({
+ orgId: org.id,
+ provider: "OPENAI",
+ credential,
+ });
+
+ expect(SharedServer.testModelCall).toHaveBeenCalledWith(
+ expect.objectContaining({
+ provider: "openai",
+ apiKey: expect.objectContaining({
+ secretKey: expect.not.stringContaining(credential),
+ }),
+ }),
+ );
+ await expect(
+ prisma.gatewayAiConnection.count({
+ where: { organizationId: org.id },
+ }),
+ ).resolves.toBe(0);
+ });
+
+ it("validates new and updated Anthropic credentials with an active compatible model", async () => {
+ const { caller, org } = await prepare();
+
+ const connection = await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name: "Anthropic primary",
+ provider: "ANTHROPIC",
+ credential: "sk-ant-test-gateway",
+ });
+ await caller.aiGateway.updateConnection({
+ orgId: org.id,
+ id: connection.id,
+ credential: "sk-ant-test-gateway-updated",
+ });
+
+ expect(SharedServer.testModelCall).toHaveBeenCalledTimes(2);
+ expect(SharedServer.testModelCall).toHaveBeenNthCalledWith(
+ 2,
+ expect.objectContaining({
+ provider: "anthropic",
+ model: "claude-haiku-4-5-20251001",
+ apiKey: expect.objectContaining({
+ adapter: "anthropic",
+ baseURL: "https://api.anthropic.com/v1",
+ }),
+ }),
+ );
+ });
+
+ it("creates, lists, and revokes only associated organization keys", async () => {
+ const { caller, org } = await prepare();
+ const created = await caller.aiGateway.createApiKey({
+ orgId: org.id,
+ note: "Production gateway",
+ metadata: { environment: "production", costCenter: 42 },
+ });
+ expect(created.secretKey).toMatch(/^sk-lf-/);
+
+ const listed = await caller.aiGateway.listApiKeys({ orgId: org.id });
+ expect(listed.data).toHaveLength(1);
+ expect(listed.data[0]).toMatchObject({
+ apiKey: { id: created.id, note: "Production gateway" },
+ metadata: { environment: "production", costCenter: 42 },
+ });
+ expect(JSON.stringify(listed)).not.toContain(created.secretKey);
+
+ await expect(
+ caller.aiGateway.createApiKey({
+ orgId: org.id,
+ metadata: { nested: { rejected: true } } as never,
+ }),
+ ).rejects.toThrow();
+
+ await expect(
+ caller.aiGateway.revokeApiKey({ orgId: org.id, id: created.id }),
+ ).resolves.toEqual({ success: true });
+ expect(
+ await prisma.gatewayApiKeyAssociation.findUnique({
+ where: { apiKeyId: created.id },
+ }),
+ ).toBeNull();
+
+ await expect(
+ caller.aiGateway.revokeApiKey({ orgId: org.id, id: created.id }),
+ ).rejects.toThrow("Gateway API key not found");
+ });
+
+ it("owns successful and failed mutation auditing in gateway services", async () => {
+ const { org, project, session } = await prepare();
+ const gatewayService = new GatewayConfigService(prisma);
+ const providerService = new GatewayProviderService(prisma);
+ const apiKeyService = new GatewayApiKeyService(prisma);
+
+ await gatewayService.updateConfig({
+ organizationId: org.id,
+ defaultIngestionProjectId: project.id,
+ ingestionMode: "USAGE",
+ session,
+ });
+ await providerService.create({
+ organizationId: org.id,
+ name: "Audited provider",
+ provider: "OPENAI",
+ credential: "sk-audited-provider",
+ session,
+ });
+ const key = await apiKeyService.create({
+ organizationId: org.id,
+ metadata: {},
+ session,
+ });
+
+ expect(
+ await prisma.auditLog.findMany({
+ where: { orgId: org.id },
+ select: { resourceType: true, action: true },
+ orderBy: { createdAt: "asc" },
+ }),
+ ).toEqual([
+ { resourceType: "gatewayConfig", action: "create" },
+ { resourceType: "gatewayAiConnection", action: "create" },
+ { resourceType: "apiKey", action: "create" },
+ ]);
+
+ await apiKeyService.revoke({
+ organizationId: org.id,
+ apiKeyId: key.id,
+ session,
+ });
+ const auditCountAfterSuccess = await prisma.auditLog.count({
+ where: { orgId: org.id },
+ });
+ await expect(
+ apiKeyService.revoke({
+ organizationId: org.id,
+ apiKeyId: key.id,
+ session,
+ }),
+ ).rejects.toThrow("Gateway API key not found");
+ expect(await prisma.auditLog.count({ where: { orgId: org.id } })).toBe(
+ auditCountAfterSuccess,
+ );
+
+ const failingProviderService = new GatewayProviderService(
+ prisma,
+ vi.fn().mockRejectedValue(new Error("invalid credential")),
+ );
+ await expect(
+ failingProviderService.create({
+ organizationId: org.id,
+ name: "Rejected provider",
+ provider: "OPENAI",
+ credential: "sk-rejected",
+ session,
+ }),
+ ).rejects.toThrow("invalid credential");
+ expect(await prisma.auditLog.count({ where: { orgId: org.id } })).toBe(
+ auditCountAfterSuccess,
+ );
+ });
+
+ it("cursor-paginates provider connections", async () => {
+ const { caller, org } = await prepare();
+
+ for (const name of ["First", "Second", "Third"]) {
+ await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name,
+ provider: "OPENAI",
+ credential: `sk-test-${name.toLowerCase()}`,
+ });
+ }
+
+ const firstConnections = await caller.aiGateway.listConnections({
+ orgId: org.id,
+ limit: 2,
+ });
+ expect(firstConnections.data.map((connection) => connection.name)).toEqual([
+ "First",
+ "Second",
+ ]);
+ expect(firstConnections.nextCursor).toBeTruthy();
+ const remainingConnections = await caller.aiGateway.listConnections({
+ orgId: org.id,
+ limit: 2,
+ cursor: firstConnections.nextCursor ?? undefined,
+ });
+ expect(
+ remainingConnections.data.map((connection) => connection.name),
+ ).toEqual(["Third"]);
+ expect(remainingConnections.nextCursor).toBeNull();
+ });
+
+ it("cursor-paginates gateway keys", async () => {
+ const { caller, org } = await prepare();
+
+ for (const note of ["First", "Second", "Third"]) {
+ await caller.aiGateway.createApiKey({
+ orgId: org.id,
+ note,
+ metadata: {},
+ });
+ }
+
+ const firstKeys = await caller.aiGateway.listApiKeys({
+ orgId: org.id,
+ limit: 2,
+ });
+ expect(firstKeys.data).toHaveLength(2);
+ expect(firstKeys.nextCursor).toBeTruthy();
+ const remainingKeys = await caller.aiGateway.listApiKeys({
+ orgId: org.id,
+ limit: 2,
+ cursor: firstKeys.nextCursor ?? undefined,
+ });
+ expect(remainingKeys.data).toHaveLength(1);
+ expect(remainingKeys.nextCursor).toBeNull();
+ });
+
+ it("rejects gateway page limits above 100", async () => {
+ const { caller, org } = await prepare();
+
+ await expect(
+ caller.aiGateway.listConnections({ orgId: org.id, limit: 101 }),
+ ).rejects.toThrow();
+ await expect(
+ caller.aiGateway.listApiKeys({ orgId: org.id, limit: 101 }),
+ ).rejects.toThrow();
+ });
+
+ it("routes by format and priority and issues a verifiable ingestion token", async () => {
+ const { caller, org, project } = await prepare();
+ await caller.aiGateway.updateConfig({
+ orgId: org.id,
+ defaultIngestionProjectId: project.id,
+ ingestionMode: "FULL",
+ });
+ const openAiPrimary = await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name: "OpenAI first",
+ provider: "OPENAI",
+ credential: "sk-test-openai-primary",
+ });
+ await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name: "OpenAI second",
+ provider: "OPENAI",
+ credential: "sk-test-openai-secondary",
+ });
+ const gatewayKey = await caller.aiGateway.createApiKey({
+ orgId: org.id,
+ metadata: {},
+ });
+
+ configureGatewayJwtSigning();
+
+ const apiFormat = "openai.chat-completions" as const;
+ const jwtVerifier = createEs256JwtVerifier({
+ publicKeys: [{ id: "current", publicKey: gatewayPublicKey }],
+ issuer: "test-issuer",
+ audience: "test-audience",
+ claimsSchema: GatewayIngestionClaimsSchema,
+ });
+ const result = await authenticateRequest({
+ fastHashedSecretKey: createShaHash(gatewayKey.secretKey, env.SALT),
+ apiFormat,
+ });
+
+ expect(openAiPrimary.routingPriority).toBe(0);
+ expect(result.connection).toEqual({
+ id: openAiPrimary.id,
+ provider: "openai",
+ api_format: apiFormat,
+ base_url: "https://api.openai.com/v1",
+ auth: { type: "Bearer", token: "sk-test-openai-primary" },
+ });
+ const ingestionToken = result.ingestion!.access_token;
+ const ingestionClaims = jwtVerifier.verify({ token: ingestionToken });
+ const encodedClaims = ingestionToken.split(".")[1];
+ const rawClaims = JSON.parse(
+ Buffer.from(encodedClaims, "base64url").toString("utf8"),
+ ) as Record<string, unknown>;
+ expect(rawClaims).not.toHaveProperty("key_id");
+ expect(ingestionClaims).toMatchObject({
+ organization_id: org.id,
+ project_id: project.id,
+ });
+ expect(rawClaims).not.toHaveProperty("api_key_id");
+ expect(result.ingestion_mode).toBe("full");
+ expect(result.ingestion?.expires_at).toBe(ingestionClaims.exp);
+ });
+
+ it("changes ERROR only for credential auth failures and explicit recovery", async () => {
+ const { caller, org, session } = await prepare();
+ const connection = await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name: "Anthropic",
+ provider: "ANTHROPIC",
+ credential: "sk-ant-test",
+ });
+ const unauthorizedFetch = vi
+ .fn<typeof fetch>()
+ .mockResolvedValue(new Response(null, { status: 401 }));
+ const unauthorized = new GatewayModelCatalogService(
+ prisma,
+ unauthorizedFetch,
+ );
+ await unauthorized.refreshModels({
+ organizationId: org.id,
+ connectionId: connection.id,
+ });
+ expect(
+ await prisma.gatewayAiConnection.findUnique({
+ where: { id: connection.id },
+ select: { status: true },
+ }),
+ ).toEqual({ status: "ERROR" });
+ await expect(
+ caller.aiGateway.updateConnection({
+ orgId: org.id,
+ id: connection.id,
+ status: "ENABLED",
+ }),
+ ).rejects.toThrow("credential update or successful retry");
+ const automaticRefresh = vi.fn<typeof fetch>();
+ await new GatewayModelCatalogService(
+ prisma,
+ automaticRefresh,
+ ).refreshAllModels(org.id);
+ expect(automaticRefresh).not.toHaveBeenCalled();
+
+ const failingFetch = vi
+ .fn<typeof fetch>()
+ .mockResolvedValue(new Response(null, { status: 500 }));
+ const failedRetry = await new GatewayModelCatalogService(
+ prisma,
+ failingFetch,
+ ).retryConnection({
+ organizationId: org.id,
+ connectionId: connection.id,
+ session,
+ });
+ expect(failedRetry.success).toBe(false);
+ expect(
+ await prisma.auditLog.count({
+ where: { orgId: org.id, action: "retry" },
+ }),
+ ).toBe(0);
+ expect(
+ await prisma.gatewayAiConnection.findUnique({
+ where: { id: connection.id },
+ select: { status: true },
+ }),
+ ).toEqual({ status: "ERROR" });
+
+ const successfulFetch = vi.fn<typeof fetch>().mockResolvedValue(
+ Response.json({
+ data: [
+ {
+ id: "claude-test",
+ display_name: "Claude Test",
+ created_at: "2026-01-01T00:00:00.000Z",
+ },
+ ],
+ has_more: false,
+ }),
+ );
+ await new GatewayModelCatalogService(
+ prisma,
+ successfulFetch,
+ ).retryConnection({
+ organizationId: org.id,
+ connectionId: connection.id,
+ session,
+ });
+ expect(
+ await prisma.gatewayAiConnection.findUnique({
+ where: { id: connection.id },
+ select: { status: true },
+ }),
+ ).toEqual({ status: "ENABLED" });
+ expect(
+ await prisma.auditLog.count({
+ where: { orgId: org.id, action: "retry" },
+ }),
+ ).toBe(1);
+ });
+
+ it("keeps routing priorities contiguous through reorders and deletes", async () => {
+ const { caller, org } = await prepare();
+ const created = [];
+ for (const name of ["first", "second", "third"]) {
+ created.push(
+ await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name,
+ provider: "OPENAI",
+ credential: `sk-${name}`,
+ }),
+ );
+ }
+ const priorities = async () =>
+ (
+ await prisma.gatewayAiConnection.findMany({
+ where: { organizationId: org.id },
+ select: { name: true, routingPriority: true },
+ orderBy: { routingPriority: "asc" },
+ })
+ ).map(({ name, routingPriority }) => [name, routingPriority]);
+
+ expect(await priorities()).toEqual([
+ ["first", 0],
+ ["second", 1],
+ ["third", 2],
+ ]);
+
+ // Reversing every row would violate the unique (organization_id,
+ // routing_priority) index if the writes were applied in place.
+ await caller.aiGateway.reorderConnections({
+ orgId: org.id,
+ connectionIds: [...created].reverse().map(({ id }) => id),
+ });
+ expect(await priorities()).toEqual([
+ ["third", 0],
+ ["second", 1],
+ ["first", 2],
+ ]);
+
+ // Two reorders racing on one organization must serialize rather than
+ // deadlock on rows locked in opposite orders.
+ await expect(
+ Promise.all([
+ caller.aiGateway.reorderConnections({
+ orgId: org.id,
+ connectionIds: created.map(({ id }) => id),
+ }),
+ caller.aiGateway.reorderConnections({
+ orgId: org.id,
+ connectionIds: [...created].reverse().map(({ id }) => id),
+ }),
+ ]),
+ ).resolves.toHaveLength(2);
+ expect((await priorities()).map(([, priority]) => priority)).toEqual([
+ 0, 1, 2,
+ ]);
+
+ // Deleting from the middle must close the gap it leaves behind.
+ await caller.aiGateway.deleteConnection({
+ orgId: org.id,
+ id: created[1].id,
+ });
+ expect((await priorities()).map(([, priority]) => priority)).toEqual([
+ 0, 1,
+ ]);
+
+ await expect(
+ caller.aiGateway.reorderConnections({
+ orgId: org.id,
+ connectionIds: [created[0].id],
+ }),
+ ).rejects.toThrow(
+ "Reorder must contain every organization gateway connection exactly once",
+ );
+ expect((await priorities()).map(([, priority]) => priority)).toEqual([
+ 0, 1,
+ ]);
+ });
+
+ it("serves repeat resolves from cache and drops it when a connection changes", async () => {
+ configureGatewayJwtSigning();
+ const { caller, org, project } = await prepare();
+ await caller.aiGateway.updateConfig({
+ orgId: org.id,
+ defaultIngestionProjectId: project.id,
+ ingestionMode: "USAGE",
+ });
+ const connection = await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name: "OpenAI",
+ provider: "OPENAI",
+ credential: "sk-test",
+ });
+ const key = await caller.aiGateway.createApiKey({
+ orgId: org.id,
+ metadata: {},
+ });
+ const resolveParams = {
+ fastHashedSecretKey: createShaHash(key.secretKey, env.SALT),
+ apiFormat: "openai.responses" as const,
+ };
+ const lookup = vi.spyOn(prisma.gatewayApiKeyAssociation, "findFirst");
+
+ for (let attempt = 0; attempt < 2; attempt++) {
+ await expect(authenticateRequest(resolveParams)).resolves.toMatchObject({
+ connection: { auth: { type: "Bearer", token: "sk-test" } },
+ });
+ }
+ // The second call must not reach Postgres: this endpoint runs on every LLM
+ // request through the gateway.
+ expect(lookup).toHaveBeenCalledOnce();
+
+ // Disabling a connection has to take effect now, not when the TTL expires.
+ await caller.aiGateway.updateConnection({
+ orgId: org.id,
+ id: connection.id,
+ status: "DISABLED",
+ });
+ await expect(authenticateRequest(resolveParams)).rejects.toEqual(
+ expect.objectContaining<Partial<GatewayResolveError>>({ status: 404 }),
+ );
+ expect(lookup).toHaveBeenCalledTimes(2);
+ lookup.mockRestore();
+ });
+
+ it("negatively caches unknown gateway keys", async () => {
+ const resolveParams = {
+ fastHashedSecretKey: createShaHash(`sk-lf-${randomUUID()}`, env.SALT),
+ apiFormat: "openai.responses" as const,
+ };
+ const lookup = vi.spyOn(prisma.gatewayApiKeyAssociation, "findFirst");
+
+ for (let attempt = 0; attempt < 3; attempt++) {
+ await expect(authenticateRequest(resolveParams)).rejects.toEqual(
+ expect.objectContaining<Partial<GatewayResolveError>>({ status: 401 }),
+ );
+ }
+ // Without a negative cache, a client looping on a bad key is an unmetered
+ // query generator against the primary.
+ expect(lookup).toHaveBeenCalledOnce();
+ lookup.mockRestore();
+ });
+
+ it("blocks deletion of the default ingestion project", async () => {
+ const { caller, org, project } = await prepare();
+ await caller.aiGateway.updateConfig({
+ orgId: org.id,
+ defaultIngestionProjectId: project.id,
+ ingestionMode: "USAGE",
+ });
+
+ await expect(
+ caller.projects.deletionProtection({ projectId: project.id }),
+ ).resolves.toEqual({ isGatewayIngestionProject: true });
+ await expect(
+ caller.projects.delete({ projectId: project.id }),
+ ).rejects.toThrow("Select another ingestion project before deleting it");
+ await expect(
+ prisma.project.findUnique({ where: { id: project.id } }),
+ ).resolves.toMatchObject({ deletedAt: null });
+ });
+
+ it("blocks resolve after the default ingestion project is deleted", async () => {
+ const { caller, org, project } = await prepare();
+ await caller.aiGateway.updateConfig({
+ orgId: org.id,
+ defaultIngestionProjectId: project.id,
+ ingestionMode: "USAGE",
+ });
+ await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name: "OpenAI",
+ provider: "OPENAI",
+ credential: "sk-test",
+ });
+ const key = await caller.aiGateway.createApiKey({
+ orgId: org.id,
+ metadata: {},
+ });
+ await prisma.project.update({
+ where: { id: project.id },
+ data: { deletedAt: new Date() },
+ });
+ const apiFormat = "openai.responses" as const;
+ await expect(
+ authenticateRequest({
+ fastHashedSecretKey: createShaHash(key.secretKey, env.SALT),
+ apiFormat,
+ }),
+ ).rejects.toEqual(
+ expect.objectContaining<Partial<GatewayResolveError>>({ status: 403 }),
+ );
+ });
+
+ it("returns a canonical model catalog across enabled compatible connections", async () => {
+ const { caller, org } = await prepare();
+ await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name: "OpenAI",
+ provider: "OPENAI",
+ credential: "sk-openai",
+ });
+ await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name: "Anthropic",
+ provider: "ANTHROPIC",
+ credential: "sk-anthropic",
+ });
+ const key = await caller.aiGateway.createApiKey({
+ orgId: org.id,
+ metadata: {},
+ });
+ const fetcher = vi.fn<typeof fetch>().mockImplementation(async (url) => {
+ const hostname = new URL(String(url)).hostname;
+ if (hostname === "api.openai.com") {
+ return Response.json({
+ data: [
+ {
+ id: "a-model",
+ created: 10,
+ owned_by: "openai",
+ shutdown_date: "2027-01-01",
+ },
+ { id: "shared-model", created: 15, owned_by: "openai" },
+ ],
+ });
+ }
+ if (hostname === "api.anthropic.com") {
+ return Response.json({
+ data: [
+ {
+ id: "claude-a",
+ display_name: "Claude A",
+ created_at: "2026-01-01T00:00:00.000Z",
+ capabilities: { image_input: { supported: true } },
+ max_input_tokens: 200000,
+ max_tokens: 64000,
+ type: "model",
+ },
+ {
+ id: "claude-b",
+ display_name: "Claude B",
+ created_at: "2026-02-01T00:00:00.000Z",
+ capabilities: null,
+ max_input_tokens: 100000,
+ max_tokens: 32000,
+ type: "model",
+ },
+ ],
+ has_more: false,
+ });
+ }
+ throw new Error(`Unexpected provider request: ${String(url)}`);
+ });
+
+ const service = new GatewayModelsService(prisma, fetcher, null);
+ const fastHashedSecretKey = createShaHash(key.secretKey, env.SALT);
+ const openAiContext = await new GatewayApiKeyAuthenticator(
+ prisma,
+ ).authenticateRequest({
+ fastHashedSecretKey,
+ apiFormat: "openai.responses",
+ });
+ const result = await service.list({
+ context: openAiContext,
+ apiFormat: "openai.responses",
+ });
+
+ expect(result).toEqual({
+ data: [
+ {
+ id: "a-model",
+ provider: "OPENAI",
+ canonicalSlug: "a-model",
+ displayName: "a-model",
+ createdAt: "1970-01-01T00:00:10.000Z",
+ },
+ {
+ id: "shared-model",
+ provider: "OPENAI",
+ canonicalSlug: "shared-model",
+ displayName: "shared-model",
+ createdAt: "1970-01-01T00:00:15.000Z",
+ },
+ ],
+ });
+ const anthropicContext = await new GatewayApiKeyAuthenticator(
+ prisma,
+ ).authenticateRequest({
+ fastHashedSecretKey,
+ apiFormat: "anthropic.messages",
+ });
+ const anthropicResult = await service.list({
+ context: anthropicContext,
+ apiFormat: "anthropic.messages",
+ });
+ expect(anthropicResult).toEqual({
+ data: [
+ {
+ id: "claude-a",
+ provider: "ANTHROPIC",
+ canonicalSlug: "claude-a",
+ displayName: "Claude A",
+ createdAt: "2026-01-01T00:00:00.000Z",
+ },
+ {
+ id: "claude-b",
+ provider: "ANTHROPIC",
+ canonicalSlug: "claude-b",
+ displayName: "Claude B",
+ createdAt: "2026-02-01T00:00:00.000Z",
+ },
+ ],
+ });
+ expect(fetcher).toHaveBeenCalledTimes(2);
+ });
+
+ it("fails model discovery instead of returning a partial catalog", async () => {
+ const { caller, org } = await prepare();
+ await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name: "OpenAI",
+ provider: "OPENAI",
+ credential: "sk-openai",
+ });
+ await caller.aiGateway.createConnection({
+ orgId: org.id,
+ name: "OpenAI fallback",
+ provider: "OPENAI",
+ credential: "sk-openai-fallback",
+ });
+ const key = await caller.aiGateway.createApiKey({
+ orgId: org.id,
+ metadata: {},
+ });
+ const fetcher = vi
+ .fn<typeof fetch>()
+ .mockResolvedValueOnce(new Response(null, { status: 500 }))
+ .mockResolvedValue(
+ Response.json({
+ data: [{ id: "available-model", created: 10 }],
+ }),
+ );
+
+ const context = await new GatewayApiKeyAuthenticator(
+ prisma,
+ ).authenticateRequest({
+ fastHashedSecretKey: createShaHash(key.secretKey, env.SALT),
+ apiFormat: "openai.chat-completions",
+ });
+ await expect(
+ new GatewayModelsService(prisma, fetcher, null).list({
+ context,
+ apiFormat: "openai.chat-completions",
+ }),
+ ).rejects.toEqual(
+ expect.objectContaining<Partial<GatewayResolveError>>({ status: 503 }),
+ );
+ });
+});
diff --git a/web/src/__tests__/server/apiKeyCacheInvalidation.servertest.ts b/web/src/__tests__/server/apiKeyCacheInvalidation.servertest.ts
index ad6a7a9d23..dfd7064102 100644
--- a/web/src/__tests__/server/apiKeyCacheInvalidation.servertest.ts
+++ b/web/src/__tests__/server/apiKeyCacheInvalidation.servertest.ts
@@ -243,6 +243,31 @@ describe("API-key cache invalidation on project/org lifecycle", () => {
expect(await survivingKeys(keys)).toEqual([]);
});
+ it("admin project deletion keeps the ingestion project and its keys", async () => {
+ const orgId = await createOrg();
+ const projectId = await createProject(orgId);
+ const keys = await seedOrgScopedKey(orgId);
+ await prisma.gatewayConfig.create({
+ data: { organizationId: orgId, defaultIngestionProjectId: projectId },
+ });
+
+ const res = makeRes();
+ await handleDeleteProject({} as any, res, projectId, {
+ orgId,
+ apiKeyId: "ADMIN_KEY",
+ } as any);
+
+ expect(res.statusCode).toBe(409);
+ expect(res.body).toEqual({
+ message:
+ "This project is used as the AI Gateway ingestion project. Select another ingestion project before deleting it.",
+ });
+ expect(await survivingKeys(keys)).toEqual(keys);
+ await expect(
+ prisma.project.findUnique({ where: { id: projectId } }),
+ ).resolves.toMatchObject({ deletedAt: null });
+ });
+
it("admin handleDeleteProject evicts the org's cached keys", async () => {
const orgId = await createOrg();
const projectId = await createProject(orgId);
diff --git a/web/src/__tests__/server/unit/ai-gateway-models-route.servertest.ts b/web/src/__tests__/server/unit/ai-gateway-models-route.servertest.ts
new file mode 100644
index 0000000000..4e8addf925
--- /dev/null
+++ b/web/src/__tests__/server/unit/ai-gateway-models-route.servertest.ts
@@ -0,0 +1,91 @@
+import { createMocks } from "node-mocks-http";
+import type { NextApiRequest, NextApiResponse } from "next";
+import { afterAll, beforeAll, describe, expect, it } from "vitest";
+
+import { env } from "@/src/env.mjs";
+import handler from "@/src/pages/api/internal/ai-gateway/v1/models";
+
+// `env` is typed readonly, but the route reads it at call time, so overriding
+// the field is how a test controls configuration.
+const mutableEnv = env as { LANGFUSE_AI_GATEWAY_SERVICE_KEY?: string };
+const originalServiceKey = mutableEnv.LANGFUSE_AI_GATEWAY_SERVICE_KEY;
+
+// Without a configured service key the route answers 503 for everything, which
+// would hide the 400-vs-401 distinction the assertions below rely on.
+beforeAll(() => {
+ mutableEnv.LANGFUSE_AI_GATEWAY_SERVICE_KEY = "0".repeat(64);
+});
+
+afterAll(() => {
+ mutableEnv.LANGFUSE_AI_GATEWAY_SERVICE_KEY = originalServiceKey;
+});
+
+const get = async (query: Record<string, string | string[] | undefined>) => {
+ const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
+ method: "GET",
+ headers: { authorization: "Bearer test-key" },
+ query,
+ });
+
+ await handler(req, res);
+ return res;
+};
+
+describe("GET /api/internal/ai-gateway/v1/models", () => {
+ it("rejects malformed requests with no-store headers", async () => {
+ const res = await get({ api_format: "unsupported" });
+
+ expect(res.statusCode).toBe(400);
+ expect(res.getHeader("cache-control")).toBe("no-store");
+ expect(res.getHeader("pragma")).toBe("no-cache");
+ });
+
+ it.each([
+ { case: "missing api_format", query: {} },
+ {
+ case: "repeated api_format",
+ query: { api_format: ["openai.responses", "anthropic.messages"] },
+ },
+ {
+ case: "unexpected query parameter",
+ query: { api_format: "anthropic.messages", limit: "10" },
+ },
+ ])("rejects an invalid query: $case", async ({ query }) => {
+ expect((await get(query)).statusCode).toBe(400);
+ });
+
+ it.each([
+ "openai.responses",
+ "openai.chat-completions",
+ "anthropic.messages",
+ ])(
+ "accepts api_format=%s and advances to authorization",
+ async (apiFormat) => {
+ // 401 rather than 400 proves the query passed the schema: the route only
+ // reaches signature verification after parsing succeeds.
+ expect((await get({ api_format: apiFormat })).statusCode).toBe(401);
+ },
+ );
+
+ it("allows only GET", async () => {
+ const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
+ method: "POST",
+ });
+
+ await handler(req, res);
+
+ expect(res.statusCode).toBe(405);
+ expect(res.getHeader("allow")).toBe("GET");
+ });
+
+ it("rejects missing bearer authentication", async () => {
+ const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
+ method: "GET",
+ query: { api_format: "openai.responses" },
+ });
+
+ await handler(req, res);
+
+ expect(res.statusCode).toBe(401);
+ });
+});
diff --git a/web/src/__tests__/server/unit/ai-gateway-resolve-route.servertest.ts b/web/src/__tests__/server/unit/ai-gateway-resolve-route.servertest.ts
new file mode 100644
index 0000000000..d4783b8ba7
--- /dev/null
+++ b/web/src/__tests__/server/unit/ai-gateway-resolve-route.servertest.ts
@@ -0,0 +1,107 @@
+import { createMocks } from "node-mocks-http";
+import type { NextApiRequest, NextApiResponse } from "next";
+import { afterAll, beforeAll, describe, expect, it } from "vitest";
+
+import { env } from "@/src/env.mjs";
+import handler from "@/src/pages/api/internal/ai-gateway/v1/resolve";
+
+// `env` is typed readonly, but the route reads it at call time, so overriding
+// the field is how a test controls configuration.
+const mutableEnv = env as { LANGFUSE_AI_GATEWAY_SERVICE_KEY?: string };
+const originalServiceKey = mutableEnv.LANGFUSE_AI_GATEWAY_SERVICE_KEY;
+
+// Without a configured service key the route answers 503 for everything, which
+// would hide the 400-vs-401 distinction the assertions below rely on.
+beforeAll(() => {
+ mutableEnv.LANGFUSE_AI_GATEWAY_SERVICE_KEY = "0".repeat(64);
+});
+
+afterAll(() => {
+ mutableEnv.LANGFUSE_AI_GATEWAY_SERVICE_KEY = originalServiceKey;
+});
+
+// The route reads the raw body itself (`bodyParser: false`), so bodies must be
+// strings. A parsed object short-circuits with 400 before the schema runs,
+// which would make every assertion below pass for the wrong reason.
+const post = async (body: unknown, authorization = "Bearer test-key") => {
+ const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
+ method: "POST",
+ headers: { authorization },
+ body: JSON.stringify(body) as never,
+ });
+
+ await handler(req, res);
+ return res;
+};
+
+describe("POST /api/internal/ai-gateway/v1/resolve", () => {
+ it("rejects malformed requests with no-store headers", async () => {
+ const res = await post({ apiFormat: "unsupported" });
+
+ expect(res.statusCode).toBe(400);
+ expect(res.getHeader("cache-control")).toBe("no-store");
+ expect(res.getHeader("pragma")).toBe("no-cache");
+ });
+
+ it.each([
+ { case: "unknown api format", body: { apiFormat: "openai.completions" } },
+ { case: "missing api format", body: {} },
+ {
+ case: "unknown extra field",
+ body: { apiFormat: "openai.responses", limit: 10 },
+ },
+ { case: "not an object", body: "openai.responses" },
+ ])("rejects an invalid body: $case", async ({ body }) => {
+ expect((await post(body)).statusCode).toBe(400);
+ });
+
+ it.each([
+ "openai.responses",
+ "openai.chat-completions",
+ "anthropic.messages",
+ ])("accepts api format %s and advances to authorization", async (format) => {
+ // 401 rather than 400 proves the body passed the schema: the route only
+ // reaches signature verification after parsing succeeds.
+ expect((await post({ apiFormat: format })).statusCode).toBe(401);
+ });
+
+ it("accepts a model without using it for resolution", async () => {
+ expect(
+ (
+ await post({
+ apiFormat: "openai.responses",
+ model: "gpt-4o-mini",
+ })
+ ).statusCode,
+ ).toBe(401);
+ });
+
+ it("rejects a body that is not valid JSON", async () => {
+ const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
+ method: "POST",
+ headers: { authorization: "Bearer test-key" },
+ body: "{not-json" as never,
+ });
+
+ await handler(req, res);
+
+ expect(res.statusCode).toBe(400);
+ });
+
+ it("allows only POST", async () => {
+ const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
+ method: "GET",
+ });
+
+ await handler(req, res);
+
+ expect(res.statusCode).toBe(405);
+ expect(res.getHeader("allow")).toBe("POST");
+ });
+
+ it("rejects missing bearer authentication", async () => {
+ expect((await post({ apiFormat: "openai.responses" }, "")).statusCode).toBe(
+ 401,
+ );
+ });
+});
diff --git a/web/src/__tests__/server/unit/create-authed-project-api-route-auth-errors.servertest.ts b/web/src/__tests__/server/unit/create-authed-project-api-route-auth-errors.servertest.ts
index 8a117ed182..396854da1b 100644
--- a/web/src/__tests__/server/unit/create-authed-project-api-route-auth-errors.servertest.ts
+++ b/web/src/__tests__/server/unit/create-authed-project-api-route-auth-errors.servertest.ts
@@ -1,6 +1,7 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { createMocks } from "node-mocks-http";
import { z } from "zod";
+import type * as PrismaClientModule from "@prisma/client";
const {
mockVerifyAuthHeaderAndReturnScope,
@@ -26,9 +27,16 @@ vi.mock("@/src/features/public-api/server/apiAuth", () => ({
},
}));
-vi.mock("@langfuse/shared/src/db", () => ({
- prisma: {},
-}));
+vi.mock("@langfuse/shared/src/db", async () => {
+ const { GatewayConnectionStatus, GatewayIngestionMode, GatewayProvider } =
+ await vi.importActual<typeof PrismaClientModule>("@prisma/client");
+ return {
+ GatewayConnectionStatus,
+ GatewayIngestionMode,
+ GatewayProvider,
+ prisma: {},
+ };
+});
vi.mock("@langfuse/shared/src/server", () => ({
redis: null,
diff --git a/web/src/__tests__/server/unit/policy/verifier.servertest.ts b/web/src/__tests__/server/unit/policy/verifier.servertest.ts
index 7900fe1d83..09ad7837b4 100644
--- a/web/src/__tests__/server/unit/policy/verifier.servertest.ts
+++ b/web/src/__tests__/server/unit/policy/verifier.servertest.ts
@@ -1,7 +1,7 @@
import { describe, expect, it, vi } from "vitest";
-import { type ApiKey } from "@langfuse/shared/src/db";
import { InternalServerError } from "@langfuse/shared";
+import { type ApiKey } from "@langfuse/shared/src/db";
import { hashSecretKey } from "@langfuse/shared/src/server";
import { Verifier } from "@/src/features/apiKey/verifier";
diff --git a/web/src/__tests__/server/unit/policy/verifyAuth.servertest.ts b/web/src/__tests__/server/unit/policy/verifyAuth.servertest.ts
index 16a91d972f..76271fd18f 100644
--- a/web/src/__tests__/server/unit/policy/verifyAuth.servertest.ts
+++ b/web/src/__tests__/server/unit/policy/verifyAuth.servertest.ts
@@ -9,12 +9,14 @@ const {
mockEnforceProjectAuth,
mockDiffResults,
mockRecordCoverage,
+ mockVerifyGatewayIngestionAuthorization,
} = vi.hoisted(() => ({
env: { API_AUTH_MIGRATION: "legacy" as string },
mockLegacyVerifyAuth: vi.fn(),
mockEnforceProjectAuth: vi.fn(),
mockDiffResults: vi.fn(),
mockRecordCoverage: vi.fn(),
+ mockVerifyGatewayIngestionAuthorization: vi.fn(),
}));
vi.mock("@/src/env.mjs", () => ({ env }));
@@ -33,6 +35,10 @@ vi.mock("@/src/features/auth/policy/shadow", async (importOriginal) => ({
recordCoverage: mockRecordCoverage,
}));
+vi.mock("@/src/features/ai-gateway/server", () => ({
+ verifyGatewayIngestionAuthorization: mockVerifyGatewayIngestionAuthorization,
+}));
+
import { verifyAuth } from "@/src/features/public-api/server/createAuthedProjectAPIRoute";
describe("project seam verifyAuth", () => {
@@ -101,6 +107,47 @@ describe("project seam verifyAuth", () => {
});
});
+ describe("gateway ingestion token opt-in", () => {
+ it("returns gateway authorization before selecting an API auth migration path", async () => {
+ const gatewayAuthorization = {
+ validKey: true,
+ scope: {
+ projectId: "gateway-project",
+ accessLevel: "project",
+ },
+ };
+ mockVerifyGatewayIngestionAuthorization.mockResolvedValue(
+ gatewayAuthorization,
+ );
+
+ await expect(
+ verifyAuth({
+ req,
+ name: "Ingest Traces",
+ action: "traces:create",
+ allowGatewayIngestionToken: true,
+ }),
+ ).resolves.toBe(gatewayAuthorization);
+ expect(mockLegacyVerifyAuth).not.toHaveBeenCalled();
+ expect(mockEnforceProjectAuth).not.toHaveBeenCalled();
+ });
+
+ it("falls through to the selected API auth path for non-gateway credentials", async () => {
+ mockVerifyGatewayIngestionAuthorization.mockResolvedValue(null);
+ legacyAllows();
+
+ await expect(
+ verifyAuth({
+ req,
+ name: "Ingest Traces",
+ action: "traces:create",
+ allowGatewayIngestionToken: true,
+ }),
+ ).resolves.toBe(legacyScope);
+ expect(mockLegacyVerifyAuth).toHaveBeenCalledOnce();
+ });
+ });
+
describe("an unrecognized mode fails safe to legacy", () => {
beforeEach(() => {
env.API_AUTH_MIGRATION = "";
diff --git a/web/src/components/PagedSettingsContainer.tsx b/web/src/components/PagedSettingsContainer.tsx
index 2aa7e8ed70..b422e0463a 100644
--- a/web/src/components/PagedSettingsContainer.tsx
+++ b/web/src/components/PagedSettingsContainer.tsx
@@ -1,11 +1,13 @@
import { cn } from "@/src/utils/tailwind";
import { ArrowUpRight } from "lucide-react";
import Link from "next/link";
-import { type ReactNode } from "react";
+import { Fragment, type ReactNode } from "react";
import {
Select,
SelectContent,
+ SelectGroup,
SelectItem,
+ SelectLabel,
SelectTrigger,
SelectValue,
} from "@/src/components/ui/select";
@@ -16,15 +18,18 @@ type SettingsProps = {
{
title: string;
slug: string;
+ section?: string;
show?: boolean | (() => boolean);
} & ({ content: ReactNode } | { href: string })
>;
activeSlug?: string;
+ fullHeight?: boolean;
};
export const PagedSettingsContainer = ({
pages,
activeSlug,
+ fullHeight = false,
}: SettingsProps) => {
const router = useRouter();
const availablePages = pages.filter((page) =>
@@ -48,8 +53,19 @@ export const PagedSettingsContainer = ({
};
return (
- <main className="flex flex-1 flex-col gap-4 py-4 md:gap-8">
- <div className="grid w-full items-start gap-4 md:grid-cols-[150px_1fr] lg:grid-cols-[220px_1fr]">
+ <main
+ className={cn(
+ "flex flex-1 flex-col gap-4 py-4 md:gap-8",
+ fullHeight && "min-h-0 overflow-hidden",
+ )}
+ >
+ <div
+ className={cn(
+ "grid w-full items-start gap-4 md:grid-cols-[150px_1fr] lg:grid-cols-[220px_1fr]",
+ fullHeight &&
+ "min-h-0 flex-1 grid-rows-[auto_minmax(0,1fr)] md:grid-rows-1",
+ )}
+ >
<nav className="block md:hidden">
<Select
onValueChange={(slug) => {
@@ -63,49 +79,86 @@ export const PagedSettingsContainer = ({
<SelectValue placeholder="Select a page" />
</SelectTrigger>
<SelectContent>
- {availablePages.map((page) => (
- <SelectItem key={page.title} value={page.slug}>
- {page.title}
- {"href" in page && (
- <ArrowUpRight size={14} className="ml-1 inline" />
- )}
- </SelectItem>
+ {groupPagesBySection(availablePages).map((group) => (
+ <SelectGroup key={group.section ?? "default"}>
+ {group.section ? (
+ <SelectLabel>{group.section}</SelectLabel>
+ ) : null}
+ {group.pages.map((page) => (
+ <SelectItem key={page.title} value={page.slug}>
+ {page.title}
+ {"href" in page && (
+ <ArrowUpRight size={14} className="ml-1 inline" />
+ )}
+ </SelectItem>
+ ))}
+ </SelectGroup>
))}
</SelectContent>
</Select>
</nav>
<nav
- className="text-muted-foreground hidden gap-4 text-sm md:sticky md:top-5 md:grid"
+ className={cn(
+ "text-muted-foreground hidden text-sm md:sticky md:top-5 md:grid",
+ fullHeight && "md:top-0",
+ )}
x-chunk="dashboard-04-chunk-0"
>
- {availablePages.map((page) =>
- "href" in page ? (
- <Link
- key={page.title}
- href={page.href}
- className="flex flex-row items-center gap-2 font-bold"
- >
- {page.title}
- <ArrowUpRight size={14} className="inline" />
- </Link>
- ) : (
- <span
- key={page.title}
- onClick={() => onChange(page.slug)}
- className={cn(
- "cursor-pointer font-bold",
- page.slug === currentPage.slug && "text-primary",
- )}
- >
- {page.title}
- </span>
- ),
- )}
+ {availablePages.map((page, index) => (
+ <Fragment key={page.title}>
+ {page.section &&
+ page.section !== availablePages[index - 1]?.section ? (
+ <span className="text-muted-foreground mt-4 flex h-8 items-center px-2 text-xs font-bold first:mt-0">
+ {page.section}
+ </span>
+ ) : null}
+ {"href" in page ? (
+ <Link
+ href={page.href}
+ className="hover:bg-muted hover:text-foreground flex h-8 flex-row items-center gap-2 rounded-sm px-2"
+ >
+ {page.title}
+ <ArrowUpRight size={14} className="inline" />
+ </Link>
+ ) : (
+ <span
+ onClick={() => onChange(page.slug)}
+ className={cn(
+ "hover:bg-muted hover:text-foreground flex h-8 cursor-pointer items-center rounded-sm px-2",
+ page.slug === currentPage.slug &&
+ "bg-muted text-primary font-bold",
+ )}
+ >
+ {page.title}
+ </span>
+ )}
+ </Fragment>
+ ))}
</nav>
- <div className="w-full overflow-hidden p-1">
+ <div
+ className={cn(
+ "w-full overflow-hidden p-1",
+ fullHeight && "h-full min-h-0",
+ )}
+ >
{currentPage && "content" in currentPage ? currentPage.content : null}
</div>
</div>
</main>
);
};
+
+function groupPagesBySection<T extends { section?: string }>(pages: T[]) {
+ return pages.reduce<Array<{ section: string | undefined; pages: T[] }>>(
+ (groups, page) => {
+ const previous = groups.at(-1);
+ if (previous && previous.section === page.section) {
+ previous.pages.push(page);
+ } else {
+ groups.push({ section: page.section, pages: [page] });
+ }
+ return groups;
+ },
+ [],
+ );
+}
diff --git a/web/src/components/layouts/container-page.tsx b/web/src/components/layouts/container-page.tsx
index 0704c16e02..1b3ce99a45 100644
--- a/web/src/components/layouts/container-page.tsx
+++ b/web/src/components/layouts/container-page.tsx
@@ -9,19 +9,36 @@ import { cn } from "@/src/utils/tailwind";
type SettingsContainerProps = {
children: React.ReactNode;
headerProps: Omit<PageHeaderProps, "container">;
+ fullHeight?: boolean;
+ extendRight?: boolean;
};
const containerLayoutClassName =
"lg:mx-auto lg:w-full lg:max-w-screen-lg lg:px-8 xl:max-w-screen-xl 2xl:max-w-[1400px]";
-const ContainerPage = ({ children, headerProps }: SettingsContainerProps) => {
+const rightExtendedContainerLayoutClassName =
+ "lg:ml-[max(0px,calc((100%_-_1024px)/2))] lg:px-8 xl:ml-[max(0px,calc((100%_-_1280px)/2))] 2xl:ml-[max(0px,calc((100%_-_1400px)/2))]";
+
+const ContainerPage = ({
+ children,
+ headerProps,
+ fullHeight = false,
+ extendRight = false,
+}: SettingsContainerProps) => {
// Same minimal-chrome mobile shell as Page (slim top bar + page-title block),
// so settings/container pages match the rest of the app on mobile. Desktop is
// unchanged: the existing container PageHeader renders on `md` and up.
const isMobile = useIsMobile();
return (
- <div className="min-h-screen-with-banner relative flex flex-1 flex-col">
+ <div
+ className={cn(
+ "relative flex flex-1 flex-col",
+ fullHeight
+ ? "h-screen-with-banner overflow-hidden"
+ : "min-h-screen-with-banner",
+ )}
+ >
<header className="sticky top-0 z-50 w-full">
{isMobile ? (
<MobileTopBar
@@ -36,7 +53,9 @@ const ContainerPage = ({ children, headerProps }: SettingsContainerProps) => {
<main
className={cn(
"relative flex min-h-0 flex-1 flex-col p-3",
- containerLayoutClassName,
+ extendRight
+ ? rightExtendedContainerLayoutClassName
+ : containerLayoutClassName,
)}
>
{children}
diff --git a/web/src/components/table/data-table.tsx b/web/src/components/table/data-table.tsx
index 87c9acc76a..0d8ff19ffc 100644
--- a/web/src/components/table/data-table.tsx
+++ b/web/src/components/table/data-table.tsx
@@ -30,6 +30,7 @@ import {
Table,
TableBody,
TableCell,
+ TableFooter,
TableHead,
TableHeader,
TableRow,
@@ -120,8 +121,13 @@ interface DataTableProps<TData, TValue> {
className?: string;
shouldRenderGroupHeaders?: boolean;
onRowClick?: (row: TData, event?: React.MouseEvent) => void;
+ renderRow?: (props: {
+ row: Row<TData>;
+ children: React.ReactNode;
+ }) => React.ReactNode;
/** Used for row click handling and MemoizedTableBody snapshot only. Render <TablePeekView> as a sibling outside DataTable. */
peekView?: DataTablePeekViewProps;
+ footer?: React.ReactNode;
hidePagination?: boolean;
tableName: string;
getRowClassName?: (row: TData) => string;
@@ -234,7 +240,9 @@ export function DataTable<TData extends object, TValue>({
className,
shouldRenderGroupHeaders = false,
onRowClick,
+ renderRow,
peekView,
+ footer,
hidePagination = false,
tableName,
getRowClassName,
@@ -434,12 +442,14 @@ export function DataTable<TData extends object, TValue>({
)}
>
<div
- // pr-2 + scrollbar-gutter:stable reserve a small gutter on the right so the
- // last column's resize handle is never flush against the scrollbar/edge and
- // always has some cursor room. Partial mitigation for LFE-10460: a maximized
- // browser still clamps the cursor at the screen edge, so this guarantees room
- // to the right, not a complete fix.
- className="relative min-h-full w-full overflow-auto border-t pr-2 [scrollbar-gutter:stable]"
+ // When the final visible column can be resized, reserve a small gutter so
+ // its handle is never flush against the scrollbar or edge and always has
+ // some cursor room. Partial mitigation for LFE-10460: a maximized browser
+ // still clamps the cursor at the screen edge.
+ className={cn(
+ "relative min-h-full w-full overflow-auto border-t [scrollbar-gutter:stable]",
+ table.getVisibleLeafColumns().at(-1)?.getCanResize() && "pr-2",
+ )}
style={{ ...columnSizeVars }}
onScroll={onScroll}
>
@@ -617,6 +627,7 @@ export function DataTable<TData extends object, TValue>({
help={help}
noResultsMessage={noResultsMessage}
onRowClick={hasRowClickAction ? handleOnRowClick : undefined}
+ renderRow={renderRow}
getRowClassName={getRowClassName}
highlightAllRows={highlightAllRows}
selectionStore={selectionStore}
@@ -638,6 +649,7 @@ export function DataTable<TData extends object, TValue>({
help={help}
noResultsMessage={noResultsMessage}
onRowClick={hasRowClickAction ? handleOnRowClick : undefined}
+ renderRow={renderRow}
getRowClassName={getRowClassName}
highlightAllRows={highlightAllRows}
selectionStore={selectionStore}
@@ -645,6 +657,18 @@ export function DataTable<TData extends object, TValue>({
cellPadding={cellPadding}
/>
)}
+ {footer ? (
+ <TableFooter className="bg-transparent">
+ <TableRow>
+ <TableCell
+ className="h-12 text-center"
+ colSpan={table.getVisibleLeafColumns().length}
+ >
+ {footer}
+ </TableCell>
+ </TableRow>
+ </TableFooter>
+ ) : null}
</Table>
</div>
</div>
@@ -753,6 +777,10 @@ interface TableBodyComponentProps<TData> {
help?: { description: string; href: string };
noResultsMessage?: React.ReactNode;
onRowClick?: (row: TData, event?: React.MouseEvent) => void;
+ renderRow?: (props: {
+ row: Row<TData>;
+ children: React.ReactNode;
+ }) => React.ReactNode;
getRowClassName?: (row: TData) => string;
highlightAllRows?: boolean;
selectionStore?: TableSelectionStoreLike;
@@ -831,6 +859,7 @@ function TableBodyComponent<TData>({
help,
noResultsMessage,
onRowClick,
+ renderRow,
getRowClassName,
highlightAllRows,
selectionStore,
@@ -914,82 +943,87 @@ function TableBodyComponent<TData>({
</TableRow>
))
) : rowModelRows.length ? (
- rowModelRows.map((row) => (
- <TableRowComponent
- key={row.id}
- row={row}
- onRowClick={onRowClick}
- getRowClassName={getRowClassName}
- highlightAllRows={highlightAllRows}
- selectionStore={selectionStore}
- >
- {row.getVisibleCells().map((cell) => {
- const cellValue = cell.getValue();
- const isStringCell = typeof cellValue === "string";
- const isSmallRowHeight = (rowHeight ?? "s") === "s";
- const columnDef = cell.column
- .columnDef as LangfuseColumnDef<TData>;
+ rowModelRows.map((row) => {
+ const cells = row.getVisibleCells().map((cell) => {
+ const cellValue = cell.getValue();
+ const isStringCell = typeof cellValue === "string";
+ const isSmallRowHeight = (rowHeight ?? "s") === "s";
+ const columnDef = cell.column.columnDef as LangfuseColumnDef<TData>;
- return (
- <TableCell
- key={cell.id}
+ return (
+ <TableCell
+ key={cell.id}
+ className={cn(
+ "overflow-hidden border-b text-xs first:pl-2",
+ getCellPaddingClassName(columnDef.cellPadding ?? cellPadding),
+ isSmallRowHeight && "whitespace-nowrap",
+ getPinningClasses(cell.column),
+ getCellBackgroundClassName(columnDef.cellBackground),
+ )}
+ style={{
+ ...getCommonPinningStyles(cell.column),
+ width: columnDef.isFlexWidth
+ ? "auto"
+ : `calc(var(--col-${cell.column.id}-size) * 1px)`,
+ }}
+ >
+ <div
className={cn(
- "overflow-hidden border-b text-xs first:pl-2",
- getCellPaddingClassName(
- columnDef.cellPadding ?? cellPadding,
- ),
- isSmallRowHeight && "whitespace-nowrap",
- getPinningClasses(cell.column),
- getCellBackgroundClassName(columnDef.cellBackground),
+ "flex",
+ isSmallRowHeight && !topAlignCells
+ ? "items-center"
+ : "items-start",
+ !isSmallRowHeight && "py-1",
+ rowheighttw,
)}
- style={{
- ...getCommonPinningStyles(cell.column),
- width: columnDef.isFlexWidth
- ? "auto"
- : `calc(var(--col-${cell.column.id}-size) * 1px)`,
- }}
>
- <div
- className={cn(
- "flex",
- isSmallRowHeight && !topAlignCells
- ? "items-center"
- : "items-start",
- !isSmallRowHeight && "py-1",
- rowheighttw,
- )}
- >
- {isStringCell && isSmallRowHeight ? (
- <div
- className="min-w-0 truncate leading-normal"
- title={getPlainTextFromReactNode(
- flexRender(
- cell.column.columnDef.cell,
- cell.getContext(),
- ),
- )}
- >
- {flexRender(
- cell.column.columnDef.cell,
- cell.getContext(),
- )}
- </div>
- ) : isStringCell && !isSmallRowHeight ? (
- <div className="flex h-full min-h-0 w-full min-w-0 flex-col overflow-hidden text-ellipsis">
- {flexRender(
+ {isStringCell && isSmallRowHeight ? (
+ <div
+ className="min-w-0 truncate leading-normal"
+ title={getPlainTextFromReactNode(
+ flexRender(
cell.column.columnDef.cell,
cell.getContext(),
- )}
- </div>
- ) : (
- flexRender(cell.column.columnDef.cell, cell.getContext())
- )}
- </div>
- </TableCell>
- );
- })}
- </TableRowComponent>
- ))
+ ),
+ )}
+ >
+ {flexRender(
+ cell.column.columnDef.cell,
+ cell.getContext(),
+ )}
+ </div>
+ ) : isStringCell && !isSmallRowHeight ? (
+ <div className="flex h-full min-h-0 w-full min-w-0 flex-col overflow-hidden text-ellipsis">
+ {flexRender(
+ cell.column.columnDef.cell,
+ cell.getContext(),
+ )}
+ </div>
+ ) : (
+ flexRender(cell.column.columnDef.cell, cell.getContext())
+ )}
+ </div>
+ </TableCell>
+ );
+ });
+
+ return renderRow ? (
+ <React.Fragment key={row.id}>
+ {renderRow({ row, children: cells })}
+ </React.Fragment>
+ ) : (
+ <TableRowComponent
+ key={row.id}
+ row={row}
+ onRowClick={onRowClick}
+ getRowClassName={getRowClassName}
+ highlightAllRows={highlightAllRows}
+ selectionStore={selectionStore}
+ >
+ {cells}
+ </TableRowComponent>
+ );
+ })
) : (
<TableRow className="hover:bg-transparent">
<TableCell colSpan={columns.length} className="h-24">
diff --git a/web/src/components/ui/confirm-dialog.tsx b/web/src/components/ui/confirm-dialog.tsx
index 0e2845c0da..c4164a2a8d 100644
--- a/web/src/components/ui/confirm-dialog.tsx
+++ b/web/src/components/ui/confirm-dialog.tsx
@@ -56,7 +56,7 @@ export function ConfirmDialog({
title: React.ReactNode;
description?: React.ReactNode;
trigger?: React.ReactNode;
- confirmLabel?: string;
+ confirmLabel?: React.ReactNode;
confirmVariant?: ButtonProps["variant"];
cancelLabel?: string;
onCancel?: () => void | Promise<void>;
diff --git a/web/src/components/ui/table.tsx b/web/src/components/ui/table.tsx
index e46cb895fa..d844c89519 100644
--- a/web/src/components/ui/table.tsx
+++ b/web/src/components/ui/table.tsx
@@ -174,6 +174,7 @@ export {
Table,
TableHeader,
TableBody,
+ TableFooter,
TableHead,
TableRow,
TableCell,
diff --git a/web/src/ee/features/admin-api/server/projects/projectById/index.ts b/web/src/ee/features/admin-api/server/projects/projectById/index.ts
index 0fb745aa3e..0d1b9c0e08 100644
--- a/web/src/ee/features/admin-api/server/projects/projectById/index.ts
+++ b/web/src/ee/features/admin-api/server/projects/projectById/index.ts
@@ -120,6 +120,20 @@ export async function handleDeleteProject(
scope: ApiAccessScope,
) {
try {
+ const gatewayConfig = await prisma.gatewayConfig.findFirst({
+ where: {
+ organizationId: scope.orgId,
+ defaultIngestionProjectId: projectId,
+ },
+ select: { organizationId: true },
+ });
+ if (gatewayConfig) {
+ return res.status(409).json({
+ message:
+ "This project is used as the AI Gateway ingestion project. Select another ingestion project before deleting it.",
+ });
+ }
+
// API keys need to be deleted from cache. Otherwise, they will still be valid.
await new ApiAuthService(prisma, redis).invalidateCachedProjectApiKeys(
projectId,
diff --git a/web/src/env.mjs b/web/src/env.mjs
index 0b52f15422..eac294591b 100644
--- a/web/src/env.mjs
+++ b/web/src/env.mjs
@@ -354,13 +354,51 @@ export const env = createEnv({
"ENCRYPTION_KEY must be 256 bits, 64 string characters in hex format, generate via: openssl rand -hex 32",
)
.optional(),
+ LANGFUSE_AI_GATEWAY_SERVICE_KEY: z.string().min(1).optional(),
+ LANGFUSE_AI_GATEWAY_SERVICE_KEY_PREVIOUS: z.string().min(1).optional(),
+ LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST: z
+ .string()
+ .optional()
+ .transform((value) =>
+ value
+ ? value
+ .split(",")
+ .map((organizationId) => organizationId.trim())
+ .filter(Boolean)
+ : [],
+ ),
+ LANGFUSE_AI_GATEWAY_JWT_KEY_ID: z.string().min(1).optional(),
+ LANGFUSE_AI_GATEWAY_JWT_PRIVATE_KEY: z.string().min(1).optional(),
+ LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY: z.string().min(1).optional(),
+ LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_KEY_ID: z.string().min(1).optional(),
+ LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_PUBLIC_KEY: z.string().min(1).optional(),
+ LANGFUSE_AI_GATEWAY_JWT_ISSUER: z
+ .string()
+ .min(1)
+ .default("langfuse-control-plane"),
+ LANGFUSE_AI_GATEWAY_JWT_AUDIENCE: z
+ .string()
+ .min(1)
+ .default("langfuse-ingestion"),
// langfuse caching
LANGFUSE_CACHE_API_KEY_ENABLED: z.enum(["true", "false"]).default("true"),
LANGFUSE_CACHE_API_KEY_TTL_SECONDS: z.coerce.number().default(300),
+ // The gateway data plane calls /resolve on every LLM request, so the
+ // lookup is cached. The TTL bounds how long a revoked key or a disabled
+ // connection can still be used if explicit invalidation is missed.
+ LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED: z
+ .enum(["true", "false"])
+ .default("true"),
+ LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_TTL_SECONDS: z.coerce
+ .number()
+ .default(60),
+
// auth migration; self-host and default stay legacy
- API_AUTH_MIGRATION: z.enum(["legacy", "shadow", "enforce"]).default("legacy"),
+ API_AUTH_MIGRATION: z
+ .enum(["legacy", "shadow", "enforce"])
+ .default("legacy"),
// Multimodal media upload to S3
LANGFUSE_S3_MEDIA_MAX_CONTENT_LENGTH: z.coerce
@@ -1027,10 +1065,32 @@ export const env = createEnv({
LANGFUSE_EE_LICENSE_KEY: process.env.LANGFUSE_EE_LICENSE_KEY,
ADMIN_API_KEY: process.env.ADMIN_API_KEY,
ENCRYPTION_KEY: process.env.ENCRYPTION_KEY,
+ LANGFUSE_AI_GATEWAY_SERVICE_KEY:
+ process.env.LANGFUSE_AI_GATEWAY_SERVICE_KEY,
+ LANGFUSE_AI_GATEWAY_SERVICE_KEY_PREVIOUS:
+ process.env.LANGFUSE_AI_GATEWAY_SERVICE_KEY_PREVIOUS,
+ LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST:
+ process.env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST,
+ LANGFUSE_AI_GATEWAY_JWT_KEY_ID: process.env.LANGFUSE_AI_GATEWAY_JWT_KEY_ID,
+ LANGFUSE_AI_GATEWAY_JWT_PRIVATE_KEY:
+ process.env.LANGFUSE_AI_GATEWAY_JWT_PRIVATE_KEY,
+ LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY:
+ process.env.LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY,
+ LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_KEY_ID:
+ process.env.LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_KEY_ID,
+ LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_PUBLIC_KEY:
+ process.env.LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_PUBLIC_KEY,
+ LANGFUSE_AI_GATEWAY_JWT_ISSUER: process.env.LANGFUSE_AI_GATEWAY_JWT_ISSUER,
+ LANGFUSE_AI_GATEWAY_JWT_AUDIENCE:
+ process.env.LANGFUSE_AI_GATEWAY_JWT_AUDIENCE,
// langfuse caching
LANGFUSE_CACHE_API_KEY_ENABLED: process.env.LANGFUSE_CACHE_API_KEY_ENABLED,
LANGFUSE_CACHE_API_KEY_TTL_SECONDS:
process.env.LANGFUSE_CACHE_API_KEY_TTL_SECONDS,
+ LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED:
+ process.env.LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED,
+ LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_TTL_SECONDS:
+ process.env.LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_TTL_SECONDS,
API_AUTH_MIGRATION: process.env.API_AUTH_MIGRATION,
LANGFUSE_ALLOWED_ORGANIZATION_CREATORS:
process.env.LANGFUSE_ALLOWED_ORGANIZATION_CREATORS,
diff --git a/web/src/features/ai-gateway/AGENTS.md b/web/src/features/ai-gateway/AGENTS.md
new file mode 100644
index 0000000000..d7fa3ac18e
--- /dev/null
+++ b/web/src/features/ai-gateway/AGENTS.md
@@ -0,0 +1,11 @@
+## AI Gateway
+
+### Implementation
+
+- performance of the `/resolve` API path is critical - ideally stays under 10ms
+- no tautological frontend component tests that check stuff like height / width
+
+### Testing
+
+- When changing the core flow or model provider-specifics: ensure that the end-to-end test suite
+ remains valid: `web/src/__e2e__/ai-gateway.gatewaye2e.ts`
diff --git a/web/src/features/ai-gateway/CLAUDE.md b/web/src/features/ai-gateway/CLAUDE.md
new file mode 120000
index 0000000000..47dc3e3d86
--- /dev/null
+++ b/web/src/features/ai-gateway/CLAUDE.md
@@ -0,0 +1 @@
+AGENTS.md
\ No newline at end of file
diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/GatewayApiKeysPage.clienttest.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/GatewayApiKeysPage.clienttest.tsx
new file mode 100644
index 0000000000..63a96ef365
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/GatewayApiKeysPage.clienttest.tsx
@@ -0,0 +1,147 @@
+import { fireEvent, render, screen } from "@testing-library/react";
+import type { ReactNode } from "react";
+
+vi.mock("next/router", () => ({
+ useRouter: () => ({ query: {}, push: vi.fn() }),
+}));
+
+const { fetchNextPage, queryState } = vi.hoisted(() => {
+ const fetchNextPage = vi.fn();
+ return {
+ fetchNextPage,
+ queryState: {
+ isPending: false,
+ isError: false,
+ isFetchingNextPage: false,
+ hasNextPage: true,
+ data: {
+ pages: [
+ {
+ data: [
+ {
+ metadata: { environment: "production", region: "eu" },
+ apiKey: {
+ id: "gateway-key-1",
+ publicKey: "pk-lf-gateway",
+ displaySecretKey: "sk-lf-...cdef",
+ note: "Production app",
+ createdAt: new Date("2026-09-04T12:00:00.000Z"),
+ },
+ },
+ ],
+ nextCursor: "gateway-key-1",
+ },
+ ],
+ },
+ fetchNextPage,
+ refetch: vi.fn(),
+ },
+ };
+});
+
+vi.mock("@/src/utils/api", () => {
+ const mutation = () => ({
+ isPending: false,
+ isError: false,
+ mutateAsync: vi.fn(),
+ });
+ return {
+ api: {
+ aiGateway: {
+ listApiKeys: {
+ useInfiniteQuery: () => queryState,
+ },
+ createApiKey: { useMutation: mutation },
+ revokeApiKey: { useMutation: mutation },
+ },
+ useUtils: () => ({
+ aiGateway: {
+ listApiKeys: { invalidate: vi.fn() },
+ },
+ }),
+ },
+ reportNonTrpcError: vi.fn(),
+ reportTrpcErrorWithoutToast: vi.fn(),
+ };
+});
+
+vi.mock("@/src/components/ui/CodeJsonViewer", () => ({
+ CodeView: ({ content }: { content: string }) => <pre>{content}</pre>,
+}));
+
+vi.mock("@/src/components/ui/InfoTooltip/InfoTooltip", () => ({
+ InfoTooltip: ({
+ label,
+ children,
+ }: {
+ label: string;
+ children: ReactNode;
+ }) => <span aria-label={label}>{children}</span>,
+}));
+
+import { TooltipProvider } from "@/src/components/ui/tooltip";
+
+import { GatewayApiKeysPage } from "./GatewayApiKeysPage";
+
+// The page renders tooltips for overflowing metadata badges, which Radix only
+// allows inside a provider (the app supplies one globally in `_app`).
+const page = () => (
+ <TooltipProvider>
+ <GatewayApiKeysPage organizationId="org-1" />
+ </TooltipProvider>
+);
+
+describe("GatewayApiKeysPage", () => {
+ beforeEach(() => {
+ queryState.isPending = false;
+ queryState.isError = false;
+ queryState.isFetchingNextPage = false;
+ queryState.hasNextPage = true;
+ fetchNextPage.mockClear();
+ });
+
+ it("lists only the masked gateway key", () => {
+ render(page());
+
+ expect(screen.getByText("sk-lf-...cdef")).toBeInTheDocument();
+ expect(screen.queryByText("pk-lf-gateway")).not.toBeInTheDocument();
+ });
+
+ it("adds metadata entries in the expandable editor", () => {
+ render(page());
+
+ fireEvent.click(screen.getByRole("button", { name: "Create gateway key" }));
+
+ const metadataTrigger = screen.getByRole("button", {
+ name: /Metadata \(optional\)/,
+ });
+ expect(
+ screen.queryByRole("textbox", { name: "Metadata key" }),
+ ).not.toBeInTheDocument();
+
+ fireEvent.click(metadataTrigger);
+ fireEvent.change(screen.getByRole("textbox", { name: "Metadata key" }), {
+ target: { value: "team" },
+ });
+ fireEvent.change(screen.getByRole("textbox", { name: "Metadata value" }), {
+ target: { value: "checkout" },
+ });
+ expect(metadataTrigger).toHaveTextContent("1 set");
+
+ fireEvent.click(screen.getByRole("button", { name: "Add metadata" }));
+ expect(
+ screen.getAllByRole("textbox", { name: "Metadata key" }),
+ ).toHaveLength(2);
+ });
+
+ it("loads the next page and exposes its pending state", () => {
+ const { rerender } = render(page());
+
+ fireEvent.click(screen.getByRole("button", { name: "Load more" }));
+ expect(fetchNextPage).toHaveBeenCalledOnce();
+
+ queryState.isFetchingNextPage = true;
+ rerender(page());
+ expect(screen.getByRole("button", { name: "Load more" })).toBeDisabled();
+ });
+});
diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/GatewayApiKeysPage.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/GatewayApiKeysPage.tsx
new file mode 100644
index 0000000000..281d5d3400
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/GatewayApiKeysPage.tsx
@@ -0,0 +1,80 @@
+import { Plus } from "lucide-react";
+
+import Header from "@/src/components/layouts/header";
+import { Alert } from "@/src/components/design-system/Alert/Alert";
+import { Button } from "@/src/components/ui/button";
+import { Skeleton } from "@/src/components/ui/skeleton";
+import { CreateGatewayApiKeyDialogController } from "@/src/features/ai-gateway/components/GatewayApiKeysPage/components/CreateGatewayApiKeyDialogController/CreateGatewayApiKeyDialogController";
+import { GatewayApiKeysView } from "@/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysView/GatewayApiKeysView";
+import { RevokeGatewayApiKeyDialog } from "@/src/features/ai-gateway/components/GatewayApiKeysPage/components/RevokeGatewayApiKeyDialog";
+import { api } from "@/src/utils/api";
+
+export function GatewayApiKeysPage({
+ organizationId,
+}: {
+ organizationId: string;
+}) {
+ const apiKeysQuery = api.aiGateway.listApiKeys.useInfiniteQuery(
+ { orgId: organizationId, limit: 50 },
+ { getNextPageParam: (lastPage) => lastPage.nextCursor ?? undefined },
+ );
+
+ if (apiKeysQuery.isPending) {
+ return <ApiKeysSkeleton />;
+ }
+
+ if (apiKeysQuery.isError) {
+ return (
+ <div className="flex flex-col gap-4">
+ <Header title="Gateway API keys" />
+ <Alert variant="destructive">
+ <Alert.Title>Gateway API keys could not be loaded</Alert.Title>
+ <Alert.Description>
+ Retry to manage keys for this organization.
+ </Alert.Description>
+ </Alert>
+ <Button
+ className="w-fit"
+ variant="secondary"
+ onClick={() => apiKeysQuery.refetch()}
+ >
+ Retry
+ </Button>
+ </div>
+ );
+ }
+
+ return (
+ <GatewayApiKeysView
+ apiKeys={apiKeysQuery.data?.pages.flatMap((page) => page.data) ?? []}
+ hasMore={Boolean(apiKeysQuery.hasNextPage)}
+ isLoadingMore={apiKeysQuery.isFetchingNextPage}
+ onLoadMore={() => apiKeysQuery.fetchNextPage()}
+ createAction={
+ <CreateGatewayApiKeyDialogController organizationId={organizationId}>
+ {({ openDialog }) => (
+ <Button onClick={openDialog}>
+ <Plus className="mr-1.5 size-4" />
+ Create gateway key
+ </Button>
+ )}
+ </CreateGatewayApiKeyDialogController>
+ }
+ renderRevokeAction={(apiKeyId) => (
+ <RevokeGatewayApiKeyDialog
+ organizationId={organizationId}
+ apiKeyId={apiKeyId}
+ />
+ )}
+ />
+ );
+}
+
+function ApiKeysSkeleton() {
+ return (
+ <div className="flex flex-col gap-4" data-testid="gateway-api-keys-loading">
+ <Skeleton className="h-7 w-48" />
+ <Skeleton className="h-48 w-full" />
+ </div>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/CreateGatewayApiKeyDialogController/CreateGatewayApiKeyDialogController.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/CreateGatewayApiKeyDialogController/CreateGatewayApiKeyDialogController.tsx
new file mode 100644
index 0000000000..c9c0abd2d6
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/CreateGatewayApiKeyDialogController/CreateGatewayApiKeyDialogController.tsx
@@ -0,0 +1,248 @@
+import { useState, type ReactNode } from "react";
+import { ChevronRight, Plus, X } from "lucide-react";
+
+import { Alert } from "@/src/components/design-system/Alert/Alert";
+import { Button } from "@/src/components/ui/button";
+import {
+ Collapsible,
+ CollapsibleContent,
+ CollapsibleTrigger,
+} from "@/src/components/ui/collapsible";
+import {
+ DialogBody,
+ DialogController,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/src/components/ui/dialog";
+import { InfoTooltip } from "@/src/components/ui/InfoTooltip/InfoTooltip";
+import { Input } from "@/src/components/ui/input";
+import { Label } from "@/src/components/ui/label";
+import { GeneratedKeyContent } from "@/src/features/ai-gateway/components/GatewayApiKeysPage/components/GeneratedKeyContent";
+import {
+ api,
+ reportNonTrpcError,
+ reportTrpcErrorWithoutToast,
+} from "@/src/utils/api";
+
+type MetadataField = {
+ id: number;
+ key: string;
+ value: string;
+};
+
+export function CreateGatewayApiKeyDialogController({
+ organizationId,
+ children,
+}: {
+ organizationId: string;
+ children: (control: { openDialog: () => void }) => ReactNode;
+}) {
+ const [note, setNote] = useState("");
+ const [metadata, setMetadata] = useState<MetadataField[]>(() => [
+ { id: 1, key: "", value: "" },
+ ]);
+ const [isMetadataOpen, setIsMetadataOpen] = useState(false);
+ const [nextMetadataId, setNextMetadataId] = useState(2);
+ const [generatedKeys, setGeneratedKeys] = useState<{
+ publicKey: string;
+ secretKey: string;
+ } | null>(null);
+ const utils = api.useUtils();
+ const create = api.aiGateway.createApiKey.useMutation({
+ onError: (error) =>
+ reportTrpcErrorWithoutToast(error, "ai-gateway-api-keys"),
+ });
+
+ const reset = () => {
+ setNote("");
+ setMetadata([{ id: 1, key: "", value: "" }]);
+ setIsMetadataOpen(false);
+ setGeneratedKeys(null);
+ setNextMetadataId(2);
+ };
+
+ const metadataCount = metadata.filter((field) => field.key.trim()).length;
+
+ const submit = async () => {
+ const metadataObject = Object.fromEntries(
+ metadata
+ .filter((field) => field.key.trim())
+ .map((field) => [field.key.trim(), field.value]),
+ );
+ try {
+ const created = await create.mutateAsync({
+ orgId: organizationId,
+ note: note.trim() || undefined,
+ metadata: metadataObject,
+ });
+ setGeneratedKeys({
+ publicKey: created.publicKey,
+ secretKey: created.secretKey,
+ });
+ await utils.aiGateway.listApiKeys.invalidate({
+ orgId: organizationId,
+ });
+ } catch (error) {
+ reportNonTrpcError(error, "ai-gateway-api-keys");
+ }
+ };
+
+ return (
+ <DialogController
+ size="default"
+ closeOnInteractionOutside={false}
+ onBeforeClose={() => !create.isPending}
+ onDismiss={reset}
+ renderContent={({ closeDialog }) => (
+ <>
+ <DialogHeader>
+ <DialogTitle>
+ {generatedKeys ? "Gateway API key created" : "Issue API key"}
+ </DialogTitle>
+ </DialogHeader>
+ {generatedKeys ? (
+ <GeneratedKeyContent generatedKeys={generatedKeys} />
+ ) : (
+ <>
+ <DialogBody>
+ <div>
+ <Label
+ htmlFor="gateway-key-note"
+ className="flex items-center gap-1.5"
+ >
+ Description
+ <InfoTooltip label="About gateway key descriptions">
+ Shown in the key list. Use it to describe what consumes
+ the key.
+ </InfoTooltip>
+ </Label>
+ <Input
+ id="gateway-key-note"
+ className="mt-1.5"
+ maxLength={500}
+ placeholder="e.g. Checkout agent, backend service"
+ value={note}
+ onChange={(event) => setNote(event.target.value)}
+ />
+ </div>
+ <Collapsible
+ open={isMetadataOpen}
+ onOpenChange={setIsMetadataOpen}
+ >
+ <div className="flex items-center gap-1.5">
+ <CollapsibleTrigger asChild>
+ <button
+ type="button"
+ className="group flex items-center gap-2 text-left text-sm"
+ >
+ <ChevronRight className="text-muted-foreground size-3.5 transition-transform group-data-[state=open]:rotate-90" />
+ <span>Metadata</span>
+ <span className="text-muted-foreground font-normal">
+ (optional)
+ {metadataCount > 0 ? ` · ${metadataCount} set` : ""}
+ </span>
+ </button>
+ </CollapsibleTrigger>
+ <InfoTooltip label="About gateway key metadata">
+ Attached to every trace from this key, so you can filter
+ and group by it.
+ </InfoTooltip>
+ </div>
+ <CollapsibleContent className="mt-2 pl-5.5">
+ <div className="flex flex-col gap-2">
+ {metadata.map((field) => (
+ <div
+ key={field.id}
+ className="ph-no-capture flex items-center gap-2"
+ >
+ <Input
+ aria-label="Metadata key"
+ placeholder="key"
+ value={field.key}
+ onChange={(event) =>
+ setMetadata((current) =>
+ current.map((item) =>
+ item.id === field.id
+ ? { ...item, key: event.target.value }
+ : item,
+ ),
+ )
+ }
+ />
+ <Input
+ aria-label="Metadata value"
+ placeholder="value"
+ value={field.value}
+ onChange={(event) =>
+ setMetadata((current) =>
+ current.map((item) =>
+ item.id === field.id
+ ? { ...item, value: event.target.value }
+ : item,
+ ),
+ )
+ }
+ />
+ <Button
+ size="icon-xs"
+ variant="ghost"
+ aria-label="Remove metadata field"
+ onClick={() =>
+ setMetadata((current) =>
+ current.filter((item) => item.id !== field.id),
+ )
+ }
+ >
+ <X className="size-4" />
+ </Button>
+ </div>
+ ))}
+ </div>
+ <button
+ type="button"
+ className="text-primary mt-2 flex items-center gap-1 text-sm hover:underline"
+ onClick={() => {
+ setMetadata((current) => [
+ ...current,
+ { id: nextMetadataId, key: "", value: "" },
+ ]);
+ setNextMetadataId((current) => current + 1);
+ }}
+ >
+ <Plus className="size-3.5" />
+ Add metadata
+ </button>
+ </CollapsibleContent>
+ </Collapsible>
+ {create.isError ? (
+ <Alert variant="destructive">
+ <Alert.Title>Gateway key could not be created</Alert.Title>
+ <Alert.Description>
+ {create.error?.message ??
+ "Check the metadata and try again."}
+ </Alert.Description>
+ </Alert>
+ ) : null}
+ </DialogBody>
+ <DialogFooter>
+ <Button variant="secondary" onClick={closeDialog}>
+ Cancel
+ </Button>
+ <Button
+ loading={create.isPending}
+ disabled={create.isPending}
+ onClick={submit}
+ >
+ Create key
+ </Button>
+ </DialogFooter>
+ </>
+ )}
+ </>
+ )}
+ >
+ {({ openDialog }) => children({ openDialog })}
+ </DialogController>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysView/GatewayApiKeysView.stories.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysView/GatewayApiKeysView.stories.tsx
new file mode 100644
index 0000000000..25e84eacb5
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysView/GatewayApiKeysView.stories.tsx
@@ -0,0 +1,111 @@
+import type { ComponentProps } from "react";
+import { Button } from "@/src/components/ui/button";
+import { expect, fn, userEvent } from "storybook/test";
+
+import preview from "@/.storybook/preview";
+import { GatewayApiKeysView } from "./GatewayApiKeysView";
+
+const meta = preview.meta({ component: GatewayApiKeysView });
+
+const onCreate = fn();
+const onRevoke = fn();
+const onLoadMore = fn();
+
+const apiKeys = [
+ {
+ metadata: { environment: "production", region: "eu" },
+ apiKey: {
+ id: "gateway-key-production",
+ publicKey: "pk-lf-gw-...4fa2",
+ displaySecretKey: "sk-lf-gw-...91bc",
+ note: "Production application",
+ createdAt: new Date("2026-09-04T12:00:00.000Z"),
+ },
+ },
+] satisfies ComponentProps<typeof GatewayApiKeysView>["apiKeys"];
+
+const manyApiKeys = Array.from({ length: 30 }, (_, index) => ({
+ metadata: {
+ environment: index % 2 === 0 ? "production" : "staging",
+ region: index % 3 === 0 ? "us" : "eu",
+ },
+ apiKey: {
+ id: `gateway-key-${index + 1}`,
+ publicKey: `pk-lf-gw-${String(index + 1).padStart(4, "0")}`,
+ displaySecretKey: `sk-lf-gw-...${String(index + 1).padStart(4, "0")}`,
+ note: `Application ${index + 1}`,
+ createdAt: new Date(Date.UTC(2026, 8, 4 - (index % 28))),
+ },
+})) satisfies ComponentProps<typeof GatewayApiKeysView>["apiKeys"];
+
+const actions = {
+ createAction: <Button onClick={onCreate}>Create gateway key</Button>,
+ renderRevokeAction: (apiKeyId: string) => (
+ <Button variant="ghost" onClick={() => onRevoke(apiKeyId)}>
+ Revoke
+ </Button>
+ ),
+ hasMore: false,
+ isLoadingMore: false,
+ onLoadMore,
+} satisfies Pick<
+ ComponentProps<typeof GatewayApiKeysView>,
+ | "createAction"
+ | "renderRevokeAction"
+ | "hasMore"
+ | "isLoadingMore"
+ | "onLoadMore"
+>;
+
+export const PopulatedMetadata = meta.story({
+ args: {
+ apiKeys,
+ ...actions,
+ },
+});
+
+export const ManyRowsWithMoreAvailable = meta.story({
+ args: {
+ apiKeys: manyApiKeys,
+ ...actions,
+ hasMore: true,
+ },
+});
+
+export const Empty = meta.story({
+ args: {
+ apiKeys: [],
+ ...actions,
+ },
+});
+
+export const MoreAvailable = meta.story({
+ args: {
+ apiKeys,
+ ...actions,
+ hasMore: true,
+ },
+});
+
+export const LoadingMore = meta.story({
+ args: {
+ apiKeys,
+ ...actions,
+ hasMore: true,
+ isLoadingMore: true,
+ },
+});
+
+export const LoadsMore = meta.story({
+ name: "(Test) Loads More",
+ args: {
+ apiKeys,
+ ...actions,
+ hasMore: true,
+ },
+ play: async ({ canvas }) => {
+ onLoadMore.mockClear();
+ await userEvent.click(canvas.getByRole("button", { name: "Load more" }));
+ await expect(onLoadMore).toHaveBeenCalledOnce();
+ },
+});
diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysView/GatewayApiKeysView.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysView/GatewayApiKeysView.tsx
new file mode 100644
index 0000000000..da91477717
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysView/GatewayApiKeysView.tsx
@@ -0,0 +1,164 @@
+import type { ReactNode } from "react";
+
+import Header from "@/src/components/layouts/header";
+import { DataTable } from "@/src/components/table/data-table";
+import type { LangfuseColumnDef } from "@/src/components/table/types";
+import { Badge } from "@/src/components/design-system/Badge/Badge";
+import { SingleLineOverflowList } from "@/src/components/SingleLineOverflowList";
+import { Button } from "@/src/components/ui/button";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@/src/components/ui/tooltip";
+
+type GatewayApiKey = {
+ metadata: unknown;
+ apiKey: {
+ id: string;
+ publicKey: string;
+ displaySecretKey: string;
+ note: string | null;
+ createdAt: Date;
+ };
+};
+
+export function GatewayApiKeysView({
+ apiKeys,
+ createAction,
+ renderRevokeAction,
+ hasMore,
+ isLoadingMore,
+ onLoadMore,
+}: {
+ apiKeys: GatewayApiKey[];
+ createAction: ReactNode;
+ renderRevokeAction: (apiKeyId: string) => ReactNode;
+ hasMore: boolean;
+ isLoadingMore: boolean;
+ onLoadMore: () => unknown;
+}) {
+ const columns: LangfuseColumnDef<GatewayApiKey>[] = [
+ {
+ accessorKey: "apiKey.createdAt",
+ id: "createdAt",
+ header: "Created",
+ cell: ({ row }) => row.original.apiKey.createdAt.toLocaleDateString(),
+ size: 120,
+ },
+ {
+ accessorKey: "apiKey.displaySecretKey",
+ id: "key",
+ header: "Key",
+ cell: ({ row }) => (
+ <div className="ph-no-capture font-mono">
+ {row.original.apiKey.displaySecretKey}
+ </div>
+ ),
+ },
+ {
+ accessorKey: "apiKey.note",
+ id: "description",
+ header: "Description",
+ cell: ({ row }) => row.original.apiKey.note || "—",
+ },
+ {
+ accessorKey: "metadata",
+ header: "Metadata",
+ cell: ({ row }) => <MetadataCell metadata={row.original.metadata} />,
+ },
+ {
+ accessorKey: "apiKey.id",
+ id: "actions",
+ header: "Actions",
+ cell: ({ row }) => renderRevokeAction(row.original.apiKey.id),
+ size: 100,
+ isFixedPosition: true,
+ enableSorting: false,
+ enableResizing: false,
+ },
+ ];
+
+ return (
+ <div className="flex flex-col gap-4">
+ <Header title="Gateway API keys" actionButtons={createAction} />
+ <p className="text-muted-foreground text-sm">
+ These organization keys authenticate requests to the AI Gateway only.
+ </p>
+ <div className="flex max-h-[60dvh] flex-col overflow-hidden rounded-md border">
+ <DataTable
+ tableName="gatewayApiKeys"
+ columns={columns}
+ data={{ isLoading: false, isError: false, data: apiKeys }}
+ hidePagination
+ footer={
+ hasMore ? (
+ <Button
+ variant="secondary"
+ loading={isLoadingMore}
+ disabled={isLoadingMore}
+ aria-label="Load more"
+ onClick={() => {
+ onLoadMore();
+ }}
+ >
+ Load more
+ </Button>
+ ) : undefined
+ }
+ noResultsMessage="No gateway API keys created."
+ getRowClassName={() => "h-9"}
+ cellPadding="comfortable"
+ />
+ </div>
+ </div>
+ );
+}
+
+function MetadataCell({ metadata }: { metadata: unknown }) {
+ const entries = getMetadataEntries(metadata);
+
+ if (entries.length === 0) {
+ return <span className="text-muted-foreground">—</span>;
+ }
+
+ // Rendered without a wrapper element: the list measures against its own
+ // width, so it has to stretch inside the table cell's flex row rather than
+ // shrink to the width of the badges it is measuring.
+ return (
+ <SingleLineOverflowList
+ items={entries}
+ additionalOverflowCount={0}
+ getKey={([key]) => key}
+ renderItem={([key, value]) => (
+ <span className="ph-no-capture inline-flex min-w-0">
+ <Badge size="sm" text={`${key}: ${value}`} />
+ </span>
+ )}
+ renderOverflow={({ hiddenItems, overflowItemCount }) => (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <span className="inline-flex" tabIndex={0}>
+ <Badge size="sm" text={`+${overflowItemCount}`} />
+ </span>
+ </TooltipTrigger>
+ <TooltipContent className="ph-no-capture max-w-xs">
+ {hiddenItems.map(([key, value]) => `${key}: ${value}`).join(", ")}
+ </TooltipContent>
+ </Tooltip>
+ )}
+ />
+ );
+}
+
+function getMetadataEntries(value: unknown): Array<[string, string]> {
+ if (!value || typeof value !== "object" || Array.isArray(value)) return [];
+ return Object.entries(value).flatMap(([key, item]) =>
+ item === null ||
+ typeof item === "string" ||
+ typeof item === "number" ||
+ typeof item === "boolean"
+ ? [[key, String(item)]]
+ : [],
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GeneratedKeyContent.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GeneratedKeyContent.tsx
new file mode 100644
index 0000000000..e48e2c9176
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GeneratedKeyContent.tsx
@@ -0,0 +1,20 @@
+import { CodeView } from "@/src/components/ui/CodeJsonViewer";
+import { DialogBody } from "@/src/components/ui/dialog";
+
+export function GeneratedKeyContent({
+ generatedKeys,
+}: {
+ generatedKeys: { publicKey: string; secretKey: string };
+}) {
+ return (
+ <DialogBody className="ph-no-capture">
+ <div>
+ <p className="text-muted-foreground text-sm">
+ This key can only be viewed once. You can always create new keys in
+ the organization settings.
+ </p>
+ <CodeView content={generatedKeys.secretKey} className="mt-2" />
+ </div>
+ </DialogBody>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/RevokeGatewayApiKeyDialog.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/RevokeGatewayApiKeyDialog.tsx
new file mode 100644
index 0000000000..93fcf382fc
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/RevokeGatewayApiKeyDialog.tsx
@@ -0,0 +1,51 @@
+import { useState } from "react";
+
+import { Button } from "@/src/components/ui/button";
+import { ConfirmDialog } from "@/src/components/ui/confirm-dialog";
+import { api, reportNonTrpcError } from "@/src/utils/api";
+
+export function RevokeGatewayApiKeyDialog({
+ organizationId,
+ apiKeyId,
+}: {
+ organizationId: string;
+ apiKeyId: string;
+}) {
+ const [open, setOpen] = useState(false);
+ const utils = api.useUtils();
+ const revoke = api.aiGateway.revokeApiKey.useMutation();
+
+ const submit = async () => {
+ try {
+ await revoke.mutateAsync({ orgId: organizationId, id: apiKeyId });
+ await utils.aiGateway.listApiKeys.invalidate({
+ orgId: organizationId,
+ });
+ setOpen(false);
+ } catch (error) {
+ reportNonTrpcError(error, "ai-gateway-api-keys");
+ }
+ };
+
+ return (
+ <ConfirmDialog
+ open={open}
+ onOpenChange={setOpen}
+ trigger={
+ <Button
+ type="button"
+ variant="link"
+ size="sm"
+ className="text-foreground hover:text-foreground h-auto px-0 py-0"
+ >
+ Revoke
+ </Button>
+ }
+ title="Revoke gateway API key"
+ description="Requests using this key will immediately be rejected. This action cannot be undone."
+ confirmLabel="Revoke key"
+ loading={revoke.isPending}
+ onConfirm={submit}
+ />
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayConfigurationPage/GatewayConfigurationPage.tsx b/web/src/features/ai-gateway/components/GatewayConfigurationPage/GatewayConfigurationPage.tsx
new file mode 100644
index 0000000000..3963f7c76a
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayConfigurationPage/GatewayConfigurationPage.tsx
@@ -0,0 +1,118 @@
+import { useSession } from "next-auth/react";
+
+import Header from "@/src/components/layouts/header";
+import { Alert } from "@/src/components/design-system/Alert/Alert";
+import { Button } from "@/src/components/ui/button";
+import { Skeleton } from "@/src/components/ui/skeleton";
+import { GatewayConfigurationView } from "@/src/features/ai-gateway/components/GatewayConfigurationPage/components/GatewayConfigurationView/GatewayConfigurationView";
+import { showSuccessToast } from "@/src/features/notifications";
+import { api, reportNonTrpcError } from "@/src/utils/api";
+
+type Project = {
+ id: string;
+ name: string;
+ deletedAt?: Date | string | null;
+};
+
+export function GatewayConfigurationPage({
+ organizationId,
+ projects,
+}: {
+ organizationId: string;
+ projects: Project[];
+}) {
+ const session = useSession();
+ const configQuery = api.aiGateway.getConfig.useQuery({
+ orgId: organizationId,
+ });
+ const utils = api.useUtils();
+ const createProject = api.projects.create.useMutation();
+ const updateConfig = api.aiGateway.updateConfig.useMutation();
+
+ async function saveGatewayConfig(params: {
+ projectId: string | null;
+ ingestionMode: "USAGE" | "FULL";
+ }) {
+ await updateConfig.mutateAsync({
+ orgId: organizationId,
+ defaultIngestionProjectId: params.projectId,
+ ingestionMode: params.ingestionMode,
+ });
+ await utils.aiGateway.getConfig.invalidate({ orgId: organizationId });
+ showSuccessToast({
+ title: "Gateway configuration saved",
+ description: "New gateway requests will use this configuration.",
+ });
+ }
+
+ if (configQuery.isPending) {
+ return <ConfigurationSkeleton />;
+ }
+
+ if (configQuery.isError) {
+ return (
+ <div className="flex flex-col gap-4">
+ <Header title="AI Gateway configuration" />
+ <Alert variant="destructive">
+ <Alert.Title>Configuration could not be loaded</Alert.Title>
+ <Alert.Description>
+ Retry the request to continue configuring the gateway.
+ </Alert.Description>
+ </Alert>
+ <Button
+ className="w-fit"
+ variant="secondary"
+ onClick={() => configQuery.refetch()}
+ >
+ Retry
+ </Button>
+ </div>
+ );
+ }
+
+ const config = configQuery.data;
+ return (
+ <GatewayConfigurationView
+ key={`${config?.updatedAt?.toISOString() ?? "new"}:${config?.defaultIngestionProjectId ?? "none"}:${config?.ingestionMode ?? "USAGE"}`}
+ projects={projects}
+ initialProjectId={config?.defaultIngestionProjectId ?? null}
+ initialIngestionMode={config?.ingestionMode ?? "USAGE"}
+ isSaving={createProject.isPending || updateConfig.isPending}
+ saveError={createProject.isError || updateConfig.isError}
+ onSave={async ({ projectId, ingestionMode }) => {
+ try {
+ await saveGatewayConfig({ projectId, ingestionMode });
+ } catch (error) {
+ reportNonTrpcError(error, "ai-gateway-configuration");
+ }
+ }}
+ onCreateProject={async ({ projectName, ingestionMode }) => {
+ try {
+ const project = await createProject.mutateAsync({
+ orgId: organizationId,
+ name: projectName,
+ });
+ await session.update();
+ await saveGatewayConfig({ projectId: project.id, ingestionMode });
+ } catch (error) {
+ reportNonTrpcError(error, "ai-gateway-configuration");
+ }
+ }}
+ />
+ );
+}
+
+function ConfigurationSkeleton() {
+ return (
+ <div className="flex flex-col gap-6" data-testid="gateway-config-loading">
+ <Skeleton className="h-7 w-64" />
+ <Skeleton className="h-32 w-full" />
+ <Skeleton className="h-40 w-full" />
+ <div className="grid gap-3 md:grid-cols-3">
+ <Skeleton className="h-36" />
+ <Skeleton className="h-36" />
+ <Skeleton className="h-36" />
+ </div>
+ </div>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/GatewayConfigurationView/GatewayConfigurationView.stories.tsx b/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/GatewayConfigurationView/GatewayConfigurationView.stories.tsx
new file mode 100644
index 0000000000..f838002ec1
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/GatewayConfigurationView/GatewayConfigurationView.stories.tsx
@@ -0,0 +1,36 @@
+import type { ComponentProps } from "react";
+import { fn } from "storybook/test";
+
+import preview from "@/.storybook/preview";
+import { GatewayConfigurationView } from "./GatewayConfigurationView";
+
+const meta = preview.meta({ component: GatewayConfigurationView });
+
+const projects = [
+ { id: "project-production", name: "Production", deletedAt: null },
+ { id: "project-staging", name: "Staging", deletedAt: null },
+] satisfies ComponentProps<typeof GatewayConfigurationView>["projects"];
+
+export const Enabled = meta.story({
+ args: {
+ projects,
+ initialProjectId: "project-production",
+ initialIngestionMode: "USAGE",
+ isSaving: false,
+ saveError: false,
+ onSave: fn(),
+ onCreateProject: fn(),
+ },
+});
+
+export const MissingProjectWarning = meta.story({
+ args: {
+ projects,
+ initialProjectId: "deleted-project",
+ initialIngestionMode: "FULL",
+ isSaving: false,
+ saveError: false,
+ onSave: fn(),
+ onCreateProject: fn(),
+ },
+});
diff --git a/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/GatewayConfigurationView/GatewayConfigurationView.tsx b/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/GatewayConfigurationView/GatewayConfigurationView.tsx
new file mode 100644
index 0000000000..5e6772292d
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/GatewayConfigurationView/GatewayConfigurationView.tsx
@@ -0,0 +1,331 @@
+import { useState } from "react";
+import {
+ Check,
+ Copy,
+ Gauge,
+ Plus,
+ ScanText,
+ type LucideIcon,
+} from "lucide-react";
+
+import Header from "@/src/components/layouts/header";
+import { Alert } from "@/src/components/design-system/Alert/Alert";
+import { Button } from "@/src/components/ui/button";
+import {
+ DialogBody,
+ DialogController,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/src/components/ui/dialog";
+import { Input } from "@/src/components/ui/input";
+import { Label } from "@/src/components/ui/label";
+import { CardDescription, CardTitle } from "@/src/components/ui/card";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/src/components/ui/select";
+import { DEFAULT_GATEWAY_INGESTION_PROJECT_NAME } from "@/src/features/ai-gateway/constants/gatewayConfig";
+import { useCopyToClipboard } from "@/src/hooks/useCopyToClipboard";
+import { cn } from "@/src/utils/tailwind";
+
+const GATEWAY_BASE_URL = "https://gateway.langfuse.com/v1";
+
+type IngestionMode = "USAGE" | "FULL";
+type Project = {
+ id: string;
+ name: string;
+ deletedAt?: Date | string | null;
+};
+
+const ingestionModes: Array<{
+ value: IngestionMode;
+ title: string;
+ description: string;
+ icon: LucideIcon;
+}> = [
+ {
+ value: "USAGE",
+ title: "Usage",
+ description: "Capture model, token usage, cost, and latency.",
+ icon: Gauge,
+ },
+ {
+ value: "FULL",
+ title: "Full",
+ description: "Capture full request and response payloads for tracing.",
+ icon: ScanText,
+ },
+];
+
+export function GatewayConfigurationView({
+ projects,
+ initialProjectId,
+ initialIngestionMode,
+ isSaving,
+ saveError,
+ onSave,
+ onCreateProject,
+}: {
+ projects: Project[];
+ initialProjectId: string | null;
+ initialIngestionMode: IngestionMode;
+ isSaving: boolean;
+ saveError: boolean;
+ onSave: (values: {
+ projectId: string | null;
+ ingestionMode: IngestionMode;
+ }) => void | Promise<void>;
+ onCreateProject: (values: {
+ projectName: string;
+ ingestionMode: IngestionMode;
+ }) => void | Promise<void>;
+}) {
+ const activeProjects = projects.filter((project) => !project.deletedAt);
+ const initialProjectExists =
+ initialProjectId !== null &&
+ activeProjects.some((project) => project.id === initialProjectId);
+ const [projectSelection, setProjectSelection] = useState<string | undefined>(
+ initialProjectExists ? initialProjectId : undefined,
+ );
+ const [showExistingProjects, setShowExistingProjects] =
+ useState(initialProjectExists);
+ const [ingestionMode, setIngestionMode] =
+ useState<IngestionMode>(initialIngestionMode);
+ const projectId = projectSelection ?? null;
+ const isDirty =
+ projectId !== initialProjectId || ingestionMode !== initialIngestionMode;
+
+ return (
+ <div className="flex flex-col gap-6">
+ <div>
+ <Header title="AI Gateway configuration" />
+ <p className="text-muted-foreground text-sm">
+ Configure how requests routed through the organization gateway are
+ ingested into Langfuse.
+ </p>
+ </div>
+
+ <div>
+ <div className="mb-4 flex flex-col gap-1">
+ <CardTitle className="text-base">Gateway endpoint</CardTitle>
+ <CardDescription>
+ Use this fixed base URL in any supported SDK.
+ </CardDescription>
+ </div>
+ <GatewayUrl />
+ </div>
+
+ <div>
+ <CardTitle className="mb-4 text-base">
+ Default ingestion project
+ </CardTitle>
+ {showExistingProjects ? (
+ <div className="flex max-w-md flex-row flex-wrap items-center gap-2">
+ <Select
+ value={projectSelection}
+ onValueChange={setProjectSelection}
+ >
+ <SelectTrigger className="ph-no-capture w-fit max-w-full">
+ <SelectValue placeholder="Select a project" />
+ </SelectTrigger>
+ <SelectContent className="ph-no-capture">
+ {activeProjects.map((project) => (
+ <SelectItem key={project.id} value={project.id}>
+ {project.name}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ <CreateIngestionProjectDialog
+ ingestionMode={ingestionMode}
+ isSaving={isSaving}
+ onCreate={onCreateProject}
+ triggerLabel="or create a new project"
+ />
+ </div>
+ ) : (
+ <div className="flex flex-row items-center gap-2">
+ <CreateIngestionProjectDialog
+ ingestionMode={ingestionMode}
+ isSaving={isSaving}
+ onCreate={onCreateProject}
+ triggerLabel="Create ingestion project"
+ showIcon
+ />
+ <button
+ type="button"
+ className="text-primary text-sm hover:underline"
+ onClick={() => setShowExistingProjects(true)}
+ >
+ or use an existing project
+ </button>
+ </div>
+ )}
+ <p className="text-muted-foreground mt-2 text-xs">
+ A dedicated project keeps gateway traffic separate from your
+ application traces.
+ </p>
+ </div>
+
+ <div>
+ <CardTitle className="mb-4 text-base">Ingestion mode</CardTitle>
+ <div className="grid gap-2 md:grid-cols-2">
+ {ingestionModes.map((option) => {
+ const Icon = option.icon;
+ const selected = ingestionMode === option.value;
+ return (
+ <button
+ key={option.value}
+ type="button"
+ aria-pressed={selected}
+ onClick={() => setIngestionMode(option.value)}
+ className={cn(
+ "bg-card hover:bg-muted/50 flex flex-col items-start gap-2 rounded-md border p-3 text-left transition-colors",
+ selected && "border-primary ring-primary ring-1",
+ )}
+ >
+ <div className="flex w-full items-center justify-between gap-2">
+ <Icon className="text-muted-foreground size-4" />
+ {selected ? (
+ <Check className="text-primary size-3.5" />
+ ) : null}
+ </div>
+ <div>
+ <p className="text-sm font-bold">{option.title}</p>
+ <p className="text-muted-foreground mt-0.5 text-xs leading-4">
+ {option.description}
+ </p>
+ </div>
+ </button>
+ );
+ })}
+ </div>
+ </div>
+
+ {saveError ? (
+ <Alert variant="destructive">
+ <Alert.Title>Configuration was not saved</Alert.Title>
+ <Alert.Description>
+ Check your selections and try again.
+ </Alert.Description>
+ </Alert>
+ ) : null}
+
+ <div className="flex justify-end">
+ <Button
+ disabled={!isDirty || isSaving}
+ loading={isSaving}
+ onClick={() => onSave({ projectId, ingestionMode })}
+ >
+ Save
+ </Button>
+ </div>
+ </div>
+ );
+}
+
+function CreateIngestionProjectDialog({
+ ingestionMode,
+ isSaving,
+ onCreate,
+ triggerLabel,
+ showIcon = false,
+}: {
+ ingestionMode: IngestionMode;
+ isSaving: boolean;
+ onCreate: (values: {
+ projectName: string;
+ ingestionMode: IngestionMode;
+ }) => void | Promise<void>;
+ triggerLabel: string;
+ showIcon?: boolean;
+}) {
+ const [projectName, setProjectName] = useState(
+ DEFAULT_GATEWAY_INGESTION_PROJECT_NAME,
+ );
+
+ return (
+ <DialogController
+ size="default"
+ closeOnInteractionOutside={false}
+ onBeforeClose={() => !isSaving}
+ onDismiss={() => setProjectName(DEFAULT_GATEWAY_INGESTION_PROJECT_NAME)}
+ renderContent={() => (
+ <>
+ <DialogHeader>
+ <DialogTitle>Create ingestion project</DialogTitle>
+ </DialogHeader>
+ <DialogBody>
+ <div>
+ <Label htmlFor="gateway-project-name">Project name</Label>
+ <Input
+ id="gateway-project-name"
+ className="ph-no-capture mt-1.5"
+ value={projectName}
+ onChange={(event) => setProjectName(event.target.value)}
+ />
+ <p className="text-muted-foreground mt-1.5 text-xs">
+ Project access follows organization roles and can be restricted
+ in project settings.
+ </p>
+ </div>
+ </DialogBody>
+ <DialogFooter>
+ <Button
+ disabled={!projectName.trim() || isSaving}
+ loading={isSaving}
+ onClick={() =>
+ onCreate({
+ projectName: projectName.trim(),
+ ingestionMode,
+ })
+ }
+ >
+ Create project
+ </Button>
+ </DialogFooter>
+ </>
+ )}
+ >
+ {({ openDialog }) =>
+ showIcon ? (
+ <Button onClick={openDialog}>
+ <Plus className="mr-1.5 size-4" />
+ {triggerLabel}
+ </Button>
+ ) : (
+ <button
+ type="button"
+ className="text-primary text-sm hover:underline"
+ onClick={openDialog}
+ >
+ {triggerLabel}
+ </button>
+ )
+ }
+ </DialogController>
+ );
+}
+
+function GatewayUrl() {
+ const { copy, isCopied } = useCopyToClipboard();
+ return (
+ <div className="bg-muted flex w-fit max-w-full items-center justify-between gap-3 rounded-md border px-3 py-2">
+ <code className="truncate text-sm" title={GATEWAY_BASE_URL}>
+ {GATEWAY_BASE_URL}
+ </code>
+ <Button
+ size="icon-xs"
+ variant="ghost"
+ aria-label="Copy gateway base URL"
+ onClick={() => copy(GATEWAY_BASE_URL)}
+ >
+ {isCopied ? <Check className="size-4" /> : <Copy className="size-4" />}
+ </Button>
+ </div>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayModelsPage/GatewayModelsPage.tsx b/web/src/features/ai-gateway/components/GatewayModelsPage/GatewayModelsPage.tsx
new file mode 100644
index 0000000000..82267ea4be
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayModelsPage/GatewayModelsPage.tsx
@@ -0,0 +1,150 @@
+import Header from "@/src/components/layouts/header";
+import { Alert } from "@/src/components/design-system/Alert/Alert";
+import { Button } from "@/src/components/ui/button";
+import { Skeleton } from "@/src/components/ui/skeleton";
+import { GatewayModelsView } from "@/src/features/ai-gateway/components/GatewayModelsPage/components/GatewayModelsView/GatewayModelsView";
+import type { GatewayProvider } from "@/src/features/ai-gateway/types/gatewayProvider";
+import { api, reportNonTrpcError, type RouterOutputs } from "@/src/utils/api";
+
+type Connection = RouterOutputs["aiGateway"]["listConnections"]["data"][number];
+type RefreshResult = RouterOutputs["aiGateway"]["refreshModels"][number];
+
+const providerFormats: Record<GatewayProvider, string[]> = {
+ OPENAI: ["OpenAI Responses", "OpenAI Chat Completions"],
+ ANTHROPIC: ["Anthropic Messages"],
+};
+
+type ModelRow = {
+ id: string;
+ availableVia: Array<{ connectionName: string; provider: GatewayProvider }>;
+ apiFormats: string[];
+};
+
+export function GatewayModelsPage({
+ organizationId,
+}: {
+ organizationId: string;
+}) {
+ const utils = api.useUtils();
+ const connectionsQuery = api.aiGateway.listConnections.useInfiniteQuery(
+ { orgId: organizationId, limit: 100 },
+ { getNextPageParam: (lastPage) => lastPage.nextCursor ?? undefined },
+ );
+ const modelsQuery = api.aiGateway.refreshModels.useQuery(
+ { orgId: organizationId },
+ { enabled: connectionsQuery.isSuccess },
+ );
+ const syncModelsMutation = api.aiGateway.syncModels.useMutation({
+ onSuccess: (data) => {
+ utils.aiGateway.refreshModels.setData({ orgId: organizationId }, data);
+ },
+ });
+
+ if (connectionsQuery.isPending) {
+ return <ModelsSkeleton />;
+ }
+
+ if (connectionsQuery.isError) {
+ return <ModelsLoadError retry={() => connectionsQuery.refetch()} />;
+ }
+
+ const connections =
+ connectionsQuery.data?.pages.flatMap((page) => page.data) ?? [];
+ const results = modelsQuery.data ?? null;
+ const rows = results ? aggregateModels(results, connections) : [];
+ const failedResults = results?.filter((result) => !result.success) ?? [];
+
+ const sync = () =>
+ syncModelsMutation
+ .mutateAsync({ orgId: organizationId })
+ .then(() => undefined)
+ .catch((error) => reportNonTrpcError(error, "ai-gateway-models"));
+
+ return (
+ <GatewayModelsView
+ models={rows}
+ failedProviderCount={failedResults.length}
+ providerCount={results?.length ?? connections.length}
+ hasProviders={connections.length > 0}
+ hasSynced={results !== null}
+ isLoading={
+ modelsQuery.isPending ||
+ modelsQuery.isFetching ||
+ syncModelsMutation.isPending
+ }
+ syncError={modelsQuery.isError || syncModelsMutation.isError}
+ onSync={sync}
+ hasMoreProviders={Boolean(connectionsQuery.hasNextPage)}
+ isLoadingMoreProviders={connectionsQuery.isFetchingNextPage}
+ onLoadMoreProviders={() => connectionsQuery.fetchNextPage()}
+ />
+ );
+}
+
+function aggregateModels(
+ results: RefreshResult[],
+ connections: Connection[],
+): ModelRow[] {
+ const connectionsById = new Map(
+ connections.map((connection) => [connection.id, connection]),
+ );
+ const models = new Map<string, ModelRow>();
+
+ for (const result of results) {
+ if (!result.success) continue;
+ const connection = connectionsById.get(result.connectionId);
+ if (!connection) continue;
+ for (const modelId of result.models) {
+ const existing = models.get(modelId);
+ const availableVia = {
+ connectionName: connection.name,
+ provider: connection.provider,
+ };
+ if (existing) {
+ existing.availableVia.push(availableVia);
+ existing.apiFormats = [
+ ...new Set([
+ ...existing.apiFormats,
+ ...providerFormats[connection.provider],
+ ]),
+ ];
+ } else {
+ models.set(modelId, {
+ id: modelId,
+ availableVia: [availableVia],
+ apiFormats: [...providerFormats[connection.provider]],
+ });
+ }
+ }
+ }
+
+ return [...models.values()].toSorted((left, right) =>
+ left.id.localeCompare(right.id),
+ );
+}
+
+function ModelsLoadError({ retry }: { retry: () => void }) {
+ return (
+ <div className="flex flex-col gap-4">
+ <Header title="Gateway models" />
+ <Alert variant="destructive">
+ <Alert.Title>Provider credentials could not be loaded</Alert.Title>
+ <Alert.Description>
+ Models cannot be discovered until the credential list is available.
+ </Alert.Description>
+ </Alert>
+ <Button className="w-fit" variant="secondary" onClick={retry}>
+ Retry
+ </Button>
+ </div>
+ );
+}
+
+function ModelsSkeleton() {
+ return (
+ <div className="flex flex-col gap-4" data-testid="gateway-models-loading">
+ <Skeleton className="h-7 w-48" />
+ <Skeleton className="h-48 w-full" />
+ </div>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayModelsPage/components/GatewayModelsView/GatewayModelsView.stories.tsx b/web/src/features/ai-gateway/components/GatewayModelsPage/components/GatewayModelsView/GatewayModelsView.stories.tsx
new file mode 100644
index 0000000000..c43f6e8e3e
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayModelsPage/components/GatewayModelsView/GatewayModelsView.stories.tsx
@@ -0,0 +1,109 @@
+import type { ComponentProps } from "react";
+import NextAdapterPages from "next-query-params/pages";
+import { fn } from "storybook/test";
+import { QueryParamProvider } from "use-query-params";
+
+import preview from "@/.storybook/preview";
+import { GatewayModelsView } from "./GatewayModelsView";
+
+const meta = preview.meta({
+ component: GatewayModelsView,
+ parameters: { layout: "fullscreen" },
+ decorators: [
+ (Story) => (
+ <QueryParamProvider adapter={NextAdapterPages}>
+ <Story />
+ </QueryParamProvider>
+ ),
+ ],
+});
+
+const models = [
+ {
+ id: "gpt-5-mini",
+ availableVia: [{ connectionName: "Primary", provider: "OPENAI" }],
+ apiFormats: ["OpenAI Responses", "OpenAI Chat Completions"],
+ },
+ {
+ id: "claude-sonnet-4-5",
+ availableVia: [
+ { connectionName: "Anthropic production", provider: "ANTHROPIC" },
+ ],
+ apiFormats: ["Anthropic Messages"],
+ },
+] satisfies ComponentProps<typeof GatewayModelsView>["models"];
+
+const manyModels = Array.from({ length: 60 }, (_, index) => {
+ const provider =
+ index % 2 === 0 ? ("OPENAI" as const) : ("ANTHROPIC" as const);
+
+ return {
+ id: `gateway-model-${String(index + 1).padStart(3, "0")}`,
+ availableVia: [
+ {
+ connectionName: `Credential ${(index % 6) + 1}`,
+ provider,
+ },
+ ],
+ apiFormats:
+ provider === "ANTHROPIC"
+ ? ["Anthropic Messages"]
+ : ["OpenAI Responses", "OpenAI Chat Completions"],
+ };
+}) satisfies ComponentProps<typeof GatewayModelsView>["models"];
+
+const defaultArgs = {
+ models,
+ failedProviderCount: 0,
+ providerCount: 2,
+ hasProviders: true,
+ hasSynced: true,
+ isLoading: false,
+ syncError: false,
+ onSync: fn(),
+ hasMoreProviders: false,
+ isLoadingMoreProviders: false,
+ onLoadMoreProviders: fn(),
+} satisfies ComponentProps<typeof GatewayModelsView>;
+
+export const Populated = meta.story({
+ args: defaultArgs,
+});
+
+export const ManyRowsWithMoreAvailable = meta.story({
+ args: {
+ ...defaultArgs,
+ models: manyModels,
+ hasMoreProviders: true,
+ },
+});
+
+export const PartialFailure = meta.story({
+ args: {
+ ...defaultArgs,
+ failedProviderCount: 1,
+ },
+});
+
+export const Empty = meta.story({
+ args: {
+ ...defaultArgs,
+ models: [],
+ },
+});
+
+export const Loading = meta.story({
+ args: {
+ ...defaultArgs,
+ models: [],
+ hasSynced: false,
+ isLoading: true,
+ },
+});
+
+export const MoreProvidersAvailable = meta.story({
+ args: {
+ ...defaultArgs,
+ hasMoreProviders: true,
+ },
+});
diff --git a/web/src/features/ai-gateway/components/GatewayModelsPage/components/GatewayModelsView/GatewayModelsView.tsx b/web/src/features/ai-gateway/components/GatewayModelsPage/components/GatewayModelsView/GatewayModelsView.tsx
new file mode 100644
index 0000000000..e0a3f591db
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayModelsPage/components/GatewayModelsView/GatewayModelsView.tsx
@@ -0,0 +1,329 @@
+import { type FilterState, type ColumnDefinition } from "@langfuse/shared";
+import { useMemo, useState } from "react";
+import { RefreshCw } from "lucide-react";
+
+import Header from "@/src/components/layouts/header";
+import { Alert } from "@/src/components/design-system/Alert/Alert";
+import { Badge } from "@/src/components/ui/badge";
+import { Button } from "@/src/components/ui/button";
+import { DataTable } from "@/src/components/table/data-table";
+import {
+ DataTableControls,
+ DataTableControlsProvider,
+} from "@/src/components/table/data-table-controls";
+import { DataTableToolbar } from "@/src/components/table/data-table-toolbar";
+import { ResizableFilterLayout } from "@/src/components/table/resizable-filter-layout";
+import type { LangfuseColumnDef } from "@/src/components/table/types";
+import { useSidebarFilterState } from "@/src/features/filters/hooks/useSidebarFilterState";
+import type { FilterConfig } from "@/src/features/filters/lib/filter-config";
+import { providerLabels } from "@/src/features/ai-gateway/constants/providerLabels";
+import type { GatewayProvider } from "@/src/features/ai-gateway/types/gatewayProvider";
+
+const TABLE_NAME = "gateway-models";
+
+export type GatewayModelRow = {
+ id: string;
+ availableVia: Array<{
+ connectionName: string;
+ provider: GatewayProvider;
+ }>;
+ apiFormats: string[];
+};
+
+const filterColumns: ColumnDefinition[] = [
+ {
+ name: "Provider",
+ id: "provider",
+ type: "arrayOptions",
+ internal: "provider",
+ options: Object.entries(providerLabels).map(([value, displayValue]) => ({
+ value,
+ displayValue,
+ })),
+ },
+ {
+ name: "API format",
+ id: "apiFormat",
+ type: "arrayOptions",
+ internal: "apiFormat",
+ options: [],
+ },
+];
+
+const filterConfig: FilterConfig = {
+ tableName: TABLE_NAME,
+ columnDefinitions: filterColumns,
+ defaultExpanded: ["provider", "apiFormat"],
+ facets: [
+ { type: "categorical", column: "provider", label: "Provider" },
+ { type: "categorical", column: "apiFormat", label: "API format" },
+ ],
+};
+
+const columns: LangfuseColumnDef<GatewayModelRow, unknown>[] = [
+ {
+ accessorKey: "id",
+ id: "id",
+ header: "Model",
+ size: 300,
+ isFlexWidth: true,
+ cell: ({ row }) => (
+ <span className="block truncate font-mono" title={row.original.id}>
+ {row.original.id}
+ </span>
+ ),
+ },
+ {
+ accessorKey: "availableVia",
+ id: "availableVia",
+ header: "Available via",
+ size: 360,
+ cell: ({ row }) => (
+ <div className="flex flex-wrap gap-1">
+ {row.original.availableVia.map((connection) => (
+ <Badge
+ key={`${connection.provider}:${connection.connectionName}`}
+ variant="outline-solid"
+ >
+ {connection.connectionName} · {providerLabels[connection.provider]}
+ </Badge>
+ ))}
+ </div>
+ ),
+ },
+ {
+ accessorKey: "apiFormats",
+ id: "apiFormats",
+ header: "API formats",
+ size: 280,
+ cell: ({ row }) => (
+ <div className="flex flex-wrap gap-1">
+ {row.original.apiFormats.map((format) => (
+ <Badge key={format} variant="secondary">
+ {format}
+ </Badge>
+ ))}
+ </div>
+ ),
+ },
+];
+
+export function GatewayModelsView({
+ models,
+ failedProviderCount,
+ providerCount,
+ hasProviders,
+ hasSynced,
+ isLoading,
+ syncError,
+ onSync,
+ hasMoreProviders,
+ isLoadingMoreProviders,
+ onLoadMoreProviders,
+}: {
+ models: GatewayModelRow[];
+ failedProviderCount: number;
+ providerCount: number;
+ hasProviders: boolean;
+ hasSynced: boolean;
+ isLoading: boolean;
+ syncError: boolean;
+ onSync: () => void | Promise<void>;
+ hasMoreProviders: boolean;
+ isLoadingMoreProviders: boolean;
+ onLoadMoreProviders: () => unknown;
+}) {
+ const [searchQuery, setSearchQuery] = useState("");
+ const filterOptions = useMemo(
+ () => ({
+ provider: uniqueSorted(
+ models.flatMap((model) =>
+ model.availableVia.map((connection) => connection.provider),
+ ),
+ ),
+ apiFormat: uniqueSorted(models.flatMap((model) => model.apiFormats)),
+ }),
+ [models],
+ );
+ const queryFilter = useSidebarFilterState(filterConfig, filterOptions, {
+ stateLocation: "memory",
+ });
+ const filteredModels = useMemo(
+ () => filterGatewayModels(models, searchQuery, queryFilter.filterState),
+ [models, queryFilter.filterState, searchQuery],
+ );
+ const emptyMessage = getEmptyMessage({
+ hasProviders,
+ hasSynced,
+ hasActiveFilters:
+ searchQuery.trim().length > 0 || queryFilter.filterState.length > 0,
+ });
+
+ return (
+ <div className="flex h-full min-h-0 flex-col gap-4">
+ <Header
+ title="Gateway models"
+ actionButtons={
+ <Button variant="secondary" loading={isLoading} onClick={onSync}>
+ <RefreshCw className="mr-1.5 size-4" />
+ {hasSynced ? "Retry sync" : "Sync models"}
+ </Button>
+ }
+ />
+ <p className="text-muted-foreground text-sm">
+ Models are discovered from enabled provider credentials and cached for
+ up to five minutes.
+ </p>
+
+ {failedProviderCount > 0 ? (
+ <Alert variant="warning">
+ <Alert.Title>Some providers could not be reached</Alert.Title>
+ <Alert.Description>
+ Showing models from successful providers. {failedProviderCount} of{" "}
+ {providerCount} credentials failed to sync.
+ </Alert.Description>
+ </Alert>
+ ) : null}
+
+ {syncError ? (
+ <Alert variant="destructive">
+ <Alert.Title>Model sync failed</Alert.Title>
+ <Alert.Description>
+ No provider results were returned. Retry the sync.
+ </Alert.Description>
+ </Alert>
+ ) : null}
+
+ <DataTableControlsProvider tableName={TABLE_NAME}>
+ <div className="flex min-h-0 w-full flex-1 flex-col overflow-hidden rounded-md border">
+ <DataTableToolbar
+ tableName={TABLE_NAME}
+ columns={columns}
+ filterState={queryFilter.filterState}
+ searchConfig={{
+ metadataSearchFields: ["Model"],
+ currentQuery: searchQuery,
+ tableAllowsFullTextSearch: false,
+ updateQuery: setSearchQuery,
+ }}
+ />
+ <div className="min-h-0 flex-1 overflow-hidden">
+ <ResizableFilterLayout>
+ <DataTableControls queryFilter={queryFilter} />
+ <div className="flex min-h-0 flex-1 flex-col overflow-hidden">
+ <DataTable
+ tableName={TABLE_NAME}
+ columns={columns}
+ data={
+ isLoading
+ ? { isLoading: true, isError: false }
+ : {
+ isLoading: false,
+ isError: false,
+ data: filteredModels,
+ }
+ }
+ hidePagination
+ footer={
+ hasMoreProviders ? (
+ <Button
+ variant="secondary"
+ loading={isLoadingMoreProviders}
+ disabled={isLoadingMoreProviders}
+ aria-label="Load more"
+ onClick={() => {
+ onLoadMoreProviders();
+ }}
+ >
+ Load more
+ </Button>
+ ) : undefined
+ }
+ className="min-h-0"
+ noResultsMessage={emptyMessage}
+ cellPadding="comfortable"
+ />
+ </div>
+ </ResizableFilterLayout>
+ </div>
+ </div>
+ </DataTableControlsProvider>
+ </div>
+ );
+}
+
+function uniqueSorted(values: string[]) {
+ return [...new Set(values)].toSorted((left, right) =>
+ left.localeCompare(right),
+ );
+}
+
+function getModelFilterValues(model: GatewayModelRow, column: string) {
+ switch (column) {
+ case "provider":
+ return model.availableVia.map((connection) => connection.provider);
+ case "apiFormat":
+ return model.apiFormats;
+ default:
+ return [];
+ }
+}
+
+function filterGatewayModels(
+ models: GatewayModelRow[],
+ searchQuery: string,
+ filters: FilterState,
+) {
+ const normalizedSearch = searchQuery.trim().toLowerCase();
+
+ return models.filter((model) => {
+ if (
+ normalizedSearch.length > 0 &&
+ !model.id.toLowerCase().includes(normalizedSearch)
+ ) {
+ return false;
+ }
+
+ return filters.every((filter) => {
+ const values = getModelFilterValues(model, filter.column);
+
+ if (filter.type === "string") {
+ const needle = filter.value.toLowerCase();
+ const contains = values.some((value) =>
+ value.toLowerCase().includes(needle),
+ );
+ return filter.operator === "does not contain" ? !contains : contains;
+ }
+
+ if (filter.type !== "arrayOptions" && filter.type !== "stringOptions") {
+ return true;
+ }
+
+ const selected = filter.value;
+ if (filter.operator === "none of") {
+ return selected.every((value) => !values.includes(value));
+ }
+ if (filter.operator === "all of") {
+ return selected.every((value) => values.includes(value));
+ }
+ return selected.some((value) => values.includes(value));
+ });
+ });
+}
+
+function getEmptyMessage({
+ hasProviders,
+ hasSynced,
+ hasActiveFilters,
+}: {
+ hasProviders: boolean;
+ hasSynced: boolean;
+ hasActiveFilters: boolean;
+}) {
+ if (hasActiveFilters)
+ return "No models match the current search and filters.";
+ if (!hasProviders)
+ return "Add a provider credential before discovering models.";
+ if (hasSynced) return "No models were returned by the configured providers.";
+ return "Sync models to discover what is currently available.";
+}
diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/GatewayProvidersPage.tsx b/web/src/features/ai-gateway/components/GatewayProvidersPage/GatewayProvidersPage.tsx
new file mode 100644
index 0000000000..c114694677
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/GatewayProvidersPage.tsx
@@ -0,0 +1,164 @@
+import { useState } from "react";
+import { Pencil, Plus } from "lucide-react";
+
+import Header from "@/src/components/layouts/header";
+import { Alert } from "@/src/components/design-system/Alert/Alert";
+import { Button } from "@/src/components/ui/button";
+import { Skeleton } from "@/src/components/ui/skeleton";
+import { DeleteProviderDialog } from "@/src/features/ai-gateway/components/GatewayProvidersPage/components/DeleteProviderDialog";
+import { ProviderDialogController } from "@/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/ProviderDialogController";
+import { GatewayProvidersView } from "@/src/features/ai-gateway/components/GatewayProvidersPage/components/GatewayProvidersView/GatewayProvidersView";
+import { RetryProviderButton } from "@/src/features/ai-gateway/components/GatewayProvidersPage/components/RetryProviderButton";
+import { api, reportNonTrpcError } from "@/src/utils/api";
+
+export function GatewayProvidersPage({
+ organizationId,
+}: {
+ organizationId: string;
+}) {
+ const connectionsQuery = api.aiGateway.listConnections.useInfiniteQuery(
+ { orgId: organizationId, limit: 50 },
+ { getNextPageParam: (lastPage) => lastPage.nextCursor ?? undefined },
+ );
+ const modelsQuery = api.aiGateway.refreshModels.useQuery(
+ { orgId: organizationId },
+ { enabled: connectionsQuery.isSuccess },
+ );
+ const [retriedModelCounts, setRetriedModelCounts] = useState<
+ Record<string, number>
+ >({});
+ const utils = api.useUtils();
+ const reorder = api.aiGateway.reorderConnections.useMutation();
+
+ if (connectionsQuery.isPending) {
+ return <ProvidersSkeleton />;
+ }
+
+ if (connectionsQuery.isError) {
+ return (
+ <div className="flex flex-col gap-4">
+ <Header title="Provider credentials" />
+ <Alert variant="destructive">
+ <Alert.Title>Provider credentials could not be loaded</Alert.Title>
+ <Alert.Description>
+ Retry to manage gateway routing credentials.
+ </Alert.Description>
+ </Alert>
+ <Button
+ className="w-fit"
+ variant="secondary"
+ onClick={() => connectionsQuery.refetch()}
+ >
+ Retry
+ </Button>
+ </div>
+ );
+ }
+
+ const connections =
+ connectionsQuery.data?.pages.flatMap((page) => page.data) ?? [];
+ const modelCounts: Record<string, number | "loading"> = modelsQuery.isPending
+ ? Object.fromEntries(
+ connections
+ .filter((connection) => connection.status === "ENABLED")
+ .map((connection) => [connection.id, "loading"]),
+ )
+ : Object.fromEntries(
+ (modelsQuery.data ?? [])
+ .filter((result) => result.success)
+ .map((result) => [result.connectionId, result.models.length]),
+ );
+ Object.assign(modelCounts, retriedModelCounts);
+
+ return (
+ <GatewayProvidersView
+ connections={connections}
+ modelCounts={modelCounts}
+ hasMore={Boolean(connectionsQuery.hasNextPage)}
+ isLoadingMore={connectionsQuery.isFetchingNextPage}
+ onLoadMore={() => connectionsQuery.fetchNextPage()}
+ canReorder={!connectionsQuery.hasNextPage && !reorder.isPending}
+ onReorder={async (sourceId, targetId) => {
+ const sourceIndex = connections.findIndex(
+ (connection) => connection.id === sourceId,
+ );
+ const targetIndex = connections.findIndex(
+ (connection) => connection.id === targetId,
+ );
+ if (sourceIndex < 0 || targetIndex < 0) return false;
+ const connectionIds = connections.map((connection) => connection.id);
+ const [movedId] = connectionIds.splice(sourceIndex, 1);
+ if (!movedId) return false;
+ connectionIds.splice(targetIndex, 0, movedId);
+ try {
+ await reorder.mutateAsync({ orgId: organizationId, connectionIds });
+ await utils.aiGateway.listConnections.invalidate({
+ orgId: organizationId,
+ });
+ return true;
+ } catch (error) {
+ reportNonTrpcError(error, "ai-gateway-providers");
+ return false;
+ }
+ }}
+ createAction={
+ <ProviderDialogController organizationId={organizationId}>
+ {({ openDialog }) => (
+ <Button onClick={openDialog}>
+ <Plus className="mr-1.5 size-4" />
+ Add credential
+ </Button>
+ )}
+ </ProviderDialogController>
+ }
+ renderCredentialActions={(connection) => {
+ return (
+ <>
+ <RetryProviderButton
+ organizationId={organizationId}
+ connectionId={connection.id}
+ onModelsLoaded={(count) =>
+ setRetriedModelCounts((current) => ({
+ ...current,
+ [connection.id]: count,
+ }))
+ }
+ />
+ <ProviderDialogController
+ key={`${connection.id}:${connection.updatedAt.toISOString()}`}
+ organizationId={organizationId}
+ connection={connection}
+ >
+ {({ openDialog }) => (
+ <Button
+ size="icon-xs"
+ variant="ghost"
+ aria-label="Edit credential"
+ onClick={openDialog}
+ >
+ <Pencil className="size-4" />
+ </Button>
+ )}
+ </ProviderDialogController>
+ <DeleteProviderDialog
+ organizationId={organizationId}
+ connection={connection}
+ />
+ </>
+ );
+ }}
+ />
+ );
+}
+
+function ProvidersSkeleton() {
+ return (
+ <div
+ className="flex flex-col gap-4"
+ data-testid="gateway-providers-loading"
+ >
+ <Skeleton className="h-7 w-56" />
+ <Skeleton className="h-48 w-full" />
+ </div>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/CredentialFields.tsx b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/CredentialFields.tsx
new file mode 100644
index 0000000000..d93855616d
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/CredentialFields.tsx
@@ -0,0 +1,43 @@
+import { Input } from "@/src/components/ui/input";
+import { Label } from "@/src/components/ui/label";
+
+export function CredentialFields({
+ name,
+ credential,
+ credentialPlaceholder,
+ onNameChange,
+ onCredentialChange,
+}: {
+ name: string;
+ credential: string;
+ credentialPlaceholder: string;
+ onNameChange: (name: string) => void;
+ onCredentialChange: (credential: string) => void;
+}) {
+ return (
+ <>
+ <div>
+ <Label htmlFor="gateway-credential-name">Name</Label>
+ <Input
+ id="gateway-credential-name"
+ className="mt-1.5"
+ placeholder="Production"
+ value={name}
+ onChange={(event) => onNameChange(event.target.value)}
+ />
+ </div>
+ <div className="ph-no-capture">
+ <Label htmlFor="gateway-secret-key">Secret key</Label>
+ <Input
+ id="gateway-secret-key"
+ className="mt-1.5"
+ type="password"
+ autoComplete="new-password"
+ placeholder={credentialPlaceholder}
+ value={credential}
+ onChange={(event) => onCredentialChange(event.target.value)}
+ />
+ </div>
+ </>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/DeleteProviderDialog.tsx b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/DeleteProviderDialog.tsx
new file mode 100644
index 0000000000..7738219ca2
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/DeleteProviderDialog.tsx
@@ -0,0 +1,51 @@
+import { useState } from "react";
+import { Trash2 } from "lucide-react";
+
+import { Button } from "@/src/components/ui/button";
+import { ConfirmDialog } from "@/src/components/ui/confirm-dialog";
+import type { GatewayConnection } from "@/src/features/ai-gateway/types/gatewayProvider";
+import { api, reportNonTrpcError } from "@/src/utils/api";
+
+export function DeleteProviderDialog({
+ organizationId,
+ connection,
+}: {
+ organizationId: string;
+ connection: GatewayConnection;
+}) {
+ const [open, setOpen] = useState(false);
+ const utils = api.useUtils();
+ const remove = api.aiGateway.deleteConnection.useMutation();
+
+ const deleteConnection = async () => {
+ try {
+ await remove.mutateAsync({
+ orgId: organizationId,
+ id: connection.id,
+ });
+ await utils.aiGateway.listConnections.invalidate({
+ orgId: organizationId,
+ });
+ setOpen(false);
+ } catch (error) {
+ reportNonTrpcError(error, "ai-gateway-providers");
+ }
+ };
+
+ return (
+ <ConfirmDialog
+ open={open}
+ onOpenChange={setOpen}
+ trigger={
+ <Button size="icon-xs" variant="ghost" aria-label="Delete credential">
+ <Trash2 className="size-4" />
+ </Button>
+ }
+ title="Delete provider credential"
+ description={`Delete “${connection.name}”? Requests will immediately stop using it.`}
+ confirmLabel="Delete credential"
+ loading={remove.isPending}
+ onConfirm={deleteConnection}
+ />
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/GatewayProvidersView/GatewayProvidersView.clienttest.ts b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/GatewayProvidersView/GatewayProvidersView.clienttest.ts
new file mode 100644
index 0000000000..eb76d92e52
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/GatewayProvidersView/GatewayProvidersView.clienttest.ts
@@ -0,0 +1,100 @@
+import { createElement } from "react";
+import { fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+
+import type { GatewayConnection } from "@/src/features/ai-gateway/types/gatewayProvider";
+import {
+ GatewayProvidersView,
+ getProviderReorder,
+ reorderProviderIds,
+} from "./GatewayProvidersView";
+
+const connection = (
+ id: string,
+ name: string,
+ routingPriority: number,
+): GatewayConnection => ({
+ id,
+ name,
+ provider: "OPENAI",
+ displaySecret: "sk-...test",
+ status: "ENABLED",
+ organizationId: "org-1",
+ createdById: "user-1",
+ routingPriority,
+ createdAt: new Date("2026-09-08T12:00:00.000Z"),
+ updatedAt: new Date("2026-09-08T12:00:00.000Z"),
+});
+
+describe("provider credential reordering", () => {
+ it("moves credentials in both directions", () => {
+ expect(
+ reorderProviderIds(["openai", "anthropic"], "openai", "anthropic"),
+ ).toEqual(["anthropic", "openai"]);
+ expect(
+ reorderProviderIds(["anthropic", "openai"], "openai", "anthropic"),
+ ).toEqual(["openai", "anthropic"]);
+ });
+
+ it("resets an optimistic order when the server order changes", async () => {
+ const alpha = connection("alpha", "Alpha", 0);
+ const beta = connection("beta", "Beta", 1);
+ const gamma = connection("gamma", "Gamma", 2);
+ const props = {
+ modelCounts: {},
+ createAction: null,
+ renderCredentialActions: () => null,
+ hasMore: false,
+ isLoadingMore: false,
+ onLoadMore: vi.fn(),
+ canReorder: true,
+ onReorder: vi.fn(async () => true),
+ };
+ const { rerender } = render(
+ createElement(GatewayProvidersView, {
+ ...props,
+ connections: [alpha, beta, gamma],
+ }),
+ );
+
+ fireEvent.click(
+ screen.getAllByRole("button", { name: "Move credential down" })[0]!,
+ );
+ await waitFor(() => {
+ expect(screen.getAllByRole("row")[1]).toHaveTextContent("Beta");
+ });
+
+ rerender(
+ createElement(GatewayProvidersView, {
+ ...props,
+ connections: [gamma, beta, alpha],
+ }),
+ );
+
+ expect(screen.getAllByRole("row")[1]).toHaveTextContent("Gamma");
+ expect(screen.getAllByRole("row")[2]).toHaveTextContent("Beta");
+ expect(screen.getAllByRole("row")[3]).toHaveTextContent("Alpha");
+ });
+
+ it("maps a completed drag to the source and target credentials", () => {
+ expect(
+ getProviderReorder(
+ {
+ active: { id: "openai" },
+ over: { id: "anthropic" },
+ } as Parameters<typeof getProviderReorder>[0],
+ true,
+ ),
+ ).toEqual({ sourceId: "openai", targetId: "anthropic" });
+ });
+
+ it("ignores disabled and unchanged drags", () => {
+ const drag = {
+ active: { id: "openai" },
+ over: { id: "openai" },
+ } as Parameters<typeof getProviderReorder>[0];
+
+ expect(getProviderReorder(drag, true)).toBeNull();
+ expect(getProviderReorder(drag, false)).toBeNull();
+ });
+});
diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/GatewayProvidersView/GatewayProvidersView.stories.tsx b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/GatewayProvidersView/GatewayProvidersView.stories.tsx
new file mode 100644
index 0000000000..2dd5261a50
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/GatewayProvidersView/GatewayProvidersView.stories.tsx
@@ -0,0 +1,171 @@
+import type { ComponentProps } from "react";
+import { Button } from "@/src/components/ui/button";
+import { expect, fn, userEvent } from "storybook/test";
+
+import preview from "@/.storybook/preview";
+import { GatewayProvidersView } from "./GatewayProvidersView";
+
+const meta = preview.meta({ component: GatewayProvidersView });
+
+const onCreate = fn();
+const onCredentialAction = fn();
+const onLoadMore = fn();
+const onReorder = fn(async () => true);
+
+const connections = [
+ {
+ id: "connection-openai",
+ name: "Primary",
+ provider: "OPENAI",
+ displaySecret: "sk-proj-...7d3a",
+ status: "ENABLED",
+ organizationId: "org-1",
+ createdById: "user-1",
+ routingPriority: 0,
+ createdAt: new Date("2026-09-07T12:00:00.000Z"),
+ updatedAt: new Date("2026-09-07T12:00:00.000Z"),
+ },
+ {
+ id: "connection-anthropic",
+ name: "Fallback",
+ provider: "ANTHROPIC",
+ displaySecret: "sk-ant-...91bc",
+ status: "ERROR",
+ organizationId: "org-1",
+ createdById: "user-1",
+ routingPriority: 1,
+ createdAt: new Date("2026-09-07T12:00:00.000Z"),
+ updatedAt: new Date("2026-09-07T12:00:00.000Z"),
+ },
+] satisfies ComponentProps<typeof GatewayProvidersView>["connections"];
+
+const providers = ["OPENAI", "ANTHROPIC"] as const;
+const statuses = ["ENABLED", "ERROR", "DISABLED"] as const;
+const manyConnections = Array.from({ length: 30 }, (_, index) => ({
+ id: `connection-${index + 1}`,
+ name: `Credential ${index + 1}`,
+ provider: providers[index % providers.length]!,
+ displaySecret: `sk-...${String(index + 1).padStart(4, "0")}`,
+ status: statuses[index % statuses.length]!,
+ organizationId: "org-1",
+ createdById: "user-1",
+ routingPriority: index,
+ createdAt: new Date(Date.UTC(2026, 8, 30 - index)),
+ updatedAt: new Date("2026-09-30T12:00:00.000Z"),
+})) satisfies ComponentProps<typeof GatewayProvidersView>["connections"];
+
+const manyModelCounts = Object.fromEntries(
+ manyConnections.map((connection, index) => [connection.id, index * 7 + 1]),
+);
+
+const actions = {
+ createAction: <Button onClick={onCreate}>Add credential</Button>,
+ renderCredentialActions: (connection) => (
+ <Button
+ size="sm"
+ variant="ghost"
+ onClick={() => onCredentialAction(connection.id)}
+ >
+ Manage
+ </Button>
+ ),
+ hasMore: false,
+ isLoadingMore: false,
+ onLoadMore,
+ canReorder: true,
+ onReorder,
+} satisfies Pick<
+ ComponentProps<typeof GatewayProvidersView>,
+ | "createAction"
+ | "renderCredentialActions"
+ | "hasMore"
+ | "isLoadingMore"
+ | "onLoadMore"
+ | "canReorder"
+ | "onReorder"
+>;
+
+export const OrderedCredentials = meta.story({
+ args: {
+ connections,
+ modelCounts: {
+ "connection-openai": 42,
+ "connection-anthropic": 18,
+ },
+ ...actions,
+ },
+});
+
+export const ManyRowsWithMoreAvailable = meta.story({
+ args: {
+ connections: manyConnections,
+ modelCounts: manyModelCounts,
+ ...actions,
+ hasMore: true,
+ },
+});
+
+export const Empty = meta.story({
+ args: {
+ connections: [],
+ modelCounts: {},
+ ...actions,
+ },
+});
+
+export const MoreAvailable = meta.story({
+ args: {
+ connections,
+ modelCounts: {},
+ ...actions,
+ hasMore: true,
+ },
+});
+
+export const LoadingMore = meta.story({
+ args: {
+ connections,
+ modelCounts: {},
+ ...actions,
+ hasMore: true,
+ isLoadingMore: true,
+ },
+});
+
+export const MovesImmediately = meta.story({
+ name: "(Test) Moves Immediately",
+ args: {
+ connections,
+ modelCounts: {},
+ ...actions,
+ },
+ play: async ({ canvas }) => {
+ onReorder.mockClear();
+ await userEvent.click(
+ canvas.getAllByRole("button", { name: "Move credential down" })[0]!,
+ );
+
+ const rows = canvas.getAllByRole("row").slice(1);
+ await expect(rows[0]).toHaveTextContent("Fallback");
+ await expect(rows[1]).toHaveTextContent("Primary");
+ await expect(onReorder).toHaveBeenCalledWith(
+ "connection-openai",
+ "connection-anthropic",
+ );
+ },
+});
+
+export const LoadsMore = meta.story({
+ name: "(Test) Loads More",
+ args: {
+ connections,
+ modelCounts: {},
+ ...actions,
+ hasMore: true,
+ },
+ play: async ({ canvas }) => {
+ onLoadMore.mockClear();
+ await userEvent.click(canvas.getByRole("button", { name: "Load more" }));
+ await expect(onLoadMore).toHaveBeenCalledOnce();
+ },
+});
diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/GatewayProvidersView/GatewayProvidersView.tsx b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/GatewayProvidersView/GatewayProvidersView.tsx
new file mode 100644
index 0000000000..47c696204a
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/GatewayProvidersView/GatewayProvidersView.tsx
@@ -0,0 +1,505 @@
+import {
+ createContext,
+ useCallback,
+ useContext,
+ useLayoutEffect,
+ useMemo,
+ useRef,
+ useState,
+ type CSSProperties,
+ type ReactNode,
+} from "react";
+import {
+ closestCenter,
+ DndContext,
+ KeyboardSensor,
+ MouseSensor,
+ TouchSensor,
+ type DragEndEvent,
+ useSensor,
+ useSensors,
+} from "@dnd-kit/core";
+import {
+ restrictToParentElement,
+ restrictToVerticalAxis,
+} from "@dnd-kit/modifiers";
+import {
+ SortableContext,
+ useSortable,
+ verticalListSortingStrategy,
+} from "@dnd-kit/sortable";
+import { CSS } from "@dnd-kit/utilities";
+import { ArrowDown, ArrowUp, GripVertical, Route } from "lucide-react";
+import { SiAnthropic, SiOpenai } from "react-icons/si";
+
+import Header from "@/src/components/layouts/header";
+import { DataTable } from "@/src/components/table/data-table";
+import type { LangfuseColumnDef } from "@/src/components/table/types";
+import { Badge } from "@/src/components/ui/badge";
+import { Button } from "@/src/components/ui/button";
+import { TableRow } from "@/src/components/ui/table";
+import { providerLabels } from "@/src/features/ai-gateway/constants/providerLabels";
+import type {
+ GatewayConnection,
+ GatewayProvider,
+} from "@/src/features/ai-gateway/types/gatewayProvider";
+import { cn } from "@/src/utils/tailwind";
+
+const TABLE_NAME = "gateway-provider-credentials";
+
+export function reorderProviderIds(
+ ids: string[],
+ sourceId: string,
+ targetId: string,
+) {
+ const sourceIndex = ids.indexOf(sourceId);
+ const targetIndex = ids.indexOf(targetId);
+ if (sourceIndex < 0 || targetIndex < 0 || sourceIndex === targetIndex) {
+ return ids;
+ }
+
+ const nextIds = [...ids];
+ const [movedId] = nextIds.splice(sourceIndex, 1);
+ if (!movedId) return ids;
+ nextIds.splice(targetIndex, 0, movedId);
+ return nextIds;
+}
+
+type SortableHandleContextValue = Pick<
+ ReturnType<typeof useSortable>,
+ "attributes" | "listeners"
+>;
+const SortableHandleContext = createContext<SortableHandleContextValue | null>(
+ null,
+);
+
+type GatewayConnectionRow = GatewayConnection;
+
+type GatewayProvidersViewProps = {
+ connections: GatewayConnectionRow[];
+ modelCounts: Record<string, number | "loading">;
+ createAction: ReactNode;
+ renderCredentialActions: (
+ connection: GatewayConnectionRow,
+ index: number,
+ ) => ReactNode;
+ hasMore: boolean;
+ isLoadingMore: boolean;
+ onLoadMore: () => unknown;
+ canReorder: boolean;
+ onReorder: (sourceId: string, targetId: string) => Promise<boolean>;
+};
+
+export function GatewayProvidersView(props: GatewayProvidersViewProps) {
+ const serverOrderKey = JSON.stringify(
+ props.connections.map((connection) => connection.id),
+ );
+ return <SortableGatewayProvidersView key={serverOrderKey} {...props} />;
+}
+
+function SortableGatewayProvidersView({
+ connections,
+ modelCounts,
+ createAction,
+ renderCredentialActions,
+ hasMore,
+ isLoadingMore,
+ onLoadMore,
+ canReorder,
+ onReorder,
+}: GatewayProvidersViewProps) {
+ const sensors = useSensors(
+ useSensor(MouseSensor, { activationConstraint: { distance: 4 } }),
+ useSensor(TouchSensor, { activationConstraint: { distance: 4 } }),
+ useSensor(KeyboardSensor),
+ );
+ const [orderedIds, setOrderedIds] = useState(() =>
+ connections.map((connection) => connection.id),
+ );
+ const rowNodes = useRef(new Map<string, HTMLTableRowElement>());
+ const pendingRowPositions = useRef<Map<string, number> | null>(null);
+
+ const captureRowPositions = useCallback(
+ () =>
+ new Map(
+ [...rowNodes.current].map(([id, node]) => [
+ id,
+ node.getBoundingClientRect().top,
+ ]),
+ ),
+ [],
+ );
+ const registerRowNode = useCallback(
+ (id: string, node: HTMLTableRowElement | null) => {
+ if (node) rowNodes.current.set(id, node);
+ else rowNodes.current.delete(id);
+ },
+ [],
+ );
+
+ useLayoutEffect(() => {
+ const previousPositions = pendingRowPositions.current;
+ if (!previousPositions) return;
+ pendingRowPositions.current = null;
+
+ for (const [id, node] of rowNodes.current) {
+ const previousTop = previousPositions.get(id);
+ if (previousTop === undefined) continue;
+ const offset = previousTop - node.getBoundingClientRect().top;
+ if (offset === 0) continue;
+
+ node.animate(
+ [
+ { transform: `translateY(${offset}px)` },
+ { transform: "translateY(0)" },
+ ],
+ { duration: 200, easing: "ease" },
+ );
+ }
+ }, [orderedIds]);
+
+ const orderedConnections = useMemo(() => {
+ const orderById = new Map(
+ orderedIds.map((connectionId, index) => [connectionId, index]),
+ );
+ return connections.toSorted(
+ (left, right) =>
+ (orderById.get(left.id) ?? Number.MAX_SAFE_INTEGER) -
+ (orderById.get(right.id) ?? Number.MAX_SAFE_INTEGER),
+ );
+ }, [connections, orderedIds]);
+
+ const moveConnection = useCallback(
+ async (sourceId: string, targetId: string, animateRows = false) => {
+ const previousIds = orderedIds;
+ const nextIds = reorderProviderIds(previousIds, sourceId, targetId);
+ if (nextIds === previousIds) return;
+
+ if (animateRows) pendingRowPositions.current = captureRowPositions();
+ setOrderedIds(nextIds);
+
+ if (!(await onReorder(sourceId, targetId))) {
+ if (animateRows) pendingRowPositions.current = captureRowPositions();
+ setOrderedIds(previousIds);
+ }
+ },
+ [captureRowPositions, onReorder, orderedIds],
+ );
+ const columns = useMemo<LangfuseColumnDef<GatewayConnectionRow, unknown>[]>(
+ () => [
+ {
+ accessorKey: "priority",
+ id: "priority",
+ header: "Priority",
+ size: 180,
+ cell: ({ row }) => (
+ <ProviderPriorityCell
+ connection={row.original}
+ index={row.index}
+ connectionCount={orderedConnections.length}
+ canReorder={canReorder}
+ onMove={async (targetIndex) => {
+ const target = orderedConnections[targetIndex];
+ if (target)
+ await moveConnection(row.original.id, target.id, true);
+ }}
+ />
+ ),
+ },
+ {
+ accessorKey: "provider",
+ id: "provider",
+ header: "Provider",
+ size: 220,
+ cell: ({ row }) => <ProviderName provider={row.original.provider} />,
+ },
+ {
+ accessorKey: "name",
+ id: "name",
+ header: "Name",
+ size: 220,
+ isFlexWidth: true,
+ },
+ {
+ accessorKey: "displaySecret",
+ id: "displaySecret",
+ header: "Credential",
+ size: 220,
+ cell: ({ row }) => (
+ <span className="font-mono">{row.original.displaySecret}</span>
+ ),
+ },
+ {
+ accessorKey: "status",
+ id: "status",
+ header: "Status",
+ size: 120,
+ cell: ({ row }) => <ConnectionStatus status={row.original.status} />,
+ },
+ {
+ accessorKey: "models",
+ id: "models",
+ header: "Models",
+ size: 180,
+ cell: ({ row }) => <ModelCount value={modelCounts[row.original.id]} />,
+ },
+ {
+ accessorKey: "actions",
+ id: "actions",
+ header: "Actions",
+ size: 180,
+ enableResizing: false,
+ isFixedPosition: true,
+ cell: ({ row }) => (
+ <div className="flex justify-end gap-1">
+ {renderCredentialActions(row.original, row.index)}
+ </div>
+ ),
+ },
+ ],
+ [
+ canReorder,
+ modelCounts,
+ moveConnection,
+ orderedConnections,
+ renderCredentialActions,
+ ],
+ );
+
+ const handleDragEnd = async (event: DragEndEvent) => {
+ const reorder = getProviderReorder(event, canReorder);
+ if (reorder) await moveConnection(reorder.sourceId, reorder.targetId);
+ };
+
+ return (
+ <div className="flex flex-col gap-4">
+ <Header title="Provider credentials" actionButtons={createAction} />
+ <p className="text-muted-foreground text-sm">
+ Requests use the first compatible enabled credential in routing priority
+ order. Credentials are validated against the provider when they are
+ saved.
+ </p>
+
+ <DndContext
+ autoScroll={false}
+ collisionDetection={closestCenter}
+ modifiers={[restrictToVerticalAxis, restrictToParentElement]}
+ sensors={sensors}
+ onDragEnd={handleDragEnd}
+ >
+ <SortableContext
+ items={orderedConnections.map((connection) => connection.id)}
+ strategy={verticalListSortingStrategy}
+ >
+ <div className="flex max-h-[60dvh] flex-col overflow-hidden rounded-md border">
+ <DataTable
+ tableName={TABLE_NAME}
+ columns={columns}
+ data={{
+ isLoading: false,
+ isError: false,
+ data: orderedConnections,
+ }}
+ hidePagination
+ footer={
+ hasMore ? (
+ <Button
+ variant="secondary"
+ loading={isLoadingMore}
+ disabled={isLoadingMore}
+ aria-label="Load more"
+ onClick={() => {
+ onLoadMore();
+ }}
+ >
+ Load more
+ </Button>
+ ) : undefined
+ }
+ cellPadding="comfortable"
+ noResultsMessage="No provider credentials configured."
+ renderRow={({ row, children }) => (
+ <SortableProviderRow
+ connection={row.original}
+ canReorder={canReorder}
+ registerRowNode={registerRowNode}
+ >
+ {children}
+ </SortableProviderRow>
+ )}
+ />
+ </div>
+ </SortableContext>
+ </DndContext>
+ </div>
+ );
+}
+
+export function getProviderReorder(
+ event: Pick<DragEndEvent, "active" | "over">,
+ canReorder: boolean,
+) {
+ if (
+ !canReorder ||
+ !event.over ||
+ event.active.id === event.over.id ||
+ typeof event.active.id !== "string" ||
+ typeof event.over.id !== "string"
+ ) {
+ return null;
+ }
+
+ return { sourceId: event.active.id, targetId: event.over.id };
+}
+
+function SortableProviderRow({
+ connection,
+ canReorder,
+ registerRowNode,
+ children,
+}: {
+ connection: GatewayConnectionRow;
+ canReorder: boolean;
+ registerRowNode: (id: string, node: HTMLTableRowElement | null) => void;
+ children: ReactNode;
+}) {
+ const {
+ attributes,
+ isDragging,
+ listeners,
+ setNodeRef,
+ transform,
+ transition,
+ } = useSortable({
+ id: connection.id,
+ disabled: !canReorder,
+ });
+ const handleNodeRef = useCallback(
+ (node: HTMLTableRowElement | null) => {
+ setNodeRef(node);
+ registerRowNode(connection.id, node);
+ },
+ [connection.id, registerRowNode, setNodeRef],
+ );
+ const style: CSSProperties = {
+ transform: CSS.Transform.toString(transform),
+ transition,
+ zIndex: isDragging ? 1 : undefined,
+ position: isDragging ? "relative" : undefined,
+ };
+
+ return (
+ <SortableHandleContext.Provider value={{ attributes, listeners }}>
+ <TableRow
+ ref={handleNodeRef}
+ style={style}
+ className={cn(
+ "ph-no-capture group hover:bg-accent cursor-default",
+ isDragging && "bg-muted z-10 opacity-70 shadow-sm",
+ )}
+ >
+ {children}
+ </TableRow>
+ </SortableHandleContext.Provider>
+ );
+}
+
+function ProviderPriorityCell({
+ connection,
+ index,
+ connectionCount,
+ canReorder,
+ onMove,
+}: {
+ connection: GatewayConnectionRow;
+ index: number;
+ connectionCount: number;
+ canReorder: boolean;
+ onMove: (targetIndex: number) => Promise<void>;
+}) {
+ const sortableHandle = useContext(SortableHandleContext);
+
+ return (
+ <div className="flex h-7 items-center gap-1">
+ <button
+ {...sortableHandle?.attributes}
+ {...sortableHandle?.listeners}
+ type="button"
+ disabled={!sortableHandle}
+ aria-label={`Drag ${connection.name} to reorder`}
+ className="text-muted-foreground flex size-6 shrink-0 cursor-grab touch-none items-center justify-center p-0 opacity-50 hover:opacity-100 active:cursor-grabbing disabled:cursor-not-allowed"
+ >
+ <GripVertical className="size-3.5" />
+ </button>
+ <span className="flex size-6 shrink-0 translate-y-px items-center justify-center font-mono leading-none">
+ {index + 1}
+ </span>
+ <div className="flex h-6 items-center opacity-40 transition-opacity group-hover:opacity-100">
+ <Button
+ size="icon-xs"
+ variant="ghost"
+ className="text-muted-foreground size-6 opacity-60 hover:opacity-100"
+ disabled={!canReorder || index === 0}
+ aria-label="Move credential up"
+ onClick={() => onMove(index - 1)}
+ >
+ <ArrowUp className="size-3" />
+ </Button>
+ <Button
+ size="icon-xs"
+ variant="ghost"
+ className="text-muted-foreground size-6 opacity-60 hover:opacity-100"
+ disabled={!canReorder || index === connectionCount - 1}
+ aria-label="Move credential down"
+ onClick={() => onMove(index + 1)}
+ >
+ <ArrowDown className="size-3" />
+ </Button>
+ </div>
+ </div>
+ );
+}
+
+function ModelCount({ value }: { value: number | "loading" | undefined }) {
+ if (value === "loading") return <>Loading…</>;
+ if (value === undefined) return <>—</>;
+ return (
+ <>
+ {value} {value === 1 ? "model" : "models"} available
+ </>
+ );
+}
+
+function ProviderName({ provider }: { provider: GatewayProvider }) {
+ const icon =
+ provider === "OPENAI" ? (
+ <SiOpenai className="size-4" aria-hidden="true" />
+ ) : provider === "ANTHROPIC" ? (
+ <SiAnthropic className="size-4" aria-hidden="true" />
+ ) : (
+ <Route className="size-4" aria-hidden="true" />
+ );
+
+ return (
+ <div className="flex items-center gap-2">
+ <span className="bg-muted flex size-7 items-center justify-center rounded-md border">
+ {icon}
+ </span>
+ <span>{providerLabels[provider]}</span>
+ </div>
+ );
+}
+
+function ConnectionStatus({
+ status,
+}: {
+ status: GatewayConnectionRow["status"];
+}) {
+ const variant =
+ status === "ENABLED"
+ ? "success"
+ : status === "ERROR"
+ ? "error"
+ : "secondary";
+ return <Badge variant={variant}>{status.toLowerCase()}</Badge>;
+}
diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/ProviderDialogController.tsx b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/ProviderDialogController.tsx
new file mode 100644
index 0000000000..9edf6e85f3
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/ProviderDialogController.tsx
@@ -0,0 +1,186 @@
+import { useState, type ReactNode } from "react";
+
+import { Alert } from "@/src/components/design-system/Alert/Alert";
+import { Button } from "@/src/components/ui/button";
+import {
+ DialogBody,
+ DialogController,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/src/components/ui/dialog";
+import { Input } from "@/src/components/ui/input";
+import { Label } from "@/src/components/ui/label";
+import { CredentialFields } from "@/src/features/ai-gateway/components/GatewayProvidersPage/components/CredentialFields";
+import { ProviderSelect } from "@/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderSelect";
+import { providerLabels } from "@/src/features/ai-gateway/constants/providerLabels";
+import type {
+ GatewayConnection,
+ GatewayProvider,
+} from "@/src/features/ai-gateway/types/gatewayProvider";
+import {
+ api,
+ reportNonTrpcError,
+ reportTrpcErrorWithoutToast,
+} from "@/src/utils/api";
+
+export function ProviderDialogController({
+ organizationId,
+ connection,
+ children,
+}: {
+ organizationId: string;
+ connection?: GatewayConnection;
+ children: (control: { openDialog: () => void }) => ReactNode;
+}) {
+ const [provider, setProvider] = useState<GatewayProvider>(
+ connection?.provider ?? "OPENAI",
+ );
+ const [name, setName] = useState(connection?.name ?? "");
+ const [credential, setCredential] = useState("");
+ const utils = api.useUtils();
+ const test = api.aiGateway.testConnection.useMutation({
+ onError: (error) =>
+ reportTrpcErrorWithoutToast(error, "ai-gateway-providers"),
+ });
+ const create = api.aiGateway.createConnection.useMutation({
+ onError: (error) =>
+ reportTrpcErrorWithoutToast(error, "ai-gateway-providers"),
+ });
+ const update = api.aiGateway.updateConnection.useMutation({
+ onError: (error) =>
+ reportTrpcErrorWithoutToast(error, "ai-gateway-providers"),
+ });
+ const isSaving = create.isPending || update.isPending;
+ const isPending = test.isPending || isSaving;
+ const isError = test.isError || create.isError || update.isError;
+
+ const reset = () => {
+ setProvider(connection?.provider ?? "OPENAI");
+ setName(connection?.name ?? "");
+ setCredential("");
+ };
+
+ const testCredential = async () => {
+ try {
+ await test.mutateAsync({
+ orgId: organizationId,
+ provider: connection?.provider ?? provider,
+ credential,
+ });
+ } catch (error) {
+ reportNonTrpcError(error, "ai-gateway-providers");
+ }
+ };
+
+ const submit = async (closeDialog: () => void) => {
+ try {
+ if (connection) {
+ await update.mutateAsync({
+ orgId: organizationId,
+ id: connection.id,
+ name,
+ ...(credential ? { credential } : {}),
+ });
+ } else {
+ await create.mutateAsync({
+ orgId: organizationId,
+ provider,
+ name,
+ credential,
+ });
+ }
+
+ await Promise.all([
+ utils.aiGateway.listConnections.invalidate({ orgId: organizationId }),
+ utils.aiGateway.refreshModels.invalidate({ orgId: organizationId }),
+ ]);
+ closeDialog();
+ reset();
+ } catch (error) {
+ reportNonTrpcError(error, "ai-gateway-providers");
+ }
+ };
+
+ return (
+ <DialogController
+ size="default"
+ closeOnInteractionOutside={false}
+ onDismiss={reset}
+ onBeforeClose={() => !isPending}
+ renderContent={({ closeDialog }) => (
+ <>
+ <DialogHeader>
+ <DialogTitle>
+ {connection
+ ? "Edit provider credential"
+ : "Add provider credential"}
+ </DialogTitle>
+ </DialogHeader>
+ <DialogBody>
+ {connection ? (
+ <div>
+ <Label>Provider</Label>
+ <Input
+ className="mt-1.5"
+ value={providerLabels[connection.provider]}
+ disabled
+ />
+ </div>
+ ) : (
+ <ProviderSelect value={provider} onChange={setProvider} />
+ )}
+ <CredentialFields
+ name={name}
+ credential={credential}
+ credentialPlaceholder={
+ connection
+ ? "Leave blank to keep current key"
+ : "Enter secret key"
+ }
+ onNameChange={setName}
+ onCredentialChange={setCredential}
+ />
+ {isError ? (
+ <Alert variant="destructive">
+ <Alert.Title>Provider validation failed</Alert.Title>
+ <Alert.Description>
+ {test.error?.message ??
+ create.error?.message ??
+ update.error?.message ??
+ "The credential could not be saved or validated. Check the key and try again."}
+ </Alert.Description>
+ </Alert>
+ ) : test.isSuccess ? (
+ <Alert>
+ <Alert.Title>Provider credential is valid</Alert.Title>
+ <Alert.Description>No changes were saved.</Alert.Description>
+ </Alert>
+ ) : null}
+ </DialogBody>
+ <DialogFooter>
+ <Button
+ variant="secondary"
+ disabled={!credential || isPending}
+ loading={test.isPending}
+ onClick={testCredential}
+ >
+ Test
+ </Button>
+ <Button
+ disabled={
+ !name.trim() || (!connection && !credential) || isPending
+ }
+ loading={isSaving}
+ onClick={() => submit(closeDialog)}
+ >
+ Test and save
+ </Button>
+ </DialogFooter>
+ </>
+ )}
+ >
+ {({ openDialog }) => children({ openDialog })}
+ </DialogController>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderSelect.tsx b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderSelect.tsx
new file mode 100644
index 0000000000..2b9323696f
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderSelect.tsx
@@ -0,0 +1,39 @@
+import { Label } from "@/src/components/ui/label";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/src/components/ui/select";
+import { providerLabels } from "@/src/features/ai-gateway/constants/providerLabels";
+import type { GatewayProvider } from "@/src/features/ai-gateway/types/gatewayProvider";
+
+export function ProviderSelect({
+ value,
+ onChange,
+}: {
+ value: GatewayProvider;
+ onChange: (provider: GatewayProvider) => void;
+}) {
+ return (
+ <div>
+ <Label htmlFor="gateway-provider">Provider</Label>
+ <Select
+ value={value}
+ onValueChange={(provider) => onChange(provider as GatewayProvider)}
+ >
+ <SelectTrigger id="gateway-provider" className="mt-1.5">
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ {Object.entries(providerLabels).map(([provider, label]) => (
+ <SelectItem key={provider} value={provider}>
+ {label}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/RetryProviderButton.tsx b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/RetryProviderButton.tsx
new file mode 100644
index 0000000000..f166295312
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/RetryProviderButton.tsx
@@ -0,0 +1,44 @@
+import { RefreshCw } from "lucide-react";
+
+import { Button } from "@/src/components/ui/button";
+import { api, reportNonTrpcError } from "@/src/utils/api";
+
+export function RetryProviderButton({
+ organizationId,
+ connectionId,
+ onModelsLoaded,
+}: {
+ organizationId: string;
+ connectionId: string;
+ onModelsLoaded: (count: number) => void;
+}) {
+ const utils = api.useUtils();
+ const retry = api.aiGateway.retryConnection.useMutation();
+
+ const retryConnection = async () => {
+ try {
+ const result = await retry.mutateAsync({
+ orgId: organizationId,
+ id: connectionId,
+ });
+ if (result.success) onModelsLoaded(result.models.length);
+ await utils.aiGateway.listConnections.invalidate({
+ orgId: organizationId,
+ });
+ } catch (error) {
+ reportNonTrpcError(error, "ai-gateway-providers");
+ }
+ };
+
+ return (
+ <Button
+ size="icon-xs"
+ variant="ghost"
+ loading={retry.isPending}
+ aria-label="Retry provider validation"
+ onClick={retryConnection}
+ >
+ <RefreshCw className="size-4" />
+ </Button>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/gatewayMutationErrors.clienttest.tsx b/web/src/features/ai-gateway/components/gatewayMutationErrors.clienttest.tsx
new file mode 100644
index 0000000000..2e0e700b5b
--- /dev/null
+++ b/web/src/features/ai-gateway/components/gatewayMutationErrors.clienttest.tsx
@@ -0,0 +1,356 @@
+import { TRPCClientError } from "@trpc/client";
+import { fireEvent, render, screen, waitFor } from "@testing-library/react";
+
+import { Button } from "@/src/components/ui/button";
+import { TooltipProvider } from "@/src/components/ui/tooltip";
+import type { GatewayConnection } from "@/src/features/ai-gateway/types/gatewayProvider";
+
+const {
+ reportTrpcErrorWithoutToastMock,
+ reportNonTrpcErrorMock,
+ showErrorToastMock,
+ testConnectionMutateAsync,
+ createApiKeyMutateAsync,
+ mutationStates,
+} = vi.hoisted(() => {
+ const mutationStates = {
+ testConnection: {
+ isError: false,
+ isPending: false,
+ error: null as Error | null,
+ },
+ createConnection: {
+ isError: false,
+ isPending: false,
+ error: null as Error | null,
+ },
+ updateConnection: {
+ isError: false,
+ isPending: false,
+ error: null as Error | null,
+ },
+ createApiKey: {
+ isError: false,
+ isPending: false,
+ error: null as Error | null,
+ },
+ };
+
+ return {
+ reportTrpcErrorWithoutToastMock: vi.fn(),
+ reportNonTrpcErrorMock: vi.fn(),
+ showErrorToastMock: vi.fn(),
+ testConnectionMutateAsync: vi.fn(),
+ createApiKeyMutateAsync: vi.fn(),
+ mutationStates,
+ };
+});
+
+const providerValidationError = () =>
+ new TRPCClientError("Provider credential validation failed", {
+ result: {
+ error: {
+ message: "Provider credential validation failed",
+ code: -32600,
+ data: {
+ code: "BAD_REQUEST",
+ httpStatus: 400,
+ path: "aiGateway.createConnection",
+ },
+ },
+ },
+ });
+
+const gatewayApiKeyError = () =>
+ new TRPCClientError("Gateway key limit reached", {
+ result: {
+ error: {
+ message: "Gateway key limit reached",
+ code: -32600,
+ data: {
+ code: "BAD_REQUEST",
+ httpStatus: 400,
+ path: "aiGateway.createApiKey",
+ },
+ },
+ },
+ });
+
+vi.mock("@/src/utils/api", () => {
+ const React = require("react");
+
+ const createStatefulMutation = <TArgs extends unknown[]>(
+ mutateImpl: (...args: TArgs) => Promise<unknown>,
+ stateKey: keyof typeof mutationStates,
+ ) => {
+ return (options?: { onError?: (error: unknown) => void }) => {
+ const [mutationState, setMutationState] = React.useState({
+ isError: false,
+ isPending: false,
+ error: null as Error | null,
+ });
+
+ return {
+ ...mutationState,
+ mutateAsync: async (...args: TArgs) => {
+ setMutationState(
+ (current: {
+ isError: boolean;
+ isPending: boolean;
+ error: Error | null;
+ }) => ({ ...current, isPending: true }),
+ );
+ try {
+ return await mutateImpl(...args);
+ } catch (error) {
+ const normalizedError =
+ error instanceof Error ? error : new Error(String(error));
+ mutationStates[stateKey] = {
+ isError: true,
+ isPending: false,
+ error: normalizedError,
+ };
+ options?.onError?.(error);
+ setMutationState({
+ isError: true,
+ isPending: false,
+ error: normalizedError,
+ });
+ throw error;
+ }
+ },
+ };
+ };
+ };
+
+ return {
+ api: {
+ useUtils: () => ({
+ aiGateway: {
+ listConnections: { invalidate: vi.fn() },
+ listApiKeys: { invalidate: vi.fn() },
+ },
+ }),
+ aiGateway: {
+ testConnection: {
+ useMutation: createStatefulMutation(
+ async (input: {
+ orgId: string;
+ provider: string;
+ credential: string;
+ }) => testConnectionMutateAsync(input),
+ "testConnection",
+ ),
+ },
+ createConnection: {
+ useMutation: createStatefulMutation(async () => {
+ throw providerValidationError();
+ }, "createConnection"),
+ },
+ updateConnection: {
+ useMutation: createStatefulMutation(async () => {
+ throw providerValidationError();
+ }, "updateConnection"),
+ },
+ createApiKey: {
+ useMutation: createStatefulMutation(
+ async (input: {
+ orgId: string;
+ note?: string;
+ metadata: Record<string, string>;
+ }) => {
+ createApiKeyMutateAsync(input);
+ throw gatewayApiKeyError();
+ },
+ "createApiKey",
+ ),
+ },
+ },
+ },
+ reportTrpcErrorWithoutToast: reportTrpcErrorWithoutToastMock,
+ reportNonTrpcError: reportNonTrpcErrorMock,
+ };
+});
+
+vi.mock("@/src/features/notifications/showErrorToast", () => ({
+ showErrorToast: showErrorToastMock,
+}));
+
+vi.mock("next/router", () => ({
+ useRouter: () => ({ query: {}, push: vi.fn() }),
+}));
+
+import { CreateGatewayApiKeyDialogController } from "@/src/features/ai-gateway/components/GatewayApiKeysPage/components/CreateGatewayApiKeyDialogController/CreateGatewayApiKeyDialogController";
+import { ProviderDialogController } from "@/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/ProviderDialogController";
+
+const testConnection: GatewayConnection = {
+ id: "conn-1",
+ organizationId: "org-1",
+ name: "Production",
+ provider: "OPENAI",
+ status: "ENABLED",
+ displaySecret: "sk-...abcd",
+ createdById: "user-1",
+ routingPriority: 0,
+ createdAt: new Date("2026-09-04T12:00:00.000Z"),
+ updatedAt: new Date("2026-09-04T12:00:00.000Z"),
+};
+
+describe("gateway mutation local error handling", () => {
+ beforeAll(() => {
+ vi.stubGlobal(
+ "ResizeObserver",
+ class {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ },
+ );
+ Element.prototype.scrollIntoView = vi.fn();
+ Element.prototype.hasPointerCapture = vi.fn();
+ });
+
+ beforeEach(() => {
+ reportTrpcErrorWithoutToastMock.mockClear();
+ reportNonTrpcErrorMock.mockClear();
+ showErrorToastMock.mockClear();
+ testConnectionMutateAsync.mockClear();
+ createApiKeyMutateAsync.mockClear();
+ mutationStates.testConnection = {
+ isError: false,
+ isPending: false,
+ error: null,
+ };
+ mutationStates.createConnection = {
+ isError: false,
+ isPending: false,
+ error: null,
+ };
+ mutationStates.updateConnection = {
+ isError: false,
+ isPending: false,
+ error: null,
+ };
+ mutationStates.createApiKey = {
+ isError: false,
+ isPending: false,
+ error: null,
+ };
+ });
+
+ it("tests a provider credential without saving", async () => {
+ render(
+ <ProviderDialogController organizationId="org-1">
+ {({ openDialog }) => <Button onClick={openDialog}>Add provider</Button>}
+ </ProviderDialogController>,
+ );
+
+ fireEvent.click(screen.getByRole("button", { name: "Add provider" }));
+ fireEvent.change(screen.getByLabelText("Secret key"), {
+ target: { value: "sk-test-only" },
+ });
+ fireEvent.click(screen.getByRole("button", { name: "Test" }));
+
+ await waitFor(() => {
+ expect(testConnectionMutateAsync).toHaveBeenCalledWith({
+ orgId: "org-1",
+ provider: "OPENAI",
+ credential: "sk-test-only",
+ });
+ });
+ expect(
+ screen.getByRole("heading", { name: "Add provider credential" }),
+ ).toBeInTheDocument();
+ });
+
+ it("create provider dialog routes tRPC failures locally without a global toast", async () => {
+ render(
+ <ProviderDialogController organizationId="org-1">
+ {({ openDialog }) => <Button onClick={openDialog}>Add provider</Button>}
+ </ProviderDialogController>,
+ );
+
+ fireEvent.click(screen.getByRole("button", { name: "Add provider" }));
+ fireEvent.change(screen.getByLabelText("Name"), {
+ target: { value: "Production" },
+ });
+ fireEvent.change(screen.getByLabelText("Secret key"), {
+ target: { value: "sk-invalid" },
+ });
+ fireEvent.click(screen.getByRole("button", { name: "Test and save" }));
+
+ await waitFor(() => {
+ expect(reportTrpcErrorWithoutToastMock).toHaveBeenCalledWith(
+ expect.any(TRPCClientError),
+ "ai-gateway-providers",
+ );
+ });
+ expect(reportNonTrpcErrorMock).toHaveBeenCalledWith(
+ expect.any(TRPCClientError),
+ "ai-gateway-providers",
+ );
+ expect(showErrorToastMock).not.toHaveBeenCalled();
+ expect(
+ await screen.findByText("Provider credential validation failed"),
+ ).toBeInTheDocument();
+ });
+
+ it("edit provider dialog routes tRPC failures locally without a global toast", async () => {
+ render(
+ <ProviderDialogController
+ organizationId="org-1"
+ connection={testConnection}
+ >
+ {({ openDialog }) => (
+ <Button onClick={openDialog}>Edit provider</Button>
+ )}
+ </ProviderDialogController>,
+ );
+
+ fireEvent.click(screen.getByRole("button", { name: "Edit provider" }));
+ fireEvent.change(screen.getByLabelText("Secret key"), {
+ target: { value: "sk-invalid" },
+ });
+ fireEvent.click(screen.getByRole("button", { name: "Test and save" }));
+
+ await waitFor(() => {
+ expect(reportTrpcErrorWithoutToastMock).toHaveBeenCalledWith(
+ expect.any(TRPCClientError),
+ "ai-gateway-providers",
+ );
+ });
+ expect(showErrorToastMock).not.toHaveBeenCalled();
+ expect(
+ await screen.findByText("Provider credential validation failed"),
+ ).toBeInTheDocument();
+ });
+
+ it("create gateway api key dialog routes tRPC failures locally and ignores blank metadata rows", async () => {
+ render(
+ <TooltipProvider>
+ <CreateGatewayApiKeyDialogController organizationId="org-1">
+ {({ openDialog }) => <Button onClick={openDialog}>Issue key</Button>}
+ </CreateGatewayApiKeyDialogController>
+ </TooltipProvider>,
+ );
+
+ fireEvent.click(screen.getByRole("button", { name: "Issue key" }));
+ fireEvent.click(screen.getByRole("button", { name: "Create key" }));
+
+ await waitFor(() => {
+ expect(reportTrpcErrorWithoutToastMock).toHaveBeenCalledWith(
+ expect.any(TRPCClientError),
+ "ai-gateway-api-keys",
+ );
+ });
+ expect(showErrorToastMock).not.toHaveBeenCalled();
+ expect(
+ await screen.findByText("Gateway key limit reached"),
+ ).toBeInTheDocument();
+ expect(createApiKeyMutateAsync).toHaveBeenCalledWith({
+ orgId: "org-1",
+ note: undefined,
+ metadata: {},
+ });
+ });
+});
diff --git a/web/src/features/ai-gateway/constants/gatewayConfig.ts b/web/src/features/ai-gateway/constants/gatewayConfig.ts
new file mode 100644
index 0000000000..c54fcdfea3
--- /dev/null
+++ b/web/src/features/ai-gateway/constants/gatewayConfig.ts
@@ -0,0 +1 @@
+export const DEFAULT_GATEWAY_INGESTION_PROJECT_NAME = "AI Gateway Ingestion";
diff --git a/web/src/features/ai-gateway/constants/providerLabels.ts b/web/src/features/ai-gateway/constants/providerLabels.ts
new file mode 100644
index 0000000000..deff47ab64
--- /dev/null
+++ b/web/src/features/ai-gateway/constants/providerLabels.ts
@@ -0,0 +1,6 @@
+import type { GatewayProvider } from "@/src/features/ai-gateway/types/gatewayProvider";
+
+export const providerLabels: Record<GatewayProvider, string> = {
+ OPENAI: "OpenAI",
+ ANTHROPIC: "Anthropic",
+};
diff --git a/web/src/features/ai-gateway/index.ts b/web/src/features/ai-gateway/index.ts
new file mode 100644
index 0000000000..edb046ca3c
--- /dev/null
+++ b/web/src/features/ai-gateway/index.ts
@@ -0,0 +1,4 @@
+export { GatewayApiKeysPage } from "@/src/features/ai-gateway/components/GatewayApiKeysPage/GatewayApiKeysPage";
+export { GatewayConfigurationPage } from "@/src/features/ai-gateway/components/GatewayConfigurationPage/GatewayConfigurationPage";
+export { GatewayModelsPage } from "@/src/features/ai-gateway/components/GatewayModelsPage/GatewayModelsPage";
+export { GatewayProvidersPage } from "@/src/features/ai-gateway/components/GatewayProvidersPage/GatewayProvidersPage";
diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.servertest.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.servertest.ts
new file mode 100644
index 0000000000..f8c9462c67
--- /dev/null
+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.servertest.ts
@@ -0,0 +1,57 @@
+import type { PrismaClient } from "@langfuse/shared/src/db";
+import { describe, expect, it, vi } from "vitest";
+
+import { GatewayApiKeyRepository } from "./gatewayApiKeyRepository";
+
+function gatewayApiKey(id: string) {
+ return {
+ metadata: {},
+ apiKey: {
+ id,
+ publicKey: `pk-${id}`,
+ displaySecretKey: "sk-...",
+ note: null,
+ createdAt: new Date(0),
+ expiresAt: null,
+ lastUsedAt: null,
+ createdByUserId: null,
+ },
+ };
+}
+
+describe("GatewayApiKeyRepository pagination", () => {
+ it("bounds API key pages with stable cursors", async () => {
+ const findMany = vi
+ .fn()
+ .mockResolvedValue([
+ gatewayApiKey("key-1"),
+ gatewayApiKey("key-2"),
+ gatewayApiKey("key-3"),
+ ]);
+ const repository = new GatewayApiKeyRepository({
+ gatewayApiKeyAssociation: { findMany },
+ } as unknown as PrismaClient);
+
+ await expect(
+ repository.listGatewayApiKeys({
+ organizationId: "org-1",
+ limit: 2,
+ }),
+ ).resolves.toMatchObject({
+ data: [{ apiKey: { id: "key-1" } }, { apiKey: { id: "key-2" } }],
+ nextCursor: "key-2",
+ });
+ expect(findMany).toHaveBeenCalledWith(
+ expect.objectContaining({
+ orderBy: [{ apiKey: { createdAt: "asc" } }, { apiKeyId: "asc" }],
+ take: 3,
+ where: {
+ apiKey: {
+ orgId: "org-1",
+ scope: "ORGANIZATION",
+ },
+ },
+ }),
+ );
+ });
+});
diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts
new file mode 100644
index 0000000000..8a6172a35c
--- /dev/null
+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts
@@ -0,0 +1,73 @@
+import type { PrismaClient } from "@langfuse/shared/src/db";
+
+export class GatewayApiKeyRepository {
+ constructor(private readonly prisma: PrismaClient) {}
+
+ async listGatewayApiKeys(params: {
+ organizationId: string;
+ cursor?: string;
+ limit: number;
+ }) {
+ const rows = await this.prisma.gatewayApiKeyAssociation.findMany({
+ where: {
+ 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,
+ },
+ },
+ },
+ orderBy: [{ apiKey: { createdAt: "asc" } }, { apiKeyId: "asc" }],
+ take: params.limit + 1,
+ ...(params.cursor
+ ? { cursor: { apiKeyId: params.cursor }, skip: 1 }
+ : undefined),
+ });
+ const hasMore = rows.length > params.limit;
+ const data = hasMore ? rows.slice(0, params.limit) : rows;
+ return {
+ data,
+ nextCursor: hasMore ? (data.at(-1)?.apiKey.id ?? null) : null,
+ };
+ }
+
+ getGatewayApiKey(params: { organizationId: 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,
+ },
+ },
+ },
+ });
+ }
+}
diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts
new file mode 100644
index 0000000000..0251aaf1de
--- /dev/null
+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts
@@ -0,0 +1,112 @@
+import { InternalServerError, LangfuseNotFoundError } from "@langfuse/shared";
+import type { PrismaClient } from "@langfuse/shared/src/db";
+import {
+ createAndAddApiKeysToDb,
+ deleteApiKeyFromDb,
+} from "@langfuse/shared/src/server/auth/apiKeys";
+import type { Cluster, Redis } from "ioredis";
+
+import { auditLog } from "@/src/features/audit-logs/server";
+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 { GatewayApiKeyRepository } from "./gatewayApiKeyRepository";
+
+export class GatewayApiKeyService {
+ private readonly repository: GatewayApiKeyRepository;
+
+ constructor(
+ private readonly prisma: PrismaClient,
+ private readonly redis?: Redis | Cluster | null,
+ ) {
+ this.repository = new GatewayApiKeyRepository(prisma);
+ }
+
+ list(params: { organizationId: string; cursor?: string; limit: number }) {
+ return this.repository.listGatewayApiKeys(params);
+ }
+
+ async create(params: {
+ organizationId: string;
+ note?: string;
+ metadata: GatewayMetadata;
+ session: OrgAuthedContext["session"];
+ }) {
+ const key = await this.prisma.$transaction(async (tx) => {
+ // TODO: Narrow this virtual key to the `gateway:invoke` permission once granular API-key scopes are available.
+ const key = await createAndAddApiKeysToDb({
+ prisma: tx,
+ entityId: params.organizationId,
+ scope: "ORGANIZATION",
+ note: params.note,
+ createdByUserId: params.session.user.id,
+ });
+ await tx.gatewayApiKeyAssociation.create({
+ data: {
+ apiKeyId: key.id,
+ metadata: params.metadata,
+ },
+ });
+ return key;
+ });
+ await auditLog(
+ {
+ session: params.session,
+ resourceType: "apiKey",
+ resourceId: key.id,
+ action: "create",
+ after: {
+ id: key.id,
+ publicKey: key.publicKey,
+ displaySecretKey: key.displaySecretKey,
+ note: key.note,
+ },
+ },
+ this.prisma,
+ );
+ return key;
+ }
+
+ async revoke(params: {
+ organizationId: string;
+ apiKeyId: string;
+ session: OrgAuthedContext["session"];
+ }): Promise<void> {
+ const association = await this.repository.getGatewayApiKey(params);
+ if (!association) {
+ throw new LangfuseNotFoundError("Gateway API key not found");
+ }
+ // Read the hash before the row is gone; it is the resolve cache key.
+ const revoked = await this.prisma.apiKey.findFirst({
+ where: {
+ id: params.apiKeyId,
+ orgId: params.organizationId,
+ scope: "ORGANIZATION",
+ },
+ select: { fastHashedSecretKey: true },
+ });
+ const deleted = await deleteApiKeyFromDb({
+ prisma: this.prisma,
+ id: params.apiKeyId,
+ entityId: params.organizationId,
+ scope: "ORGANIZATION",
+ redis: this.redis,
+ });
+ if (!deleted) {
+ throw new InternalServerError("Failed to revoke gateway API key");
+ }
+ if (revoked?.fastHashedSecretKey) {
+ await invalidateGatewayResolveCacheForApiKey(revoked.fastHashedSecretKey);
+ }
+ await auditLog(
+ {
+ session: params.session,
+ resourceType: "apiKey",
+ resourceId: params.apiKeyId,
+ action: "delete",
+ before: association,
+ },
+ this.prisma,
+ );
+ }
+}
diff --git a/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts b/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts
new file mode 100644
index 0000000000..9819044ce1
--- /dev/null
+++ b/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts
@@ -0,0 +1,120 @@
+import type { GatewayProvider, PrismaClient } from "@langfuse/shared/src/db";
+
+import { isGatewayEnabledForOrganization } from "@/src/features/ai-gateway/server/availability";
+import { GatewayControlPlaneError } from "@/src/features/ai-gateway/server/gatewayControlPlaneError";
+import {
+ type GatewayApiFormat,
+ type GatewayMetadata,
+ GatewayMetadataSchema,
+ gatewayProviders,
+ providerSupportsApiFormat,
+} from "@/src/features/ai-gateway/server/provider/registry";
+import {
+ type CachedResolveContext,
+ GATEWAY_RESOLVE_KEY_NON_EXISTENT,
+ GatewayResolveCache,
+} from "@/src/features/ai-gateway/server/resolve/gatewayResolveCache";
+import { GatewayResolveRepository } from "@/src/features/ai-gateway/server/resolve/gatewayResolveRepository";
+
+export type GatewayApiKeyAuthContext = CachedResolveContext;
+
+type GatewayApiKeyAuthenticatorConfig = {
+ resolveCache?: GatewayResolveCache;
+};
+
+export class GatewayApiKeyAuthenticator {
+ private readonly repository: GatewayResolveRepository;
+ private readonly cache: GatewayResolveCache;
+
+ constructor(
+ prisma: PrismaClient,
+ config: GatewayApiKeyAuthenticatorConfig = {},
+ ) {
+ this.repository = new GatewayResolveRepository(prisma);
+ this.cache = config.resolveCache ?? new GatewayResolveCache();
+ }
+
+ async authenticateRequest(params: {
+ fastHashedSecretKey: string;
+ apiFormat: GatewayApiFormat;
+ }): Promise<GatewayApiKeyAuthContext> {
+ const cached = await this.cache.get(params);
+ if (cached === GATEWAY_RESOLVE_KEY_NON_EXISTENT) {
+ throw new GatewayControlPlaneError("Invalid gateway key", 401);
+ }
+
+ const context = cached ?? (await this.loadAndCacheContext(params));
+
+ // Re-check on cache hits so removing an organization from the allowlist
+ // takes effect without waiting for the cache TTL.
+ if (!isGatewayEnabledForOrganization(context.organizationId)) {
+ throw new GatewayControlPlaneError(
+ "Gateway is not enabled for this organization",
+ 403,
+ );
+ }
+ return context;
+ }
+
+ private async loadAndCacheContext(params: {
+ fastHashedSecretKey: string;
+ apiFormat: GatewayApiFormat;
+ }): Promise<GatewayApiKeyAuthContext> {
+ const row = await this.repository.resolveContext({
+ fastHashedSecretKey: params.fastHashedSecretKey,
+ providers: supportedProviders(params.apiFormat),
+ });
+
+ const organizationId = row?.apiKey.orgId;
+ const organization = row?.apiKey.organization;
+ if (!row || !organizationId || !organization) {
+ await this.cache.set({
+ ...params,
+ context: GATEWAY_RESOLVE_KEY_NON_EXISTENT,
+ });
+ throw new GatewayControlPlaneError("Invalid gateway key", 401);
+ }
+
+ const gatewayConfig = organization.gatewayConfig;
+ const project = gatewayConfig?.defaultIngestionProject;
+ const projectIsUsable =
+ Boolean(project) &&
+ !project?.deletedAt &&
+ project?.orgId === organizationId;
+ const connection = organization.gatewayAiConnections[0];
+
+ const context: GatewayApiKeyAuthContext = {
+ organizationId,
+ apiKeyId: row.apiKeyId,
+ keyMetadata: toKeyMetadata(row.metadata),
+ ingestionProjectId: projectIsUsable
+ ? (gatewayConfig?.defaultIngestionProjectId ?? null)
+ : null,
+ ingestionMode: gatewayConfig?.ingestionMode ?? null,
+ connection: connection
+ ? {
+ id: connection.id,
+ provider: connection.provider,
+ encryptedCredential: connection.encryptedCredential,
+ }
+ : null,
+ };
+
+ await this.cache.set({ ...params, context });
+ return context;
+ }
+}
+
+function supportedProviders(apiFormat: GatewayApiFormat): GatewayProvider[] {
+ return gatewayProviders.filter((provider) =>
+ providerSupportsApiFormat(provider, apiFormat),
+ ) as GatewayProvider[];
+}
+
+// The column is JSON, so a key written before validation existed can hold a
+// scalar, an array, or nested objects. Only a flat object of scalars can be
+// attributed per event.
+function toKeyMetadata(value: unknown): GatewayMetadata {
+ const parsed = GatewayMetadataSchema.safeParse(value);
+ return parsed.success ? parsed.data : {};
+}
diff --git a/web/src/features/ai-gateway/server/auth/gatewayRequestSignatureVerifier.servertest.ts b/web/src/features/ai-gateway/server/auth/gatewayRequestSignatureVerifier.servertest.ts
new file mode 100644
index 0000000000..fb0b3a00dd
--- /dev/null
+++ b/web/src/features/ai-gateway/server/auth/gatewayRequestSignatureVerifier.servertest.ts
@@ -0,0 +1,213 @@
+import { createHash } from "node:crypto";
+import { Readable } from "node:stream";
+
+import type { NextApiRequest, NextApiResponse } from "next";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import { signHmacSha256 } from "@/src/server/utils/hmac";
+import { createShaHash } from "@langfuse/shared/src/server/auth/apiKeys";
+
+import {
+ withGatewayModelsSignatureVerification,
+ withGatewayResolveSignatureVerification,
+} from "./gatewayRequestSignatureVerifier";
+
+vi.mock("@/src/env.mjs", () => ({
+ env: {
+ LANGFUSE_AI_GATEWAY_SERVICE_KEY: "current-service-secret",
+ LANGFUSE_AI_GATEWAY_SERVICE_KEY_PREVIOUS: "previous-service-secret",
+ LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST: ["org-1"],
+ SALT: "test-salt",
+ },
+}));
+
+const now = new Date("2026-09-07T12:00:00.000Z");
+
+afterEach(() => {
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+});
+
+function response() {
+ const res = {
+ setHeader: vi.fn(),
+ status: vi.fn(),
+ json: vi.fn(),
+ };
+ res.status.mockReturnValue(res);
+ res.json.mockReturnValue(res);
+ return res as unknown as NextApiResponse;
+}
+
+function request(input: {
+ authorization?: string;
+ gatewayAuthorization?: string;
+ body?: string;
+ method?: string;
+ query?: NextApiRequest["query"];
+}) {
+ return Object.assign(Readable.from([input.body ?? ""]), {
+ method: input.method ?? "POST",
+ headers: {
+ authorization: input.authorization,
+ "langfuse-gateway-authorization": input.gatewayAuthorization,
+ },
+ body: undefined,
+ query: input.query ?? {},
+ }) as unknown as NextApiRequest;
+}
+
+function gatewayAuthorization(input: {
+ secret: string;
+ timestamp?: number;
+ key?: string;
+}) {
+ const timestamp = input.timestamp ?? Math.floor(now.getTime() / 1000);
+ const sha256 = (value: string) =>
+ createHash("sha256").update(value, "utf8").digest("hex");
+ const canonicalMessage = [
+ "gateway-web-v1",
+ timestamp.toString(),
+ sha256(input.key ?? "sk-gateway"),
+ ].join("\n");
+ return `HMAC timestamp=${timestamp},signature=${signHmacSha256(
+ canonicalMessage,
+ input.secret,
+ )}`;
+}
+
+describe("withGatewayResolveSignatureVerification", () => {
+ it.each([
+ undefined,
+ "Basic sk-gateway",
+ "Bearer",
+ "Bearer ",
+ "bearer sk-gateway",
+ "Bearer sk-gateway extra",
+ ])("rejects an invalid Bearer header: %s", async (authorization) => {
+ const handler = vi.fn();
+ const res = response();
+
+ await withGatewayResolveSignatureVerification(handler)(
+ request({
+ authorization,
+ body: '{"apiFormat":"openai.responses"}',
+ }),
+ res,
+ );
+
+ expect(res.status).toHaveBeenCalledWith(401);
+ expect(res.json).toHaveBeenCalledWith({ error: "Invalid Bearer token" });
+ expect(handler).not.toHaveBeenCalled();
+ });
+
+ it.each(["current-service-secret", "previous-service-secret"])(
+ "verifies the RFC payload with configured secret %s",
+ async (secret) => {
+ vi.useFakeTimers();
+ vi.setSystemTime(now);
+ const body = '{ "apiFormat": "openai.responses" }\n';
+ const req = request({
+ authorization: " Bearer sk-gateway ",
+ gatewayAuthorization: gatewayAuthorization({ secret }),
+ body,
+ });
+ const res = response();
+ const handler = vi.fn().mockResolvedValue(undefined);
+
+ await withGatewayResolveSignatureVerification(handler)(req, res);
+
+ expect(handler).toHaveBeenCalledWith({
+ req,
+ res,
+ fastHashedSecretKey: createShaHash("sk-gateway", "test-salt"),
+ apiFormat: "openai.responses",
+ });
+ },
+ );
+
+ it("rejects a missing Bearer token on the models endpoint", async () => {
+ const handler = vi.fn();
+ const res = response();
+
+ await withGatewayModelsSignatureVerification(handler)(
+ request({ method: "GET", query: { api_format: "anthropic.messages" } }),
+ res,
+ );
+
+ expect(res.status).toHaveBeenCalledWith(401);
+ expect(res.json).toHaveBeenCalledWith({ error: "Invalid Bearer token" });
+ expect(handler).not.toHaveBeenCalled();
+ });
+
+ it("accepts the same signature on the models endpoint", async () => {
+ vi.useFakeTimers();
+ vi.setSystemTime(now);
+ const handler = vi.fn().mockResolvedValue(undefined);
+ const req = request({
+ method: "GET",
+ authorization: "Bearer sk-gateway",
+ gatewayAuthorization: gatewayAuthorization({
+ secret: "current-service-secret",
+ }),
+ query: { api_format: "anthropic.messages" },
+ });
+ const res = response();
+
+ await withGatewayModelsSignatureVerification(handler)(req, res);
+
+ expect(handler).toHaveBeenCalledWith({
+ req,
+ res,
+ fastHashedSecretKey: createShaHash("sk-gateway", "test-salt"),
+ apiFormat: "anthropic.messages",
+ });
+ });
+
+ it("rejects a signature made for a different gateway key", async () => {
+ vi.useFakeTimers();
+ vi.setSystemTime(now);
+ const handler = vi.fn();
+ const res = response();
+
+ await withGatewayResolveSignatureVerification(handler)(
+ request({
+ authorization: "Bearer sk-gateway",
+ gatewayAuthorization: gatewayAuthorization({
+ secret: "current-service-secret",
+ key: "sk-other-gateway",
+ }),
+ body: '{"apiFormat":"openai.responses"}',
+ }),
+ res,
+ );
+
+ expect(res.status).toHaveBeenCalledWith(401);
+ expect(handler).not.toHaveBeenCalled();
+ });
+
+ it("rejects stale signatures and headers containing a key id", async () => {
+ vi.useFakeTimers();
+ vi.setSystemTime(now);
+ const body = '{"apiFormat":"openai.responses"}';
+ const staleHeader = gatewayAuthorization({
+ secret: "current-service-secret",
+ timestamp: Math.floor(now.getTime() / 1000) - 301,
+ });
+
+ for (const header of [staleHeader, `${staleHeader},keyid=current`]) {
+ const handler = vi.fn();
+ const res = response();
+ await withGatewayResolveSignatureVerification(handler)(
+ request({
+ authorization: "Bearer sk-gateway",
+ gatewayAuthorization: header,
+ body,
+ }),
+ res,
+ );
+ expect(res.status).toHaveBeenCalledWith(401);
+ expect(handler).not.toHaveBeenCalled();
+ }
+ });
+});
diff --git a/web/src/features/ai-gateway/server/auth/gatewayRequestSignatureVerifier.ts b/web/src/features/ai-gateway/server/auth/gatewayRequestSignatureVerifier.ts
new file mode 100644
index 0000000000..4ea8498121
--- /dev/null
+++ b/web/src/features/ai-gateway/server/auth/gatewayRequestSignatureVerifier.ts
@@ -0,0 +1,267 @@
+import { createHash } from "node:crypto";
+
+import type { NextApiRequest, NextApiResponse } from "next";
+import { z } from "zod/v4";
+
+import { env } from "@/src/env.mjs";
+import { verifyHmacSha256 } from "@/src/server/utils/hmac";
+import { createShaHash } from "@langfuse/shared/src/server/auth/apiKeys";
+
+import { GatewayApiFormatSchema, type GatewayApiFormat } from "../provider";
+import { GatewayControlPlaneError } from "@/src/features/ai-gateway/server/gatewayControlPlaneError";
+
+const RESOLVE_METHOD = "POST";
+const MODELS_METHOD = "GET";
+const SIGNATURE_DOMAIN = "gateway-web-v1";
+const SIGNATURE_MAX_AGE_SECONDS = 5 * 60;
+const resolveBodySchema = z
+ .object({
+ apiFormat: GatewayApiFormatSchema,
+ model: z.string().min(1).optional(),
+ })
+ .strict();
+const modelsQuerySchema = z
+ .object({ api_format: GatewayApiFormatSchema })
+ .strict();
+const gatewayAuthorizationSchema =
+ /^HMAC timestamp=(\d+),signature=([0-9a-f]{64})$/;
+
+type GatewayControlPlaneHandlerParams = {
+ req: NextApiRequest;
+ res: NextApiResponse;
+ fastHashedSecretKey: string;
+};
+
+type GatewayResolveHandler = (
+ params: GatewayControlPlaneHandlerParams & {
+ apiFormat: GatewayApiFormat;
+ },
+) => Promise<unknown>;
+
+type GatewayModelsHandler = (
+ params: GatewayControlPlaneHandlerParams & {
+ apiFormat: GatewayApiFormat;
+ },
+) => Promise<unknown>;
+
+function verifyGatewayRequestSignatureAndHashApiKey(input: {
+ virtualSecretKey: string;
+ gatewayAuthorization: string | undefined;
+}): string {
+ if (!env.LANGFUSE_AI_GATEWAY_SERVICE_KEY) {
+ throw new GatewayControlPlaneError(
+ "Gateway service is not configured",
+ 503,
+ );
+ }
+ const serviceKeys = [
+ env.LANGFUSE_AI_GATEWAY_SERVICE_KEY,
+ ...(env.LANGFUSE_AI_GATEWAY_SERVICE_KEY_PREVIOUS
+ ? [env.LANGFUSE_AI_GATEWAY_SERVICE_KEY_PREVIOUS]
+ : []),
+ ];
+
+ if (
+ !verifyGatewayRequestSignature(
+ {
+ credential: input.virtualSecretKey,
+ gatewayAuthorization: input.gatewayAuthorization,
+ },
+ serviceKeys,
+ )
+ ) {
+ throw new GatewayControlPlaneError("Invalid gateway authorization", 401);
+ }
+
+ return createShaHash(input.virtualSecretKey, env.SALT);
+}
+
+export function withGatewayResolveSignatureVerification(
+ handler: GatewayResolveHandler,
+) {
+ return withGatewayControlPlaneAuth(
+ ({ body, ...params }) => handler({ ...params, apiFormat: body.apiFormat }),
+ resolveBodySchema,
+ );
+}
+
+export function withGatewayModelsSignatureVerification(
+ handler: GatewayModelsHandler,
+) {
+ return async (req: NextApiRequest, res: NextApiResponse) => {
+ setNoStoreHeaders(res);
+
+ if (req.method !== MODELS_METHOD) {
+ res.setHeader("Allow", MODELS_METHOD);
+ return res.status(405).json({ error: "Method not allowed" });
+ }
+
+ const token = getBearerToken(req);
+ if (!token) {
+ return res.status(401).json({ error: "Invalid Bearer token" });
+ }
+
+ const query = modelsQuerySchema.safeParse(req.query);
+ if (!query.success) {
+ return res.status(400).json({ error: "Invalid request" });
+ }
+
+ try {
+ const fastHashedSecretKey = verifyGatewayRequestSignatureAndHashApiKey({
+ virtualSecretKey: token,
+ gatewayAuthorization: singleHeader(
+ req.headers["langfuse-gateway-authorization"],
+ ),
+ });
+
+ return await handler({
+ req,
+ res,
+ fastHashedSecretKey,
+ apiFormat: query.data.api_format,
+ });
+ } catch (error) {
+ if (error instanceof GatewayControlPlaneError) {
+ return res.status(error.status).json({ error: error.message });
+ }
+ return res.status(500).json({ error: "Internal server error" });
+ }
+ };
+}
+
+function withGatewayControlPlaneAuth<
+ Body extends { apiFormat: GatewayApiFormat },
+>(
+ handler: (
+ params: GatewayControlPlaneHandlerParams & { body: Body },
+ ) => Promise<unknown>,
+ schema: z.ZodType<Body>,
+) {
+ return async (req: NextApiRequest, res: NextApiResponse) => {
+ setNoStoreHeaders(res);
+
+ if (req.method !== RESOLVE_METHOD) {
+ res.setHeader("Allow", RESOLVE_METHOD);
+ return res.status(405).json({ error: "Method not allowed" });
+ }
+
+ const token = getBearerToken(req);
+ if (!token) {
+ return res.status(401).json({ error: "Invalid Bearer token" });
+ }
+
+ let requestBody: string;
+ try {
+ requestBody = await readRequestBody(req);
+ } catch {
+ return res.status(400).json({ error: "Invalid request" });
+ }
+
+ const body = schema.safeParse(parseJson(requestBody));
+ if (!body.success) {
+ return res.status(400).json({ error: "Invalid request" });
+ }
+
+ try {
+ const fastHashedSecretKey = verifyGatewayRequestSignatureAndHashApiKey({
+ virtualSecretKey: token,
+ gatewayAuthorization: singleHeader(
+ req.headers["langfuse-gateway-authorization"],
+ ),
+ });
+
+ return await handler({
+ req,
+ res,
+ fastHashedSecretKey,
+ body: body.data,
+ });
+ } catch (error) {
+ if (error instanceof GatewayControlPlaneError) {
+ return res.status(error.status).json({ error: error.message });
+ }
+ return res.status(500).json({ error: "Internal server error" });
+ }
+ };
+}
+
+function setNoStoreHeaders(res: NextApiResponse) {
+ res.setHeader("Cache-Control", "no-store");
+ res.setHeader("Pragma", "no-cache");
+}
+
+function getBearerToken(req: NextApiRequest): string | undefined {
+ const [scheme, token, ...additionalParts] = (req.headers.authorization ?? "")
+ .trim()
+ .split(/\s+/);
+ return scheme === "Bearer" && token && additionalParts.length === 0
+ ? token
+ : undefined;
+}
+
+export function verifyGatewayRequestSignature(
+ input: {
+ credential: string;
+ gatewayAuthorization: string | undefined;
+ },
+ serviceKeys: string[],
+): boolean {
+ const match = gatewayAuthorizationSchema.exec(
+ input.gatewayAuthorization ?? "",
+ );
+ if (!match) return false;
+
+ const [, timestampValue, signature] = match;
+ const timestamp = Number(timestampValue);
+ const now = Math.floor(Date.now() / 1000);
+ if (
+ !Number.isSafeInteger(timestamp) ||
+ Math.abs(now - timestamp) > SIGNATURE_MAX_AGE_SECONDS
+ ) {
+ return false;
+ }
+
+ return verifyHmacSha256({
+ message: [
+ SIGNATURE_DOMAIN,
+ timestamp.toString(),
+ sha256(input.credential),
+ ].join("\n"),
+ signature,
+ secrets: serviceKeys,
+ });
+}
+
+function parseJson(value: string): unknown {
+ try {
+ return JSON.parse(value);
+ } catch {
+ return undefined;
+ }
+}
+
+async function readRequestBody(req: NextApiRequest): Promise<string> {
+ if (typeof req.body === "string") return req.body;
+ if (Buffer.isBuffer(req.body)) return req.body.toString("utf8");
+ if (req.body !== undefined) throw new Error("Expected an unparsed body");
+
+ const chunks: Buffer[] = [];
+ let size = 0;
+ for await (const chunk of req) {
+ const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
+ size += buffer.length;
+ if (size > 1024) throw new Error("Gateway request body is too large");
+ chunks.push(buffer);
+ }
+ return Buffer.concat(chunks).toString("utf8");
+}
+
+function singleHeader(
+ value: string | string[] | undefined,
+): string | undefined {
+ return Array.isArray(value) ? undefined : value;
+}
+
+function sha256(value: string): string {
+ return createHash("sha256").update(value, "utf8").digest("hex");
+}
diff --git a/web/src/features/ai-gateway/server/auth/index.ts b/web/src/features/ai-gateway/server/auth/index.ts
new file mode 100644
index 0000000000..638c306046
--- /dev/null
+++ b/web/src/features/ai-gateway/server/auth/index.ts
@@ -0,0 +1,5 @@
+export {
+ withGatewayModelsSignatureVerification,
+ withGatewayResolveSignatureVerification,
+} from "./gatewayRequestSignatureVerifier";
+export { verifyGatewayIngestionAuthorization } from "./ingestionTokenVerifier";
diff --git a/web/src/features/ai-gateway/server/auth/ingestionTokenVerifier.servertest.ts b/web/src/features/ai-gateway/server/auth/ingestionTokenVerifier.servertest.ts
new file mode 100644
index 0000000000..7624aa023f
--- /dev/null
+++ b/web/src/features/ai-gateway/server/auth/ingestionTokenVerifier.servertest.ts
@@ -0,0 +1,302 @@
+import { createHash } from "node:crypto";
+
+import { describe, expect, it, vi } from "vitest";
+
+import { signHmacSha256 } from "@/src/server/utils/hmac";
+import { createEs256JwtSigner } from "@/src/server/utils/jwt";
+import { UnauthorizedError } from "@langfuse/shared";
+import type { PrismaClient } from "@langfuse/shared/src/db";
+
+import { verifyGatewayIngestionAuthorization } from "./ingestionTokenVerifier";
+
+const { privateKey, publicKey } = vi.hoisted(() => ({
+ privateKey: `-----BEGIN PRIVATE KEY-----
+MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgO9UaI+6vJ8r+i6e9
+Jq+IyijVtDDlhQm9po9oUWBL9aWhRANCAASKwbdc48K11rY2hLvCTdeKwqxyefeH
+jeV98Ug14YPox+zv3DAVejtbyJEsLK6lRzWuMiYUmR2zZD0Ow/bVHs7a
+-----END PRIVATE KEY-----
+`,
+ publicKey: `-----BEGIN PUBLIC KEY-----
+MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEisG3XOPCtda2NoS7wk3XisKscnn3
+h43lffFINeGD6Mfs79wwFXo7W8iRLCyupUc1rjImFJkds2Q9DsP21R7O2g==
+-----END PUBLIC KEY-----
+`,
+}));
+
+vi.mock("@/src/env.mjs", () => ({
+ env: {
+ LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY: publicKey,
+ LANGFUSE_AI_GATEWAY_JWT_KEY_ID: "current",
+ LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_KEY_ID: undefined,
+ LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_PUBLIC_KEY: undefined,
+ LANGFUSE_AI_GATEWAY_JWT_ISSUER: "test-issuer",
+ LANGFUSE_AI_GATEWAY_JWT_AUDIENCE: "test-audience",
+ LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST: ["org-1"],
+ LANGFUSE_AI_GATEWAY_SERVICE_KEY: "current-service-key",
+ LANGFUSE_AI_GATEWAY_SERVICE_KEY_PREVIOUS: "previous-service-key",
+ },
+}));
+
+const signer = createEs256JwtSigner({
+ privateKey,
+ keyId: "current",
+ issuer: "test-issuer",
+ audience: "test-audience",
+});
+
+function token(organizationId = "org-1") {
+ return signer.sign({
+ expiresInSeconds: 60,
+ claims: {
+ version: 1,
+ organization_id: organizationId,
+ project_id: "project-1",
+ ingestion_mode: "full",
+ scope: "gateway-ingest",
+ },
+ });
+}
+
+function gatewayAuthorization(
+ ingestionToken: string,
+ overrides: { timestamp?: number; secret?: string } = {},
+) {
+ const timestamp = overrides.timestamp ?? Math.floor(Date.now() / 1000);
+ const tokenHash = createHash("sha256")
+ .update(ingestionToken, "utf8")
+ .digest("hex");
+ const signature = signHmacSha256(
+ ["gateway-web-v1", timestamp.toString(), tokenHash].join("\n"),
+ overrides.secret ?? "current-service-key",
+ );
+ return `HMAC timestamp=${timestamp},signature=${signature}`;
+}
+
+function signedRequest(
+ organizationId = "org-1",
+ overrides: { timestamp?: number; secret?: string } = {},
+) {
+ const ingestionToken = token(organizationId);
+ return {
+ authorization: `Bearer ${ingestionToken}`,
+ gatewayAuthorization: gatewayAuthorization(ingestionToken, overrides),
+ };
+}
+
+function database(input: { project?: object | null } = {}) {
+ return {
+ project: {
+ findFirst: vi.fn().mockResolvedValue(
+ input.project === undefined
+ ? {
+ id: "project-1",
+ organization: {
+ id: "org-1",
+ cloudConfig: null,
+ cloudFreeTierUsageThresholdState: "OK",
+ },
+ }
+ : input.project,
+ ),
+ },
+ } as unknown as PrismaClient;
+}
+
+describe("verifyGatewayIngestionAuthorization without signing keys", () => {
+ it("rejects a public key without its stable key ID", async () => {
+ vi.resetModules();
+ vi.doMock("@/src/env.mjs", () => ({
+ env: {
+ LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY: publicKey,
+ LANGFUSE_AI_GATEWAY_JWT_KEY_ID: undefined,
+ LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_KEY_ID: undefined,
+ LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_PUBLIC_KEY: undefined,
+ LANGFUSE_AI_GATEWAY_JWT_ISSUER: "test-issuer",
+ LANGFUSE_AI_GATEWAY_JWT_AUDIENCE: "test-audience",
+ },
+ }));
+
+ await expect(import("./ingestionTokenVerifier")).rejects.toThrow(
+ "LANGFUSE_AI_GATEWAY_JWT_KEY_ID and LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY must be set together",
+ );
+
+ vi.doUnmock("@/src/env.mjs");
+ vi.resetModules();
+ });
+
+ it("stays inert so the regular API-key verifier still sees the header", async () => {
+ // Every deployment that never enabled the gateway is in this state. If the
+ // verifier rejected here, any three-part bearer token would 401 before the
+ // normal auth path ever ran.
+ vi.resetModules();
+ vi.doMock("@/src/env.mjs", () => ({
+ env: { LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST: [] },
+ }));
+ const { verifyGatewayIngestionAuthorization: verifyWithoutKeys } =
+ await import("./ingestionTokenVerifier");
+
+ await expect(
+ verifyWithoutKeys(
+ `Bearer ${token()}`,
+ undefined,
+ {} as unknown as PrismaClient,
+ ),
+ ).resolves.toBeNull();
+
+ vi.doUnmock("@/src/env.mjs");
+ vi.resetModules();
+ });
+});
+
+describe("verifyGatewayIngestionAuthorization", () => {
+ it("rejects tokens for organizations outside the allowlist", async () => {
+ const db = database();
+
+ const request = signedRequest("org-not-allowed");
+ await expect(
+ verifyGatewayIngestionAuthorization(
+ request.authorization,
+ request.gatewayAuthorization,
+ db,
+ ),
+ ).rejects.toThrow("AI Gateway is not enabled for this organization");
+ expect(db.project.findFirst).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ undefined,
+ "Basic token",
+ "Bearer",
+ "Bearer ",
+ "bearer token",
+ "Bearer token extra",
+ ])("ignores a non-gateway Bearer header: %s", async (authorization) => {
+ const db = database();
+
+ await expect(
+ verifyGatewayIngestionAuthorization(authorization, undefined, db),
+ ).resolves.toBeNull();
+ expect(db.project.findFirst).not.toHaveBeenCalled();
+ });
+
+ it("returns project authorization without API key identity", async () => {
+ const db = database();
+
+ const request = signedRequest();
+ const result = await verifyGatewayIngestionAuthorization(
+ ` ${request.authorization.replace("Bearer", "Bearer ")} `,
+ request.gatewayAuthorization,
+ db,
+ );
+ expect(result).toMatchObject({
+ validKey: true,
+ scope: {
+ projectId: "project-1",
+ orgId: "org-1",
+ publicKey: expect.stringMatching(/^gateway:/),
+ accessLevel: "project",
+ },
+ });
+ expect(db.project.findFirst).toHaveBeenCalledWith(
+ expect.objectContaining({
+ where: {
+ id: "project-1",
+ orgId: "org-1",
+ deletedAt: null,
+ },
+ }),
+ );
+ });
+
+ it("rejects a token for a missing project", async () => {
+ const request = signedRequest();
+ await expect(
+ verifyGatewayIngestionAuthorization(
+ request.authorization,
+ request.gatewayAuthorization,
+ database({ project: null }),
+ ),
+ ).rejects.toBeInstanceOf(UnauthorizedError);
+ });
+
+ it("accepts a signature from the previous service key", async () => {
+ const request = signedRequest("org-1", {
+ secret: "previous-service-key",
+ });
+
+ await expect(
+ verifyGatewayIngestionAuthorization(
+ request.authorization,
+ request.gatewayAuthorization,
+ database(),
+ ),
+ ).resolves.toMatchObject({ validKey: true });
+ });
+
+ it.each([
+ ["missing", undefined],
+ ["malformed", "HMAC timestamp=123,signature=invalid"],
+ ])("rejects %s gateway authorization", async (_label, authorization) => {
+ const ingestionToken = token();
+
+ await expect(
+ verifyGatewayIngestionAuthorization(
+ `Bearer ${ingestionToken}`,
+ authorization,
+ database(),
+ ),
+ ).rejects.toBeInstanceOf(UnauthorizedError);
+ });
+
+ it("rejects stale gateway authorization", async () => {
+ const request = signedRequest("org-1", {
+ timestamp: Math.floor(Date.now() / 1000) - 301,
+ });
+
+ await expect(
+ verifyGatewayIngestionAuthorization(
+ request.authorization,
+ request.gatewayAuthorization,
+ database(),
+ ),
+ ).rejects.toBeInstanceOf(UnauthorizedError);
+ });
+
+ it("rejects a gateway signature for another ingestion token", async () => {
+ const signed = signedRequest();
+
+ await expect(
+ verifyGatewayIngestionAuthorization(
+ `Bearer ${token()}`,
+ signed.gatewayAuthorization,
+ database(),
+ ),
+ ).rejects.toBeInstanceOf(UnauthorizedError);
+ });
+
+ it.each([
+ ["a malformed JWT", "header.payload.signature"],
+ [
+ "a JWT with API key identity",
+ signer.sign({
+ expiresInSeconds: 60,
+ claims: {
+ version: 1,
+ organization_id: "org-1",
+ project_id: "project-1",
+ api_key_id: "gateway-api-key-1",
+ ingestion_mode: "full",
+ scope: "gateway-ingest",
+ },
+ }),
+ ],
+ ])("rejects %s", async (_label, ingestionToken) => {
+ await expect(
+ verifyGatewayIngestionAuthorization(
+ `Bearer ${ingestionToken}`,
+ gatewayAuthorization(ingestionToken),
+ database(),
+ ),
+ ).rejects.toBeInstanceOf(UnauthorizedError);
+ });
+});
diff --git a/web/src/features/ai-gateway/server/auth/ingestionTokenVerifier.ts b/web/src/features/ai-gateway/server/auth/ingestionTokenVerifier.ts
new file mode 100644
index 0000000000..87078c54f5
--- /dev/null
+++ b/web/src/features/ai-gateway/server/auth/ingestionTokenVerifier.ts
@@ -0,0 +1,171 @@
+import { env } from "@/src/env.mjs";
+import { getOrganizationPlanServerSide } from "@/src/features/entitlements/server";
+import { CloudConfigSchema, UnauthorizedError } from "@langfuse/shared";
+import { prisma, type PrismaClient } from "@langfuse/shared/src/db";
+import type { ApiAccessScopeWithOptionalApiKeyId } from "@langfuse/shared/src/server";
+import { z } from "zod/v4";
+
+import {
+ createEs256JwtVerifier,
+ type JwtRegisteredClaims,
+} from "@/src/server/utils/jwt";
+import { requireGatewayEnabledForOrganization } from "../availability";
+import { verifyGatewayRequestSignature } from "./gatewayRequestSignatureVerifier";
+
+export const GATEWAY_INGESTION_TOKEN_TTL_SECONDS = 15 * 60;
+
+const GatewayIngestionClaimsSchema = z.strictObject({
+ version: z.literal(1),
+ organization_id: z.string(),
+ project_id: z.string(),
+ ingestion_mode: z.enum(["usage", "full"]),
+ scope: z.literal("gateway-ingest"),
+ exp: z.number().int(),
+ iss: z.string(),
+ aud: z.string(),
+ iat: z.number().int(),
+ jti: z.string(),
+});
+
+type GatewayIngestionClaims = z.infer<typeof GatewayIngestionClaimsSchema> &
+ JwtRegisteredClaims;
+
+type GatewayProjectAuth = {
+ validKey: true;
+ scope: ApiAccessScopeWithOptionalApiKeyId & {
+ projectId: string;
+ accessLevel: "project";
+ };
+};
+
+function configuredPublicKey(input: {
+ keyId: string | undefined;
+ publicKey: string | undefined;
+ keyIdVariable: string;
+ publicKeyVariable: string;
+}) {
+ if (!input.keyId && !input.publicKey) return [];
+ if (!input.keyId || !input.publicKey) {
+ throw new Error(
+ `${input.keyIdVariable} and ${input.publicKeyVariable} must be set together`,
+ );
+ }
+ return [{ id: input.keyId, publicKey: input.publicKey }];
+}
+
+const gatewayIngestionTokenVerifier = (() => {
+ const publicKeys = [
+ ...configuredPublicKey({
+ keyId: env.LANGFUSE_AI_GATEWAY_JWT_KEY_ID,
+ publicKey: env.LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY,
+ keyIdVariable: "LANGFUSE_AI_GATEWAY_JWT_KEY_ID",
+ publicKeyVariable: "LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY",
+ }),
+ ...configuredPublicKey({
+ keyId: env.LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_KEY_ID,
+ publicKey: env.LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_PUBLIC_KEY,
+ keyIdVariable: "LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_KEY_ID",
+ publicKeyVariable: "LANGFUSE_AI_GATEWAY_JWT_PREVIOUS_PUBLIC_KEY",
+ }),
+ ];
+
+ return publicKeys.length > 0
+ ? createEs256JwtVerifier({
+ issuer: env.LANGFUSE_AI_GATEWAY_JWT_ISSUER,
+ audience: env.LANGFUSE_AI_GATEWAY_JWT_AUDIENCE,
+ publicKeys,
+ claimsSchema: GatewayIngestionClaimsSchema,
+ })
+ : undefined;
+})();
+
+export async function verifyGatewayIngestionAuthorization(
+ authorization: string | undefined,
+ gatewayAuthorization: string | string[] | undefined,
+ database: PrismaClient = prisma,
+): Promise<GatewayProjectAuth | null> {
+ const [scheme, token, ...additionalParts] = (authorization ?? "")
+ .trim()
+ .split(/\s+/);
+ if (scheme !== "Bearer" || !token || additionalParts.length > 0) return null;
+ if (token.split(".").length !== 3) return null;
+
+ // On a deployment without gateway signing keys this path is inert: fall
+ // through so the regular API-key verifier still gets to see the header.
+ if (!gatewayIngestionTokenVerifier) return null;
+
+ const serviceKeys = [
+ ...(env.LANGFUSE_AI_GATEWAY_SERVICE_KEY
+ ? [env.LANGFUSE_AI_GATEWAY_SERVICE_KEY]
+ : []),
+ ...(env.LANGFUSE_AI_GATEWAY_SERVICE_KEY_PREVIOUS
+ ? [env.LANGFUSE_AI_GATEWAY_SERVICE_KEY_PREVIOUS]
+ : []),
+ ];
+ if (
+ Array.isArray(gatewayAuthorization) ||
+ !verifyGatewayRequestSignature(
+ {
+ credential: token,
+ gatewayAuthorization,
+ },
+ serviceKeys,
+ )
+ ) {
+ throw new UnauthorizedError("Invalid gateway ingestion authorization");
+ }
+
+ let claims: GatewayIngestionClaims;
+ try {
+ claims = gatewayIngestionTokenVerifier.verify({ token });
+ } catch {
+ throw new UnauthorizedError("Invalid gateway ingestion token");
+ }
+ requireGatewayEnabledForOrganization(claims.organization_id);
+
+ const project = await database.project.findFirst({
+ where: {
+ id: claims.project_id,
+ orgId: claims.organization_id,
+ deletedAt: null,
+ },
+ select: {
+ id: true,
+ organization: {
+ select: {
+ id: true,
+ cloudConfig: true,
+ cloudFreeTierUsageThresholdState: true,
+ },
+ },
+ },
+ });
+ if (!project) {
+ throw new UnauthorizedError("Invalid gateway ingestion token project");
+ }
+
+ const parsedCloudConfig = CloudConfigSchema.safeParse(
+ project.organization.cloudConfig ?? {},
+ );
+ if (!parsedCloudConfig.success) {
+ throw new UnauthorizedError("Invalid gateway ingestion token project");
+ }
+ const cloudConfig = project.organization.cloudConfig
+ ? parsedCloudConfig.data
+ : undefined;
+
+ return {
+ validKey: true,
+ scope: {
+ projectId: project.id,
+ accessLevel: "project",
+ orgId: project.organization.id,
+ plan: getOrganizationPlanServerSide(cloudConfig),
+ rateLimitOverrides: cloudConfig?.rateLimitOverrides ?? [],
+ publicKey: `gateway:${claims.jti}`,
+ isIngestionSuspended:
+ project.organization.cloudFreeTierUsageThresholdState === "BLOCKED",
+ isInAppAgentKey: false,
+ },
+ };
+}
diff --git a/web/src/features/ai-gateway/server/availability.servertest.ts b/web/src/features/ai-gateway/server/availability.servertest.ts
new file mode 100644
index 0000000000..8c1d73daa2
--- /dev/null
+++ b/web/src/features/ai-gateway/server/availability.servertest.ts
@@ -0,0 +1,33 @@
+import { ForbiddenError } from "@langfuse/shared";
+import { describe, expect, it } from "vitest";
+
+import {
+ isGatewayEnabledForOrganization,
+ requireGatewayEnabledForOrganization,
+} from "./availability";
+
+describe("gateway organization allowlist", () => {
+ it("matches organization IDs exactly", () => {
+ const environment = { nodeEnv: "production" };
+
+ expect(
+ isGatewayEnabledForOrganization("org-1", ["org-1", "org-2"], environment),
+ ).toBe(true);
+ expect(isGatewayEnabledForOrganization("org", ["org-1"], environment)).toBe(
+ false,
+ );
+ });
+
+ it("keeps the allowlist active outside local development", () => {
+ expect(
+ isGatewayEnabledForOrganization("org-not-allowed", [], {
+ nodeEnv: "production",
+ }),
+ ).toBe(false);
+ expect(() =>
+ requireGatewayEnabledForOrganization("org-not-allowed", [], {
+ nodeEnv: "production",
+ }),
+ ).toThrow(ForbiddenError);
+ });
+});
diff --git a/web/src/features/ai-gateway/server/availability.ts b/web/src/features/ai-gateway/server/availability.ts
new file mode 100644
index 0000000000..10ca3b1385
--- /dev/null
+++ b/web/src/features/ai-gateway/server/availability.ts
@@ -0,0 +1,38 @@
+import { ForbiddenError } from "@langfuse/shared";
+
+import { env } from "@/src/env.mjs";
+
+type GatewayAvailabilityEnvironment = {
+ nodeEnv: string | undefined;
+};
+
+const getGatewayAvailabilityEnvironment =
+ (): GatewayAvailabilityEnvironment => ({
+ nodeEnv: env.NODE_ENV,
+ });
+
+export const isGatewayEnabledForOrganization = (
+ organizationId: string,
+ allowedOrganizationIds = env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST ??
+ [],
+ environment = getGatewayAvailabilityEnvironment(),
+): boolean =>
+ environment.nodeEnv === "development" ||
+ allowedOrganizationIds.includes(organizationId);
+
+export const requireGatewayEnabledForOrganization = (
+ organizationId: string,
+ allowedOrganizationIds = env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST ??
+ [],
+ environment = getGatewayAvailabilityEnvironment(),
+): void => {
+ if (
+ !isGatewayEnabledForOrganization(
+ organizationId,
+ allowedOrganizationIds,
+ environment,
+ )
+ ) {
+ throw new ForbiddenError("AI Gateway is not enabled for this organization");
+ }
+};
diff --git a/web/src/features/ai-gateway/server/config/gatewayConfigRepository.ts b/web/src/features/ai-gateway/server/config/gatewayConfigRepository.ts
new file mode 100644
index 0000000000..8cae1e656f
--- /dev/null
+++ b/web/src/features/ai-gateway/server/config/gatewayConfigRepository.ts
@@ -0,0 +1,30 @@
+import type { PrismaClient } from "@langfuse/shared/src/db";
+
+export class GatewayConfigRepository {
+ constructor(private readonly prisma: PrismaClient) {}
+
+ getConfig(organizationId: string) {
+ return this.prisma.gatewayConfig.findUnique({
+ where: { organizationId },
+ include: {
+ defaultIngestionProject: {
+ select: { id: true, orgId: true, deletedAt: true },
+ },
+ },
+ });
+ }
+
+ getActiveOrganizationProject(params: {
+ organizationId: string;
+ projectId: string;
+ }) {
+ return this.prisma.project.findFirst({
+ where: {
+ id: params.projectId,
+ orgId: params.organizationId,
+ deletedAt: null,
+ },
+ select: { id: true },
+ });
+ }
+}
diff --git a/web/src/features/ai-gateway/server/config/gatewayConfigService.ts b/web/src/features/ai-gateway/server/config/gatewayConfigService.ts
new file mode 100644
index 0000000000..0d0ac123e5
--- /dev/null
+++ b/web/src/features/ai-gateway/server/config/gatewayConfigService.ts
@@ -0,0 +1,69 @@
+import { InvalidRequestError } from "@langfuse/shared";
+import type {
+ GatewayIngestionMode,
+ PrismaClient,
+} from "@langfuse/shared/src/db";
+import { auditLog } from "@/src/features/audit-logs/server";
+import type { OrgAuthedContext } from "@/src/server/api/trpc";
+import { invalidateGatewayResolveCacheForOrganization } from "@/src/features/ai-gateway/server/resolve/gatewayResolveCache";
+import { GatewayConfigRepository } from "./gatewayConfigRepository";
+
+export class GatewayConfigService {
+ private readonly repository: GatewayConfigRepository;
+
+ constructor(private readonly prisma: PrismaClient) {
+ this.repository = new GatewayConfigRepository(prisma);
+ }
+
+ getConfig(organizationId: string) {
+ return this.repository.getConfig(organizationId);
+ }
+
+ async updateConfig(params: {
+ organizationId: string;
+ defaultIngestionProjectId: string | null;
+ ingestionMode: GatewayIngestionMode;
+ session: OrgAuthedContext["session"];
+ }) {
+ const before = await this.getConfig(params.organizationId);
+
+ if (params.defaultIngestionProjectId) {
+ const project = await this.repository.getActiveOrganizationProject({
+ organizationId: params.organizationId,
+ projectId: params.defaultIngestionProjectId,
+ });
+ if (!project) {
+ throw new InvalidRequestError(
+ "Default ingestion project must be an active project in the organization",
+ );
+ }
+ }
+
+ const config = await this.prisma.gatewayConfig.upsert({
+ where: { organizationId: params.organizationId },
+ create: {
+ organizationId: params.organizationId,
+ defaultIngestionProjectId: params.defaultIngestionProjectId,
+ ingestionMode: params.ingestionMode,
+ },
+ update: {
+ defaultIngestionProjectId: params.defaultIngestionProjectId,
+ ingestionMode: params.ingestionMode,
+ },
+ });
+
+ await invalidateGatewayResolveCacheForOrganization(params.organizationId);
+ await auditLog(
+ {
+ session: params.session,
+ resourceType: "gatewayConfig",
+ resourceId: params.organizationId,
+ action: before ? "update" : "create",
+ before,
+ after: config,
+ },
+ this.prisma,
+ );
+ return config;
+ }
+}
diff --git a/web/src/features/ai-gateway/server/gatewayControlPlaneError.ts b/web/src/features/ai-gateway/server/gatewayControlPlaneError.ts
new file mode 100644
index 0000000000..f488f39374
--- /dev/null
+++ b/web/src/features/ai-gateway/server/gatewayControlPlaneError.ts
@@ -0,0 +1,8 @@
+export class GatewayControlPlaneError extends Error {
+ constructor(
+ message: string,
+ readonly status: 400 | 401 | 403 | 404 | 503,
+ ) {
+ super(message);
+ }
+}
diff --git a/web/src/features/ai-gateway/server/index.ts b/web/src/features/ai-gateway/server/index.ts
new file mode 100644
index 0000000000..8c15fe7119
--- /dev/null
+++ b/web/src/features/ai-gateway/server/index.ts
@@ -0,0 +1,8 @@
+export {
+ verifyGatewayIngestionAuthorization,
+ withGatewayModelsSignatureVerification,
+ withGatewayResolveSignatureVerification,
+} from "./auth";
+export { gatewayModelsApiHandler } from "./provider/models/gatewayModelsApiHandler";
+export { gatewayResolveApiHandler } from "./resolve/gatewayResolveApiHandler";
+export { aiGatewayRouter } from "./router";
diff --git a/web/src/features/ai-gateway/server/provider/connection/gatewayProviderRepository.servertest.ts b/web/src/features/ai-gateway/server/provider/connection/gatewayProviderRepository.servertest.ts
new file mode 100644
index 0000000000..e711c466bf
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/connection/gatewayProviderRepository.servertest.ts
@@ -0,0 +1,69 @@
+import { describe, expect, it, vi } from "vitest";
+import type { PrismaClient } from "@langfuse/shared/src/db";
+
+import { GatewayModelCatalogService } from "../models/gatewayModelCatalogService";
+import { GatewayProviderRepository } from "./gatewayProviderRepository";
+
+function connection(id: string, routingPriority: number) {
+ return {
+ id,
+ organizationId: "org-1",
+ name: id,
+ provider: "OPENAI" as const,
+ displaySecret: "sk-...",
+ createdById: null,
+ routingPriority,
+ status: "ENABLED" as const,
+ createdAt: new Date(0),
+ updatedAt: new Date(0),
+ };
+}
+
+describe("GatewayProviderRepository pagination", () => {
+ it("bounds provider pages with stable cursors", async () => {
+ const connectionFindMany = vi
+ .fn()
+ .mockResolvedValue([
+ connection("connection-1", 0),
+ connection("connection-2", 1),
+ connection("connection-3", 2),
+ ]);
+ const repository = new GatewayProviderRepository({
+ gatewayAiConnection: { findMany: connectionFindMany },
+ } as unknown as PrismaClient);
+
+ await expect(
+ repository.listConnections({
+ organizationId: "org-1",
+ limit: 2,
+ }),
+ ).resolves.toMatchObject({
+ data: [{ id: "connection-1" }, { id: "connection-2" }],
+ nextCursor: "connection-2",
+ });
+ expect(connectionFindMany).toHaveBeenCalledWith(
+ expect.objectContaining({
+ orderBy: [{ routingPriority: "asc" }, { id: "asc" }],
+ take: 3,
+ }),
+ );
+ });
+
+ it("fetches only enabled connections for model refresh", async () => {
+ const findMany = vi.fn().mockResolvedValue([]);
+ const service = new GatewayModelCatalogService({
+ gatewayAiConnection: { findMany },
+ } as unknown as PrismaClient);
+
+ await expect(service.refreshAllModels("org-1")).resolves.toEqual([]);
+ expect(findMany).toHaveBeenCalledWith(
+ expect.objectContaining({
+ take: 101,
+ where: {
+ organizationId: "org-1",
+ status: "ENABLED",
+ },
+ }),
+ );
+ });
+});
diff --git a/web/src/features/ai-gateway/server/provider/connection/gatewayProviderRepository.ts b/web/src/features/ai-gateway/server/provider/connection/gatewayProviderRepository.ts
new file mode 100644
index 0000000000..436b32ca48
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/connection/gatewayProviderRepository.ts
@@ -0,0 +1,202 @@
+import type {
+ GatewayConnectionStatus,
+ GatewayProvider,
+ Prisma,
+ PrismaClient,
+} from "@langfuse/shared/src/db";
+
+type DatabaseClient = PrismaClient | Prisma.TransactionClient;
+
+const safeConnectionSelect = {
+ id: true,
+ organizationId: true,
+ name: true,
+ provider: true,
+ displaySecret: true,
+ createdById: true,
+ routingPriority: true,
+ status: true,
+ createdAt: true,
+ updatedAt: true,
+} satisfies Prisma.GatewayAiConnectionSelect;
+
+export class GatewayProviderRepository {
+ constructor(private readonly prisma: PrismaClient) {}
+
+ async listConnections(params: {
+ organizationId: string;
+ cursor?: string;
+ limit: number;
+ status?: GatewayConnectionStatus;
+ }) {
+ const rows = await this.prisma.gatewayAiConnection.findMany({
+ where: {
+ organizationId: params.organizationId,
+ status: params.status,
+ },
+ select: safeConnectionSelect,
+ orderBy: [{ routingPriority: "asc" }, { id: "asc" }],
+ take: params.limit + 1,
+ ...(params.cursor
+ ? { cursor: { id: params.cursor }, skip: 1 }
+ : undefined),
+ });
+ const hasMore = rows.length > params.limit;
+ const data = hasMore ? rows.slice(0, params.limit) : rows;
+ return {
+ data,
+ nextCursor: hasMore ? (data.at(-1)?.id ?? null) : null,
+ };
+ }
+
+ getSafeConnection(params: { organizationId: string; id: string }) {
+ return this.prisma.gatewayAiConnection.findFirst({
+ where: params,
+ select: safeConnectionSelect,
+ });
+ }
+
+ getConnectionWithCredential(params: { organizationId: string; id: string }) {
+ return this.prisma.gatewayAiConnection.findFirst({
+ where: params,
+ select: {
+ ...safeConnectionSelect,
+ encryptedCredential: true,
+ },
+ });
+ }
+
+ createConnection(params: {
+ organizationId: string;
+ name: string;
+ provider: GatewayProvider;
+ encryptedCredential: string;
+ displaySecret: string;
+ createdById: string | null;
+ status: GatewayConnectionStatus;
+ }) {
+ return this.prisma.$transaction(async (tx) => {
+ await tx.$executeRaw`SELECT pg_advisory_xact_lock(hashtext(${params.organizationId}))`;
+ const aggregate = await tx.gatewayAiConnection.aggregate({
+ where: { organizationId: params.organizationId },
+ _max: { routingPriority: true },
+ });
+ return tx.gatewayAiConnection.create({
+ data: {
+ ...params,
+ routingPriority: (aggregate._max.routingPriority ?? -1) + 1,
+ },
+ select: safeConnectionSelect,
+ });
+ });
+ }
+
+ updateConnection(params: {
+ organizationId: string;
+ id: string;
+ name?: string;
+ encryptedCredential?: string;
+ displaySecret?: string;
+ status?: GatewayConnectionStatus;
+ }) {
+ const { organizationId, id, ...data } = params;
+ return this.prisma.gatewayAiConnection.update({
+ where: { id, organizationId },
+ data,
+ select: safeConnectionSelect,
+ });
+ }
+
+ /**
+ * Deletes a connection and closes the gap it leaves in the routing order in
+ * one transaction. Splitting the two lets a concurrent delete compact
+ * against rows that no longer exist, which abandons the gap.
+ */
+ deleteConnection(params: { organizationId: string; id: string }) {
+ return this.prisma.$transaction(async (tx) => {
+ await this.lockOrganization(tx, params.organizationId);
+ const deleted = await tx.gatewayAiConnection.delete({
+ where: { id: params.id, organizationId: params.organizationId },
+ select: safeConnectionSelect,
+ });
+ const remaining = await tx.gatewayAiConnection.findMany({
+ where: { organizationId: params.organizationId },
+ select: { id: true },
+ orderBy: [{ routingPriority: "asc" }, { id: "asc" }],
+ });
+ await this.writePriorities(tx, {
+ organizationId: params.organizationId,
+ connectionIds: remaining.map((connection) => connection.id),
+ });
+ return deleted;
+ });
+ }
+
+ async reorderConnections(params: {
+ organizationId: string;
+ connectionIds: string[];
+ }): Promise<void> {
+ await this.prisma.$transaction(async (tx) => {
+ // Rows are locked in caller-supplied order, so two concurrent reorders
+ // of one organization deadlock unless they serialize on the same key
+ // createConnection uses.
+ await this.lockOrganization(tx, params.organizationId);
+ await this.writePriorities(tx, params);
+ });
+ }
+
+ private lockOrganization(tx: DatabaseClient, organizationId: string) {
+ return tx.$executeRaw`SELECT pg_advisory_xact_lock(hashtext(${organizationId}))`;
+ }
+
+ /**
+ * Assigns priorities 0..n in list order. Written in two passes through
+ * negative values because (organization_id, routing_priority) uniqueness is
+ * enforced by an index, which Postgres cannot defer to commit time.
+ */
+ private async writePriorities(
+ tx: DatabaseClient,
+ params: { organizationId: string; connectionIds: string[] },
+ ) {
+ for (const [index, id] of params.connectionIds.entries()) {
+ await this.updatePriority(tx, {
+ organizationId: params.organizationId,
+ id,
+ routingPriority: -(index + 1),
+ });
+ }
+ for (const [index, id] of params.connectionIds.entries()) {
+ await this.updatePriority(tx, {
+ organizationId: params.organizationId,
+ id,
+ routingPriority: index,
+ });
+ }
+ }
+
+ updateConnectionStatus(params: {
+ organizationId: string;
+ id: string;
+ status: GatewayConnectionStatus;
+ }) {
+ return this.prisma.gatewayAiConnection.update({
+ where: { id: params.id, organizationId: params.organizationId },
+ data: { status: params.status },
+ select: safeConnectionSelect,
+ });
+ }
+
+ private updatePriority(
+ tx: DatabaseClient,
+ params: {
+ organizationId: string;
+ id: string;
+ routingPriority: number;
+ },
+ ) {
+ return tx.gatewayAiConnection.update({
+ where: { id: params.id, organizationId: params.organizationId },
+ data: { routingPriority: params.routingPriority },
+ });
+ }
+}
diff --git a/web/src/features/ai-gateway/server/provider/connection/gatewayProviderService.ts b/web/src/features/ai-gateway/server/provider/connection/gatewayProviderService.ts
new file mode 100644
index 0000000000..a84e4e8e0c
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/connection/gatewayProviderService.ts
@@ -0,0 +1,259 @@
+import type {
+ GatewayConnectionStatus,
+ GatewayProvider,
+ PrismaClient,
+} from "@langfuse/shared/src/db";
+import { InvalidRequestError, LangfuseNotFoundError } from "@langfuse/shared";
+import { encrypt } from "@langfuse/shared/encryption";
+import { LLMAdapter, testModelCall } from "@langfuse/shared/src/server";
+import { getDisplaySecretKey } from "@langfuse/shared/src/server/auth/apiKeys";
+
+import { auditLog } from "@/src/features/audit-logs/server";
+import type { OrgAuthedContext } from "@/src/server/api/trpc";
+import {
+ type GatewayProviderName,
+ getGatewayProviderDefinition,
+} from "@/src/features/ai-gateway/server/provider/registry";
+import { invalidateGatewayResolveCacheForOrganization } from "@/src/features/ai-gateway/server/resolve/gatewayResolveCache";
+import { GatewayProviderRepository } from "./gatewayProviderRepository";
+import { GatewayModelCatalogService } from "../models/gatewayModelCatalogService";
+
+type CredentialValidator = (params: {
+ provider: GatewayProviderName;
+ credential: string;
+}) => Promise<void>;
+
+export class GatewayProviderService {
+ private readonly repository: GatewayProviderRepository;
+ private readonly modelCatalogService: GatewayModelCatalogService;
+
+ constructor(
+ private readonly prisma: PrismaClient,
+ private readonly validateCredential: CredentialValidator = validateGatewayCredential,
+ modelCatalogService?: GatewayModelCatalogService,
+ ) {
+ this.repository = new GatewayProviderRepository(prisma);
+ this.modelCatalogService =
+ modelCatalogService ?? new GatewayModelCatalogService(prisma);
+ }
+
+ list(params: { organizationId: string; cursor?: string; limit: number }) {
+ return this.repository.listConnections(params);
+ }
+
+ async listAll(organizationId: string, status?: GatewayConnectionStatus) {
+ const connections = [];
+ let cursor: string | undefined;
+ do {
+ const page = await this.repository.listConnections({
+ organizationId,
+ cursor,
+ limit: 100,
+ status,
+ });
+ connections.push(...page.data);
+ cursor = page.nextCursor ?? undefined;
+ } while (cursor);
+ return connections;
+ }
+
+ testCredential(params: { provider: GatewayProvider; credential: string }) {
+ return this.validateCredential(params);
+ }
+
+ async create(params: {
+ organizationId: string;
+ name: string;
+ provider: GatewayProvider;
+ credential: string;
+ session: OrgAuthedContext["session"];
+ }) {
+ await this.testCredential({
+ provider: params.provider,
+ credential: params.credential,
+ });
+ const connection = await this.repository.createConnection({
+ organizationId: params.organizationId,
+ name: params.name,
+ provider: params.provider,
+ encryptedCredential: encrypt(params.credential),
+ displaySecret: getDisplaySecretKey(params.credential),
+ createdById: params.session.user.id,
+ status: "ENABLED",
+ });
+ await invalidateGatewayResolveCacheForOrganization(params.organizationId);
+ await auditLog(
+ {
+ session: params.session,
+ resourceType: "gatewayAiConnection",
+ resourceId: connection.id,
+ action: "create",
+ after: connection,
+ },
+ this.prisma,
+ );
+ return connection;
+ }
+
+ async update(params: {
+ organizationId: string;
+ id: string;
+ name?: string;
+ credential?: string;
+ status?: GatewayConnectionStatus;
+ session: OrgAuthedContext["session"];
+ }) {
+ const existing = await this.repository.getSafeConnection({
+ organizationId: params.organizationId,
+ id: params.id,
+ });
+ if (!existing) throw new LangfuseNotFoundError("Gateway connection");
+ if (
+ existing.status === "ERROR" &&
+ params.status === "ENABLED" &&
+ !params.credential
+ ) {
+ throw new InvalidRequestError(
+ "Errored gateway connections require a credential update or successful retry",
+ );
+ }
+ if (params.credential) {
+ await this.testCredential({
+ provider: existing.provider,
+ credential: params.credential,
+ });
+ }
+
+ const updated = await this.repository.updateConnection({
+ organizationId: params.organizationId,
+ id: params.id,
+ name: params.name,
+ status: params.credential ? "ENABLED" : params.status,
+ encryptedCredential: params.credential
+ ? encrypt(params.credential)
+ : undefined,
+ displaySecret: params.credential
+ ? getDisplaySecretKey(params.credential)
+ : undefined,
+ });
+ await this.modelCatalogService.clearModelCache(
+ params.organizationId,
+ params.id,
+ );
+ await invalidateGatewayResolveCacheForOrganization(params.organizationId);
+ await auditLog(
+ {
+ session: params.session,
+ resourceType: "gatewayAiConnection",
+ resourceId: params.id,
+ action: "update",
+ before: existing,
+ after: updated,
+ },
+ this.prisma,
+ );
+ return updated;
+ }
+
+ async delete(params: {
+ organizationId: string;
+ id: string;
+ session: OrgAuthedContext["session"];
+ }) {
+ // Only the two identifiers, never the whole params object: the extra
+ // `session` field would land in the Prisma `where` and fail validation.
+ const deleted = await this.repository.deleteConnection({
+ organizationId: params.organizationId,
+ id: params.id,
+ });
+ await this.modelCatalogService.clearModelCache(
+ params.organizationId,
+ params.id,
+ );
+ await invalidateGatewayResolveCacheForOrganization(params.organizationId);
+ await auditLog(
+ {
+ session: params.session,
+ resourceType: "gatewayAiConnection",
+ resourceId: params.id,
+ action: "delete",
+ before: deleted,
+ },
+ this.prisma,
+ );
+ return deleted;
+ }
+
+ async reorder(params: {
+ organizationId: string;
+ connectionIds: string[];
+ session: OrgAuthedContext["session"];
+ }) {
+ const existing = await this.listAll(params.organizationId);
+ const existingIds = new Set(existing.map((connection) => connection.id));
+ const requestedIds = new Set(params.connectionIds);
+ if (
+ requestedIds.size !== params.connectionIds.length ||
+ requestedIds.size !== existingIds.size ||
+ params.connectionIds.some((id) => !existingIds.has(id))
+ ) {
+ throw new InvalidRequestError(
+ "Reorder must contain every organization gateway connection exactly once",
+ );
+ }
+ await this.repository.reorderConnections({
+ organizationId: params.organizationId,
+ connectionIds: params.connectionIds,
+ });
+ const reordered = await this.listAll(params.organizationId);
+ await invalidateGatewayResolveCacheForOrganization(params.organizationId);
+ await auditLog(
+ {
+ session: params.session,
+ resourceType: "gatewayAiConnection",
+ resourceId: params.organizationId,
+ action: "reorder",
+ before: existing,
+ after: reordered,
+ },
+ this.prisma,
+ );
+ return reordered;
+ }
+}
+
+async function validateGatewayCredential(params: {
+ provider: GatewayProviderName;
+ credential: string;
+}): Promise<void> {
+ const definition = getGatewayProviderDefinition(params.provider);
+ const adapter =
+ params.provider === "ANTHROPIC" ? LLMAdapter.Anthropic : LLMAdapter.OpenAI;
+ const model = definition.validationModel;
+
+ try {
+ await testModelCall({
+ provider: params.provider.toLowerCase(),
+ model,
+ apiKey: {
+ id: "gateway-credential-validation",
+ projectId: "gateway-credential-validation",
+ createdAt: new Date(0),
+ updatedAt: new Date(0),
+ adapter,
+ provider: params.provider.toLowerCase(),
+ displaySecretKey: "",
+ secretKey: encrypt(params.credential),
+ extraHeaders: null,
+ extraHeaderKeys: [],
+ baseURL: definition.baseUrl,
+ customModels: [],
+ withDefaultModels: true,
+ config: null,
+ },
+ timeout: 10_000,
+ });
+ } catch {
+ throw new InvalidRequestError("Provider credential validation failed");
+ }
+}
diff --git a/web/src/features/ai-gateway/server/provider/index.ts b/web/src/features/ai-gateway/server/provider/index.ts
new file mode 100644
index 0000000000..f3c53b8921
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/index.ts
@@ -0,0 +1,13 @@
+export { GatewayProviderService } from "./connection/gatewayProviderService";
+export { GatewayModelCatalogService } from "./models/gatewayModelCatalogService";
+export {
+ GatewayApiFormatSchema,
+ GatewayMetadataSchema,
+ GatewayModelsResponseSchema,
+ GatewayResolveResponseSchema,
+ getGatewayProviderDefinition,
+ providerSupportsApiFormat,
+ type GatewayApiFormat,
+ type GatewayMetadata,
+ type GatewayProviderId,
+} from "./registry";
diff --git a/web/src/features/ai-gateway/server/provider/models/adapters/anthropic.ts b/web/src/features/ai-gateway/server/provider/models/adapters/anthropic.ts
new file mode 100644
index 0000000000..67c6b0af66
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/models/adapters/anthropic.ts
@@ -0,0 +1,67 @@
+import { z } from "zod/v4";
+
+import { getGatewayProviderDefinition } from "@/src/features/ai-gateway/server/provider/registry";
+import { requestJson, standardHttpError } from "./shared";
+import type { GatewayModelCatalogEntry, ModelDiscoveryAdapter } from "../types";
+
+// API contract: https://platform.claude.com/docs/en/api/models/list
+const responseSchema = z.object({
+ data: z.array(
+ z.object({
+ id: z.string(),
+ display_name: z.string(),
+ created_at: z.iso.datetime(),
+ }),
+ ),
+ has_more: z.boolean(),
+ last_id: z.string().nullable().optional(),
+});
+
+export const anthropicModelDiscoveryAdapter: ModelDiscoveryAdapter = {
+ async fetchCatalog({ credential, fetcher }) {
+ const definition = getGatewayProviderDefinition("ANTHROPIC");
+ const models: GatewayModelCatalogEntry[] = [];
+ let cursor: string | undefined;
+
+ for (let page = 0; page < 100; page += 1) {
+ const url = new URL(`${definition.baseUrl}${definition.modelsPath}`);
+ url.searchParams.set("limit", "1000");
+ if (cursor) url.searchParams.set("after_id", cursor);
+
+ const result = await requestJson({
+ fetcher,
+ url,
+ headers: {
+ "x-api-key": credential,
+ "anthropic-version": "2023-06-01",
+ },
+ });
+ if (!result.success) return result;
+
+ const error = standardHttpError(result.response);
+ if (error) return { success: false, error };
+
+ const parsed = responseSchema.safeParse(result.value);
+ if (!parsed.success) return { success: false, error: "provider_error" };
+ models.push(
+ ...parsed.data.data.map((model) => ({
+ id: model.id,
+ provider: "ANTHROPIC" as const,
+ canonicalSlug: model.id,
+ displayName: model.display_name,
+ createdAt: model.created_at,
+ })),
+ );
+
+ if (!parsed.data.has_more) {
+ return { success: true, models };
+ }
+ if (!parsed.data.last_id) {
+ return { success: false, error: "provider_error" };
+ }
+ cursor = parsed.data.last_id;
+ }
+
+ return { success: false, error: "provider_error" };
+ },
+};
diff --git a/web/src/features/ai-gateway/server/provider/models/adapters/openAi.ts b/web/src/features/ai-gateway/server/provider/models/adapters/openAi.ts
new file mode 100644
index 0000000000..146778535a
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/models/adapters/openAi.ts
@@ -0,0 +1,43 @@
+import { z } from "zod/v4";
+
+import { getGatewayProviderDefinition } from "@/src/features/ai-gateway/server/provider/registry";
+import { requestJson, standardHttpError } from "./shared";
+import type { ModelDiscoveryAdapter } from "../types";
+
+// API contract: https://developers.openai.com/api/reference/resources/models/methods/list
+const responseSchema = z.object({
+ data: z.array(
+ z.object({
+ id: z.string(),
+ created: z.number().int().nonnegative().max(253_402_300_799),
+ }),
+ ),
+});
+
+export const openAiModelDiscoveryAdapter: ModelDiscoveryAdapter = {
+ async fetchCatalog({ credential, fetcher }) {
+ const definition = getGatewayProviderDefinition("OPENAI");
+ const result = await requestJson({
+ fetcher,
+ url: new URL(`${definition.baseUrl}${definition.modelsPath}`),
+ headers: { Authorization: `Bearer ${credential}` },
+ });
+ if (!result.success) return result;
+
+ const error = standardHttpError(result.response);
+ if (error) return { success: false, error };
+
+ const parsed = responseSchema.safeParse(result.value);
+ if (!parsed.success) return { success: false, error: "provider_error" };
+ return {
+ success: true,
+ models: parsed.data.data.map((model) => ({
+ id: model.id,
+ provider: "OPENAI" as const,
+ canonicalSlug: model.id,
+ displayName: model.id,
+ createdAt: new Date(model.created * 1000).toISOString(),
+ })),
+ };
+ },
+};
diff --git a/web/src/features/ai-gateway/server/provider/models/adapters/shared.ts b/web/src/features/ai-gateway/server/provider/models/adapters/shared.ts
new file mode 100644
index 0000000000..7abac1e8f1
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/models/adapters/shared.ts
@@ -0,0 +1,37 @@
+import type { ModelDiscoveryError } from "../types";
+
+export type JsonRequestResult =
+ | { success: true; response: Response; value: unknown }
+ | { success: false; error: "timeout" };
+
+export async function requestJson(params: {
+ fetcher: typeof fetch;
+ url: URL;
+ headers: Record<string, string>;
+}): Promise<JsonRequestResult> {
+ let response: Response;
+ try {
+ response = await params.fetcher(params.url, {
+ method: "GET",
+ headers: params.headers,
+ signal: AbortSignal.timeout(10_000),
+ redirect: "error",
+ });
+ } catch {
+ return { success: false, error: "timeout" };
+ }
+
+ return {
+ success: true,
+ response,
+ value: await response.json().catch(() => null),
+ };
+}
+
+export function standardHttpError(
+ response: Response,
+): ModelDiscoveryError | null {
+ if (response.status === 401 || response.status === 403) return "unauthorized";
+ if (response.status === 429) return "rate_limited";
+ return response.ok ? null : "provider_error";
+}
diff --git a/web/src/features/ai-gateway/server/provider/models/gatewayModelCatalogService.ts b/web/src/features/ai-gateway/server/provider/models/gatewayModelCatalogService.ts
new file mode 100644
index 0000000000..dfd37f5373
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/models/gatewayModelCatalogService.ts
@@ -0,0 +1,303 @@
+import type {
+ GatewayConnectionStatus,
+ PrismaClient,
+} from "@langfuse/shared/src/db";
+import { LangfuseNotFoundError } from "@langfuse/shared";
+import { decrypt } from "@langfuse/shared/encryption";
+import { redis as defaultRedis } from "@langfuse/shared/src/server";
+import type { Cluster, Redis } from "ioredis";
+
+import { auditLog } from "@/src/features/audit-logs/server";
+import { invalidateGatewayResolveCacheForOrganization } from "@/src/features/ai-gateway/server/resolve/gatewayResolveCache";
+import type { OrgAuthedContext } from "@/src/server/api/trpc";
+import {
+ type GatewayProviderName,
+ gatewayProviders,
+} from "@/src/features/ai-gateway/server/provider/registry";
+import { GatewayProviderRepository } from "../connection/gatewayProviderRepository";
+import {
+ type GatewayModelCatalogEntry,
+ type ModelDiscoveryError,
+ getModelDiscoveryAdapter,
+} from "./modelDiscoveryRegistry";
+
+export type { GatewayModelCatalogEntry } from "./modelDiscoveryRegistry";
+
+type ModelRefreshError = {
+ connectionId: string;
+ success: false;
+ error: ModelDiscoveryError;
+};
+
+export type ModelRefreshResult =
+ | { connectionId: string; success: true; models: string[] }
+ | ModelRefreshError;
+
+export type ModelCatalogRefreshResult =
+ | {
+ connectionId: string;
+ success: true;
+ models: GatewayModelCatalogEntry[];
+ }
+ | ModelRefreshError;
+
+const MODEL_CACHE_PREFIX = "ai-gateway:models";
+const MODEL_CACHE_TTL_SECONDS = 5 * 60;
+
+export class GatewayModelCatalogService {
+ private readonly repository: GatewayProviderRepository;
+
+ constructor(
+ private readonly prisma: PrismaClient,
+ private readonly fetcher: typeof fetch = fetch,
+ private readonly redis: Redis | Cluster | null = defaultRedis,
+ ) {
+ this.repository = new GatewayProviderRepository(prisma);
+ }
+
+ async refreshAllModels(
+ organizationId: string,
+ forceRefresh = false,
+ ): Promise<ModelRefreshResult[]> {
+ const connections = await this.listAllConnections(
+ organizationId,
+ "ENABLED",
+ );
+ return Promise.all(
+ connections.map((connection) =>
+ this.refreshConnectionModels({
+ organizationId,
+ connectionId: connection.id,
+ forceRefresh,
+ explicitRetry: false,
+ }),
+ ),
+ );
+ }
+
+ async refreshModels(params: {
+ organizationId: string;
+ connectionId: string;
+ forceRefresh?: boolean;
+ }): Promise<ModelRefreshResult> {
+ return this.refreshConnectionModels({
+ ...params,
+ forceRefresh: params.forceRefresh ?? false,
+ explicitRetry: false,
+ });
+ }
+
+ async retryConnection(params: {
+ organizationId: string;
+ connectionId: string;
+ session: OrgAuthedContext["session"];
+ }): Promise<ModelRefreshResult> {
+ const result = await this.refreshConnectionModels({
+ organizationId: params.organizationId,
+ connectionId: params.connectionId,
+ forceRefresh: true,
+ explicitRetry: true,
+ });
+ if (result.success) {
+ await auditLog(
+ {
+ session: params.session,
+ resourceType: "gatewayAiConnection",
+ resourceId: params.connectionId,
+ action: "retry",
+ },
+ this.prisma,
+ );
+ }
+ return result;
+ }
+
+ async getModelCatalog(params: {
+ organizationId: string;
+ connectionId: string;
+ }): Promise<ModelCatalogRefreshResult> {
+ return this.refreshConnectionModelCatalog({
+ ...params,
+ forceRefresh: false,
+ explicitRetry: false,
+ });
+ }
+
+ async clearModelCache(organizationId: string, connectionId: string) {
+ if (!this.redis) return;
+ try {
+ await this.redis.del(this.modelCacheKey(organizationId, connectionId));
+ } catch {
+ // Redis is optional. Credential updates still succeed when it is unavailable.
+ }
+ }
+
+ private async listAllConnections(
+ organizationId: string,
+ status?: GatewayConnectionStatus,
+ ) {
+ const connections = [];
+ let cursor: string | undefined;
+ do {
+ const page = await this.repository.listConnections({
+ organizationId,
+ cursor,
+ limit: 100,
+ status,
+ });
+ connections.push(...page.data);
+ cursor = page.nextCursor ?? undefined;
+ } while (cursor);
+ return connections;
+ }
+
+ private async refreshConnectionModels(params: {
+ organizationId: string;
+ connectionId: string;
+ forceRefresh: boolean;
+ explicitRetry: boolean;
+ }): Promise<ModelRefreshResult> {
+ const result = await this.refreshConnectionModelCatalog(params);
+ return result.success
+ ? {
+ connectionId: result.connectionId,
+ success: true,
+ models: result.models.map(({ id }) => id),
+ }
+ : result;
+ }
+
+ private async refreshConnectionModelCatalog(params: {
+ organizationId: string;
+ connectionId: string;
+ forceRefresh: boolean;
+ explicitRetry: boolean;
+ }): Promise<ModelCatalogRefreshResult> {
+ const connection = await this.repository.getConnectionWithCredential({
+ organizationId: params.organizationId,
+ id: params.connectionId,
+ });
+ if (!connection) throw new LangfuseNotFoundError("Gateway connection");
+
+ if (params.forceRefresh) {
+ await this.clearModelCache(params.organizationId, params.connectionId);
+ } else {
+ const cachedModels = await this.getCachedModels(
+ params.organizationId,
+ params.connectionId,
+ );
+ if (cachedModels) {
+ return {
+ connectionId: connection.id,
+ success: true,
+ models: cachedModels,
+ };
+ }
+ }
+
+ const provider = connection.provider as GatewayProviderName;
+ const credential = decrypt(connection.encryptedCredential);
+ const models = await getModelDiscoveryAdapter(provider).fetchCatalog({
+ credential,
+ fetcher: this.fetcher,
+ });
+ if (!models.success) {
+ if (models.error === "unauthorized") {
+ await this.repository.updateConnectionStatus({
+ organizationId: params.organizationId,
+ id: connection.id,
+ status: "ERROR",
+ });
+ // A connection that just failed authorization must stop being handed
+ // to the data plane.
+ await invalidateGatewayResolveCacheForOrganization(
+ params.organizationId,
+ );
+ }
+ return {
+ connectionId: connection.id,
+ success: false,
+ error: models.error,
+ };
+ }
+ await this.cacheModels(
+ params.organizationId,
+ params.connectionId,
+ models.models,
+ );
+ if (params.explicitRetry && connection.status === "ERROR") {
+ await this.repository.updateConnectionStatus({
+ organizationId: params.organizationId,
+ id: connection.id,
+ status: "ENABLED",
+ });
+ await invalidateGatewayResolveCacheForOrganization(params.organizationId);
+ }
+ return {
+ connectionId: connection.id,
+ success: true,
+ models: models.models,
+ };
+ }
+
+ private modelCacheKey(organizationId: string, connectionId: string) {
+ return `${MODEL_CACHE_PREFIX}:${organizationId}:${connectionId}`;
+ }
+
+ private async getCachedModels(
+ organizationId: string,
+ connectionId: string,
+ ): Promise<GatewayModelCatalogEntry[] | null> {
+ if (!this.redis) return null;
+ try {
+ const cached = await this.redis.get(
+ this.modelCacheKey(organizationId, connectionId),
+ );
+ if (!cached) return null;
+ const parsed: unknown = JSON.parse(cached);
+ return isGatewayModelCatalog(parsed) ? parsed : null;
+ } catch {
+ return null;
+ }
+ }
+
+ private async cacheModels(
+ organizationId: string,
+ connectionId: string,
+ models: GatewayModelCatalogEntry[],
+ ) {
+ if (!this.redis) return;
+ try {
+ await this.redis.setex(
+ this.modelCacheKey(organizationId, connectionId),
+ MODEL_CACHE_TTL_SECONDS,
+ JSON.stringify(models),
+ );
+ } catch {
+ // Redis is optional. Provider discovery still succeeds when it is unavailable.
+ }
+ }
+}
+
+function isGatewayModelCatalog(
+ value: unknown,
+): value is GatewayModelCatalogEntry[] {
+ return (
+ Array.isArray(value) &&
+ value.every(
+ (model) =>
+ model &&
+ typeof model === "object" &&
+ "id" in model &&
+ typeof model.id === "string" &&
+ "provider" in model &&
+ gatewayProviders.includes(model.provider as GatewayProviderName) &&
+ "canonicalSlug" in model &&
+ typeof model.canonicalSlug === "string" &&
+ "displayName" in model &&
+ typeof model.displayName === "string" &&
+ "createdAt" in model &&
+ typeof model.createdAt === "string",
+ )
+ );
+}
diff --git a/web/src/features/ai-gateway/server/provider/models/gatewayModelsApiHandler.ts b/web/src/features/ai-gateway/server/provider/models/gatewayModelsApiHandler.ts
new file mode 100644
index 0000000000..5a22db936a
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/models/gatewayModelsApiHandler.ts
@@ -0,0 +1,38 @@
+import type { NextApiRequest, NextApiResponse } from "next";
+
+import { prisma } from "@langfuse/shared/src/db";
+
+import { GatewayApiKeyAuthenticator } from "@/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator";
+import { GatewayControlPlaneError } from "@/src/features/ai-gateway/server/gatewayControlPlaneError";
+import {
+ type GatewayApiFormat,
+ GatewayModelsResponseSchema,
+} from "@/src/features/ai-gateway/server/provider";
+import { GatewayModelsService } from "./gatewayModelsService";
+
+export async function gatewayModelsApiHandler({
+ res,
+ fastHashedSecretKey,
+ apiFormat,
+}: {
+ req: NextApiRequest;
+ res: NextApiResponse;
+ fastHashedSecretKey: string;
+ apiFormat: GatewayApiFormat;
+}) {
+ try {
+ const context = await new GatewayApiKeyAuthenticator(
+ prisma,
+ ).authenticateRequest({ fastHashedSecretKey, apiFormat });
+ const result = await new GatewayModelsService(prisma).list({
+ context,
+ apiFormat,
+ });
+ return res.status(200).json(GatewayModelsResponseSchema.parse(result));
+ } catch (error) {
+ if (error instanceof GatewayControlPlaneError) {
+ return res.status(error.status).json({ error: error.message });
+ }
+ return res.status(500).json({ error: "Internal server error" });
+ }
+}
diff --git a/web/src/features/ai-gateway/server/provider/models/gatewayModelsRepository.ts b/web/src/features/ai-gateway/server/provider/models/gatewayModelsRepository.ts
new file mode 100644
index 0000000000..a914b7211e
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/models/gatewayModelsRepository.ts
@@ -0,0 +1,20 @@
+import type { GatewayProvider, PrismaClient } from "@langfuse/shared/src/db";
+
+export class GatewayModelsRepository {
+ constructor(private readonly prisma: PrismaClient) {}
+
+ getConnections(params: {
+ organizationId: string;
+ providers: GatewayProvider[];
+ }) {
+ return this.prisma.gatewayAiConnection.findMany({
+ where: {
+ organizationId: params.organizationId,
+ provider: { in: params.providers },
+ status: "ENABLED",
+ },
+ orderBy: [{ routingPriority: "asc" }, { id: "asc" }],
+ select: { id: true },
+ });
+ }
+}
diff --git a/web/src/features/ai-gateway/server/provider/models/gatewayModelsService.ts b/web/src/features/ai-gateway/server/provider/models/gatewayModelsService.ts
new file mode 100644
index 0000000000..c502d03090
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/models/gatewayModelsService.ts
@@ -0,0 +1,78 @@
+import type { GatewayProvider, PrismaClient } from "@langfuse/shared/src/db";
+import { redis as defaultRedis } from "@langfuse/shared/src/server";
+import type { Cluster, Redis } from "ioredis";
+
+import type { GatewayApiKeyAuthContext } from "@/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator";
+import { GatewayControlPlaneError } from "@/src/features/ai-gateway/server/gatewayControlPlaneError";
+import {
+ type GatewayApiFormat,
+ gatewayProviders,
+ providerSupportsApiFormat,
+} from "@/src/features/ai-gateway/server/provider/registry";
+import {
+ type GatewayModelCatalogEntry,
+ GatewayModelCatalogService,
+} from "./gatewayModelCatalogService";
+import { GatewayModelsRepository } from "./gatewayModelsRepository";
+
+export class GatewayModelsService {
+ private readonly repository: GatewayModelsRepository;
+ private readonly modelCatalogService: GatewayModelCatalogService;
+
+ constructor(
+ prisma: PrismaClient,
+ fetcher: typeof fetch = fetch,
+ redis: Redis | Cluster | null = defaultRedis,
+ ) {
+ this.repository = new GatewayModelsRepository(prisma);
+ this.modelCatalogService = new GatewayModelCatalogService(
+ prisma,
+ fetcher,
+ redis,
+ );
+ }
+
+ async list(params: {
+ context: GatewayApiKeyAuthContext;
+ apiFormat: GatewayApiFormat;
+ }) {
+ const connections = await this.repository.getConnections({
+ organizationId: params.context.organizationId,
+ providers: gatewayProviders.filter((provider) =>
+ providerSupportsApiFormat(provider, params.apiFormat),
+ ) as GatewayProvider[],
+ });
+ const results = await Promise.all(
+ connections.map((connection) =>
+ this.modelCatalogService.getModelCatalog({
+ organizationId: params.context.organizationId,
+ connectionId: connection.id,
+ }),
+ ),
+ );
+ if (results.some((result) => !result.success)) {
+ throw new GatewayControlPlaneError(
+ "Gateway model discovery is temporarily unavailable",
+ 503,
+ );
+ }
+
+ const modelsById = new Map<string, GatewayModelCatalogEntry>();
+ for (const result of results) {
+ if (!result.success) continue;
+ for (const model of result.models) {
+ if (!modelsById.has(model.id)) modelsById.set(model.id, model);
+ }
+ }
+ return {
+ data: [...modelsById.values()].sort(compareModels),
+ };
+ }
+}
+
+function compareModels(
+ left: GatewayModelCatalogEntry,
+ right: GatewayModelCatalogEntry,
+) {
+ return left.id < right.id ? -1 : left.id > right.id ? 1 : 0;
+}
diff --git a/web/src/features/ai-gateway/server/provider/models/modelDiscoveryRegistry.ts b/web/src/features/ai-gateway/server/provider/models/modelDiscoveryRegistry.ts
new file mode 100644
index 0000000000..de70f90eb5
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/models/modelDiscoveryRegistry.ts
@@ -0,0 +1,18 @@
+import type { GatewayProviderName } from "@/src/features/ai-gateway/server/provider/registry";
+import { anthropicModelDiscoveryAdapter } from "./adapters/anthropic";
+import { openAiModelDiscoveryAdapter } from "./adapters/openAi";
+import type { ModelDiscoveryAdapter } from "./types";
+
+export type { GatewayModelCatalogEntry, ModelDiscoveryError } from "./types";
+
+const MODEL_DISCOVERY_ADAPTERS: Record<
+ GatewayProviderName,
+ ModelDiscoveryAdapter
+> = {
+ OPENAI: openAiModelDiscoveryAdapter,
+ ANTHROPIC: anthropicModelDiscoveryAdapter,
+};
+
+export function getModelDiscoveryAdapter(provider: GatewayProviderName) {
+ return MODEL_DISCOVERY_ADAPTERS[provider];
+}
diff --git a/web/src/features/ai-gateway/server/provider/models/types.ts b/web/src/features/ai-gateway/server/provider/models/types.ts
new file mode 100644
index 0000000000..4604a36449
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/models/types.ts
@@ -0,0 +1,26 @@
+import type { GatewayProviderName } from "@/src/features/ai-gateway/server/provider/registry";
+
+export type GatewayModelCatalogEntry = {
+ id: string;
+ provider: GatewayProviderName;
+ canonicalSlug: string;
+ displayName: string;
+ createdAt: string;
+};
+
+export type ModelDiscoveryError =
+ | "unauthorized"
+ | "rate_limited"
+ | "provider_error"
+ | "timeout";
+
+type ModelDiscoveryResult =
+ | { success: true; models: GatewayModelCatalogEntry[] }
+ | { success: false; error: ModelDiscoveryError };
+
+export interface ModelDiscoveryAdapter {
+ fetchCatalog(params: {
+ credential: string;
+ fetcher: typeof fetch;
+ }): Promise<ModelDiscoveryResult>;
+}
diff --git a/web/src/features/ai-gateway/server/provider/registry.servertest.ts b/web/src/features/ai-gateway/server/provider/registry.servertest.ts
new file mode 100644
index 0000000000..4de0b08914
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/registry.servertest.ts
@@ -0,0 +1,261 @@
+import { describe, expect, it, vi } from "vitest";
+
+import {
+ GatewayModelCatalogService,
+ GatewayProviderService,
+ getGatewayProviderDefinition,
+ providerSupportsApiFormat,
+} from ".";
+import type { PrismaClient } from "@langfuse/shared/src/db";
+import { encrypt } from "@langfuse/shared/encryption";
+import type { Redis } from "ioredis";
+import type { OrgAuthedContext } from "@/src/server/api/trpc";
+
+const session = {
+ user: { id: "user-1" },
+ orgId: "org-1",
+ orgRole: "OWNER",
+} as OrgAuthedContext["session"];
+
+describe("AI gateway provider registry", () => {
+ it("exposes only controlled provider URLs and explicit capabilities", () => {
+ expect(getGatewayProviderDefinition("OPENAI")).toMatchObject({
+ baseUrl: "https://api.openai.com/v1",
+ authType: "bearer",
+ });
+ expect(getGatewayProviderDefinition("ANTHROPIC")).toMatchObject({
+ baseUrl: "https://api.anthropic.com/v1",
+ authType: "x-api-key",
+ validationModel: "claude-haiku-4-5-20251001",
+ });
+ expect(providerSupportsApiFormat("OPENAI", "openai.responses")).toBe(true);
+ expect(providerSupportsApiFormat("ANTHROPIC", "anthropic.messages")).toBe(
+ true,
+ );
+ });
+
+ it("validates credentials before persisting a connection", async () => {
+ const validator = vi
+ .fn()
+ .mockRejectedValue(new Error("invalid provider credential"));
+ const service = new GatewayProviderService({} as PrismaClient, validator);
+
+ await expect(
+ service.create({
+ organizationId: "org-1",
+ name: "Primary",
+ provider: "OPENAI",
+ credential: "invalid",
+ session,
+ }),
+ ).rejects.toThrow("invalid provider credential");
+ expect(validator).toHaveBeenCalledWith({
+ provider: "OPENAI",
+ credential: "invalid",
+ });
+ });
+
+ it("reads model lists from an organization and connection scoped cache", async () => {
+ const fetcher = vi.fn<typeof fetch>();
+ const redis = {
+ get: vi.fn().mockResolvedValue(
+ JSON.stringify([
+ {
+ id: "gpt-5-mini",
+ provider: "OPENAI",
+ canonicalSlug: "gpt-5-mini",
+ displayName: "gpt-5-mini",
+ createdAt: "2026-01-01T00:00:00.000Z",
+ },
+ ]),
+ ),
+ setex: vi.fn(),
+ } as unknown as Redis;
+ const prisma = {
+ gatewayAiConnection: {
+ findFirst: vi.fn().mockResolvedValue({
+ id: "connection-1",
+ organizationId: "org-1",
+ provider: "OPENAI",
+ encryptedCredential: "unused",
+ }),
+ },
+ } as unknown as PrismaClient;
+ const service = new GatewayModelCatalogService(prisma, fetcher, redis);
+
+ await expect(
+ service.refreshModels({
+ organizationId: "org-1",
+ connectionId: "connection-1",
+ }),
+ ).resolves.toEqual({
+ connectionId: "connection-1",
+ success: true,
+ models: ["gpt-5-mini"],
+ });
+ expect(redis.get).toHaveBeenCalledWith(
+ "ai-gateway:models:org-1:connection-1",
+ );
+ expect(fetcher).not.toHaveBeenCalled();
+ });
+
+ it("collects every Anthropic model page as canonical metadata", async () => {
+ const fetcher = vi
+ .fn<typeof fetch>()
+ .mockResolvedValueOnce(
+ Response.json({
+ data: [
+ {
+ id: "claude-a",
+ display_name: "Claude A",
+ created_at: "2026-01-01T00:00:00.000Z",
+ },
+ ],
+ has_more: true,
+ last_id: "claude-a",
+ }),
+ )
+ .mockResolvedValueOnce(
+ Response.json({
+ data: [
+ {
+ id: "claude-b",
+ display_name: "Claude B",
+ created_at: "2026-02-01T00:00:00.000Z",
+ },
+ ],
+ has_more: false,
+ }),
+ );
+ const prisma = {
+ gatewayAiConnection: {
+ findFirst: vi.fn().mockResolvedValue({
+ id: "connection-1",
+ organizationId: "org-1",
+ provider: "ANTHROPIC",
+ encryptedCredential: encrypt("sk-test"),
+ status: "ENABLED",
+ }),
+ },
+ } as unknown as PrismaClient;
+ const service = new GatewayModelCatalogService(prisma, fetcher, null);
+
+ await expect(
+ service.getModelCatalog({
+ organizationId: "org-1",
+ connectionId: "connection-1",
+ }),
+ ).resolves.toMatchObject({
+ success: true,
+ models: [
+ {
+ id: "claude-a",
+ provider: "ANTHROPIC",
+ canonicalSlug: "claude-a",
+ displayName: "Claude A",
+ createdAt: "2026-01-01T00:00:00.000Z",
+ },
+ {
+ id: "claude-b",
+ provider: "ANTHROPIC",
+ canonicalSlug: "claude-b",
+ displayName: "Claude B",
+ createdAt: "2026-02-01T00:00:00.000Z",
+ },
+ ],
+ });
+ expect(String(fetcher.mock.calls[1]?.[0])).toContain("after_id=claude-a");
+ });
+
+ it("treats malformed provider model responses as failures", async () => {
+ const fetcher = vi
+ .fn<typeof fetch>()
+ .mockResolvedValue(Response.json({ data: [{}] }));
+ const redis = {
+ get: vi.fn().mockResolvedValue(null),
+ setex: vi.fn(),
+ } as unknown as Redis;
+ const prisma = {
+ gatewayAiConnection: {
+ findFirst: vi.fn().mockResolvedValue({
+ id: "connection-1",
+ organizationId: "org-1",
+ provider: "OPENAI",
+ encryptedCredential: encrypt("sk-test"),
+ status: "ENABLED",
+ }),
+ },
+ } as unknown as PrismaClient;
+ const service = new GatewayModelCatalogService(prisma, fetcher, redis);
+
+ await expect(
+ service.refreshModels({
+ organizationId: "org-1",
+ connectionId: "connection-1",
+ }),
+ ).resolves.toEqual({
+ connectionId: "connection-1",
+ success: false,
+ error: "provider_error",
+ });
+ expect(redis.setex).not.toHaveBeenCalled();
+ });
+
+ it("bypasses and refreshes the cache during an explicit sync", async () => {
+ const fetcher = vi.fn<typeof fetch>().mockResolvedValue(
+ new Response(JSON.stringify({ data: [{ id: "gpt-5", created: 10 }] }), {
+ status: 200,
+ }),
+ );
+ const redis = {
+ get: vi
+ .fn()
+ .mockResolvedValue(
+ JSON.stringify([{ id: "stale-model", provider: "OPENAI" }]),
+ ),
+ setex: vi.fn().mockResolvedValue("OK"),
+ del: vi.fn().mockResolvedValue(1),
+ } as unknown as Redis;
+ const prisma = {
+ gatewayAiConnection: {
+ findFirst: vi.fn().mockResolvedValue({
+ id: "connection-1",
+ organizationId: "org-1",
+ provider: "OPENAI",
+ encryptedCredential: encrypt("sk-test"),
+ status: "ENABLED",
+ }),
+ },
+ } as unknown as PrismaClient;
+ const service = new GatewayModelCatalogService(prisma, fetcher, redis);
+
+ await expect(
+ service.refreshModels({
+ organizationId: "org-1",
+ connectionId: "connection-1",
+ forceRefresh: true,
+ }),
+ ).resolves.toEqual({
+ connectionId: "connection-1",
+ success: true,
+ models: ["gpt-5"],
+ });
+ expect(redis.get).not.toHaveBeenCalled();
+ expect(redis.del).toHaveBeenCalledWith(
+ "ai-gateway:models:org-1:connection-1",
+ );
+ expect(redis.setex).toHaveBeenCalledWith(
+ "ai-gateway:models:org-1:connection-1",
+ 300,
+ JSON.stringify([
+ {
+ id: "gpt-5",
+ provider: "OPENAI",
+ canonicalSlug: "gpt-5",
+ displayName: "gpt-5",
+ createdAt: "1970-01-01T00:00:10.000Z",
+ },
+ ]),
+ );
+ });
+});
diff --git a/web/src/features/ai-gateway/server/provider/registry.ts b/web/src/features/ai-gateway/server/provider/registry.ts
new file mode 100644
index 0000000000..bab4aae129
--- /dev/null
+++ b/web/src/features/ai-gateway/server/provider/registry.ts
@@ -0,0 +1,114 @@
+import { z } from "zod/v4";
+
+export const gatewayApiFormats = [
+ "openai.responses",
+ "openai.chat-completions",
+ "anthropic.messages",
+] as const;
+
+export const gatewayProviders = ["OPENAI", "ANTHROPIC"] as const;
+
+export type GatewayApiFormat = (typeof gatewayApiFormats)[number];
+export type GatewayProviderName = (typeof gatewayProviders)[number];
+
+export const GatewayApiFormatSchema = z.enum(gatewayApiFormats);
+const metadataValueSchema = z.union([z.string(), z.number(), z.boolean()]);
+export const GatewayMetadataSchema = z.record(z.string(), metadataValueSchema);
+export type GatewayMetadata = z.infer<typeof GatewayMetadataSchema>;
+
+const gatewayProviderIds = ["openai", "anthropic"] as const;
+export type GatewayProviderId = (typeof gatewayProviderIds)[number];
+
+export const GatewayResolveResponseSchema = z
+ .object({
+ version: z.literal(1),
+ connection: z
+ .object({
+ id: z.string(),
+ provider: z.enum(gatewayProviderIds),
+ api_format: GatewayApiFormatSchema,
+ base_url: z.url(),
+ auth: z.union([
+ z.object({ type: z.literal("Bearer"), token: z.string() }).strict(),
+ z
+ .object({
+ type: z.literal("x-api-key"),
+ header: z.literal("x-api-key"),
+ value: z.string(),
+ })
+ .strict(),
+ ]),
+ })
+ .strict(),
+ attribution: z
+ .object({
+ organization_id: z.string(),
+ project_id: z.string(),
+ key_id: z.string(),
+ key_metadata: GatewayMetadataSchema,
+ })
+ .strict(),
+ ingestion_mode: z.enum(["usage", "full"]),
+ ingestion: z
+ .object({
+ access_token: z.string(),
+ token_type: z.literal("Bearer"),
+ expires_at: z.number().int().positive(),
+ })
+ .strict(),
+ })
+ .strict();
+
+export const GatewayModelsResponseSchema = z
+ .object({
+ data: z.array(
+ z
+ .object({
+ id: z.string(),
+ provider: z.enum(gatewayProviders),
+ canonicalSlug: z.string(),
+ displayName: z.string(),
+ createdAt: z.iso.datetime(),
+ })
+ .strict(),
+ ),
+ })
+ .strict();
+
+type ProviderDefinition = {
+ baseUrl: string;
+ modelsPath: string;
+ authType: "bearer" | "x-api-key";
+ validationModel: string;
+ apiFormats: readonly GatewayApiFormat[];
+};
+
+const PROVIDER_REGISTRY: Record<GatewayProviderName, ProviderDefinition> = {
+ OPENAI: {
+ baseUrl: "https://api.openai.com/v1",
+ modelsPath: "/models",
+ authType: "bearer",
+ validationModel: "gpt-4o-mini",
+ apiFormats: ["openai.responses", "openai.chat-completions"],
+ },
+ ANTHROPIC: {
+ baseUrl: "https://api.anthropic.com/v1",
+ modelsPath: "/models",
+ authType: "x-api-key",
+ validationModel: "claude-haiku-4-5-20251001",
+ apiFormats: ["anthropic.messages"],
+ },
+};
+
+export function getGatewayProviderDefinition(
+ provider: GatewayProviderName,
+): ProviderDefinition {
+ return PROVIDER_REGISTRY[provider];
+}
+
+export function providerSupportsApiFormat(
+ provider: GatewayProviderName,
+ apiFormat: GatewayApiFormat,
+): boolean {
+ return PROVIDER_REGISTRY[provider].apiFormats.includes(apiFormat);
+}
diff --git a/web/src/features/ai-gateway/server/resolve/gatewayResolveApiHandler.ts b/web/src/features/ai-gateway/server/resolve/gatewayResolveApiHandler.ts
new file mode 100644
index 0000000000..7f7b08d7b1
--- /dev/null
+++ b/web/src/features/ai-gateway/server/resolve/gatewayResolveApiHandler.ts
@@ -0,0 +1,42 @@
+import { prisma } from "@langfuse/shared/src/db";
+import { logger, traceException } from "@langfuse/shared/src/server";
+import type { NextApiRequest, NextApiResponse } from "next";
+
+import { GatewayApiKeyAuthenticator } from "@/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator";
+import {
+ type GatewayApiFormat,
+ GatewayResolveResponseSchema,
+} from "@/src/features/ai-gateway/server/provider";
+import { GatewayResolveError, GatewayResolveService } from "./resolveService";
+
+export async function gatewayResolveApiHandler({
+ res,
+ fastHashedSecretKey,
+ apiFormat,
+}: {
+ req: NextApiRequest;
+ res: NextApiResponse;
+ fastHashedSecretKey: string;
+ apiFormat: GatewayApiFormat;
+}) {
+ try {
+ const context = await new GatewayApiKeyAuthenticator(
+ prisma,
+ ).authenticateRequest({ fastHashedSecretKey, apiFormat });
+ const result = await new GatewayResolveService().resolve({
+ context,
+ apiFormat,
+ });
+ return res.status(200).json(GatewayResolveResponseSchema.parse(result));
+ } catch (error) {
+ if (error instanceof GatewayResolveError) {
+ // The data plane retries a 503, so tell it when to come back rather than
+ // letting it hammer a database that is already struggling.
+ if (error.status === 503) res.setHeader("Retry-After", "1");
+ return res.status(error.status).json({ error: error.message });
+ }
+ logger.error("Unexpected error resolving gateway request", error);
+ traceException(error);
+ return res.status(500).json({ error: "Internal server error" });
+ }
+}
diff --git a/web/src/features/ai-gateway/server/resolve/gatewayResolveCache.ts b/web/src/features/ai-gateway/server/resolve/gatewayResolveCache.ts
new file mode 100644
index 0000000000..e8c4722cd0
--- /dev/null
+++ b/web/src/features/ai-gateway/server/resolve/gatewayResolveCache.ts
@@ -0,0 +1,174 @@
+import {
+ logger,
+ recordIncrement,
+ redis as defaultRedis,
+} from "@langfuse/shared/src/server";
+import type { Cluster, Redis } from "ioredis";
+import { z } from "zod/v4";
+
+import { env } from "@/src/env.mjs";
+// Imported from the registry rather than the provider barrel: the barrel pulls
+// in the provider services, which invalidate this cache, and the resulting
+// import cycle leaves these schemas undefined at module init.
+import {
+ GatewayMetadataSchema,
+ gatewayApiFormats,
+} from "@/src/features/ai-gateway/server/provider/registry";
+
+const CACHE_PREFIX = "ai-gateway:resolve";
+
+/**
+ * Marker for a secret key that no gateway API key matches. Cached like a hit so
+ * that a client looping on a bad key cannot generate unbounded Postgres load.
+ */
+export const GATEWAY_RESOLVE_KEY_NON_EXISTENT = "gateway-key-non-existent";
+
+export const CachedResolveContext = z.object({
+ organizationId: z.string(),
+ apiKeyId: z.string(),
+ keyMetadata: GatewayMetadataSchema,
+ ingestionProjectId: z.string().nullable(),
+ ingestionMode: z.enum(["USAGE", "FULL"]).nullable(),
+ connection: z
+ .object({
+ id: z.string(),
+ provider: z.enum(["OPENAI", "ANTHROPIC"]),
+ encryptedCredential: z.string(),
+ })
+ .nullable(),
+});
+export type CachedResolveContext = z.infer<typeof CachedResolveContext>;
+
+const CachedResolveEntry = z.union([
+ CachedResolveContext,
+ z.literal(GATEWAY_RESOLVE_KEY_NON_EXISTENT),
+]);
+
+const contextCacheKey = (fastHashedSecretKey: string, apiFormat: string) =>
+ `${CACHE_PREFIX}:context:${apiFormat}:${fastHashedSecretKey}`;
+
+/**
+ * Index of the cache keys written for one organization. Entries are keyed by
+ * secret-key hash, which an organization-level change (a new connection, a
+ * different ingestion project) cannot enumerate on its own.
+ */
+const organizationIndexKey = (organizationId: string) =>
+ `${CACHE_PREFIX}:org:${organizationId}`;
+
+/**
+ * Caches the /resolve lookup. Every failure is swallowed: a Redis outage must
+ * degrade the gateway to Postgres reads, never take it down.
+ */
+export class GatewayResolveCache {
+ constructor(private readonly redis: Redis | Cluster | null = defaultRedis) {}
+
+ private get enabled() {
+ return (
+ this.redis !== null &&
+ env.LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED === "true"
+ );
+ }
+
+ async get(params: { fastHashedSecretKey: string; apiFormat: string }) {
+ if (!this.enabled) return null;
+
+ try {
+ const cached = await this.redis!.get(
+ contextCacheKey(params.fastHashedSecretKey, params.apiFormat),
+ );
+ if (!cached) {
+ recordIncrement("langfuse.gateway.resolve.cache_miss", 1);
+ return null;
+ }
+
+ const parsed = CachedResolveEntry.safeParse(JSON.parse(cached));
+ if (!parsed.success) {
+ // A shape change between deployments must not serve stale garbage.
+ await this.redis!.del(
+ contextCacheKey(params.fastHashedSecretKey, params.apiFormat),
+ );
+ recordIncrement("langfuse.gateway.resolve.cache_miss", 1);
+ return null;
+ }
+
+ recordIncrement("langfuse.gateway.resolve.cache_hit", 1);
+ return parsed.data;
+ } catch (error) {
+ logger.error("Error reading gateway resolve cache", error);
+ return null;
+ }
+ }
+
+ async set(params: {
+ fastHashedSecretKey: string;
+ apiFormat: string;
+ context: CachedResolveContext | typeof GATEWAY_RESOLVE_KEY_NON_EXISTENT;
+ }) {
+ if (!this.enabled) return;
+
+ const key = contextCacheKey(params.fastHashedSecretKey, params.apiFormat);
+ const ttl = env.LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_TTL_SECONDS;
+ try {
+ if (params.context === GATEWAY_RESOLVE_KEY_NON_EXISTENT) {
+ await this.redis!.set(key, JSON.stringify(params.context), "EX", ttl);
+ return;
+ }
+
+ // These keys can live in different Redis Cluster slots, so issue the
+ // commands separately instead of wrapping them in one transaction.
+ const index = organizationIndexKey(params.context.organizationId);
+ await Promise.all([
+ this.redis!.set(key, JSON.stringify(params.context), "EX", ttl),
+ this.redis!.sadd(index, key),
+ // The index must outlive its entries, otherwise invalidation can miss a
+ // cached key that is still live.
+ this.redis!.expire(index, ttl * 2),
+ ]);
+ } catch (error) {
+ logger.error("Error writing gateway resolve cache", error);
+ }
+ }
+
+ /**
+ * Drops every cached resolve context for an organization. Call this after any
+ * change that alters what /resolve would answer: connections, credentials,
+ * routing priority, connection status, or the gateway config.
+ */
+ async invalidateOrganization(organizationId: string) {
+ if (!this.enabled) return;
+
+ try {
+ const index = organizationIndexKey(organizationId);
+ const keys = await this.redis!.smembers(index);
+ await Promise.all([
+ this.redis!.del(index),
+ ...keys.map((key) => this.redis!.del(key)),
+ ]);
+ } catch (error) {
+ logger.error("Error invalidating gateway resolve cache", error);
+ }
+ }
+
+ /** Drops the cached contexts for a single gateway API key, e.g. on revoke. */
+ async invalidateApiKey(fastHashedSecretKey: string) {
+ if (!this.enabled) return;
+
+ try {
+ await Promise.all(
+ gatewayApiFormats.map((apiFormat) =>
+ this.redis!.del(contextCacheKey(fastHashedSecretKey, apiFormat)),
+ ),
+ );
+ } catch (error) {
+ logger.error("Error invalidating gateway resolve cache", error);
+ }
+ }
+}
+
+export const invalidateGatewayResolveCacheForOrganization = (
+ organizationId: string,
+) => new GatewayResolveCache().invalidateOrganization(organizationId);
+
+export const invalidateGatewayResolveCacheForApiKey = (
+ fastHashedSecretKey: string,
+) => new GatewayResolveCache().invalidateApiKey(fastHashedSecretKey);
diff --git a/web/src/features/ai-gateway/server/resolve/gatewayResolveRepository.ts b/web/src/features/ai-gateway/server/resolve/gatewayResolveRepository.ts
new file mode 100644
index 0000000000..c9c20502c2
--- /dev/null
+++ b/web/src/features/ai-gateway/server/resolve/gatewayResolveRepository.ts
@@ -0,0 +1,58 @@
+import type { GatewayProvider, PrismaClient } from "@langfuse/shared/src/db";
+
+export class GatewayResolveRepository {
+ constructor(private readonly prisma: PrismaClient) {}
+
+ resolveContext(params: {
+ fastHashedSecretKey: string;
+ providers: GatewayProvider[];
+ }) {
+ const now = new Date();
+ return this.prisma.gatewayApiKeyAssociation.findFirst({
+ relationLoadStrategy: "join",
+ where: {
+ apiKey: {
+ fastHashedSecretKey: params.fastHashedSecretKey,
+ scope: "ORGANIZATION",
+ orgId: { not: null },
+ OR: [{ expiresAt: null }, { expiresAt: { gt: now } }],
+ },
+ },
+ select: {
+ apiKeyId: true,
+ metadata: true,
+ apiKey: {
+ select: {
+ orgId: true,
+ organization: {
+ select: {
+ gatewayConfig: {
+ select: {
+ defaultIngestionProjectId: true,
+ ingestionMode: true,
+ defaultIngestionProject: {
+ select: { id: true, orgId: true, deletedAt: true },
+ },
+ },
+ },
+ gatewayAiConnections: {
+ where: {
+ provider: { in: params.providers },
+ status: "ENABLED",
+ },
+ orderBy: { routingPriority: "asc" },
+ take: 1,
+ select: {
+ id: true,
+ provider: true,
+ encryptedCredential: true,
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ });
+ }
+}
diff --git a/web/src/features/ai-gateway/server/resolve/resolveService.servertest.ts b/web/src/features/ai-gateway/server/resolve/resolveService.servertest.ts
new file mode 100644
index 0000000000..3806a72f25
--- /dev/null
+++ b/web/src/features/ai-gateway/server/resolve/resolveService.servertest.ts
@@ -0,0 +1,182 @@
+import type { PrismaClient } from "@langfuse/shared/src/db";
+import { describe, expect, it, vi } from "vitest";
+
+import { GatewayApiKeyAuthenticator } from "@/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator";
+import type { GatewayApiFormat } from "@/src/features/ai-gateway/server/provider";
+import { GatewayResolveService } from "./resolveService";
+
+vi.mock("@/src/env.mjs", () => ({
+ env: {
+ LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST: ["org-1"],
+ // Caching is covered against real Redis in the control-plane servertest;
+ // these cases are about what the service does with a database row.
+ LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED: "false",
+ LANGFUSE_AI_GATEWAY_JWT_PRIVATE_KEY: "private-key",
+ LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY: "public-key",
+ LANGFUSE_AI_GATEWAY_JWT_KEY_ID: "key-id",
+ LANGFUSE_AI_GATEWAY_JWT_ISSUER: "test-issuer",
+ LANGFUSE_AI_GATEWAY_JWT_AUDIENCE: "test-audience",
+ },
+}));
+
+vi.mock("@/src/server/utils/jwt", () => ({
+ createEs256JwtSigner: vi.fn(() => ({
+ sign: vi.fn(() => "ingestion-token"),
+ })),
+ createEs256JwtVerifier: vi.fn(() => ({ verify: vi.fn() })),
+}));
+
+vi.mock("@langfuse/shared/encryption", () => ({
+ decrypt: vi.fn(() => "sk-test"),
+}));
+
+const serviceWith = (findFirst: () => Promise<unknown>) => {
+ const prisma = {
+ gatewayApiKeyAssociation: { findFirst },
+ } as unknown as PrismaClient;
+ const contextResolver = new GatewayApiKeyAuthenticator(prisma);
+ const service = new GatewayResolveService();
+
+ return {
+ resolve: async (params: {
+ fastHashedSecretKey: string;
+ apiFormat: GatewayApiFormat;
+ }) => {
+ const context = await contextResolver.authenticateRequest(params);
+ return service.resolve({ context, apiFormat: params.apiFormat });
+ },
+ };
+};
+
+const row = (overrides: {
+ ingestionMode?: string;
+ projectOrgId?: string;
+ metadata?: unknown;
+}) => ({
+ apiKeyId: "key-1",
+ metadata: overrides.metadata,
+ apiKey: {
+ orgId: "org-1",
+ organization: {
+ gatewayConfig: {
+ defaultIngestionProjectId: "project-1",
+ ingestionMode: overrides.ingestionMode ?? "USAGE",
+ defaultIngestionProject: {
+ id: "project-1",
+ orgId: overrides.projectOrgId ?? "org-1",
+ deletedAt: null,
+ },
+ },
+ gatewayAiConnections: [
+ {
+ id: "connection-1",
+ provider: "OPENAI",
+ encryptedCredential: "encrypted-credential",
+ },
+ ],
+ },
+ },
+});
+
+describe("GatewayResolveService", () => {
+ it("hands over the provider credential and cannot be told to attribute elsewhere", async () => {
+ const service = serviceWith(() =>
+ Promise.resolve(
+ row({ metadata: { team: "platform", project_id: "spoofed-project" } }),
+ ),
+ );
+
+ await expect(
+ service.resolve({
+ fastHashedSecretKey: "hashed-secret-key",
+ apiFormat: "openai.responses",
+ }),
+ ).resolves.toEqual({
+ version: 1,
+ connection: {
+ id: "connection-1",
+ provider: "openai",
+ api_format: "openai.responses",
+ base_url: "https://api.openai.com/v1",
+ auth: { type: "Bearer", token: "sk-test" },
+ },
+ // Metadata stays in its own object, so a metadata key called project_id
+ // cannot redirect attribution.
+ attribution: {
+ organization_id: "org-1",
+ project_id: "project-1",
+ key_id: "key-1",
+ key_metadata: { team: "platform", project_id: "spoofed-project" },
+ },
+ ingestion_mode: "usage",
+ ingestion: {
+ access_token: "ingestion-token",
+ token_type: "Bearer",
+ expires_at: expect.any(Number),
+ },
+ });
+ });
+
+ it("drops key metadata that is not a flat object of scalars", async () => {
+ const service = serviceWith(() =>
+ Promise.resolve(row({ metadata: [{ team: "platform" }] })),
+ );
+
+ await expect(
+ service.resolve({
+ fastHashedSecretKey: "hashed-secret-key",
+ apiFormat: "openai.responses",
+ }),
+ ).resolves.toMatchObject({
+ attribution: {
+ organization_id: "org-1",
+ project_id: "project-1",
+ key_id: "key-1",
+ key_metadata: {},
+ },
+ });
+ });
+
+ it("refuses to resolve when the ingestion project belongs to another organization", async () => {
+ const service = serviceWith(() =>
+ Promise.resolve(row({ projectOrgId: "org-other" })),
+ );
+
+ await expect(
+ service.resolve({
+ fastHashedSecretKey: "hashed-secret-key",
+ apiFormat: "openai.responses",
+ }),
+ ).rejects.toMatchObject({ status: 403 });
+ });
+
+ it("rejects an unknown or expired gateway key", async () => {
+ const service = serviceWith(() => Promise.resolve(null));
+
+ await expect(
+ service.resolve({
+ fastHashedSecretKey: "invalid-hashed-secret-key",
+ apiFormat: "openai.responses",
+ }),
+ ).rejects.toMatchObject({ status: 401 });
+ });
+
+ it("rejects gateway keys from organizations outside the allowlist", async () => {
+ const service = serviceWith(() =>
+ Promise.resolve({
+ apiKeyId: "key-1",
+ apiKey: {
+ orgId: "org-not-allowed",
+ organization: { gatewayConfig: null, gatewayAiConnections: [] },
+ },
+ }),
+ );
+
+ await expect(
+ service.resolve({
+ fastHashedSecretKey: "hashed-secret-key",
+ apiFormat: "openai.responses",
+ }),
+ ).rejects.toMatchObject({ status: 403 });
+ });
+});
diff --git a/web/src/features/ai-gateway/server/resolve/resolveService.ts b/web/src/features/ai-gateway/server/resolve/resolveService.ts
new file mode 100644
index 0000000000..4a4c34890c
--- /dev/null
+++ b/web/src/features/ai-gateway/server/resolve/resolveService.ts
@@ -0,0 +1,155 @@
+import type { GatewayIngestionMode } from "@langfuse/shared/src/db";
+import { decrypt } from "@langfuse/shared/encryption";
+import { instrumentAsync } from "@langfuse/shared/src/server";
+
+import { env } from "@/src/env.mjs";
+import {
+ createEs256JwtSigner,
+ type Es256JwtSigner,
+} from "@/src/server/utils/jwt";
+
+import type { GatewayApiKeyAuthContext } from "@/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator";
+import { GATEWAY_INGESTION_TOKEN_TTL_SECONDS } from "@/src/features/ai-gateway/server/auth/ingestionTokenVerifier";
+import { GatewayControlPlaneError as GatewayResolveError } from "@/src/features/ai-gateway/server/gatewayControlPlaneError";
+import {
+ type GatewayApiFormat,
+ type GatewayProviderId,
+ getGatewayProviderDefinition,
+} from "@/src/features/ai-gateway/server/provider";
+
+export { GatewayControlPlaneError as GatewayResolveError } from "@/src/features/ai-gateway/server/gatewayControlPlaneError";
+
+let cachedGatewayIngestionTokenSigner:
+ | {
+ privateKey: string;
+ keyId: string;
+ issuer: string;
+ audience: string;
+ signer: Es256JwtSigner;
+ }
+ | undefined;
+
+function getGatewayIngestionTokenSigner() {
+ const privateKey = env.LANGFUSE_AI_GATEWAY_JWT_PRIVATE_KEY;
+ if (!privateKey) return undefined;
+
+ const publicKey = env.LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY;
+ const keyId = env.LANGFUSE_AI_GATEWAY_JWT_KEY_ID;
+ if (!publicKey || !keyId) {
+ throw new Error(
+ "LANGFUSE_AI_GATEWAY_JWT_PRIVATE_KEY, LANGFUSE_AI_GATEWAY_JWT_PUBLIC_KEY, and LANGFUSE_AI_GATEWAY_JWT_KEY_ID must be set together",
+ );
+ }
+
+ const config = {
+ privateKey,
+ keyId,
+ issuer: env.LANGFUSE_AI_GATEWAY_JWT_ISSUER,
+ audience: env.LANGFUSE_AI_GATEWAY_JWT_AUDIENCE,
+ };
+ if (
+ !cachedGatewayIngestionTokenSigner ||
+ cachedGatewayIngestionTokenSigner.privateKey !== config.privateKey ||
+ cachedGatewayIngestionTokenSigner.keyId !== config.keyId ||
+ cachedGatewayIngestionTokenSigner.issuer !== config.issuer ||
+ cachedGatewayIngestionTokenSigner.audience !== config.audience
+ ) {
+ cachedGatewayIngestionTokenSigner = {
+ ...config,
+ signer: createEs256JwtSigner(config),
+ };
+ }
+ return cachedGatewayIngestionTokenSigner.signer;
+}
+
+export class GatewayResolveService {
+ async resolve(params: {
+ context: GatewayApiKeyAuthContext;
+ apiFormat: GatewayApiFormat;
+ }) {
+ return instrumentAsync({ name: "gateway-resolve" }, async (span) => {
+ span.setAttribute(
+ "langfuse.organization.id",
+ params.context.organizationId,
+ );
+ const { context } = params;
+
+ if (!context.ingestionProjectId || !context.ingestionMode) {
+ throw new GatewayResolveError(
+ "Gateway ingestion project is unavailable",
+ 403,
+ );
+ }
+ if (!context.connection) {
+ throw new GatewayResolveError(
+ "No enabled gateway connection supports this API format",
+ 404,
+ );
+ }
+
+ const provider = getGatewayProviderDefinition(
+ context.connection.provider,
+ );
+ const credential = decrypt(context.connection.encryptedCredential);
+ return {
+ version: 1 as const,
+ connection: {
+ id: context.connection.id,
+ provider:
+ context.connection.provider.toLowerCase() as GatewayProviderId,
+ api_format: params.apiFormat,
+ base_url: provider.baseUrl,
+ auth:
+ provider.authType === "bearer"
+ ? ({ type: "Bearer", token: credential } as const)
+ : ({
+ type: "x-api-key",
+ header: "x-api-key",
+ value: credential,
+ } as const),
+ },
+ attribution: {
+ organization_id: context.organizationId,
+ project_id: context.ingestionProjectId,
+ key_id: context.apiKeyId,
+ key_metadata: context.keyMetadata,
+ },
+ ingestion_mode: context.ingestionMode.toLowerCase() as "usage" | "full",
+ ingestion: this.createIngestionResponse({
+ mode: context.ingestionMode,
+ organizationId: context.organizationId,
+ projectId: context.ingestionProjectId,
+ }),
+ };
+ });
+ }
+
+ private createIngestionResponse(params: {
+ mode: GatewayIngestionMode;
+ organizationId: string;
+ projectId: string;
+ }) {
+ const signer = getGatewayIngestionTokenSigner();
+ if (!signer) {
+ throw new GatewayResolveError(
+ "Gateway ingestion signing is not configured",
+ 503,
+ );
+ }
+ return {
+ access_token: signer.sign({
+ expiresInSeconds: GATEWAY_INGESTION_TOKEN_TTL_SECONDS,
+ claims: {
+ version: 1,
+ organization_id: params.organizationId,
+ project_id: params.projectId,
+ ingestion_mode: params.mode.toLowerCase() as "usage" | "full",
+ scope: "gateway-ingest",
+ },
+ }),
+ token_type: "Bearer" as const,
+ expires_at:
+ Math.floor(Date.now() / 1000) + GATEWAY_INGESTION_TOKEN_TTL_SECONDS,
+ };
+ }
+}
diff --git a/web/src/features/ai-gateway/server/router.ts b/web/src/features/ai-gateway/server/router.ts
new file mode 100644
index 0000000000..e9583a6e4c
--- /dev/null
+++ b/web/src/features/ai-gateway/server/router.ts
@@ -0,0 +1,237 @@
+import { z } from "zod/v4";
+import type { Session } from "next-auth";
+
+import { throwIfNoOrganizationAccess } from "@/src/features/rbac";
+import {
+ createTRPCRouter,
+ protectedOrganizationProcedure,
+ protectedOrganizationProcedureWithoutTracing,
+} from "@/src/server/api/trpc";
+import {
+ GatewayConnectionStatus,
+ GatewayIngestionMode,
+ GatewayProvider,
+} from "@langfuse/shared/src/db";
+import { redis } from "@langfuse/shared/src/server";
+
+import { GatewayApiKeyService } from "./apiKey/gatewayApiKeyService";
+import { requireGatewayEnabledForOrganization } from "./availability";
+import { GatewayConfigService } from "./config/gatewayConfigService";
+import {
+ GatewayMetadataSchema,
+ GatewayModelCatalogService,
+ GatewayProviderService,
+} from "./provider";
+
+const organizationInput = z.object({ orgId: z.string() });
+const paginatedOrganizationInput = organizationInput.extend({
+ cursor: z.string().optional(),
+ limit: z.number().int().min(1).max(100).default(50),
+});
+
+function requireGatewayAdmin(params: { session: Session; orgId: string }) {
+ requireGatewayEnabledForOrganization(params.orgId);
+ throwIfNoOrganizationAccess({
+ session: params.session,
+ organizationId: params.orgId,
+ scope: "gateway:manage",
+ });
+}
+
+export const aiGatewayRouter = createTRPCRouter({
+ getConfig: protectedOrganizationProcedure
+ .input(organizationInput)
+ .query(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ return new GatewayConfigService(ctx.prisma).getConfig(input.orgId);
+ }),
+
+ updateConfig: protectedOrganizationProcedure
+ .input(
+ organizationInput.extend({
+ defaultIngestionProjectId: z.string().nullable(),
+ ingestionMode: z.enum(GatewayIngestionMode),
+ }),
+ )
+ .mutation(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ return new GatewayConfigService(ctx.prisma).updateConfig({
+ organizationId: input.orgId,
+ defaultIngestionProjectId: input.defaultIngestionProjectId,
+ ingestionMode: input.ingestionMode,
+ session: ctx.session,
+ });
+ }),
+
+ listConnections: protectedOrganizationProcedure
+ .input(paginatedOrganizationInput)
+ .query(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ return new GatewayProviderService(ctx.prisma).list({
+ organizationId: input.orgId,
+ cursor: input.cursor,
+ limit: input.limit,
+ });
+ }),
+
+ testConnection: protectedOrganizationProcedureWithoutTracing
+ .input(
+ organizationInput.extend({
+ provider: z.enum(GatewayProvider),
+ credential: z.string().min(1),
+ }),
+ )
+ .mutation(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ await new GatewayProviderService(ctx.prisma).testCredential({
+ provider: input.provider,
+ credential: input.credential,
+ });
+ return { success: true };
+ }),
+
+ createConnection: protectedOrganizationProcedureWithoutTracing
+ .input(
+ organizationInput.extend({
+ name: z.string().trim().min(1).max(200),
+ provider: z.enum(GatewayProvider),
+ credential: z.string().min(1),
+ }),
+ )
+ .mutation(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ const connection = await new GatewayProviderService(ctx.prisma).create({
+ organizationId: input.orgId,
+ name: input.name,
+ provider: input.provider,
+ credential: input.credential,
+ session: ctx.session,
+ });
+ return connection;
+ }),
+
+ updateConnection: protectedOrganizationProcedureWithoutTracing
+ .input(
+ organizationInput.extend({
+ id: z.string(),
+ name: z.string().trim().min(1).max(200).optional(),
+ credential: z.string().min(1).optional(),
+ status: z
+ .enum([
+ GatewayConnectionStatus.ENABLED,
+ GatewayConnectionStatus.DISABLED,
+ ])
+ .optional(),
+ }),
+ )
+ .mutation(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ return new GatewayProviderService(ctx.prisma).update({
+ organizationId: input.orgId,
+ id: input.id,
+ name: input.name,
+ credential: input.credential,
+ status: input.status,
+ session: ctx.session,
+ });
+ }),
+
+ deleteConnection: protectedOrganizationProcedure
+ .input(organizationInput.extend({ id: z.string() }))
+ .mutation(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ await new GatewayProviderService(ctx.prisma).delete({
+ organizationId: input.orgId,
+ id: input.id,
+ session: ctx.session,
+ });
+ return { success: true };
+ }),
+
+ reorderConnections: protectedOrganizationProcedure
+ .input(
+ organizationInput.extend({
+ connectionIds: z.array(z.string()),
+ }),
+ )
+ .mutation(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ return new GatewayProviderService(ctx.prisma).reorder({
+ organizationId: input.orgId,
+ connectionIds: input.connectionIds,
+ session: ctx.session,
+ });
+ }),
+
+ refreshModels: protectedOrganizationProcedure
+ .input(organizationInput)
+ .query(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ return new GatewayModelCatalogService(ctx.prisma).refreshAllModels(
+ input.orgId,
+ );
+ }),
+
+ syncModels: protectedOrganizationProcedure
+ .input(organizationInput)
+ .mutation(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ return new GatewayModelCatalogService(ctx.prisma).refreshAllModels(
+ input.orgId,
+ true,
+ );
+ }),
+
+ retryConnection: protectedOrganizationProcedure
+ .input(organizationInput.extend({ id: z.string() }))
+ .mutation(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ return new GatewayModelCatalogService(ctx.prisma).retryConnection({
+ organizationId: input.orgId,
+ connectionId: input.id,
+ session: ctx.session,
+ });
+ }),
+
+ listApiKeys: protectedOrganizationProcedure
+ .input(paginatedOrganizationInput)
+ .query(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ return new GatewayApiKeyService(ctx.prisma, redis).list({
+ organizationId: input.orgId,
+ cursor: input.cursor,
+ limit: input.limit,
+ });
+ }),
+
+ // Returns the plaintext secret key once, so the procedure must not be traced.
+ createApiKey: protectedOrganizationProcedureWithoutTracing
+ .input(
+ organizationInput.extend({
+ note: z.string().max(500).optional(),
+ metadata: GatewayMetadataSchema.default({}),
+ }),
+ )
+ .mutation(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ const key = await new GatewayApiKeyService(ctx.prisma, redis).create({
+ organizationId: input.orgId,
+ note: input.note,
+ metadata: input.metadata,
+ session: ctx.session,
+ });
+ return key;
+ }),
+
+ revokeApiKey: protectedOrganizationProcedure
+ .input(organizationInput.extend({ id: z.string() }))
+ .mutation(async ({ input, ctx }) => {
+ requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
+ await new GatewayApiKeyService(ctx.prisma, redis).revoke({
+ organizationId: input.orgId,
+ apiKeyId: input.id,
+ session: ctx.session,
+ });
+ return { success: true };
+ }),
+});
diff --git a/web/src/features/ai-gateway/types/gatewayProvider.ts b/web/src/features/ai-gateway/types/gatewayProvider.ts
new file mode 100644
index 0000000000..43f55d8954
--- /dev/null
+++ b/web/src/features/ai-gateway/types/gatewayProvider.ts
@@ -0,0 +1,6 @@
+import type { RouterOutputs } from "@/src/utils/api";
+
+export type GatewayConnection =
+ RouterOutputs["aiGateway"]["listConnections"]["data"][number];
+
+export type GatewayProvider = "OPENAI" | "ANTHROPIC";
diff --git a/web/src/features/audit-logs/auditLog.ts b/web/src/features/audit-logs/auditLog.ts
index aec41a8a4b..de38e02b9a 100644
--- a/web/src/features/audit-logs/auditLog.ts
+++ b/web/src/features/audit-logs/auditLog.ts
@@ -48,6 +48,8 @@ type AuditableResource =
| "cloudSpendAlert"
| "verifiedDomain"
| "ssoConfig"
+ | "gatewayConfig"
+ | "gatewayAiConnection"
// legacy resources
| "membership";
diff --git a/web/src/features/feature-flags/available-flags.ts b/web/src/features/feature-flags/available-flags.ts
index fd8bc61f0d..e08ca7ba5a 100644
--- a/web/src/features/feature-flags/available-flags.ts
+++ b/web/src/features/feature-flags/available-flags.ts
@@ -7,6 +7,13 @@ export const featurePreviewFlags = [
export type FeaturePreviewFlag = (typeof featurePreviewFlags)[number];
+const restrictedFlags = ["aiGateway"] as const;
+
+type RestrictedFlag = (typeof restrictedFlags)[number];
+
+export const isRestrictedFlag = (flag: string): flag is RestrictedFlag =>
+ restrictedFlags.some((restrictedFlag) => restrictedFlag === flag);
+
export const isFeaturePreviewFlag = (
flag: string,
): flag is FeaturePreviewFlag =>
@@ -42,6 +49,7 @@ export const isFeaturePreviewAvailable = (
export const availableFlags = [
...featurePreviewFlags,
+ ...restrictedFlags,
"searchBar",
"templateFlag",
"excludeClickhouseRead",
diff --git a/web/src/features/feature-flags/hooks/useIsFeatureEnabled.clienttest.ts b/web/src/features/feature-flags/hooks/useIsFeatureEnabled.clienttest.ts
new file mode 100644
index 0000000000..0ae6c0b420
--- /dev/null
+++ b/web/src/features/feature-flags/hooks/useIsFeatureEnabled.clienttest.ts
@@ -0,0 +1,61 @@
+import { renderHook } from "@testing-library/react";
+import { useSession } from "next-auth/react";
+
+import useIsFeatureEnabled from "./useIsFeatureEnabled";
+
+vi.mock("next-auth/react", () => ({
+ useSession: vi.fn(),
+}));
+
+const mockSession = ({
+ aiGateway,
+ admin = false,
+ enableExperimentalFeatures = false,
+}: {
+ aiGateway: boolean;
+ admin?: boolean;
+ enableExperimentalFeatures?: boolean;
+}) => {
+ vi.mocked(useSession).mockReturnValue({
+ data: {
+ environment: { enableExperimentalFeatures },
+ user: {
+ admin,
+ featureFlags: { aiGateway: false },
+ organizations: [
+ {
+ id: "org-1",
+ featureFlags: { aiGateway },
+ projects: [],
+ },
+ ],
+ },
+ },
+ } as unknown as ReturnType<typeof useSession>);
+};
+
+describe("useIsFeatureEnabled", () => {
+ it("does not let admin or experimental-feature overrides enable restricted flags", () => {
+ mockSession({
+ aiGateway: false,
+ admin: true,
+ enableExperimentalFeatures: true,
+ });
+
+ const { result } = renderHook(() =>
+ useIsFeatureEnabled("aiGateway", { organizationId: "org-1" }),
+ );
+
+ expect(result.current).toBe(false);
+ });
+
+ it("returns the server-resolved organization flag", () => {
+ mockSession({ aiGateway: true });
+
+ const { result } = renderHook(() =>
+ useIsFeatureEnabled("aiGateway", { organizationId: "org-1" }),
+ );
+
+ expect(result.current).toBe(true);
+ });
+});
diff --git a/web/src/features/feature-flags/hooks/useIsFeatureEnabled.ts b/web/src/features/feature-flags/hooks/useIsFeatureEnabled.ts
index 8f79c8a65d..3ff653f6db 100644
--- a/web/src/features/feature-flags/hooks/useIsFeatureEnabled.ts
+++ b/web/src/features/feature-flags/hooks/useIsFeatureEnabled.ts
@@ -1,4 +1,5 @@
import { useSession } from "next-auth/react";
+import { isRestrictedFlag } from "../available-flags";
import type { Flag } from "../types";
import { getContextualFeatureFlags } from "../utils";
@@ -27,6 +28,10 @@ export default function useIsFeatureEnabled(
organizationId,
})?.[feature] ?? false;
+ if (isRestrictedFlag(feature)) {
+ return isFeatureEnabledOnUser;
+ }
+
return (
isExperimentalFeaturesEnabled ||
(enableForAdmins && isAdmin) ||
diff --git a/web/src/features/feature-flags/types.ts b/web/src/features/feature-flags/types.ts
index 5aea25f1f1..f136781dd2 100644
--- a/web/src/features/feature-flags/types.ts
+++ b/web/src/features/feature-flags/types.ts
@@ -4,11 +4,12 @@ export type Flag = (typeof availableFlags)[number];
export type Flags = {
[key in Exclude<
Flag,
- "modernSession" | "normalizedIoPreview" | "sessionTimeline"
+ "modernSession" | "normalizedIoPreview" | "aiGateway" | "sessionTimeline"
>]: boolean;
} & {
// Optional while older sessions and test fixtures roll across new flags.
modernSession?: boolean;
normalizedIoPreview?: boolean;
+ aiGateway?: boolean;
sessionTimeline?: boolean;
};
diff --git a/web/src/features/feature-flags/utils.clienttest.ts b/web/src/features/feature-flags/utils.clienttest.ts
index 318c8407c3..7b691ede33 100644
--- a/web/src/features/feature-flags/utils.clienttest.ts
+++ b/web/src/features/feature-flags/utils.clienttest.ts
@@ -37,6 +37,28 @@ describe("parseFlags", () => {
expect(flags.modernSession).toBe(false);
});
+ it("enables the gateway flag only for allowlisted organizations", () => {
+ expect(
+ parseFlags([], {
+ email: "[email protected]",
+ v4BetaEnabled: true,
+ aiGatewayEnabled: true,
+ }).aiGateway,
+ ).toBe(true);
+ expect(
+ parseFlags(["aiGateway"], {
+ email: "[email protected]",
+ v4BetaEnabled: true,
+ }).aiGateway,
+ ).toBe(false);
+ expect(
+ parseFlags(["aiGateway"], {
+ email: "[email protected]",
+ v4BetaEnabled: true,
+ }).aiGateway,
+ ).toBe(false);
+ });
+
it("honors a Langfuse team member's explicit opt-out", () => {
const flags = parseFlags(
[getFeaturePreviewOptOutFlag("modernSession"), "templateFlag"],
diff --git a/web/src/features/feature-flags/utils.ts b/web/src/features/feature-flags/utils.ts
index 1ff3ddc6d5..88ecc7b7e1 100644
--- a/web/src/features/feature-flags/utils.ts
+++ b/web/src/features/feature-flags/utils.ts
@@ -1,6 +1,7 @@
import {
availableFlags,
filterFeaturePreviewFlags,
+ isRestrictedFlag,
isFeaturePreviewFlag,
isFeaturePreviewAvailable,
type FeaturePreviewAvailabilityContext,
@@ -23,6 +24,7 @@ export const parseFlags = (
dbFlags: string[],
context: FeaturePreviewAvailabilityContext & {
email: string | null | undefined;
+ aiGatewayEnabled?: boolean;
},
): Flags => {
const parsedFlags = {} as Flags;
@@ -31,6 +33,11 @@ export const parseFlags = (
);
availableFlags.forEach((flag) => {
+ if (isRestrictedFlag(flag)) {
+ parsedFlags[flag] = context.aiGatewayEnabled === true;
+ return;
+ }
+
if (
isFeaturePreviewFlag(flag) &&
dbFlags.includes(getFeaturePreviewOptOutFlag(flag))
@@ -59,6 +66,7 @@ export const parseFlagsWithOrganizationDefaults = (
organizationDefaults: string[],
context: FeaturePreviewAvailabilityContext & {
email: string | null | undefined;
+ aiGatewayEnabled?: boolean;
},
): Flags => {
const featurePreviewDefaults =
diff --git a/web/src/features/projects/components/DeleteProjectDialog.stories.tsx b/web/src/features/projects/components/DeleteProjectDialog.stories.tsx
index 73d795818a..adf585cb87 100644
--- a/web/src/features/projects/components/DeleteProjectDialog.stories.tsx
+++ b/web/src/features/projects/components/DeleteProjectDialog.stories.tsx
@@ -1,7 +1,7 @@
import { expect, fn, userEvent, within } from "storybook/test";
+import preview from "@/.storybook/preview";
import { Dialog, DialogContent } from "@/src/components/ui/dialog";
-import preview from "../../../../.storybook/preview";
import {
DeleteProjectDialog,
type DeleteProjectDialogProps,
@@ -39,6 +39,15 @@ export const Loading = meta.story({
render: renderDialog,
});
+export const GatewayIngestionProject = meta.story({
+ name: "Gateway ingestion project",
+ args: {
+ blocked: true,
+ onOpenGatewaySettings: fn(),
+ },
+ render: renderDialog,
+});
+
export const ConfirmsDeletion = meta.story({
name: "(Test) Confirms deletion",
args: {
@@ -56,6 +65,7 @@ export const ConfirmsDeletion = meta.story({
);
await userEvent.click(body.getByRole("button", { name: "Delete project" }));
+ if (!("onSubmit" in args)) throw new Error("Expected deletion dialog");
await expect(args.onSubmit).toHaveBeenCalledOnce();
},
});
diff --git a/web/src/features/projects/components/DeleteProjectDialog.tsx b/web/src/features/projects/components/DeleteProjectDialog.tsx
index df8b5a5e63..df21068120 100644
--- a/web/src/features/projects/components/DeleteProjectDialog.tsx
+++ b/web/src/features/projects/components/DeleteProjectDialog.tsx
@@ -1,4 +1,5 @@
import { zodResolver } from "@hookform/resolvers/zod";
+import { ExternalLink } from "lucide-react";
import { useForm } from "react-hook-form";
import * as z from "zod";
@@ -23,20 +24,62 @@ type DeleteProjectForm = {
name: string;
};
-export type DeleteProjectDialogProps = {
+export type DeleteProjectDialogProps =
+ | {
+ blocked: true;
+ onOpenGatewaySettings: () => void;
+ }
+ | {
+ blocked?: false;
+ confirmMessage: string;
+ isPending: boolean;
+ onSubmit: () => void;
+ };
+
+export function DeleteProjectDialog(props: DeleteProjectDialogProps) {
+ return props.blocked ? (
+ <BlockedDeleteProjectDialog
+ onOpenGatewaySettings={props.onOpenGatewaySettings}
+ />
+ ) : (
+ <DeleteProjectConfirmationDialog {...props} />
+ );
+}
+
+function BlockedDeleteProjectDialog({
+ onOpenGatewaySettings,
+}: {
+ onOpenGatewaySettings: () => void;
+}) {
+ return (
+ <>
+ <DialogHeader>
+ <DialogTitle className="text-lg font-bold">
+ Project cannot be deleted
+ </DialogTitle>
+ <DialogDescription>
+ This project is used as the AI Gateway ingestion project. Select
+ another ingestion project before deleting it.
+ </DialogDescription>
+ </DialogHeader>
+ <DialogFooter>
+ <Button className="w-full" onClick={onOpenGatewaySettings}>
+ Open AI Gateway settings
+ <ExternalLink className="relative -top-px ml-1.5 size-3.5 shrink-0" />
+ </Button>
+ </DialogFooter>
+ </>
+ );
+}
+
+function DeleteProjectConfirmationDialog(props: {
confirmMessage: string;
isPending: boolean;
onSubmit: () => void;
-};
-
-export function DeleteProjectDialog({
- confirmMessage,
- isPending,
- onSubmit,
-}: DeleteProjectDialogProps) {
+}) {
const formSchema = z.object({
- name: z.string().includes(confirmMessage, {
- message: `Please confirm with "${confirmMessage}"`,
+ name: z.string().includes(props.confirmMessage, {
+ message: `Please confirm with "${props.confirmMessage}"`,
}),
});
@@ -52,11 +95,11 @@ export function DeleteProjectDialog({
<DialogHeader>
<DialogTitle className="text-lg font-bold">Delete Project</DialogTitle>
<DialogDescription>
- {`To confirm, type "${confirmMessage}" in the input box`}
+ {`To confirm, type "${props.confirmMessage}" in the input box`}
</DialogDescription>
</DialogHeader>
<Form {...form}>
- <form onSubmit={form.handleSubmit(onSubmit)}>
+ <form onSubmit={form.handleSubmit(props.onSubmit)}>
<DialogBody>
<FormField
control={form.control}
@@ -64,7 +107,7 @@ export function DeleteProjectDialog({
render={({ field }) => (
<FormItem>
<FormControl>
- <Input placeholder={confirmMessage} {...field} />
+ <Input placeholder={props.confirmMessage} {...field} />
</FormControl>
<FormMessage />
</FormItem>
@@ -75,7 +118,7 @@ export function DeleteProjectDialog({
<Button
type="submit"
variant="destructive"
- loading={isPending}
+ loading={props.isPending}
className="w-full"
>
Delete project
diff --git a/web/src/features/projects/components/DeleteProjectDialogController.tsx b/web/src/features/projects/components/DeleteProjectDialogController.tsx
index 3bca0e53ee..9f0f6ab640 100644
--- a/web/src/features/projects/components/DeleteProjectDialogController.tsx
+++ b/web/src/features/projects/components/DeleteProjectDialogController.tsx
@@ -1,4 +1,5 @@
import { type ReactNode } from "react";
+import { useRouter } from "next/router";
import { DialogController } from "@/src/components/ui/dialog";
import { env } from "@/src/env.mjs";
@@ -18,6 +19,7 @@ type DeleteProjectDialogControllerProps = {
export function DeleteProjectDialogController({
children,
}: DeleteProjectDialogControllerProps) {
+ const router = useRouter();
const capture = usePostHogClientCapture();
const { project, organization } = useQueryProject();
const confirmMessage = `${organization?.name}/${project?.name}`
@@ -27,6 +29,10 @@ export function DeleteProjectDialogController({
projectId: project?.id,
scope: "project:delete",
});
+ const deletionProtection = api.projects.deletionProtection.useQuery(
+ { projectId: project?.id ?? "" },
+ { enabled: Boolean(project?.id) && hasAccess },
+ );
const deleteProject = api.projects.delete.useMutation();
const handleDelete = () => {
@@ -47,15 +53,31 @@ export function DeleteProjectDialogController({
<DialogController
closeOnInteractionOutside={false}
size="default"
- renderContent={() => (
- <DeleteProjectDialog
- confirmMessage={confirmMessage}
- isPending={deleteProject.isPending}
- onSubmit={handleDelete}
- />
- )}
+ renderContent={() =>
+ deletionProtection.data?.isGatewayIngestionProject && organization ? (
+ <DeleteProjectDialog
+ blocked
+ onOpenGatewaySettings={() =>
+ router.push(
+ `/organization/${organization.id}/settings/ai-gateway`,
+ )
+ }
+ />
+ ) : (
+ <DeleteProjectDialog
+ confirmMessage={confirmMessage}
+ isPending={deleteProject.isPending}
+ onSubmit={handleDelete}
+ />
+ )
+ }
>
- {({ openDialog }) => children({ hasAccess, openDialog })}
+ {({ openDialog }) =>
+ children({
+ hasAccess: hasAccess && !deletionProtection.isLoading,
+ openDialog,
+ })
+ }
</DialogController>
);
}
diff --git a/web/src/features/projects/server/index.ts b/web/src/features/projects/server/index.ts
new file mode 100644
index 0000000000..ada68cf8e6
--- /dev/null
+++ b/web/src/features/projects/server/index.ts
@@ -0,0 +1 @@
+export { projectsRouter } from "@/src/features/projects/server/projectsRouter";
diff --git a/web/src/features/projects/server/projectsRouter.ts b/web/src/features/projects/server/projectsRouter.ts
index 18d380c747..73b3db72f7 100644
--- a/web/src/features/projects/server/projectsRouter.ts
+++ b/web/src/features/projects/server/projectsRouter.ts
@@ -21,7 +21,8 @@ import {
invalidateCachedOrgApiKeys,
} from "@langfuse/shared/src/server";
import { randomUUID } from "crypto";
-import { StringNoHTMLNonEmpty } from "@langfuse/shared";
+import { LangfuseConflictError, StringNoHTMLNonEmpty } from "@langfuse/shared";
+import type { PrismaClient } from "@langfuse/shared/src/db";
import { buildAdminOrgContext } from "@/src/features/organizations/server/adminOrgContext";
import { emitChbProjectEvent } from "@/src/ee/features/billing/server/chb/chbProjectEvents";
@@ -179,6 +180,23 @@ export const projectsRouter = createTRPCRouter({
return true;
}),
+ deletionProtection: protectedProjectProcedure
+ .input(z.object({ projectId: z.string() }))
+ .query(async ({ input, ctx }) => {
+ throwIfNoProjectAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "project:delete",
+ });
+ return {
+ isGatewayIngestionProject: await isGatewayIngestionProject({
+ prisma: ctx.prisma,
+ organizationId: ctx.session.orgId,
+ projectId: input.projectId,
+ }),
+ };
+ }),
+
delete: protectedProjectProcedure
.input(
z.object({
@@ -191,6 +209,11 @@ export const projectsRouter = createTRPCRouter({
projectId: ctx.session.projectId,
scope: "project:delete",
});
+ await throwIfGatewayIngestionProject({
+ prisma: ctx.prisma,
+ organizationId: ctx.session.orgId,
+ projectId: input.projectId,
+ });
// API keys need to be deleted from cache. Otherwise, they will still be valid.
await new ApiAuthService(
@@ -371,3 +394,30 @@ export const projectsRouter = createTRPCRouter({
return { project, organization };
}),
});
+
+async function isGatewayIngestionProject(params: {
+ prisma: PrismaClient;
+ organizationId: string;
+ projectId: string;
+}) {
+ const config = await params.prisma.gatewayConfig.findFirst({
+ where: {
+ organizationId: params.organizationId,
+ defaultIngestionProjectId: params.projectId,
+ },
+ select: { organizationId: true },
+ });
+ return config !== null;
+}
+
+async function throwIfGatewayIngestionProject(params: {
+ prisma: PrismaClient;
+ organizationId: string;
+ projectId: string;
+}) {
+ if (await isGatewayIngestionProject(params)) {
+ throw new LangfuseConflictError(
+ "This project is used as the AI Gateway ingestion project. Select another ingestion project before deleting it.",
+ );
+ }
+}
diff --git a/web/src/features/public-api/server/RateLimitService.ts b/web/src/features/public-api/server/RateLimitService.ts
index bfbe86650b..39c4863d39 100644
--- a/web/src/features/public-api/server/RateLimitService.ts
+++ b/web/src/features/public-api/server/RateLimitService.ts
@@ -10,7 +10,7 @@ import {
} from "@langfuse/shared";
import {
recordIncrement,
- type ApiAccessScope,
+ type ApiAccessScopeWithOptionalApiKeyId,
logger,
createNewRedisInstance,
redisQueueRetryOptions,
@@ -78,7 +78,7 @@ export class RateLimitService {
}
async rateLimitRequest(
- scope: ApiAccessScope,
+ scope: ApiAccessScopeWithOptionalApiKeyId,
resource: z.infer<typeof RateLimitResource>,
) {
// if cloud config is not present, we don't apply rate limits and just return
@@ -99,7 +99,7 @@ export class RateLimitService {
}
async checkRateLimit(
- scope: ApiAccessScope,
+ scope: ApiAccessScopeWithOptionalApiKeyId,
resource: z.infer<typeof RateLimitResource>,
) {
const effectiveConfig = getRateLimitConfig(scope, resource);
@@ -233,7 +233,7 @@ export const createHttpHeaderFromRateLimit = (res: RateLimitResult) => {
};
const getRateLimitConfig = (
- scope: ApiAccessScope,
+ scope: ApiAccessScopeWithOptionalApiKeyId,
resource: z.infer<typeof RateLimitResource>,
) => {
const planBasedConfig = getPlanBasedRateLimitConfig(scope.plan, resource);
diff --git a/web/src/features/public-api/server/apiAuth.ts b/web/src/features/public-api/server/apiAuth.ts
index ffddded3aa..5a79334377 100644
--- a/web/src/features/public-api/server/apiAuth.ts
+++ b/web/src/features/public-api/server/apiAuth.ts
@@ -154,7 +154,6 @@ export class ApiAuthService {
logger.info("No project id found for key", publicKey);
throw new Error("Invalid credentials");
}
-
const plan = finalApiKey.plan;
if (!isPlan(plan)) {
diff --git a/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts b/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts
index 098ba459b5..af272f0221 100644
--- a/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts
+++ b/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts
@@ -1,6 +1,7 @@
import { type NextApiRequest, type NextApiResponse } from "next";
import { type ZodType, type z } from "zod";
import {
+ type ApiAccessScopeWithOptionalApiKeyId,
type AuthHeaderValidVerificationResult,
traceException,
logger,
@@ -44,6 +45,7 @@ import {
type ProjectAction,
} from "@/src/features/auth/policy/types";
import { prisma } from "@langfuse/shared/src/db";
+import { verifyGatewayIngestionAuthorization } from "@/src/features/ai-gateway/server";
// Next's res.json uses JSON.stringify; V8 throws this when the JSON string
// exceeds the engine limit. Keep this check scoped to the response write.
@@ -93,6 +95,11 @@ export type AuthedProjectAPIRouteConfig<
* Only set this to true on non-mutating (GET) routes that should be callable by the in-app agent.
*/
allowInAppAgentKey?: boolean;
+ /**
+ * Accept a short-lived gateway ingestion JWT in addition to normal project
+ * credentials. This must only be enabled on gateway ingestion boundaries.
+ */
+ allowGatewayIngestionToken?: boolean;
/**
* When true, this route returns 404 if LANGFUSE_MIGRATION_V4_WRITE_MODE is
* "events_only". Set this on routes that read from the legacy traces,
@@ -118,6 +125,14 @@ export type AuthedProjectAPIRouteConfig<
export async function verifyAuth(
params: VerifyAuthParams,
): Promise<VerifyAuthResult> {
+ if (params.allowGatewayIngestionToken) {
+ const gatewayAuth = await verifyGatewayIngestionAuthorization(
+ params.req.headers.authorization,
+ params.req.headers["langfuse-gateway-authorization"],
+ );
+ if (gatewayAuth) return gatewayAuth;
+ }
+
// enforce mode runs only the new pipeline, which is the sole authority.
if (env.API_AUTH_MIGRATION === "enforce") {
const authz = await runNewAuth(params);
@@ -268,11 +283,16 @@ export type VerifyAuthParams = {
isAdminApiKeyAuthAllowed?: boolean;
allowedAccessLevels?: RouteAccessLevel[];
allowInAppAgentKey?: boolean;
+ allowGatewayIngestionToken?: boolean;
};
/** VerifyAuthResult is the verified project scope the route handler receives. */
-type VerifyAuthResult = AuthHeaderValidVerificationResult & {
- scope: { projectId: string; accessLevel: RouteAccessLevel };
+type VerifyAuthResult = {
+ validKey: true;
+ scope: ApiAccessScopeWithOptionalApiKeyId & {
+ projectId: string;
+ accessLevel: RouteAccessLevel;
+ };
};
/** ApiKeyPrincipal is the api-key variant of `Principal` the mapper consumes. */
@@ -316,9 +336,7 @@ export const createAuthedProjectAPIRoute = <
return;
}
- let auth: AuthHeaderValidVerificationResult & {
- scope: { projectId: string; accessLevel: RouteAccessLevel };
- };
+ let auth: VerifyAuthResult;
// Verify authentication (API key or admin API key)
try {
@@ -329,6 +347,8 @@ export const createAuthedProjectAPIRoute = <
isAdminApiKeyAuthAllowed: routeConfig.isAdminApiKeyAuthAllowed || false,
allowedAccessLevels: routeConfig.allowedAccessLevels || ["project"],
allowInAppAgentKey: routeConfig.allowInAppAgentKey === true,
+ allowGatewayIngestionToken:
+ routeConfig.allowGatewayIngestionToken === true,
});
} catch (error: any) {
if (isPrismaException(error)) {
diff --git a/web/src/features/rbac/constants/organizationAccessRights.ts b/web/src/features/rbac/constants/organizationAccessRights.ts
index a4ba13012d..844d4aa863 100644
--- a/web/src/features/rbac/constants/organizationAccessRights.ts
+++ b/web/src/features/rbac/constants/organizationAccessRights.ts
@@ -6,6 +6,8 @@ export const organizationScopes = [
"organization:CRUD_apiKeys",
"organization:update",
"organization:delete",
+ "gateway:manage",
+ "gateway:invoke",
"organizationMembers:read",
"organizationMembers:CUD",
"langfuseCloudBilling:CRUD",
@@ -22,6 +24,8 @@ export const organizationRoleAccessRights: Record<Role, OrganizationScope[]> = {
"organization:CRUD_apiKeys",
"organization:update",
"organization:delete",
+ "gateway:manage",
+ "gateway:invoke",
"organizationMembers:CUD",
"organizationMembers:read",
"langfuseCloudBilling:CRUD",
@@ -31,11 +35,13 @@ export const organizationRoleAccessRights: Record<Role, OrganizationScope[]> = {
"projects:create",
"projects:transfer_org",
"organization:update",
+ "gateway:manage",
+ "gateway:invoke",
"organizationMembers:CUD",
"organizationMembers:read",
"orgAuditLogs:read",
],
- MEMBER: ["organizationMembers:read"],
+ MEMBER: ["gateway:invoke", "organizationMembers:read"],
VIEWER: [],
NONE: [],
};
diff --git a/web/src/features/rbac/utils/checkAccess.clienttest.ts b/web/src/features/rbac/utils/checkAccess.clienttest.ts
index 2bff8863a5..22394517e0 100644
--- a/web/src/features/rbac/utils/checkAccess.clienttest.ts
+++ b/web/src/features/rbac/utils/checkAccess.clienttest.ts
@@ -72,6 +72,20 @@ describe("RBAC access checks", () => {
).toBe(true);
});
+ it("allows organization members to invoke the gateway", () => {
+ for (const role of ["OWNER", "ADMIN", "MEMBER"] as const) {
+ expect(hasOrganizationAccess({ role, scope: "gateway:invoke" })).toBe(
+ true,
+ );
+ }
+
+ for (const role of ["VIEWER", "NONE"] as const) {
+ expect(hasOrganizationAccess({ role, scope: "gateway:invoke" })).toBe(
+ false,
+ );
+ }
+ });
+
it("allows members to manage llm tools", () => {
expect(
hasProjectAccess({
diff --git a/web/src/pages/api/internal/ai-gateway/v1/models.ts b/web/src/pages/api/internal/ai-gateway/v1/models.ts
new file mode 100644
index 0000000000..9b61b5f355
--- /dev/null
+++ b/web/src/pages/api/internal/ai-gateway/v1/models.ts
@@ -0,0 +1,6 @@
+import {
+ gatewayModelsApiHandler,
+ withGatewayModelsSignatureVerification,
+} from "@/src/features/ai-gateway/server";
+
+export default withGatewayModelsSignatureVerification(gatewayModelsApiHandler);
diff --git a/web/src/pages/api/internal/ai-gateway/v1/resolve.ts b/web/src/pages/api/internal/ai-gateway/v1/resolve.ts
new file mode 100644
index 0000000000..174620c459
--- /dev/null
+++ b/web/src/pages/api/internal/ai-gateway/v1/resolve.ts
@@ -0,0 +1,14 @@
+import {
+ gatewayResolveApiHandler,
+ withGatewayResolveSignatureVerification,
+} from "@/src/features/ai-gateway/server";
+
+export default withGatewayResolveSignatureVerification(
+ gatewayResolveApiHandler,
+);
+
+export const config = {
+ api: {
+ bodyParser: false,
+ },
+};
diff --git a/web/src/pages/api/public/otel/v1/traces/index.ts b/web/src/pages/api/public/otel/v1/traces/index.ts
index 59e4bf8fe2..a54e44eab3 100644
--- a/web/src/pages/api/public/otel/v1/traces/index.ts
+++ b/web/src/pages/api/public/otel/v1/traces/index.ts
@@ -30,6 +30,7 @@ export default withMiddlewares({
querySchema: z.any(),
responseSchema: z.any(),
rateLimitResource: "ingestion",
+ allowGatewayIngestionToken: true,
fn: async ({ req, res, auth }) => {
// Check if ingestion is suspended due to usage threshold
if (auth.scope.isIngestionSuspended) {
diff --git a/web/src/pages/organization/[organizationId]/settings/index.tsx b/web/src/pages/organization/[organizationId]/settings/index.tsx
index 2986f63dec..52d9292d0f 100644
--- a/web/src/pages/organization/[organizationId]/settings/index.tsx
+++ b/web/src/pages/organization/[organizationId]/settings/index.tsx
@@ -23,10 +23,18 @@ import { OrgAuditLogsSettingsPage } from "@/src/ee/features/audit-log-viewer/Org
import { useHasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
import { useV4UpgradeUiFlag } from "@/src/features/v4-migration/useV4UpgradeUiEnabled";
import { OrganizationFeaturePreviewsSettings } from "@/src/features/feature-flags/components/OrganizationFeaturePreviewsSettings";
+import useIsFeatureEnabled from "@/src/features/feature-flags/hooks/useIsFeatureEnabled";
+import {
+ GatewayApiKeysPage,
+ GatewayConfigurationPage,
+ GatewayModelsPage,
+ GatewayProvidersPage,
+} from "@/src/features/ai-gateway";
type OrganizationSettingsPage = {
title: string;
slug: string;
+ section?: string;
show?: boolean | (() => boolean);
cmdKKeywords?: string[];
} & ({ content: React.ReactNode } | { href: string });
@@ -45,10 +53,18 @@ export function useOrganizationSettingsPages(): OrganizationSettingsPage[] {
organizationId: organization?.id,
scope: "organization:update",
});
+ const canManageGateway = useHasOrganizationAccess({
+ organizationId: organization?.id,
+ scope: "gateway:manage",
+ });
const plan = usePlan();
const isLangfuseCloud = isCloudPlan(plan) ?? false;
const isCloudBillingAvailable = useIsCloudBillingAvailable();
const showV4Migration = useV4UpgradeUiFlag();
+ const isAiGatewayEnabled = useIsFeatureEnabled("aiGateway", {
+ enableForAdmins: false,
+ organizationId: organization?.id,
+ });
if (!organization) return [];
@@ -59,6 +75,7 @@ export function useOrganizationSettingsPages(): OrganizationSettingsPage[] {
showAuditLogs,
isLangfuseCloud,
showV4Migration,
+ showAiGateway: canManageGateway && isAiGatewayEnabled,
showFeaturePreviews:
canUpdateOrganization && organization.id !== env.NEXT_PUBLIC_DEMO_ORG_ID,
});
@@ -71,19 +88,31 @@ export const getOrganizationSettingsPages = ({
showAuditLogs,
isLangfuseCloud,
showV4Migration,
+ showAiGateway,
showFeaturePreviews,
}: {
- organization: { id: string; name: string; metadata: Record<string, unknown> };
+ organization: {
+ id: string;
+ name: string;
+ metadata: Record<string, unknown>;
+ projects: Array<{
+ id: string;
+ name: string;
+ deletedAt?: Date | string | null;
+ }>;
+ };
showBillingSettings: boolean;
showOrgApiKeySettings: boolean;
showAuditLogs: boolean;
isLangfuseCloud: boolean;
showV4Migration: boolean;
+ showAiGateway: boolean;
showFeaturePreviews: boolean;
}): OrganizationSettingsPage[] => [
{
title: "General",
slug: "index",
+ section: "Organization",
cmdKKeywords: ["name", "id", "delete"],
content: (
<div className="flex flex-col gap-6">
@@ -131,6 +160,7 @@ export const getOrganizationSettingsPages = ({
{
title: "Feature Previews",
slug: "feature-previews",
+ section: "Organization",
cmdKKeywords: ["feature", "preview", "flags", "beta"],
content: <OrganizationFeaturePreviewsSettings orgId={organization.id} />,
show: showFeaturePreviews,
@@ -138,6 +168,7 @@ export const getOrganizationSettingsPages = ({
{
title: "API Keys",
slug: "api-keys",
+ section: "Organization",
content: (
<div className="flex flex-col gap-6">
<ApiKeyList entityId={organization.id} scope="organization" />
@@ -148,6 +179,7 @@ export const getOrganizationSettingsPages = ({
{
title: "Members",
slug: "members",
+ section: "Organization",
cmdKKeywords: ["invite", "user", "rbac"],
content: (
<div className="flex flex-col gap-6">
@@ -164,6 +196,7 @@ export const getOrganizationSettingsPages = ({
{
title: "Audit Logs",
slug: "audit-logs",
+ section: "Organization",
cmdKKeywords: ["audit", "logs", "history", "changes"],
content: <OrgAuditLogsSettingsPage orgId={organization.id} />,
show: showAuditLogs,
@@ -171,6 +204,7 @@ export const getOrganizationSettingsPages = ({
{
title: "Billing",
slug: "billing",
+ section: "Organization",
cmdKKeywords: ["payment", "subscription", "plan", "invoice"],
content: <BillingSettings />,
show: showBillingSettings,
@@ -178,6 +212,7 @@ export const getOrganizationSettingsPages = ({
{
title: "SSO",
slug: "sso",
+ section: "Organization",
cmdKKeywords: [
"sso",
"login",
@@ -196,14 +231,59 @@ export const getOrganizationSettingsPages = ({
{
title: "Projects",
slug: "projects",
+ section: "Organization",
href: `/organization/${organization.id}`,
},
{
title: "v4 Migration",
slug: "v4-migration",
+ section: "Organization",
href: "/v4-migration",
show: showV4Migration,
},
+ {
+ title: "Configuration",
+ slug: "ai-gateway",
+ section: "AI Gateway",
+ cmdKKeywords: ["gateway", "llm", "configuration", "instrumentation"],
+ content: (
+ <GatewayConfigurationPage
+ organizationId={organization.id}
+ projects={organization.projects}
+ />
+ ),
+ show: showAiGateway,
+ },
+ {
+ title: "Provider credentials",
+ slug: "ai-gateway-providers",
+ section: "AI Gateway",
+ cmdKKeywords: [
+ "gateway",
+ "providers",
+ "credentials",
+ "openai",
+ "anthropic",
+ ],
+ content: <GatewayProvidersPage organizationId={organization.id} />,
+ show: showAiGateway,
+ },
+ {
+ title: "Models",
+ slug: "ai-gateway-models",
+ section: "AI Gateway",
+ cmdKKeywords: ["gateway", "models", "discovery", "sync"],
+ content: <GatewayModelsPage organizationId={organization.id} />,
+ show: showAiGateway,
+ },
+ {
+ title: "Gateway API keys",
+ slug: "ai-gateway-api-keys",
+ section: "AI Gateway",
+ cmdKKeywords: ["gateway", "api", "keys", "metadata", "credentials"],
+ content: <GatewayApiKeysPage organizationId={organization.id} />,
+ show: showAiGateway,
+ },
];
const OrgSettingsPage = () => {
@@ -219,10 +299,13 @@ const OrgSettingsPage = () => {
headerProps={{
title: "Organization Settings",
}}
+ extendRight={router.query.page === "ai-gateway-models"}
+ fullHeight={router.query.page === "ai-gateway-models"}
>
<PagedSettingsContainer
activeSlug={page as string | undefined}
pages={pages}
+ fullHeight={router.query.page === "ai-gateway-models"}
/>
</ContainerPage>
);
diff --git a/web/src/server/api/root.ts b/web/src/server/api/root.ts
index 79a1fdd10b..1fa131c52b 100644
--- a/web/src/server/api/root.ts
+++ b/web/src/server/api/root.ts
@@ -5,7 +5,7 @@ import { eventsRouter } from "@/src/features/events/server/eventsRouter";
import { scoresRouter } from "./routers/scores";
import { scoreAnalyticsRouter } from "@/src/features/score-analytics/server/scoreAnalyticsRouter";
import { dashboardRouter } from "@/src/features/dashboard/server/dashboard-router";
-import { projectsRouter } from "@/src/features/projects/server/projectsRouter";
+import { projectsRouter } from "@/src/features/projects/server";
import { projectApiKeysRouter } from "@/src/features/public-api/server/projectApiKeyRouter";
import { membersRouter } from "@/src/features/rbac/server/membersRouter";
import { userRouter } from "@/src/server/api/routers/users";
@@ -62,6 +62,7 @@ import { onboardingRouter } from "@/src/features/onboarding/server/onboardingRou
import { webCalloutsRouter } from "@/src/features/web-callouts/server/router";
import { inAppAgentRouter } from "@/src/features/in-app-agent/server/router";
import { v4TransitionRouter } from "@/src/features/v4/server/v4TransitionRouter";
+import { aiGatewayRouter } from "@/src/features/ai-gateway/server";
/**
* This is the primary router for your server.
@@ -132,6 +133,7 @@ export const appRouter = createTRPCRouter({
webCallouts: webCalloutsRouter,
inAppAgent: inAppAgentRouter,
v4Transition: v4TransitionRouter,
+ aiGateway: aiGatewayRouter,
});
// export type definition of API
diff --git a/web/src/server/api/trpc.ts b/web/src/server/api/trpc.ts
index 49f8cfa1a9..8ca8a7b023 100644
--- a/web/src/server/api/trpc.ts
+++ b/web/src/server/api/trpc.ts
@@ -478,6 +478,10 @@ export const protectedOrganizationProcedure = withOtelTracingProcedure
.use(withErrorHandling)
.use(enforceIsAuthedAndOrgMember);
+export const protectedOrganizationProcedureWithoutTracing = t.procedure
+ .use(withErrorHandling)
+ .use(enforceIsAuthedAndOrgMember);
+
/*
* Protect trace-level getter routes.
* - Users need to be member of the project to access the trace.
diff --git a/web/src/server/auth.ts b/web/src/server/auth.ts
index 801498fc63..45f8758757 100644
--- a/web/src/server/auth.ts
+++ b/web/src/server/auth.ts
@@ -15,6 +15,7 @@ import {
parseFlags,
parseFlagsWithOrganizationDefaults,
} from "@/src/features/feature-flags/utils";
+import { isGatewayEnabledForOrganization } from "@/src/features/ai-gateway/server/availability";
import { env } from "@/src/env.mjs";
import { createProjectMembershipsOnSignup } from "@/src/features/auth/lib/createProjectMembershipsOnSignup";
import { getSessionLoginAt } from "@/src/features/auth/lib/sessionExpiration";
@@ -930,6 +931,9 @@ export async function getAuthOptions(signupAttribution?: {
{
email: dbUser.email,
v4BetaEnabled,
+ aiGatewayEnabled: isGatewayEnabledForOrganization(
+ orgMembership.organization.id,
+ ),
},
),
cloudConfig: parsedCloudConfig.data,
diff --git a/web/src/server/utils/hmac.servertest.ts b/web/src/server/utils/hmac.servertest.ts
new file mode 100644
index 0000000000..a3ecf6a5eb
--- /dev/null
+++ b/web/src/server/utils/hmac.servertest.ts
@@ -0,0 +1,42 @@
+import { describe, expect, it } from "vitest";
+
+import { signHmacSha256, verifyHmacSha256 } from "@/src/server/utils/hmac";
+
+describe("HMAC-SHA256 utilities", () => {
+ it("signs messages as lowercase hexadecimal", () => {
+ expect(signHmacSha256("message", "secret")).toBe(
+ "8b5f48702995c1598c573db1e21866a9b825d4a794d169d7060a03605796360b",
+ );
+ });
+
+ it("verifies signatures against current and previous secrets", () => {
+ const signature = signHmacSha256("message", "previous-secret");
+
+ expect(
+ verifyHmacSha256({
+ message: "message",
+ signature,
+ secrets: ["current-secret", "previous-secret"],
+ }),
+ ).toBe(true);
+ });
+
+ it("rejects changed messages and malformed signatures", () => {
+ const signature = signHmacSha256("message", "secret");
+
+ expect(
+ verifyHmacSha256({
+ message: "changed-message",
+ signature,
+ secrets: ["secret"],
+ }),
+ ).toBe(false);
+ expect(
+ verifyHmacSha256({
+ message: "message",
+ signature: `${signature}=`,
+ secrets: ["secret"],
+ }),
+ ).toBe(false);
+ });
+});
diff --git a/web/src/server/utils/hmac.ts b/web/src/server/utils/hmac.ts
new file mode 100644
index 0000000000..8b01b98115
--- /dev/null
+++ b/web/src/server/utils/hmac.ts
@@ -0,0 +1,21 @@
+import { createHmac, timingSafeEqual } from "node:crypto";
+
+export function signHmacSha256(message: string, secret: string): string {
+ return createHmac("sha256", secret).update(message, "utf8").digest("hex");
+}
+
+export function verifyHmacSha256(input: {
+ message: string;
+ signature: string;
+ secrets: string[];
+}): boolean {
+ const signature = Buffer.from(input.signature, "utf8");
+
+ return input.secrets.some((secret) => {
+ const expected = Buffer.from(signHmacSha256(input.message, secret), "utf8");
+ return (
+ signature.length === expected.length &&
+ timingSafeEqual(signature, expected)
+ );
+ });
+}
diff --git a/web/src/server/utils/jwt.servertest.ts b/web/src/server/utils/jwt.servertest.ts
new file mode 100644
index 0000000000..45c18ae54f
--- /dev/null
+++ b/web/src/server/utils/jwt.servertest.ts
@@ -0,0 +1,212 @@
+import { generateKeyPairSync } from "node:crypto";
+
+import { describe, expect, it } from "vitest";
+import { z } from "zod/v4";
+
+import {
+ createEs256JwtSigner,
+ createEs256JwtVerifier,
+ type JwtRegisteredClaims,
+} from "@/src/server/utils/jwt";
+
+const TestClaimsSchema = z.object({
+ subjectId: z.string().min(1),
+ role: z.enum(["reader", "writer"]),
+ iss: z.string(),
+ aud: z.string(),
+ exp: z.number().int(),
+ iat: z.number().int(),
+ jti: z.string(),
+});
+
+type TestClaims = z.infer<typeof TestClaimsSchema> & JwtRegisteredClaims;
+
+function generatePemKeyPair() {
+ const { privateKey, publicKey } = generateKeyPairSync("ec", {
+ namedCurve: "P-256",
+ });
+ return {
+ privateKey: privateKey.export({ format: "pem", type: "pkcs8" }).toString(),
+ publicKey: publicKey.export({ format: "pem", type: "spki" }).toString(),
+ };
+}
+
+const now = new Date("2026-09-07T12:00:00.000Z");
+
+function signToken(input: {
+ privateKey: string;
+ keyId: string;
+ claims?: Record<string, unknown>;
+ issuer?: string;
+ audience?: string;
+}) {
+ return createEs256JwtSigner({
+ privateKey: input.privateKey,
+ keyId: input.keyId,
+ issuer: input.issuer ?? "test-issuer",
+ audience: input.audience ?? "test-audience",
+ }).sign({
+ expiresInSeconds: 60,
+ now,
+ claims: input.claims ?? { subjectId: "subject-1", role: "reader" },
+ });
+}
+
+function verifyToken(
+ token: string,
+ publicKeys: Array<{ id: string; publicKey: string }>,
+ overrides: Partial<{
+ issuer: string;
+ audience: string;
+ now: Date;
+ }> = {},
+): TestClaims {
+ return createEs256JwtVerifier({
+ publicKeys,
+ claimsSchema: TestClaimsSchema,
+ issuer: overrides.issuer ?? "test-issuer",
+ audience: overrides.audience ?? "test-audience",
+ }).verify({
+ token,
+ now: overrides.now ?? now,
+ });
+}
+
+describe("ES256 JWT utilities", () => {
+ it("emits the JWS ES256 header and fixed-width signature", () => {
+ const key = generatePemKeyPair();
+ const token = signToken({ ...key, keyId: "current" });
+ const [encodedHeader, , encodedSignature] = token.split(".");
+
+ expect(
+ JSON.parse(Buffer.from(encodedHeader, "base64url").toString("utf8")),
+ ).toEqual({ alg: "ES256", typ: "JWT", kid: "current" });
+ expect(Buffer.from(encodedSignature, "base64url")).toHaveLength(64);
+ });
+
+ it("rejects signing keys on other EC curves", () => {
+ const { privateKey } = generateKeyPairSync("ec", {
+ namedCurve: "P-384",
+ });
+ const pem = privateKey.export({ format: "pem", type: "pkcs8" }).toString();
+
+ expect(() =>
+ createEs256JwtSigner({
+ privateKey: pem,
+ keyId: "current",
+ issuer: "test-issuer",
+ audience: "test-audience",
+ }),
+ ).toThrow("JWT signing keys must use the P-256 curve");
+ });
+
+ it("reuses constructed signer and verifier keys across operations", () => {
+ const key = generatePemKeyPair();
+ const signer = createEs256JwtSigner({
+ privateKey: key.privateKey.replaceAll("\n", "\\n"),
+ keyId: "current",
+ issuer: "test-issuer",
+ audience: "test-audience",
+ });
+ const verifier = createEs256JwtVerifier({
+ publicKeys: [
+ {
+ id: "current",
+ publicKey: key.publicKey.replaceAll("\n", "\\n"),
+ },
+ ],
+ issuer: "test-issuer",
+ audience: "test-audience",
+ claimsSchema: TestClaimsSchema,
+ });
+
+ for (const subjectId of ["subject-1", "subject-2"]) {
+ const claims = verifier.verify({
+ token: signer.sign({
+ expiresInSeconds: 60,
+ now,
+ claims: { subjectId, role: "reader" },
+ }),
+ now,
+ });
+
+ expect(claims.subjectId).toBe(subjectId);
+ }
+ });
+
+ it("selects current and previous verification keys by kid", () => {
+ const current = generatePemKeyPair();
+ const previous = generatePemKeyPair();
+ const publicKeys = [
+ { id: "current", publicKey: current.publicKey },
+ { id: "previous", publicKey: previous.publicKey },
+ ];
+
+ const currentClaims = verifyToken(
+ signToken({ ...current, keyId: "current" }),
+ publicKeys,
+ );
+ const previousClaims = verifyToken(
+ signToken({ ...previous, keyId: "previous" }),
+ publicKeys,
+ );
+
+ expect(currentClaims).toMatchObject({
+ subjectId: "subject-1",
+ role: "reader",
+ iss: "test-issuer",
+ aud: "test-audience",
+ });
+ expect(previousClaims.subjectId).toBe("subject-1");
+ expect(currentClaims.exp - currentClaims.iat).toBe(60);
+ expect(currentClaims.jti).toEqual(expect.any(String));
+ });
+
+ it("rejects unknown keys and invalid signatures", () => {
+ const trusted = generatePemKeyPair();
+ const untrusted = generatePemKeyPair();
+
+ expect(() =>
+ verifyToken(signToken({ ...trusted, keyId: "missing" }), [
+ { id: "current", publicKey: trusted.publicKey },
+ ]),
+ ).toThrow("Unknown JWT signing key");
+
+ expect(() =>
+ verifyToken(signToken({ ...untrusted, keyId: "current" }), [
+ { id: "current", publicKey: trusted.publicKey },
+ ]),
+ ).toThrow("Invalid JWT signature");
+ });
+
+ it("rejects expired tokens and mismatched issuer or audience", () => {
+ const key = generatePemKeyPair();
+ const token = signToken({ ...key, keyId: "current" });
+ const publicKeys = [{ id: "current", publicKey: key.publicKey }];
+
+ expect(() =>
+ verifyToken(token, publicKeys, {
+ now: new Date(now.getTime() + 60_000),
+ }),
+ ).toThrow("Invalid JWT claims");
+ expect(() =>
+ verifyToken(token, publicKeys, { issuer: "other-issuer" }),
+ ).toThrow("Invalid JWT claims");
+ expect(() =>
+ verifyToken(token, publicKeys, { audience: "other-audience" }),
+ ).toThrow("Invalid JWT claims");
+ });
+
+ it("validates application claims with the caller-provided schema", () => {
+ const key = generatePemKeyPair();
+ const token = signToken({
+ ...key,
+ keyId: "current",
+ claims: { subjectId: "", role: "admin" },
+ });
+
+ expect(() =>
+ verifyToken(token, [{ id: "current", publicKey: key.publicKey }]),
+ ).toThrow();
+ });
+});
diff --git a/web/src/server/utils/jwt.ts b/web/src/server/utils/jwt.ts
new file mode 100644
index 0000000000..2ca2fc0aa0
--- /dev/null
+++ b/web/src/server/utils/jwt.ts
@@ -0,0 +1,197 @@
+import {
+ createPrivateKey,
+ createPublicKey,
+ type KeyObject,
+ randomUUID,
+ sign,
+ verify,
+} from "node:crypto";
+
+import { z } from "zod/v4";
+
+export type JwtRegisteredClaims = {
+ iss: string;
+ aud: string;
+ exp: number;
+ iat: number;
+ jti: string;
+};
+
+type Es256JwtPublicKey = {
+ id: string;
+ publicKey: KeyObject;
+};
+
+export type Es256JwtSigner = {
+ sign<TClaims extends Record<string, unknown>>(input: {
+ expiresInSeconds: number;
+ claims: TClaims;
+ now?: Date;
+ }): string;
+};
+
+export type Es256JwtVerifier<TClaims extends JwtRegisteredClaims> = {
+ verify(input: { token: string; now?: Date }): TClaims;
+};
+
+// ES256 is ECDSA P-256 with SHA-256:
+// https://www.rfc-editor.org/rfc/rfc7518.html#section-3.4
+// RHEL 9's validated OpenSSL provider approves that combination in Table 6:
+// https://csrc.nist.gov/CSRC/media/projects/cryptographic-module-validation-program/documents/security-policies/140sp4857.pdf
+const Es256JwtHeaderSchema = z.object({
+ alg: z.literal("ES256"),
+ typ: z.literal("JWT"),
+ kid: z.string().min(1),
+});
+
+const JwtRegisteredClaimsSchema = z.object({
+ iss: z.string(),
+ aud: z.string(),
+ exp: z.number().int(),
+ iat: z.number().int(),
+ jti: z.string(),
+});
+
+function encodeJson(value: unknown): string {
+ return Buffer.from(JSON.stringify(value), "utf8").toString("base64url");
+}
+
+function decodeJson(value: string): unknown {
+ return JSON.parse(Buffer.from(value, "base64url").toString("utf8"));
+}
+
+function assertP256Key(key: KeyObject): void {
+ if (
+ key.asymmetricKeyType !== "ec" ||
+ key.asymmetricKeyDetails?.namedCurve !== "prime256v1"
+ ) {
+ throw new Error("JWT signing keys must use the P-256 curve");
+ }
+}
+
+function parsePrivateKey(value: string): KeyObject {
+ const key = createPrivateKey(value.replaceAll("\\n", "\n"));
+ assertP256Key(key);
+ return key;
+}
+
+function parsePublicKey(value: string): KeyObject {
+ const key = createPublicKey(value.replaceAll("\\n", "\n"));
+ assertP256Key(key);
+ return key;
+}
+
+export function createEs256JwtSigner(input: {
+ privateKey: string;
+ keyId: string;
+ issuer: string;
+ audience: string;
+}): Es256JwtSigner {
+ const privateKey = parsePrivateKey(input.privateKey);
+
+ return {
+ sign: (request) =>
+ signEs256Jwt({
+ ...request,
+ privateKey,
+ keyId: input.keyId,
+ issuer: input.issuer,
+ audience: input.audience,
+ }),
+ };
+}
+
+export function createEs256JwtVerifier<
+ TClaims extends JwtRegisteredClaims,
+>(input: {
+ publicKeys: Array<{ id: string; publicKey: string }>;
+ issuer: string;
+ audience: string;
+ claimsSchema: z.ZodType<TClaims>;
+}): Es256JwtVerifier<TClaims> {
+ const publicKeys = input.publicKeys.map((key) => ({
+ id: key.id,
+ publicKey: parsePublicKey(key.publicKey),
+ }));
+
+ return {
+ verify: (request) =>
+ verifyEs256Jwt({
+ ...request,
+ publicKeys,
+ issuer: input.issuer,
+ audience: input.audience,
+ claimsSchema: input.claimsSchema,
+ }),
+ };
+}
+
+function signEs256Jwt<TClaims extends Record<string, unknown>>(input: {
+ privateKey: KeyObject;
+ keyId: string;
+ issuer: string;
+ audience: string;
+ expiresInSeconds: number;
+ claims: TClaims;
+ now?: Date;
+}): string {
+ const issuedAt = Math.floor((input.now ?? new Date()).getTime() / 1000);
+ const header = encodeJson({
+ alg: "ES256",
+ typ: "JWT",
+ kid: input.keyId,
+ });
+ const payload = encodeJson({
+ ...input.claims,
+ iat: issuedAt,
+ exp: issuedAt + input.expiresInSeconds,
+ iss: input.issuer,
+ aud: input.audience,
+ jti: randomUUID(),
+ });
+ const signingInput = `${header}.${payload}`;
+ const signature = sign("sha256", Buffer.from(signingInput, "utf8"), {
+ key: input.privateKey,
+ dsaEncoding: "ieee-p1363",
+ }).toString("base64url");
+
+ return `${signingInput}.${signature}`;
+}
+
+function verifyEs256Jwt<TClaims extends JwtRegisteredClaims>(input: {
+ token: string;
+ issuer: string;
+ audience: string;
+ publicKeys: Es256JwtPublicKey[];
+ claimsSchema: z.ZodType<TClaims>;
+ now?: Date;
+}): TClaims {
+ const parts = input.token.split(".");
+ if (parts.length !== 3) throw new Error("Invalid JWT");
+ const [encodedHeader, encodedPayload, encodedSignature] = parts;
+
+ const header = Es256JwtHeaderSchema.parse(decodeJson(encodedHeader));
+ const key = input.publicKeys.find((candidate) => candidate.id === header.kid);
+ if (!key) throw new Error("Unknown JWT signing key");
+
+ const validSignature = verify(
+ "sha256",
+ Buffer.from(`${encodedHeader}.${encodedPayload}`, "utf8"),
+ { key: key.publicKey, dsaEncoding: "ieee-p1363" },
+ Buffer.from(encodedSignature, "base64url"),
+ );
+ if (!validSignature) throw new Error("Invalid JWT signature");
+
+ const decodedClaims = decodeJson(encodedPayload);
+ const registeredClaims = JwtRegisteredClaimsSchema.parse(decodedClaims);
+ const now = Math.floor((input.now ?? new Date()).getTime() / 1000);
+ if (
+ registeredClaims.iss !== input.issuer ||
+ registeredClaims.aud !== input.audience ||
+ registeredClaims.exp <= now
+ ) {
+ throw new Error("Invalid JWT claims");
+ }
+
+ return input.claimsSchema.parse(decodedClaims);
+}
diff --git a/web/vitest.config.mts b/web/vitest.config.mts
index cf4c678499..64a732e7cc 100644
--- a/web/vitest.config.mts
+++ b/web/vitest.config.mts
@@ -130,11 +130,7 @@ const sharedSourceResolve = {
],
// Runtime source resolves these through shared's node_modules symlinks.
// Dedupe keeps one module identity so mocks registered from web intercept.
- dedupe: [
- "@ag-ui/core",
- "@ag-ui/client",
- "langfuse",
- ],
+ dedupe: ["@ag-ui/core", "@ag-ui/client", "langfuse"],
};
function serverProject(
@@ -280,6 +276,11 @@ export default defineConfig({
globalSetup: ["./src/__tests__/vitest-test-db-setup.ts"],
},
},
+ serverProject(
+ "ai-gateway-e2e-server",
+ ["src/__e2e__/**/ai-gateway.gatewaye2e.{ts,tsx}"],
+ { isolate: true },
+ ),
],
},
});
diff --git a/worker/src/features/in-app-agent/runtime/reasoning.test.ts b/worker/src/features/in-app-agent/runtime/reasoning.test.ts
index 12e070279b..a83abddcba 100644
--- a/worker/src/features/in-app-agent/runtime/reasoning.test.ts
+++ b/worker/src/features/in-app-agent/runtime/reasoning.test.ts
@@ -166,7 +166,7 @@ describe("OpenAI Responses request shape", () => {
modelId: "claude-opus-5",
titleModelId: "gpt-5.6-luna",
apiKey: "sk-test",
- baseURL: "https://llm-gateway.internal/v1",
+ baseURL: "https://ai-gateway.internal/v1",
};
const { calls, fetch } = createCaptureFetch(OPENAI_RESPONSES_RESPONSE);
vi.stubGlobal("fetch", fetch);
@@ -187,7 +187,7 @@ describe("OpenAI Responses request shape", () => {
}),
);
- expect(calls[0]?.url).toBe("https://llm-gateway.internal/v1/responses");
+ expect(calls[0]?.url).toBe("https://ai-gateway.internal/v1/responses");
expect(calls[0]?.body.input).toEqual([
{
role: "system",
@@ -249,7 +249,7 @@ describe("OpenAI Responses request shape", () => {
modelId: "claude-opus-5",
titleModelId: "gpt-5.6-luna",
apiKey: "sk-test",
- baseURL: "https://llm-gateway.internal/v1",
+ baseURL: "https://ai-gateway.internal/v1",
};
const { calls, fetch } = createCaptureFetch(OPENAI_RESPONSES_RESPONSE);
vi.stubGlobal("fetch", fetch);
@@ -282,7 +282,7 @@ describe("OpenAI Responses request shape", () => {
modelId: "custom-chat-model",
titleModelId: "custom-chat-model",
apiKey: "sk-test",
- baseURL: "https://llm-gateway.internal/v1",
+ baseURL: "https://ai-gateway.internal/v1",
};
const { calls, fetch } = createCaptureFetch(OPENAI_RESPONSES_RESPONSE);
vi.stubGlobal("fetch", fetch);