🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
chore(billing): read cycle usage from db instead of clickhouse for hobby plan users (#9619)
Michael Fröhlichbe00006ae275
diff --git a/web/src/ee/features/billing/components/BillingUsageChart.tsx b/web/src/ee/features/billing/components/BillingUsageChart.tsxindex 45dd24fe0d..e0395381ff 100644--- a/web/src/ee/features/billing/components/BillingUsageChart.tsx+++ b/web/src/ee/features/billing/components/BillingUsageChart.tsx@@ -46,7 +46,7 @@ export const BillingUsageChart = () => {<><p className="text-sm text-muted-foreground">{usage.data.billingPeriod- ? `${usageType} in current billing period`+ ? `${usageType} in current billing period (updated about once every 60 minutes)`: `${usageType} / last 30d`}</p><div className="text-3xl font-bold">diff --git a/web/src/ee/features/billing/server/stripeBillingService.ts b/web/src/ee/features/billing/server/stripeBillingService.tsindex 60efaaa642..dbd31ce4df 100644--- a/web/src/ee/features/billing/server/stripeBillingService.ts+++ b/web/src/ee/features/billing/server/stripeBillingService.ts@@ -6,10 +6,9 @@ import { env } from "@/src/env.mjs";import { parseDbOrg } from "@langfuse/shared";import {- getTraceCountOfProjectsSinceCreationDate,- getObservationCountOfProjectsSinceCreationDate,- getScoreCountOfProjectsSinceCreationDate,logger,+ getBillingCycleStart,+ getBillingCycleEnd,} from "@langfuse/shared/src/server";import {@@ -259,16 +258,23 @@ class BillingService {async getSubscriptionInfo(orgId: string): Promise<BillingSubscriptionInfo> {const client = this.stripe;- const { parsedOrg } = await this.getParsedOrg(orgId);+ const { org, parsedOrg } = await this.getParsedOrg(orgId);const subscriptionId = parsedOrg.cloudConfig?.stripe?.activeSubscriptionId;if (!subscriptionId) {- // No active subscription → nothing scheduled+ // No active subscription (Hobby plan) → calculate billing period from cached data+ const now = new Date();+ const billingCycleStart = getBillingCycleStart(org, now);+ const billingCycleEnd = getBillingCycleEnd(org, now);+return {cancellation: null,scheduledChange: null,- billingPeriod: null,+ billingPeriod: {+ start: billingCycleStart,+ end: billingCycleEnd,+ },hasValidPaymentMethod: false,};}@@ -1550,7 +1556,6 @@ class BillingService {billingPeriod,};} catch (e) {- // Fail softly and fallback to Clickhouselogger.error("Failed to get usage from Stripe, using usage from Clickhouse",{ error: e },@@ -1558,35 +1563,25 @@ class BillingService {}}// [A] End ------------------------------------------------------------------------------------------ // [B] We have no active subscription -> get usage from Clickhouse for past 30 days (likely hobby plan or fallback)+ // [B] We have no active subscription -> get usage from cached Organization data (likely hobby plan or fallback)// ------------------------------------------------------------------------------------------------- const thirtyDaysAgo = new Date();- thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);- thirtyDaysAgo.setHours(0, 0, 0, 0);-- const projectIds = org.projects.map((p) => p.id);-- const [countTraces, countObservations, countScores] = await Promise.all([- getTraceCountOfProjectsSinceCreationDate({- projectIds,- start: thirtyDaysAgo,- }),- getObservationCountOfProjectsSinceCreationDate({- projectIds,- start: thirtyDaysAgo,- }),- getScoreCountOfProjectsSinceCreationDate({- projectIds,- start: thirtyDaysAgo,- }),- ]);++ // Use cached usage data populated by background job (runs every 60 minutes)+ const cachedUsage = org.cloudCurrentCycleUsage ?? 0;++ // Calculate billing period using the billing cycle anchor+ const now = new Date();+ const billingCycleStart = getBillingCycleStart(org, now);+ const billingCycleEnd = getBillingCycleEnd(org, now);return {- usageCount: countTraces + countObservations + countScores,+ usageCount: cachedUsage,usageType: "units",+ billingPeriod: {+ start: billingCycleStart,+ end: billingCycleEnd,+ },};-// [B] End ----------------------------------------------------------------------------------------}