Skip to content

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

feat(ai-gateway): let LANGFUSE_AI_GATEWAY_PUBLIC_URL override the gateway URL shown in web (#19234)

Tobias Wochinger4642a0898cb7
diff --git a/.env.dev.example b/.env.dev.example
index 7969e54685..ffb09ecdf1 100644
--- a/.env.dev.example
+++ b/.env.dev.example
@@ -242,6 +242,9 @@ ENCRYPTION_KEY=0000000000000000000000000000000000000000000000000000000000000000
# Unset enables a deployed gateway for every organization, except on Langfuse Cloud;
# false limits it to organizations in the allowlist.
# LANGFUSE_AI_GATEWAY_ENABLED=
+# Public gateway root URL (http(s) origin, no path) shown in the UI; unset derives it from NEXTAUTH_URL
+# (localhost:8080 locally).
+# LANGFUSE_AI_GATEWAY_PUBLIC_URL=
# LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST=
# LANGFUSE_AI_GATEWAY_SERVICE_KEY=
# LANGFUSE_AI_GATEWAY_SERVICE_KEY_PREVIOUS=
diff --git a/.env.prod.example b/.env.prod.example
index a72025b89f..0e8826ccb2 100644
--- a/.env.prod.example
+++ b/.env.prod.example
@@ -616,6 +616,10 @@ LANGFUSE_ENABLE_BLOB_STORAGE_FILE_LOG=true
# Web only: enable a deployed gateway for every organization (the default). When
# false, only organizations in LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST can use it.
# LANGFUSE_AI_GATEWAY_ENABLED=true
+# Web only: public gateway root URL shown to users, e.g. your gateway ingress host.
+# An http(s) origin: no credentials, path, query or fragment; a trailing slash is
+# dropped. Unset derives `gateway.<NEXTAUTH_URL host>`.
+# LANGFUSE_AI_GATEWAY_PUBLIC_URL=https://gateway.langfuse.example.com
# Web only: ES256 (P-256) private key, PKCS#8 or SEC1 PEM, that signs gateway
# ingestion tokens. The public key and key ID default to values derived from it.
# Set the key ID on every Web deployment or on none, so all of them agree on it.
diff --git a/web/src/env.mjs b/web/src/env.mjs
index 1bc8fc6740..dcc60f0932 100644
--- a/web/src/env.mjs
+++ b/web/src/env.mjs
@@ -377,6 +377,28 @@ export const env = createEnv({
// organizations in LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST can use it.
// Unset means true, except on Langfuse Cloud where it means false.
LANGFUSE_AI_GATEWAY_ENABLED: z.enum(["true", "false"]).optional(),
+ // Public gateway root URL shown to users. Unset derives it from NEXTAUTH_URL.
+ // Path prefixes are rejected because the setup instructions use the origin.
+ LANGFUSE_AI_GATEWAY_PUBLIC_URL: z
+ .url({ protocol: /^https?$/ })
+ .refine(
+ (value) => {
+ // z.url() already reports unparsable values; refine still runs on them.
+ if (!URL.canParse(value)) return true;
+ const url = new URL(value);
+ return (
+ !url.username &&
+ !url.password &&
+ url.pathname === "/" &&
+ !/[?#]/.test(value)
+ );
+ },
+ {
+ message:
+ "LANGFUSE_AI_GATEWAY_PUBLIC_URL must be an http(s) origin without credentials, path, query or fragment",
+ },
+ )
+ .optional(),
LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST: z
.string()
.optional()
@@ -1161,6 +1183,7 @@ export const env = createEnv({
process.env.LANGFUSE_AI_GATEWAY_SERVICE_KEY_PREVIOUS,
LANGFUSE_AI_GATEWAY_DEPLOYED: process.env.LANGFUSE_AI_GATEWAY_DEPLOYED,
LANGFUSE_AI_GATEWAY_ENABLED: process.env.LANGFUSE_AI_GATEWAY_ENABLED,
+ LANGFUSE_AI_GATEWAY_PUBLIC_URL: process.env.LANGFUSE_AI_GATEWAY_PUBLIC_URL,
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,
diff --git a/web/src/features/ai-gateway/fns/gatewayUrls/getGatewayBaseUrl.ts b/web/src/features/ai-gateway/fns/gatewayUrls/getGatewayBaseUrl.ts
index 00be73f1d4..c0762647e8 100644
--- a/web/src/features/ai-gateway/fns/gatewayUrls/getGatewayBaseUrl.ts
+++ b/web/src/features/ai-gateway/fns/gatewayUrls/getGatewayBaseUrl.ts
@@ -7,7 +7,15 @@ const PREVIEW_HOST = /^pr-(\d+)\.preview\.langfuse\.com$/;
/** Staging serves the gateway on the product host under `/openai/v1` and `/anthropic`. */
const STAGING_ORIGIN = "https://staging.langfuse.com";
-export function getGatewayBaseUrl(productBaseUrl: URL) {
+/**
+ * Returns the gateway URL shown to users. A configured `publicUrl` wins and is
+ * returned without trailing slashes; otherwise it is derived from the product URL.
+ */
+export function getGatewayBaseUrl(productBaseUrl: URL, publicUrl?: string) {
+ if (publicUrl) {
+ return new URL(publicUrl).toString().replace(/\/+$/, "");
+ }
+
if (productBaseUrl.origin === STAGING_ORIGIN) {
return STAGING_ORIGIN;
}
diff --git a/web/src/features/ai-gateway/server/router.ts b/web/src/features/ai-gateway/server/router.ts
index c57d502b06..89076e2857 100644
--- a/web/src/features/ai-gateway/server/router.ts
+++ b/web/src/features/ai-gateway/server/router.ts
@@ -146,7 +146,10 @@ export const aiGatewayRouter = createTRPCRouter({
GatewayConnectionStatus.ENABLED,
);
return {
- gatewayBaseUrl: getGatewayBaseUrl(getProductBaseUrl()),
+ gatewayBaseUrl: getGatewayBaseUrl(
+ getProductBaseUrl(),
+ env.LANGFUSE_AI_GATEWAY_PUBLIC_URL,
+ ),
providers: [
...new Set(connections.map((connection) => connection.provider)),
],