Skip to content

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

fix(billing): fix self-hosted billing error (#9578)

Michael Fröhlichaedee34611fd
diff --git a/web/src/components/nav/app-sidebar.tsx b/web/src/components/nav/app-sidebar.tsx
index 2dfc2b697c..4ad0dd74d8 100644
--- a/web/src/components/nav/app-sidebar.tsx
+++ b/web/src/components/nav/app-sidebar.tsx
@@ -28,6 +28,7 @@ import { UsageTracker } from "@/src/ee/features/billing/components/UsageTracker"
import { type RouteGroup } from "@/src/components/layouts/routes";
import { ExternalLink, Grid2X2 } from "lucide-react";
import { useLangfuseCloudRegion } from "@/src/features/organizations/hooks";
+import { useIsCloudBillingAvailable } from "@/src/ee/features/billing/utils/isCloudBilling";
type AppSidebarProps = {
navItems: {
@@ -47,6 +48,7 @@ export function AppSidebar({
userNavProps,
...props
}: AppSidebarProps) {
+ const isCloudBillingAvailable = useIsCloudBillingAvailable();
return (
<Sidebar collapsible="icon" variant="sidebar" {...props}>
<SidebarHeader>
@@ -60,7 +62,7 @@ export function AppSidebar({
<NavMain items={navItems} />
<div className="flex-1" />
<div className="flex flex-col gap-2 p-2">
- <UsageTracker />
+ {isCloudBillingAvailable && <UsageTracker />}
<SidebarNotifications />
</div>
<NavMain items={secondaryNavItems} />
diff --git a/web/src/ee/features/billing/components/BillingInvoiceTable.tsx b/web/src/ee/features/billing/components/BillingInvoiceTable.tsx
index e23f2620af..462febc077 100644
--- a/web/src/ee/features/billing/components/BillingInvoiceTable.tsx
+++ b/web/src/ee/features/billing/components/BillingInvoiceTable.tsx
@@ -10,6 +10,7 @@ import { formatLocalIsoDate } from "@/src/components/LocalIsoDate";
import { useEffect, useMemo, useState } from "react";
import { useBillingInformation } from "./useBillingInformation";
+import { useIsCloudBillingAvailable } from "@/src/ee/features/billing/utils/isCloudBilling";
type InvoiceRow = {
id: string;
@@ -30,9 +31,10 @@ type InvoiceRow = {
export function BillingInvoiceTable() {
const { organization } = useBillingInformation();
- const shouldShowTable = Boolean(
- organization?.cloudConfig?.stripe?.customerId,
- );
+ const isCloudBillingAvailable = useIsCloudBillingAvailable();
+ const shouldShowTable =
+ isCloudBillingAvailable &&
+ Boolean(organization?.cloudConfig?.stripe?.customerId);
const [virtualTotal, setVirtualTotal] = useState(9999);
const [paginationState, setPaginationState] = useState<{
diff --git a/web/src/ee/features/billing/components/BillingSettings.tsx b/web/src/ee/features/billing/components/BillingSettings.tsx
index 28ff023a2b..25fd8e9292 100644
--- a/web/src/ee/features/billing/components/BillingSettings.tsx
+++ b/web/src/ee/features/billing/components/BillingSettings.tsx
@@ -12,6 +12,7 @@ import { BillingScheduleNotification } from "./BillingScheduleNotification";
import { BillingInvoiceTable } from "./BillingInvoiceTable";
import { BillingDiscountView } from "./BillingDiscountView";
import { BillingPlanPeriodView } from "@/src/ee/features/billing/components/BillingPlanPeriodView";
+import { useIsCloudBillingAvailable } from "@/src/ee/features/billing/utils/isCloudBilling";
import { SpendAlertsSection } from "./SpendAlerts/SpendAlertsSection";
export const BillingSettings = () => {
@@ -22,11 +23,17 @@ export const BillingSettings = () => {
scope: "langfuseCloudBilling:CRUD",
});
- const entitled = useHasEntitlement("cloud-billing");
+ const isCloudBillingAvailable = useIsCloudBillingAvailable();
+ const isCloudBillingEntitled = useHasEntitlement("cloud-billing");
const isSpendAlertEntitled = useHasEntitlement("cloud-spend-alerts");
+ // Don't render billing settings if cloud billing is not available
+ if (!isCloudBillingAvailable) {
+ return null;
+ }
+
// Handle conditional rendering without early returns
- if (!entitled) {
+ if (!isCloudBillingEntitled) {
return null;
}
diff --git a/web/src/ee/features/billing/components/UsageTracker.tsx b/web/src/ee/features/billing/components/UsageTracker.tsx
index 00215d139c..a88e83ac2a 100644
--- a/web/src/ee/features/billing/components/UsageTracker.tsx
+++ b/web/src/ee/features/billing/components/UsageTracker.tsx
@@ -14,6 +14,7 @@ import { ActionButton } from "@/src/components/ActionButton";
import { cn } from "@/src/utils/tailwind";
import { AlertTriangle } from "lucide-react";
import { useSidebar } from "@/src/components/ui/sidebar";
+import { useIsCloudBillingAvailable } from "@/src/ee/features/billing/utils/isCloudBilling";
export const UsageTracker = () => {
const { organization } = useQueryProjectOrOrganization();
@@ -24,13 +25,18 @@ export const UsageTracker = () => {
scope: "langfuseCloudBilling:CRUD",
});
const { setOpen } = useSidebar();
+ const isCloudBillingAvailable = useIsCloudBillingAvailable();
const usageQuery = api.cloudBilling.getUsage.useQuery(
{
orgId: organization?.id!,
},
{
- enabled: !!organization && hasAccess && hasEntitlement,
+ enabled:
+ !!organization &&
+ hasAccess &&
+ hasEntitlement &&
+ isCloudBillingAvailable,
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
@@ -38,6 +44,11 @@ export const UsageTracker = () => {
},
);
+ // Don't show usage tracker if cloud billing is not available
+ if (!isCloudBillingAvailable) {
+ return null;
+ }
+
if (
usageQuery.isPending ||
!usageQuery.data ||
diff --git a/web/src/ee/features/billing/server/cloudBillingRouter.ts b/web/src/ee/features/billing/server/cloudBillingRouter.ts
index c1e435fc18..5191644095 100644
--- a/web/src/ee/features/billing/server/cloudBillingRouter.ts
+++ b/web/src/ee/features/billing/server/cloudBillingRouter.ts
@@ -11,7 +11,7 @@ import { throwIfNoOrganizationAccess } from "@/src/features/rbac/utils/checkOrga
import { auditLog } from "@/src/features/audit-logs/auditLog";
import { logger } from "@langfuse/shared/src/server";
import { createBillingServiceFromContext } from "./stripeBillingService";
-import { env } from "@/src/env.mjs";
+import { isCloudBillingEnabled } from "../utils/isCloudBilling";
export const cloudBillingRouter = createTRPCRouter({
getSubscriptionInfo: protectedOrganizationProcedure
@@ -33,6 +33,20 @@ export const cloudBillingRouter = createTRPCRouter({
session: ctx.session,
});
+ // Return null for non-cloud environments to avoid 500 errors
+ if (!isCloudBillingEnabled()) {
+ logger.info(
+ "cloudBilling.getSubscriptionInfo called in non-cloud environment, returning null",
+ { orgId: input.orgId },
+ );
+ return {
+ cancellation: null,
+ scheduledChange: null,
+ billingPeriod: null,
+ hasValidPaymentMethod: false,
+ };
+ }
+
const res = await createBillingServiceFromContext(
ctx,
).getSubscriptionInfo(input.orgId);
@@ -58,6 +72,14 @@ export const cloudBillingRouter = createTRPCRouter({
orgId: input.orgId,
});
+ if (!isCloudBillingEnabled()) {
+ throw new TRPCError({
+ code: "PRECONDITION_FAILED",
+ message:
+ "Cloud billing is not available in this environment. This feature requires NEXT_PUBLIC_LANGFUSE_CLOUD_REGION to be configured.",
+ });
+ }
+
const stripeBillingService = createBillingServiceFromContext(ctx);
const url = await stripeBillingService.createCheckoutSession(
input.orgId,
@@ -94,6 +116,14 @@ export const cloudBillingRouter = createTRPCRouter({
orgId: input.orgId,
});
+ if (!isCloudBillingEnabled()) {
+ throw new TRPCError({
+ code: "PRECONDITION_FAILED",
+ message:
+ "Cloud billing is not available in this environment. This feature requires NEXT_PUBLIC_LANGFUSE_CLOUD_REGION to be configured.",
+ });
+ }
+
const stripeBillingService = createBillingServiceFromContext(ctx);
await stripeBillingService.changePlan(input.orgId, input.stripeProductId);
@@ -117,6 +147,14 @@ export const cloudBillingRouter = createTRPCRouter({
orgId: input.orgId,
});
+ if (!isCloudBillingEnabled()) {
+ throw new TRPCError({
+ code: "PRECONDITION_FAILED",
+ message:
+ "Cloud billing is not available in this environment. This feature requires NEXT_PUBLIC_LANGFUSE_CLOUD_REGION to be configured.",
+ });
+ }
+
const stripeBillingService = createBillingServiceFromContext(ctx);
await stripeBillingService.cancel(input.orgId, input.opId);
@@ -142,6 +180,14 @@ export const cloudBillingRouter = createTRPCRouter({
orgId: input.orgId,
});
+ if (!isCloudBillingEnabled()) {
+ throw new TRPCError({
+ code: "PRECONDITION_FAILED",
+ message:
+ "Cloud billing is not available in this environment. This feature requires NEXT_PUBLIC_LANGFUSE_CLOUD_REGION to be configured.",
+ });
+ }
+
const stripeBillingService = createBillingServiceFromContext(ctx);
await stripeBillingService.reactivate(input.orgId, input.opId);
@@ -162,6 +208,14 @@ export const cloudBillingRouter = createTRPCRouter({
orgId: input.orgId,
});
+ if (!isCloudBillingEnabled()) {
+ throw new TRPCError({
+ code: "PRECONDITION_FAILED",
+ message:
+ "Cloud billing is not available in this environment. This feature requires NEXT_PUBLIC_LANGFUSE_CLOUD_REGION to be configured.",
+ });
+ }
+
const stripeBillingService = createBillingServiceFromContext(ctx);
await stripeBillingService.clearPlanSwitchSchedule(
@@ -190,6 +244,14 @@ export const cloudBillingRouter = createTRPCRouter({
session: ctx.session,
});
+ if (!isCloudBillingEnabled()) {
+ logger.info(
+ "cloudBilling.getStripeCustomerPortalUrl called in non-cloud environment, returning null",
+ { orgId: input.orgId },
+ );
+ return null;
+ }
+
try {
return await createBillingServiceFromContext(ctx).getCustomerPortalUrl(
input.orgId,
@@ -230,6 +292,14 @@ export const cloudBillingRouter = createTRPCRouter({
session: ctx.session,
});
+ if (!isCloudBillingEnabled()) {
+ logger.info(
+ "cloudBilling.getInvoices called in non-cloud environment, returning empty",
+ { orgId: input.orgId },
+ );
+ return { invoices: [], hasMore: false, cursors: {} };
+ }
+
try {
return await createBillingServiceFromContext(ctx).getInvoices(
input.orgId,
@@ -273,11 +343,12 @@ export const cloudBillingRouter = createTRPCRouter({
session: ctx.session,
});
- if (
- env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION == "DEV" &&
- !env.STRIPE_SECRET_KEY
- ) {
- logger.warn("STRIPE_SECRET_KEY not set, returning 0 usage");
+ // Return null for non-cloud environments to avoid 500 errors
+ if (!isCloudBillingEnabled()) {
+ logger.info(
+ "cloudBilling.getUsage called in non-cloud environment, returning null",
+ { orgId: input.orgId },
+ );
return null;
}
@@ -305,6 +376,14 @@ export const cloudBillingRouter = createTRPCRouter({
session: ctx.session,
});
+ if (!isCloudBillingEnabled()) {
+ throw new TRPCError({
+ code: "PRECONDITION_FAILED",
+ message:
+ "Cloud billing is not available in this environment. This feature requires NEXT_PUBLIC_LANGFUSE_CLOUD_REGION to be configured.",
+ });
+ }
+
const stripeBillingService = createBillingServiceFromContext(ctx);
const result = await stripeBillingService.applyPromotionCode(
diff --git a/web/src/ee/features/billing/utils/isCloudBilling.ts b/web/src/ee/features/billing/utils/isCloudBilling.ts
new file mode 100644
index 0000000000..203b3163b9
--- /dev/null
+++ b/web/src/ee/features/billing/utils/isCloudBilling.ts
@@ -0,0 +1,28 @@
+import { env } from "@/src/env.mjs";
+import { useLangfuseCloudRegion } from "@/src/features/organizations/hooks";
+
+/**
+ * Server-side check to determine if cloud billing is enabled.
+ * Cloud billing requires NEXT_PUBLIC_LANGFUSE_CLOUD_REGION to be set.
+ *
+ * Use this in server-side code (tRPC routers, API routes).
+ * For client-side components, use the hook useIsCloudBillingAvailable.
+ *
+ * @returns true if cloud billing should be active
+ */
+export function isCloudBillingEnabled(): boolean {
+ return Boolean(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION);
+}
+
+/**
+ * Client-side hook to check if cloud billing features are available.
+ * Uses the NEXT_PUBLIC_LANGFUSE_CLOUD_REGION to determine availability.
+ *
+ * Components should use this to conditionally render billing features.
+ *
+ * @returns true if cloud billing features should be shown/enabled
+ */
+export function useIsCloudBillingAvailable(): boolean {
+ const cloudRegion = useLangfuseCloudRegion();
+ return Boolean(cloudRegion);
+}
diff --git a/web/src/pages/organization/[organizationId]/settings/index.tsx b/web/src/pages/organization/[organizationId]/settings/index.tsx
index 8ebdaed73e..f3971e2d58 100644
--- a/web/src/pages/organization/[organizationId]/settings/index.tsx
+++ b/web/src/pages/organization/[organizationId]/settings/index.tsx
@@ -16,6 +16,7 @@ import { isCloudPlan } from "@langfuse/shared";
import { useQueryProjectOrOrganization } from "@/src/features/projects/hooks";
import { ApiKeyList } from "@/src/features/public-api/components/ApiKeyList";
import AIFeatureSwitch from "@/src/features/organizations/components/AIFeatureSwitch";
+import { useIsCloudBillingAvailable } from "@/src/ee/features/billing/utils/isCloudBilling";
type OrganizationSettingsPage = {
title: string;
@@ -30,12 +31,13 @@ export function useOrganizationSettingsPages(): OrganizationSettingsPage[] {
const showOrgApiKeySettings = useHasEntitlement("admin-api");
const plan = usePlan();
const isLangfuseCloud = isCloudPlan(plan) ?? false;
+ const isCloudBillingAvailable = useIsCloudBillingAvailable();
if (!organization) return [];
return getOrganizationSettingsPages({
organization,
- showBillingSettings,
+ showBillingSettings: showBillingSettings && isCloudBillingAvailable,
showOrgApiKeySettings,
isLangfuseCloud,
});