🪢 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
fix(billing): correct metrics for paid vs free tier orgs when enforcement disabled (#9544)
Michael Fröhlich3ce6c5100912
diff --git a/worker/src/ee/usageThresholds/thresholdProcessing.ts b/worker/src/ee/usageThresholds/thresholdProcessing.tsindex 379ac043d8..d7cba18a16 100644--- a/worker/src/ee/usageThresholds/thresholdProcessing.ts+++ b/worker/src/ee/usageThresholds/thresholdProcessing.ts@@ -283,12 +283,8 @@ export async function processThresholds(org: ParsedOrganization,cumulativeUsage: number,): Promise<ThresholdProcessingResult> {- if (env.LANGFUSE_FREE_TIER_USAGE_THRESHOLD_ENFORCEMENT_ENABLED !== "true") {- logger.info(- `[FREE TIER USAGE THRESHOLDS] Enforcement disabled via feature flag for org ${org.id}, tracking usage only`,- );-- // Always track usage even when enforcement is disabled+ // 1. Skip notifications if org is on a paid plan (check this first, regardless of enforcement flag)+ if (org.cloudConfig?.stripe?.activeSubscriptionId) {await prisma.organization.update({where: { id: org.id },data: {@@ -298,15 +294,24 @@ export async function processThresholds(},});+ // If org was previously blocked, invalidate cache+ if (org.cloudFreeTierUsageThresholdState === "BLOCKED") {+ logger.info(+ `[FREE TIER USAGE THRESHOLDS] Org ${org.id} moved to paid plan, was previously blocked, invalidating API key cache`,+ );+ await invalidateCachedOrgApiKeys(org.id);+ }+return {- actionTaken: "ENFORCEMENT_DISABLED",+ actionTaken: "PAID_PLAN",emailSent: false,emailFailed: false,};}- // 1. Skip notifications if org is on a paid plan- if (org.cloudConfig?.stripe?.activeSubscriptionId) {+ // 2. Check if enforcement is enabled (only for free tier orgs)+ if (env.LANGFUSE_FREE_TIER_USAGE_THRESHOLD_ENFORCEMENT_ENABLED !== "true") {+ // Always track usage even when enforcement is disabledawait prisma.organization.update({where: { id: org.id },data: {@@ -316,25 +321,17 @@ export async function processThresholds(},});- // If org was previously blocked, invalidate cache- if (org.cloudFreeTierUsageThresholdState === "BLOCKED") {- logger.info(- `[FREE TIER USAGE THRESHOLDS] Org ${org.id} moved to paid plan, was previously blocked, invalidating API key cache`,- );- await invalidateCachedOrgApiKeys(org.id);- }-return {- actionTaken: "PAID_PLAN",+ actionTaken: "ENFORCEMENT_DISABLED",emailSent: false,emailFailed: false,};}- // 2. Get previous state+ // 3. Get previous stateconst previousState = org.cloudFreeTierUsageThresholdState;- // 3. Determine current state based on cumulative usage (state-based, not transition-based)+ // 4. Determine current state based on cumulative usage (state-based, not transition-based)// This makes the system idempotent and self-healinglet currentState: string | null = null;@@ -347,7 +344,7 @@ export async function processThresholds(currentState = null;}- // 4. Determine if we should send email (only on state transitions)+ // 5. Determine if we should send email (only on state transitions)let emailSent = false;let emailFailed = false;@@ -391,7 +388,7 @@ export async function processThresholds(emailFailed = emailResult.emailFailed;}- // 5. Update last processed usage in DB with current state+ // 6. Update last processed usage in DB with current stateawait prisma.organization.update({where: { id: org.id },data: {@@ -401,7 +398,7 @@ export async function processThresholds(},});- // 6. Invalidate API key cache if blocking state changed+ // 7. Invalidate API key cache if blocking state changedconst blockingStateChanged =(previousState === "BLOCKED" && currentState !== "BLOCKED") ||(previousState !== "BLOCKED" && currentState === "BLOCKED");@@ -413,7 +410,7 @@ export async function processThresholds(await invalidateCachedOrgApiKeys(org.id);}- // 7. Return result for metrics tracking+ // 8. Return result for metrics trackingconst actionTaken =currentState === "BLOCKED"? "BLOCKED"