Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. πŸ”₯

feat(limits): source rate limits/concurrency/priority from Autumn behind a ramp flag (#3599)

firecrawl-spring[bot]76ce8c880be2
diff --git a/apps/api/src/config.ts b/apps/api/src/config.ts
index 63c66a047..773e18282 100644
--- a/apps/api/src/config.ts
+++ b/apps/api/src/config.ts
@@ -85,6 +85,18 @@ const configSchema = z.object({
LLAMAPARSE_API_KEY: z.string().optional(),
STRIPE_SECRET_KEY: z.string().optional(),
AUTUMN_SECRET_KEY: z.string().optional(),
+ // Ramp control for sourcing rate limits / concurrency / job priority from
+ // Autumn instead of ACUC. Two independent knobs (an org is enabled if it
+ // matches either); both default to the old ACUC behavior:
+ // - AUTUMN_LIMITS_ENABLED_ORG_IDS: comma-separated org IDs to always enable
+ // (or "*" for all). Use to pin specific verified orgs on first.
+ // - AUTUMN_LIMITS_EXPERIMENT_PERCENT: stable per-org bucket ramp in [0,100].
+ AUTUMN_LIMITS_ENABLED_ORG_IDS: z.string().default(""),
+ AUTUMN_LIMITS_EXPERIMENT_PERCENT: z.coerce
+ .number()
+ .min(0)
+ .max(100)
+ .default(0),
RESEND_API_KEY: z.string().optional(),
PREVIEW_TOKEN: z.string().optional(),
SEARCH_PREVIEW_TOKEN: z.string().optional(),
diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts
index 9ce2cb5ab..54dd2ed0f 100644
--- a/apps/api/src/controllers/auth.ts
+++ b/apps/api/src/controllers/auth.ts
@@ -6,7 +6,7 @@ import { logger } from "../lib/logger";
import { parseApi } from "../lib/parseApi";
import { withAuth } from "../lib/withAuth";
import { getAgentSponsorStatus } from "../services/agent-sponsor";
-import { getRateLimiter } from "../services/rate-limiter";
+import { getRateLimiter, getAutumnRateLimiter } from "../services/rate-limiter";
import {
KEYLESS_FREE_TIER_LIMIT_MESSAGE,
consumeKeylessRequest,
@@ -27,6 +27,10 @@ import {
} from "../db/rpc";
import { AuthResponse, RateLimiterMode } from "../types";
import { AuthCreditUsageChunk, AuthCreditUsageChunkFromTeam } from "./v1/types";
+import {
+ autumnService,
+ isAutumnLimitsEnabled,
+} from "../services/autumn/autumn.service";
function normalizedApiIsUuid(potentialUuid: string): boolean {
// Check if the string is a valid UUID
@@ -655,11 +659,11 @@ async function supaAuthenticateUser(
}
if (token == config.PREVIEW_TOKEN) {
if (mode == RateLimiterMode.CrawlStatus) {
- rateLimiter = getRateLimiter(RateLimiterMode.CrawlStatus, token);
+ rateLimiter = getRateLimiter(RateLimiterMode.CrawlStatus, null);
} else if (mode == RateLimiterMode.ExtractStatus) {
- rateLimiter = getRateLimiter(RateLimiterMode.ExtractStatus, token);
+ rateLimiter = getRateLimiter(RateLimiterMode.ExtractStatus, null);
} else {
- rateLimiter = getRateLimiter(RateLimiterMode.Preview, token);
+ rateLimiter = getRateLimiter(RateLimiterMode.Preview, null);
}
teamId = `preview_${iptoken}`;
} else if (token.startsWith("fco_")) {
@@ -690,10 +694,21 @@ async function supaAuthenticateUser(
subscriptionData = {
team_id: teamId,
};
- rateLimiter = getRateLimiter(
- mode ?? RateLimiterMode.Crawl,
- chunk.rate_limits,
- );
+ if (isAutumnLimitsEnabled(chunk.org_id)) {
+ const rateLimitMultiplier = await autumnService.getRateLimitMultiplier(
+ teamId,
+ chunk.org_id,
+ );
+ rateLimiter = getAutumnRateLimiter(
+ mode ?? RateLimiterMode.Crawl,
+ rateLimitMultiplier,
+ );
+ } else {
+ rateLimiter = getRateLimiter(
+ mode ?? RateLimiterMode.Crawl,
+ chunk.rate_limits,
+ );
+ }
} else {
normalizedApi = parseApi(token);
if (!normalizedApiIsUuid(normalizedApi)) {
@@ -719,10 +734,21 @@ async function supaAuthenticateUser(
subscriptionData = {
team_id: teamId,
};
- rateLimiter = getRateLimiter(
- mode ?? RateLimiterMode.Crawl,
- chunk.rate_limits,
- );
+ if (isAutumnLimitsEnabled(chunk.org_id)) {
+ const rateLimitMultiplier = await autumnService.getRateLimitMultiplier(
+ teamId,
+ chunk.org_id,
+ );
+ rateLimiter = getAutumnRateLimiter(
+ mode ?? RateLimiterMode.Crawl,
+ rateLimitMultiplier,
+ );
+ } else {
+ rateLimiter = getRateLimiter(
+ mode ?? RateLimiterMode.Crawl,
+ chunk.rate_limits,
+ );
+ }
}
if (chunk?.flags?.ipRestriction) {
diff --git a/apps/api/src/controllers/v1/concurrency-check.ts b/apps/api/src/controllers/v1/concurrency-check.ts
index 0824f9e8d..6cc955e72 100644
--- a/apps/api/src/controllers/v1/concurrency-check.ts
+++ b/apps/api/src/controllers/v1/concurrency-check.ts
@@ -5,6 +5,7 @@ import {
} from "./types";
import { Response } from "express";
import { getCombinedTeamActiveCount } from "../../services/worker/nuq-router";
+import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
// Basically just middleware and error wrapping
export async function concurrencyCheckController(
@@ -16,6 +17,10 @@ export async function concurrencyCheckController(
return res.status(200).json({
success: true,
concurrency: activeJobsOfTeam,
- maxConcurrency: req.acuc?.concurrency ?? 0,
+ maxConcurrency: await getEffectiveConcurrencyLimit(
+ req.auth.team_id,
+ req.acuc?.concurrency,
+ req.acuc?.org_id,
+ ),
});
}
diff --git a/apps/api/src/controllers/v1/crawl.ts b/apps/api/src/controllers/v1/crawl.ts
index 877cc43f8..82d863357 100644
--- a/apps/api/src/controllers/v1/crawl.ts
+++ b/apps/api/src/controllers/v1/crawl.ts
@@ -34,6 +34,7 @@ import { checkUrl } from "../../lib/threat-protection";
import { UnsafeDomainBlockedError } from "../../lib/threat-protection/error";
import { calculateThreatScanCredits } from "../../lib/scrape-billing";
import { billTeam } from "../../services/billing/credit_billing";
+import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
export async function crawlController(
req: RequestWithAuth<{}, CrawlResponse, CrawlRequest>,
@@ -211,9 +212,14 @@ export async function crawlController(
createdAt: Date.now(),
maxConcurrency:
req.body.maxConcurrency !== undefined
- ? req.acuc?.concurrency !== undefined
- ? Math.min(req.body.maxConcurrency, req.acuc.concurrency)
- : req.body.maxConcurrency
+ ? Math.min(
+ req.body.maxConcurrency,
+ await getEffectiveConcurrencyLimit(
+ req.auth.team_id,
+ req.acuc?.concurrency,
+ req.acuc?.org_id,
+ ),
+ )
: undefined,
zeroDataRetention,
v1: true,
diff --git a/apps/api/src/controllers/v1/queue-status.ts b/apps/api/src/controllers/v1/queue-status.ts
index 3a4c965bb..4c5956ae3 100644
--- a/apps/api/src/controllers/v1/queue-status.ts
+++ b/apps/api/src/controllers/v1/queue-status.ts
@@ -1,6 +1,4 @@
-import { RateLimiterMode } from "../../types";
-import { getACUCTeam } from "../auth";
-import { AuthCreditUsageChunkFromTeam, RequestWithAuth } from "./types";
+import { RequestWithAuth } from "./types";
import { Response } from "express";
import { redisEvictConnection } from "../../services/redis";
import { isFdbTeam } from "../../services/worker/nuq-router";
@@ -15,6 +13,7 @@ import {
cleanOldConcurrencyLimitEntries,
getConcurrencyLimitActiveJobsCount,
getConcurrencyQueueJobsCount,
+ getEffectiveConcurrencyLimit,
} from "../../lib/concurrency-limit";
type QueueStatusResponse = {
@@ -32,23 +31,6 @@ export async function queueStatusController(
req: RequestWithAuth<{}, undefined, QueueStatusResponse>,
res: Response<QueueStatusResponse>,
) {
- let otherACUC: AuthCreditUsageChunkFromTeam | null = null;
- if (!req.acuc?.is_extract) {
- otherACUC = await getACUCTeam(
- req.auth.team_id,
- false,
- true,
- RateLimiterMode.Extract,
- );
- } else {
- otherACUC = await getACUCTeam(
- req.auth.team_id,
- false,
- true,
- RateLimiterMode.Crawl,
- );
- }
-
await cleanOldConcurrencyLimitEntries(req.auth.team_id);
let activeJobsOfTeam = await getConcurrencyLimitActiveJobsCount(
req.auth.team_id,
@@ -95,9 +77,10 @@ export async function queueStatusController(
jobsInQueue: activeJobsOfTeam + queuedJobsOfTeam,
activeJobsInQueue: activeJobsOfTeam,
waitingJobsInQueue: queuedJobsOfTeam,
- maxConcurrency: Math.max(
- req.acuc?.concurrency ?? 1,
- otherACUC?.concurrency ?? 1,
+ maxConcurrency: await getEffectiveConcurrencyLimit(
+ req.auth.team_id,
+ req.acuc?.concurrency,
+ req.acuc?.org_id,
),
mostRecentSuccess: mostRecentSuccess
diff --git a/apps/api/src/controllers/v1/scrape.ts b/apps/api/src/controllers/v1/scrape.ts
index d5bc60f09..1d76d7d3c 100644
--- a/apps/api/src/controllers/v1/scrape.ts
+++ b/apps/api/src/controllers/v1/scrape.ts
@@ -37,6 +37,7 @@ import {
import { projectScrapeCredits } from "../../lib/keyless-credit-projection";
import { applyAgentAuthDiscoveryHeader } from "../../lib/agent-auth-discovery";
import { resolveThreatProtection } from "../../lib/threat-protection/request";
+import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
export async function scrapeController(
req: RequestWithAuth<{}, ScrapeResponse, ScrapeRequest>,
@@ -189,7 +190,11 @@ export async function scrapeController(
doc = await teamConcurrencySemaphore.withSemaphore(
req.auth.team_id,
jobId,
- req.acuc?.concurrency || 1,
+ await getEffectiveConcurrencyLimit(
+ req.auth.team_id,
+ req.acuc?.concurrency,
+ req.acuc?.org_id,
+ ),
aborter.signal,
timeout ?? 60_000,
async limited => {
diff --git a/apps/api/src/controllers/v2/browser.ts b/apps/api/src/controllers/v2/browser.ts
index efa1404f0..0d461abad 100644
--- a/apps/api/src/controllers/v2/browser.ts
+++ b/apps/api/src/controllers/v2/browser.ts
@@ -22,6 +22,7 @@ import {
mirrorExternalSlotAcquire,
mirrorExternalSlotRelease,
} from "../../services/worker/nuq-router";
+import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
import { RequestWithAuth } from "./types";
import { billTeam } from "../../services/billing/credit_billing";
import { enqueueBrowserSessionActivity } from "../../lib/browser-session-activity";
@@ -256,7 +257,11 @@ export async function browserCreateController(
}
// 0b. Enforce concurrency limit (shared pool with scrape/crawl/interact)
- const concurrencyLimit = req.acuc?.concurrency ?? 2;
+ const concurrencyLimit = await getEffectiveConcurrencyLimit(
+ req.auth.team_id,
+ req.acuc?.concurrency,
+ req.acuc?.org_id,
+ );
const activeCount = await getCombinedTeamActiveCount(req.auth.team_id);
if (activeCount >= concurrencyLimit) {
logger.warn("Concurrency limit reached for browser session", {
diff --git a/apps/api/src/controllers/v2/concurrency-check.ts b/apps/api/src/controllers/v2/concurrency-check.ts
index 36017e8e9..748f83694 100644
--- a/apps/api/src/controllers/v2/concurrency-check.ts
+++ b/apps/api/src/controllers/v2/concurrency-check.ts
@@ -3,11 +3,9 @@ import {
ConcurrencyCheckResponse,
RequestWithAuth,
} from "./types";
-import { AuthCreditUsageChunkFromTeam } from "../v1/types";
import { Response } from "express";
-import { getACUCTeam } from "../auth";
-import { RateLimiterMode } from "../../types";
import { getCombinedTeamActiveCount } from "../../services/worker/nuq-router";
+import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
// Basically just middleware and error wrapping
export async function concurrencyCheckController(
@@ -21,28 +19,15 @@ export async function concurrencyCheckController(
});
}
- let otherACUC: AuthCreditUsageChunkFromTeam | null = null;
- if (!req.acuc.is_extract) {
- otherACUC = await getACUCTeam(
- req.auth.team_id,
- false,
- true,
- RateLimiterMode.Extract,
- );
- } else {
- otherACUC = await getACUCTeam(
- req.auth.team_id,
- false,
- true,
- RateLimiterMode.Crawl,
- );
- }
-
const activeJobsOfTeam = await getCombinedTeamActiveCount(req.auth.team_id);
return res.status(200).json({
success: true,
concurrency: activeJobsOfTeam,
- maxConcurrency: Math.max(req.acuc.concurrency, otherACUC?.concurrency ?? 0),
+ maxConcurrency: await getEffectiveConcurrencyLimit(
+ req.auth.team_id,
+ req.acuc.concurrency,
+ req.acuc.org_id,
+ ),
});
}
diff --git a/apps/api/src/controllers/v2/crawl.ts b/apps/api/src/controllers/v2/crawl.ts
index a0f1f9b1e..efddfb326 100644
--- a/apps/api/src/controllers/v2/crawl.ts
+++ b/apps/api/src/controllers/v2/crawl.ts
@@ -36,6 +36,7 @@ import { checkUrl } from "../../lib/threat-protection";
import { UnsafeDomainBlockedError } from "../../lib/threat-protection/error";
import { calculateThreatScanCredits } from "../../lib/scrape-billing";
import { billTeam } from "../../services/billing/credit_billing";
+import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
export async function crawlController(
req: RequestWithAuth<{}, CrawlResponse, CrawlRequest>,
@@ -256,6 +257,11 @@ export async function crawlController(
originalBodyLimit: preNormalizedBody.limit,
});
+ const effectiveConcurrency = await getEffectiveConcurrencyLimit(
+ req.auth.team_id,
+ req.acuc?.concurrency,
+ req.acuc?.org_id,
+ );
const sc: StoredCrawl = {
originUrl: req.body.url,
crawlerOptions: toV0CrawlerOptions(finalCrawlerOptions),
@@ -273,9 +279,7 @@ export async function crawlController(
createdAt: Date.now(),
maxConcurrency:
req.body.maxConcurrency !== undefined
- ? req.acuc?.concurrency !== undefined
- ? Math.min(req.body.maxConcurrency, req.acuc.concurrency)
- : req.body.maxConcurrency
+ ? Math.min(req.body.maxConcurrency, effectiveConcurrency)
: undefined,
zeroDataRetention,
v1: true,
diff --git a/apps/api/src/controllers/v2/parse.ts b/apps/api/src/controllers/v2/parse.ts
index 9e879c2c3..7b9738914 100644
--- a/apps/api/src/controllers/v2/parse.ts
+++ b/apps/api/src/controllers/v2/parse.ts
@@ -34,6 +34,7 @@ import {
} from "../../lib/keyless";
import { projectScrapeCredits } from "../../lib/keyless-credit-projection";
import { applyAgentAuthDiscoveryHeader } from "../../lib/agent-auth-discovery";
+import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
import path from "node:path";
const AGENT_INTEROP_CONCURRENCY_BOOST = 3;
@@ -448,7 +449,11 @@ export async function parseController(
}
req.on("close", () => aborter.abort());
- const baseConcurrency = req.acuc?.concurrency || 1;
+ const baseConcurrency = await getEffectiveConcurrencyLimit(
+ req.auth.team_id,
+ req.acuc?.concurrency,
+ req.acuc?.org_id,
+ );
const concurrency = boostConcurrency
? baseConcurrency * AGENT_INTEROP_CONCURRENCY_BOOST
: baseConcurrency;
diff --git a/apps/api/src/controllers/v2/queue-status.ts b/apps/api/src/controllers/v2/queue-status.ts
index b71773669..005229a4c 100644
--- a/apps/api/src/controllers/v2/queue-status.ts
+++ b/apps/api/src/controllers/v2/queue-status.ts
@@ -1,7 +1,4 @@
-import { RateLimiterMode } from "../../types";
-import { getACUCTeam } from "../auth";
import { RequestWithAuth } from "./types";
-import { AuthCreditUsageChunkFromTeam } from "../v1/types";
import { Response } from "express";
import { redisEvictConnection } from "../../services/redis";
import { isFdbTeam } from "../../services/worker/nuq-router";
@@ -16,6 +13,7 @@ import {
cleanOldConcurrencyLimitEntries,
getConcurrencyLimitActiveJobsCount,
getConcurrencyQueueJobsCount,
+ getEffectiveConcurrencyLimit,
} from "../../lib/concurrency-limit";
type QueueStatusResponse = {
@@ -33,23 +31,6 @@ export async function queueStatusController(
req: RequestWithAuth<{}, undefined, QueueStatusResponse>,
res: Response<QueueStatusResponse>,
) {
- let otherACUC: AuthCreditUsageChunkFromTeam | null = null;
- if (!req.acuc?.is_extract) {
- otherACUC = await getACUCTeam(
- req.auth.team_id,
- false,
- true,
- RateLimiterMode.Extract,
- );
- } else {
- otherACUC = await getACUCTeam(
- req.auth.team_id,
- false,
- true,
- RateLimiterMode.Crawl,
- );
- }
-
await cleanOldConcurrencyLimitEntries(req.auth.team_id);
let activeJobsOfTeam = await getConcurrencyLimitActiveJobsCount(
req.auth.team_id,
@@ -96,9 +77,10 @@ export async function queueStatusController(
jobsInQueue: activeJobsOfTeam + queuedJobsOfTeam,
activeJobsInQueue: activeJobsOfTeam,
waitingJobsInQueue: queuedJobsOfTeam,
- maxConcurrency: Math.max(
- req.acuc?.concurrency ?? 1,
- otherACUC?.concurrency ?? 1,
+ maxConcurrency: await getEffectiveConcurrencyLimit(
+ req.auth.team_id,
+ req.acuc?.concurrency,
+ req.acuc?.org_id,
),
mostRecentSuccess: mostRecentSuccess
diff --git a/apps/api/src/controllers/v2/scrape-browser.ts b/apps/api/src/controllers/v2/scrape-browser.ts
index 58646407c..4a48a6250 100644
--- a/apps/api/src/controllers/v2/scrape-browser.ts
+++ b/apps/api/src/controllers/v2/scrape-browser.ts
@@ -17,7 +17,7 @@ import {
didBrowserSessionUsePrompt,
clearBrowserSessionPromptFlag,
} from "../../lib/browser-sessions";
-import {} from "../../lib/concurrency-limit";
+import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
import {
getCombinedTeamActiveCount,
mirrorExternalSlotAcquire,
@@ -590,7 +590,11 @@ async function createSessionForScrape(
}
// Active session limit β€” uses the same concurrency pool as scrape/crawl
- const concurrencyLimit = req.acuc?.concurrency ?? 2;
+ const concurrencyLimit = await getEffectiveConcurrencyLimit(
+ req.auth.team_id,
+ req.acuc?.concurrency,
+ req.acuc?.org_id,
+ );
const activeCount = await getCombinedTeamActiveCount(req.auth.team_id);
if (activeCount >= concurrencyLimit) {
adjustKeylessCredits(req.auth.team_id, -keylessReserved).catch(() => {});
diff --git a/apps/api/src/controllers/v2/scrape.ts b/apps/api/src/controllers/v2/scrape.ts
index 3ce6e1f77..fb2bd2c7e 100644
--- a/apps/api/src/controllers/v2/scrape.ts
+++ b/apps/api/src/controllers/v2/scrape.ts
@@ -38,6 +38,7 @@ import {
import { projectScrapeCredits } from "../../lib/keyless-credit-projection";
import { applyAgentAuthDiscoveryHeader } from "../../lib/agent-auth-discovery";
import { resolveThreatProtection } from "../../lib/threat-protection/request";
+import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
const AGENT_INTEROP_CONCURRENCY_BOOST = 3;
@@ -264,7 +265,11 @@ export async function scrapeController(
}
req.on("close", () => aborter.abort());
- const baseConcurrency = req.acuc?.concurrency || 1;
+ const baseConcurrency = await getEffectiveConcurrencyLimit(
+ req.auth.team_id,
+ req.acuc?.concurrency,
+ req.acuc?.org_id,
+ );
const concurrency = boostConcurrency
? baseConcurrency * AGENT_INTEROP_CONCURRENCY_BOOST
: baseConcurrency;
diff --git a/apps/api/src/lib/concurrency-limit.ts b/apps/api/src/lib/concurrency-limit.ts
index dd332b478..a81314c5a 100644
--- a/apps/api/src/lib/concurrency-limit.ts
+++ b/apps/api/src/lib/concurrency-limit.ts
@@ -20,6 +20,32 @@ import {
pushConcurrencyLimitActiveJob,
removeConcurrencyLimitActiveJob,
} from "./concurrency-redis";
+import {
+ autumnService,
+ isAutumnLimitsEnabled,
+} from "../services/autumn/autumn.service";
+
+// Legacy fallback when neither ACUC nor Autumn gives us a concurrency value.
+const DEFAULT_CONCURRENCY_LIMIT = 2;
+
+/**
+ * Returns the team's effective concurrency limit. When the Autumn-limits ramp
+ * is enabled for the org, this comes from Autumn's CONCURRENCY balance (falling
+ * back to the ACUC value, then the default, if Autumn can't answer). Otherwise
+ * it's the ACUC value β€” the old, default behavior.
+ */
+export async function getEffectiveConcurrencyLimit(
+ teamId: string,
+ acucConcurrency: number | null | undefined,
+ orgId?: string | null,
+): Promise<number> {
+ const acucValue = acucConcurrency ?? DEFAULT_CONCURRENCY_LIMIT;
+ if (!isAutumnLimitsEnabled(orgId)) {
+ return acucValue;
+ }
+ const autumnValue = await autumnService.getConcurrencyLimit(teamId, orgId);
+ return autumnValue ?? acucValue;
+}
const constructKey = constructConcurrencyLimitKey;
const constructQueueKey = (team_id: string) =>
@@ -304,15 +330,17 @@ export async function concurrentJobDone(job: NuQJob<any>) {
await cleanOldCrawlConcurrencyLimitEntries(job.data.crawl_id);
}
- const maxTeamConcurrency =
- (
- await getACUCTeam(
- job.data.team_id,
- false,
- true,
- job.data.is_extract ? RateLimiterMode.Extract : RateLimiterMode.Crawl,
- )
- )?.concurrency ?? 2;
+ const acuc = await getACUCTeam(
+ job.data.team_id,
+ false,
+ true,
+ job.data.is_extract ? RateLimiterMode.Extract : RateLimiterMode.Crawl,
+ );
+ const maxTeamConcurrency = await getEffectiveConcurrencyLimit(
+ job.data.team_id,
+ acuc?.concurrency,
+ acuc?.org_id,
+ );
let staleSkipped = 0;
while (staleSkipped < 100) {
diff --git a/apps/api/src/lib/concurrency-queue-reconciler.ts b/apps/api/src/lib/concurrency-queue-reconciler.ts
index 2bae7007b..69616fc9e 100644
--- a/apps/api/src/lib/concurrency-queue-reconciler.ts
+++ b/apps/api/src/lib/concurrency-queue-reconciler.ts
@@ -6,6 +6,7 @@ import { scrapeQueue, type NuQJob } from "../services/worker/nuq";
import { RateLimiterMode, type ScrapeJobData } from "../types";
import {
getConcurrencyLimitActiveJobs,
+ getEffectiveConcurrencyLimit,
getNextConcurrentJob,
MAX_BACKLOG_TIMEOUT_MS,
pushConcurrencyLimitActiveJob,
@@ -106,12 +107,27 @@ async function reconcileTeam(
return null;
}
- const maxCrawlConcurrency =
- (await getACUCTeam(ownerId, false, true, RateLimiterMode.Crawl))
- ?.concurrency ?? 2;
- const maxExtractConcurrency =
- (await getACUCTeam(ownerId, false, true, RateLimiterMode.Extract))
- ?.concurrency ?? 2;
+ // Concurrency comes from getEffectiveConcurrencyLimit, which honors the
+ // Autumn-limits ramp per org and otherwise uses ACUC. Fetch the crawl and
+ // extract ACUC values so the ramp-off path keeps its separate per-type
+ // limits; when the ramp is on, both resolve to the single Autumn pool.
+ // TODO: once fully on Autumn, gate crawl + extract against one combined pool
+ // (sum of both active counts vs the single limit) instead of applying the
+ // same limit to each type independently.
+ const [crawlAcuc, extractAcuc] = await Promise.all([
+ getACUCTeam(ownerId, false, true, RateLimiterMode.Crawl),
+ getACUCTeam(ownerId, false, true, RateLimiterMode.Extract),
+ ]);
+ const maxCrawlConcurrency = await getEffectiveConcurrencyLimit(
+ ownerId,
+ crawlAcuc?.concurrency,
+ crawlAcuc?.org_id,
+ );
+ const maxExtractConcurrency = await getEffectiveConcurrencyLimit(
+ ownerId,
+ extractAcuc?.concurrency,
+ extractAcuc?.org_id,
+ );
// Split active count by type so one type's active jobs don't gate the other
const activeJobIds = await getConcurrencyLimitActiveJobs(ownerId);
@@ -201,12 +217,27 @@ async function drainQueue(
ownerId: string,
teamLogger: Logger,
): Promise<{ jobsPromoted: number; staleSkipped: number }> {
- const maxCrawlConcurrency =
- (await getACUCTeam(ownerId, false, true, RateLimiterMode.Crawl))
- ?.concurrency ?? 2;
- const maxExtractConcurrency =
- (await getACUCTeam(ownerId, false, true, RateLimiterMode.Extract))
- ?.concurrency ?? 2;
+ // Concurrency comes from getEffectiveConcurrencyLimit, which honors the
+ // Autumn-limits ramp per org and otherwise uses ACUC. Fetch the crawl and
+ // extract ACUC values so the ramp-off path keeps its separate per-type
+ // limits; when the ramp is on, both resolve to the single Autumn pool.
+ // TODO: once fully on Autumn, gate crawl + extract against one combined pool
+ // (sum of both active counts vs the single limit) instead of applying the
+ // same limit to each type independently.
+ const [crawlAcuc, extractAcuc] = await Promise.all([
+ getACUCTeam(ownerId, false, true, RateLimiterMode.Crawl),
+ getACUCTeam(ownerId, false, true, RateLimiterMode.Extract),
+ ]);
+ const maxCrawlConcurrency = await getEffectiveConcurrencyLimit(
+ ownerId,
+ crawlAcuc?.concurrency,
+ crawlAcuc?.org_id,
+ );
+ const maxExtractConcurrency = await getEffectiveConcurrencyLimit(
+ ownerId,
+ extractAcuc?.concurrency,
+ extractAcuc?.org_id,
+ );
const activeIds = await getConcurrencyLimitActiveJobs(ownerId);
const activeJobs = await scrapeQueue.getJobs(activeIds, teamLogger);
diff --git a/apps/api/src/lib/job-priority.ts b/apps/api/src/lib/job-priority.ts
index 78ad5290c..7c3a24336 100644
--- a/apps/api/src/lib/job-priority.ts
+++ b/apps/api/src/lib/job-priority.ts
@@ -2,6 +2,11 @@ import { RateLimiterMode } from "../types";
import { getACUCTeam } from "../controllers/auth";
import { redisEvictConnection } from "../services/redis";
import { logger } from "./logger";
+import {
+ autumnService,
+ isAutumnLimitsEnabled,
+} from "../services/autumn/autumn.service";
+import { inferPlanPriorityFromMultiplier } from "../services/rate-limiter";
const SET_KEY_PREFIX = "limit_team_id:";
export async function addJobPriority(team_id, job_id) {
@@ -54,9 +59,22 @@ export async function getJobPriority({
// Get the length of the set
const setLength = await redisEvictConnection.scard(setKey);
- // Determine the priority based on the plan and set length
- let planModifier = acuc?.plan_priority.planModifier ?? 1;
- let bucketLimit = acuc?.plan_priority.bucketLimit ?? 25;
+ // When the Autumn-limits ramp is enabled for the org, infer plan priority
+ // from the rate-limit multiplier; otherwise use the ACUC plan_priority (the
+ // old, default behavior).
+ let bucketLimit: number;
+ let planModifier: number;
+ if (isAutumnLimitsEnabled(acuc?.org_id)) {
+ const multiplier = await autumnService.getRateLimitMultiplier(
+ team_id,
+ acuc?.org_id,
+ );
+ ({ bucketLimit, planModifier } =
+ inferPlanPriorityFromMultiplier(multiplier));
+ } else {
+ planModifier = acuc?.plan_priority.planModifier ?? 1;
+ bucketLimit = acuc?.plan_priority.bucketLimit ?? 25;
+ }
// if length set is smaller than set, just return base priority
if (setLength <= bucketLimit) {
diff --git a/apps/api/src/services/autumn/autumn.service.ts b/apps/api/src/services/autumn/autumn.service.ts
index 001c8f1ff..87d5673a8 100644
--- a/apps/api/src/services/autumn/autumn.service.ts
+++ b/apps/api/src/services/autumn/autumn.service.ts
@@ -1,4 +1,5 @@
import { randomUUID } from "crypto";
+import { config } from "../../config";
import { logger } from "../../lib/logger";
import { eq } from "drizzle-orm";
import { dbRr } from "../../db/connection";
@@ -21,6 +22,59 @@ import type {
export const TEAM_FEATURE_ID = "TEAM";
export const CREDITS_FEATURE_ID = "CREDITS";
export const SEARCH_CREDITS_FEATURE_ID = "SEARCH_CREDITS";
+const CONCURRENCY_FEATURE_ID = "CONCURRENCY";
+const RATE_LIMIT_FEATURE_ID = "rate_limits";
+
+// Explicit allowlist of orgs for which rate limits / concurrency / job priority
+// are sourced from Autumn instead of ACUC. Parsed once from
+// AUTUMN_LIMITS_ENABLED_ORG_IDS: comma-separated org IDs, or "*" for all orgs.
+const autumnLimitsAllowlist = new Set(
+ config.AUTUMN_LIMITS_ENABLED_ORG_IDS.split(",")
+ .map(id => id.trim())
+ .filter(Boolean),
+);
+
+/**
+ * Deterministic bucket for an org UUID in [0, 100). Takes the first 8 hex
+ * digits (after stripping dashes) so the same org always lands in the same
+ * bucket β€” the ramp decision is stable across requests, which matters for
+ * rate-limit/concurrency limits that would otherwise flip request-to-request.
+ */
+function orgBucket(orgId: string): number {
+ const hex = orgId.replace(/-/g, "").slice(0, 8);
+ return parseInt(hex, 16) % 100;
+}
+
+/**
+ * Whether to source rate limits / concurrency / job priority from Autumn
+ * (instead of ACUC) for this org. Enabled when the org is in the explicit
+ * allowlist (AUTUMN_LIMITS_ENABLED_ORG_IDS, or "*") OR falls within the stable
+ * percentage ramp (AUTUMN_LIMITS_EXPERIMENT_PERCENT). Defaults to false (old
+ * ACUC behavior) and always requires a configured Autumn client.
+ */
+export function isAutumnLimitsEnabled(orgId?: string | null): boolean {
+ if (!autumnClient) return false;
+ if (autumnLimitsAllowlist.has("*")) return true;
+ if (orgId && autumnLimitsAllowlist.has(orgId)) return true;
+
+ const percent = config.AUTUMN_LIMITS_EXPERIMENT_PERCENT;
+ if (percent <= 0) return false;
+ if (percent >= 100) return true;
+ return !!orgId && orgBucket(orgId) < percent;
+}
+
+/**
+ * Coerces a raw Autumn balance figure into a usable non-negative number, or
+ * null when it's absent or not a sane finite value. These balances feed
+ * directly into rate-limit and concurrency controls, so NaN, Infinity, and
+ * negatives are rejected rather than passed through a bare `typeof` check.
+ */
+function sanitizeBalanceValue(value: unknown): number | null {
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
+ return null;
+ }
+ return value;
+}
/**
* Maps a billing endpoint to the Autumn feature ID it should bill against.
@@ -509,6 +563,122 @@ export class AutumnService {
}
}
+ // Cache the team's entity-derived limits briefly so concurrency enforcement
+ // and rate-limit gating on every scrape/crawl/browser request don't fan out
+ // to Autumn each time. Both the CONCURRENCY limit and the rate-limit
+ // multiplier come from a single entity.get, so one cache entry (and one
+ // Autumn round-trip per team per TTL window) serves both callers.
+ private entityLimitsCache = new BoundedMap<
+ string,
+ {
+ concurrency: number | null;
+ rateLimitMultiplier: number | null;
+ expiresAt: number;
+ }
+ >(50_000);
+ private static readonly ENTITY_LIMITS_TTL_MS = 60_000;
+
+ /**
+ * Fetches the team's Autumn entity once and derives both the CONCURRENCY
+ * limit and the rate-limit multiplier from it. Each team has its own Autumn
+ * entity, so the entity balances are per-team regardless of whether the org
+ * has one or many teams. Returns nulls when Autumn is unavailable, the entity
+ * is missing, or a balance isn't present β€” callers fall back accordingly.
+ */
+ private async getEntityLimits(
+ teamId: string,
+ orgId?: string | null,
+ ): Promise<{
+ concurrency: number | null;
+ rateLimitMultiplier: number | null;
+ }> {
+ if (!autumnClient || this.isPreviewTeam(teamId)) {
+ return { concurrency: null, rateLimitMultiplier: null };
+ }
+
+ const now = Date.now();
+ const cached = this.entityLimitsCache.get(teamId);
+ if (cached && cached.expiresAt > now) {
+ return {
+ concurrency: cached.concurrency,
+ rateLimitMultiplier: cached.rateLimitMultiplier,
+ };
+ }
+
+ const store = (
+ concurrency: number | null,
+ rateLimitMultiplier: number | null,
+ ) => {
+ this.entityLimitsCache.set(teamId, {
+ concurrency,
+ rateLimitMultiplier,
+ expiresAt: now + AutumnService.ENTITY_LIMITS_TTL_MS,
+ });
+ return { concurrency, rateLimitMultiplier };
+ };
+
+ try {
+ const resolvedOrgId = orgId ?? (await this.resolveOrgId(teamId));
+ if (!resolvedOrgId)
+ return { concurrency: null, rateLimitMultiplier: null };
+
+ const entity: any = await autumnClient.entities.get({
+ customerId: resolvedOrgId,
+ entityId: teamId,
+ });
+ const balances = entity?.balances ?? {};
+
+ // CONCURRENCY: use `remaining` (the post-drain effective per-team cap;
+ // `granted` would surface the pre-drain inherited customer total).
+ const concurrency = sanitizeBalanceValue(
+ balances[CONCURRENCY_FEATURE_ID]?.remaining,
+ );
+
+ // rate_limits: a static per-plan multiplier that is never consumed, so
+ // read `granted` (the entitled amount) rather than `remaining`.
+ const rateLimitMultiplier = sanitizeBalanceValue(
+ balances[RATE_LIMIT_FEATURE_ID]?.granted,
+ );
+
+ return store(concurrency, rateLimitMultiplier);
+ } catch (error) {
+ const status = this.getErrorStatus(error);
+ if (status === 404) return store(null, null);
+ logger.error(
+ "Autumn getEntityLimits failed β€” billing API may be unavailable",
+ { teamId, error },
+ );
+ return { concurrency: null, rateLimitMultiplier: null };
+ }
+ }
+
+ /**
+ * Reads the team's allowed concurrent-browser count from Autumn's
+ * entity-scoped CONCURRENCY balance. Returns null when Autumn is unavailable,
+ * the entity is missing, or there's no balance β€” callers fall back to the
+ * default via getEffectiveConcurrencyLimit.
+ */
+ async getConcurrencyLimit(
+ teamId: string,
+ orgId?: string | null,
+ ): Promise<number | null> {
+ return (await this.getEntityLimits(teamId, orgId)).concurrency;
+ }
+
+ /**
+ * Reads the team's rate-limit multiplier from Autumn's `rate_limits` feature.
+ * Effective rate limits are `base Γ— multiplier`. Falls back to a multiplier of
+ * 1 when Autumn is unavailable or the feature is missing, so callers don't
+ * have to. Shares a single cached entity fetch with getConcurrencyLimit, so it
+ * adds no Autumn call.
+ */
+ async getRateLimitMultiplier(
+ teamId: string,
+ orgId?: string | null,
+ ): Promise<number> {
+ return (await this.getEntityLimits(teamId, orgId)).rateLimitMultiplier ?? 1;
+ }
+
/**
* Reverses a prior trackCredits call by tracking a negative usage event.
*/
diff --git a/apps/api/src/services/queue-jobs.ts b/apps/api/src/services/queue-jobs.ts
index 60dcb0800..370ad2ed9 100644
--- a/apps/api/src/services/queue-jobs.ts
+++ b/apps/api/src/services/queue-jobs.ts
@@ -5,6 +5,7 @@ import {
getConcurrencyLimitActiveJobs,
getConcurrencyQueueJobsCount,
getCrawlConcurrencyLimitActiveJobs,
+ getEffectiveConcurrencyLimit,
getTeamQueueLimit,
MAX_BACKLOG_TIMEOUT_MS,
pushConcurrencyLimitActiveJob,
@@ -398,15 +399,19 @@ async function addScrapeJobRaw(
}
}
- maxConcurrency =
- (
- await getACUCTeam(
- webScraperOptions.team_id,
- false,
- true,
- RateLimiterMode.Crawl,
- )
- )?.concurrency ?? 2;
+ {
+ const acuc = await getACUCTeam(
+ webScraperOptions.team_id,
+ false,
+ true,
+ RateLimiterMode.Crawl,
+ );
+ maxConcurrency = await getEffectiveConcurrencyLimit(
+ webScraperOptions.team_id,
+ acuc?.concurrency,
+ acuc?.org_id,
+ );
+ }
if (concurrencyLimited === null) {
const now = Date.now();
@@ -699,9 +704,17 @@ export async function addScrapeJobs(
addToCQ = jobsForcedToCQ;
} else {
const now = Date.now();
- maxConcurrency =
- (await getACUCTeam(teamId, false, true, RateLimiterMode.Scrape))
- ?.concurrency ?? 2;
+ const acuc = await getACUCTeam(
+ teamId,
+ false,
+ true,
+ RateLimiterMode.Scrape,
+ );
+ maxConcurrency = await getEffectiveConcurrencyLimit(
+ teamId,
+ acuc?.concurrency,
+ acuc?.org_id,
+ );
await cleanOldConcurrencyLimitEntries(teamId, now);
currentActiveConcurrency = (
diff --git a/apps/api/src/services/rate-limiter.ts b/apps/api/src/services/rate-limiter.ts
index 2b61e965f..63f9ba0c7 100644
--- a/apps/api/src/services/rate-limiter.ts
+++ b/apps/api/src/services/rate-limiter.ts
@@ -36,6 +36,33 @@ const fallbackRateLimits: AuthCreditUsageChunk["rate_limits"] = {
research: 100,
};
+/**
+ * Per-minute base rate limits, i.e. the Γ—1 values. The effective limit is
+ * `base Γ— multiplier`, where the multiplier is read from Autumn's `rate_limits`
+ * feature. Modes absent here are not multiplier-scaled and use the fallback
+ * table.
+ *
+ * Endpoint β†’ mode mapping: agent + extract share `Extract`; interact is
+ * `Browser`; interactExecute is `BrowserExecute`; agentStatus is
+ * `ExtractStatus`.
+ */
+const BASE_RATE_LIMITS: Partial<Record<RateLimiterMode, number>> = {
+ [RateLimiterMode.Scrape]: 10,
+ [RateLimiterMode.Map]: 10,
+ [RateLimiterMode.Crawl]: 2,
+ [RateLimiterMode.Search]: 10,
+ [RateLimiterMode.Extract]: 2,
+ [RateLimiterMode.Browser]: 2,
+ [RateLimiterMode.BrowserExecute]: 10,
+ [RateLimiterMode.CrawlStatus]: 500,
+ [RateLimiterMode.ExtractStatus]: 500,
+};
+
+/**
+ * Builds the per-minute rate limiter for a mode from the team's ACUC
+ * rate_limits (the default path). Used unless the Autumn-limits ramp is enabled
+ * for the org, in which case getAutumnRateLimiter is used instead.
+ */
export function getRateLimiter(
mode: RateLimiterMode,
rate_limits: AuthCreditUsageChunk["rate_limits"] | null,
@@ -49,3 +76,69 @@ export function getRateLimiter(
return createRateLimiter(`${mode}`, rateLimit);
}
+
+/**
+ * Builds the per-minute rate limiter for a mode from an Autumn rate-limit
+ * multiplier: the effective limit is `base Γ— multiplier` for multiplier-scaled
+ * modes (default Γ—1). Modes without a base fall back to the static table. Used
+ * only when the Autumn-limits ramp is enabled for the org.
+ */
+export function getAutumnRateLimiter(
+ mode: RateLimiterMode,
+ multiplier: number = 1,
+): RateLimiterRedis {
+ const base = BASE_RATE_LIMITS[mode];
+ let rateLimit: number;
+ if (base !== undefined) {
+ const safeMultiplier = multiplier > 0 ? multiplier : 1;
+ rateLimit = base * safeMultiplier;
+ } else {
+ rateLimit = fallbackRateLimits?.[mode] ?? 500;
+ }
+
+ if (mode === RateLimiterMode.Search || mode === RateLimiterMode.Scrape) {
+ // TEMP: Mogery
+ rateLimit = Math.max(rateLimit, 100);
+ }
+
+ return createRateLimiter(`${mode}`, rateLimit);
+}
+
+/**
+ * Plan-priority tiers keyed by the minimum Autumn rate-limit multiplier that
+ * qualifies. Values mirror the tuned production `plan_priority` for each plan.
+ * A customer's multiplier selects the highest tier they meet or exceed, so
+ * off-tier multipliers round down.
+ *
+ * `bucketLimit` / `planModifier` only affect internal job-scheduling priority
+ * (see getJobPriority) β€” never request success β€” so inferring them from the
+ * multiplier is safe: a wrong guess shifts queue ordering, not correctness.
+ */
+const PLAN_PRIORITY_TIERS: {
+ minMultiplier: number;
+ bucketLimit: number;
+ planModifier: number;
+}[] = [
+ { minMultiplier: 1, bucketLimit: 25, planModifier: 0.5 }, // free
+ { minMultiplier: 10, bucketLimit: 100, planModifier: 0.3 }, // hobby
+ { minMultiplier: 50, bucketLimit: 200, planModifier: 0.2 }, // standard
+ { minMultiplier: 500, bucketLimit: 400, planModifier: 0.1 }, // growth
+ { minMultiplier: 1000, bucketLimit: 400, planModifier: 0.1 }, // scale
+ { minMultiplier: 2500, bucketLimit: 1000, planModifier: 0.05 }, // enterprise
+];
+
+/**
+ * Infers safe `bucketLimit` / `planModifier` values from a rate-limit
+ * multiplier. Monotonic: bucketLimit never decreases and planModifier never
+ * increases as the multiplier grows.
+ */
+export function inferPlanPriorityFromMultiplier(multiplier: number): {
+ bucketLimit: number;
+ planModifier: number;
+} {
+ let chosen = PLAN_PRIORITY_TIERS[0];
+ for (const tier of PLAN_PRIORITY_TIERS) {
+ if (multiplier >= tier.minMultiplier) chosen = tier;
+ }
+ return { bucketLimit: chosen.bucketLimit, planModifier: chosen.planModifier };
+}