Skip to content

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

feat(cloud): alert when free plan limit is approaching or reached (#3350)

Marc Klingen8ea14ac53761
diff --git a/web/src/components/layouts/routes.tsx b/web/src/components/layouts/routes.tsx
index 90f4d97d6b..3f75c07e6e 100644
--- a/web/src/components/layouts/routes.tsx
+++ b/web/src/components/layouts/routes.tsx
@@ -22,6 +22,7 @@ import { type Entitlement } from "@/src/features/entitlements/constants/entitlem
import { type UiCustomizationOption } from "@/src/ee/features/ui-customization/useUiCustomization";
import { type User } from "next-auth";
import { type OrganizationScope } from "@/src/features/rbac/constants/organizationAccessRights";
+import { UsageTracker } from "@/src/ee/features/billing/components/UsageTracker";
export type Route = {
name: string;
@@ -140,6 +141,7 @@ export const ROUTES: Route[] = [
entitlement: "cloud-billing",
organizationRbacScope: "langfuseCloudBilling:CRUD",
show: ({ organization }) => organization?.plan === "cloud:hobby",
+ label: <UsageTracker />,
},
{
name: "Upgrade",
@@ -149,6 +151,7 @@ export const ROUTES: Route[] = [
entitlement: "cloud-billing",
organizationRbacScope: "langfuseCloudBilling:CRUD",
show: ({ organization }) => organization?.plan === "cloud:hobby",
+ label: <UsageTracker />,
},
{
name: "Settings",
diff --git a/web/src/ee/features/billing/components/BillingSettings.tsx b/web/src/ee/features/billing/components/BillingSettings.tsx
index 4cf27c0f98..cc7b3f0e7c 100644
--- a/web/src/ee/features/billing/components/BillingSettings.tsx
+++ b/web/src/ee/features/billing/components/BillingSettings.tsx
@@ -26,6 +26,7 @@ import {
import { env } from "@/src/env.mjs";
import { useHasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
import { Alert, AlertDescription, AlertTitle } from "@/src/components/ui/alert";
+import { MAX_OBSERVATIONS_FREE_PLAN } from "@/src/ee/features/billing/constants";
export const BillingSettings = () => {
const router = useRouter();
@@ -72,7 +73,8 @@ const OrganizationUsageChart = () => {
},
);
const planLimit =
- organization?.cloudConfig?.monthlyObservationLimit ?? 50_000;
+ organization?.cloudConfig?.monthlyObservationLimit ??
+ MAX_OBSERVATIONS_FREE_PLAN;
const plan: Plan = organization?.plan ?? "cloud:hobby";
const planLabel = planLabels[plan];
diff --git a/web/src/ee/features/billing/components/UsageTracker.tsx b/web/src/ee/features/billing/components/UsageTracker.tsx
new file mode 100644
index 0000000000..c422f643d7
--- /dev/null
+++ b/web/src/ee/features/billing/components/UsageTracker.tsx
@@ -0,0 +1,72 @@
+import {
+ HoverCard,
+ HoverCardContent,
+ HoverCardTrigger,
+} from "@/src/components/ui/hover-card";
+import { api } from "@/src/utils/api";
+import { useQueryProjectOrOrganization } from "@/src/features/projects/hooks";
+import { AlertTriangle } from "lucide-react";
+import { cn } from "@/src/utils/tailwind";
+import { useHasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
+import { MAX_OBSERVATIONS_FREE_PLAN } from "@/src/ee/features/billing/constants";
+import { useHasOrgEntitlement } from "@/src/features/entitlements/hooks";
+
+export const UsageTracker = () => {
+ const { organization } = useQueryProjectOrOrganization();
+ const hasEntitlement = useHasOrgEntitlement("cloud-billing");
+ const hasAccess = useHasOrganizationAccess({
+ organizationId: organization?.id,
+ scope: "langfuseCloudBilling:CRUD",
+ });
+
+ const usageQuery = api.cloudBilling.getUsage.useQuery(
+ {
+ orgId: organization?.id!,
+ },
+ {
+ enabled: !!organization && hasAccess && hasEntitlement,
+ refetchOnMount: false,
+ refetchOnReconnect: false,
+ refetchOnWindowFocus: false,
+ },
+ );
+
+ if (
+ usageQuery.isLoading ||
+ !usageQuery.data ||
+ !hasAccess ||
+ !hasEntitlement
+ ) {
+ return null;
+ }
+
+ const usage = usageQuery.data.countObservations || 0;
+ const percentage = (usage / MAX_OBSERVATIONS_FREE_PLAN) * 100;
+
+ if (percentage < 80) {
+ return null;
+ }
+
+ return (
+ <HoverCard>
+ <HoverCardTrigger>
+ <AlertTriangle
+ className={cn(
+ "h-4 w-4",
+ percentage >= 100 ? "text-destructive" : "text-dark-yellow",
+ )}
+ />
+ </HoverCardTrigger>
+ <HoverCardContent className="w-80">
+ <div className="flex justify-between space-x-4">
+ <div className="space-y-1">
+ <h4 className="text-sm font-semibold">Free Plan Usage Limit</h4>
+ <p className="text-sm font-normal">
+ {`You've used ${usage.toLocaleString()} out of ${MAX_OBSERVATIONS_FREE_PLAN.toLocaleString()} included observations (${percentage.toFixed(2)}%) over the last 30 days. Please upgrade your plan to avoid interruptions.`}
+ </p>
+ </div>
+ </div>
+ </HoverCardContent>
+ </HoverCard>
+ );
+};
diff --git a/web/src/ee/features/billing/constants.ts b/web/src/ee/features/billing/constants.ts
new file mode 100644
index 0000000000..330420c009
--- /dev/null
+++ b/web/src/ee/features/billing/constants.ts
@@ -0,0 +1 @@
+export const MAX_OBSERVATIONS_FREE_PLAN = 50_000;