Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(billing): require auto-recharge for graceful billing (#2786)

firecrawl-spring[bot]b577af5c3fe5
diff --git a/apps/api/src/services/billing/credit_billing.ts b/apps/api/src/services/billing/credit_billing.ts
index 3c6b66a5c..5086f4f55 100644
--- a/apps/api/src/services/billing/credit_billing.ts
+++ b/apps/api/src/services/billing/credit_billing.ts
@@ -107,7 +107,10 @@ async function supaCheckTeamCredits(
}
} catch (error) {
// If organization check fails, continue with normal credit checks
- logger.warn("Organization check failed, continuing with normal credit checks", { team_id, error });
+ logger.warn(
+ "Organization check failed, continuing with normal credit checks",
+ { team_id, error },
+ );
}
// If bypassCreditChecks flag is set, return success with infinite credits (infinitely graceful)
@@ -120,21 +123,6 @@ async function supaCheckTeamCredits(
};
}
- const remainingCredits = chunk.price_should_be_graceful
- ? chunk.remaining_credits + chunk.price_credits
- : chunk.remaining_credits;
-
- const creditsWillBeUsed = chunk.adjusted_credits_used + credits;
-
- // In case chunk.price_credits is undefined, set it to a large number to avoid mistakes
- const totalPriceCredits = chunk.price_should_be_graceful
- ? (chunk.total_credits_sum ?? 100000000) + chunk.price_credits
- : (chunk.total_credits_sum ?? 100000000);
-
- // Removal of + credits
- const creditUsagePercentage =
- chunk.adjusted_credits_used / (chunk.total_credits_sum ?? 100000000);
-
let isAutoRechargeEnabled = false,
autoRechargeThreshold = 1000;
const cacheKey = `team_auto_recharge_${team_id}`;
@@ -157,6 +145,24 @@ async function supaCheckTeamCredits(
}
}
+ // Graceful billing only applies if the plan supports it AND auto-recharge is enabled
+ const allowOverages = chunk.price_should_be_graceful && isAutoRechargeEnabled;
+
+ const remainingCredits = allowOverages
+ ? chunk.remaining_credits + chunk.price_credits
+ : chunk.remaining_credits;
+
+ const creditsWillBeUsed = chunk.adjusted_credits_used + credits;
+
+ // In case chunk.price_credits is undefined, set it to a large number to avoid mistakes
+ const totalPriceCredits = allowOverages
+ ? (chunk.total_credits_sum ?? 100000000) + chunk.price_credits
+ : (chunk.total_credits_sum ?? 100000000);
+
+ // Removal of + credits
+ const creditUsagePercentage =
+ chunk.adjusted_credits_used / (chunk.total_credits_sum ?? 100000000);
+
if (
isAutoRechargeEnabled &&
chunk.remaining_credits < autoRechargeThreshold &&
@@ -175,12 +181,12 @@ async function supaCheckTeamCredits(
return {
success: true,
message: autoChargeResult.message,
- remainingCredits: chunk.price_should_be_graceful
+ remainingCredits: allowOverages
? autoChargeResult.remainingCredits + chunk.price_credits
: autoChargeResult.remainingCredits,
chunk: autoChargeResult.chunk,
};
- } else if (chunk.price_should_be_graceful) {
+ } else if (allowOverages) {
return {
success: true,
message: "Auto-recharge failed, but price should be graceful",
@@ -219,6 +225,7 @@ async function supaCheckTeamCredits(
is_extract: chunk.is_extract,
bypassCreditChecks: chunk.flags?.bypassCreditChecks,
price_should_be_graceful: chunk.price_should_be_graceful,
+ allowOverages,
price_credits: chunk.price_credits,
coupon_credits: chunk.coupon_credits,
total_credits_sum: chunk.total_credits_sum,