Skip to content

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

perf(api): skip Autumn multiplier fetch when a rate-limit override applies

Claudee6e896cb7c36
diff --git a/apps/api/src/controllers/__tests__/auth.test.ts b/apps/api/src/controllers/__tests__/auth.test.ts
index b8febf4bc..10bcb22ef 100644
--- a/apps/api/src/controllers/__tests__/auth.test.ts
+++ b/apps/api/src/controllers/__tests__/auth.test.ts
@@ -51,11 +51,24 @@ vi.mock("../../db/rpc", () => ({
authCreditUsageChunkFromTeam: vi.fn(),
}));
-vi.mock("../../services/rate-limiter", () => ({
- getRateLimiter: vi.fn(),
- getAutumnRateLimiter: vi.fn(),
+// The limiter builders are mocked, but getRateLimitOverride is kept real: it is
+// the single source of truth for override resolution, and auth.ts calls it to
+// decide whether the Autumn multiplier is needed at all. Stub ioredis so
+// importing the real module doesn't open a connection.
+vi.mock("ioredis", () => ({
+ default: class {},
}));
+vi.mock("../../services/rate-limiter", async importOriginal => {
+ const actual =
+ await importOriginal<typeof import("../../services/rate-limiter")>();
+ return {
+ ...actual,
+ getRateLimiter: vi.fn(),
+ getAutumnRateLimiter: vi.fn(),
+ };
+});
+
vi.mock("../../lib/keyless", async importOriginal => {
const actual = await importOriginal<typeof import("../../lib/keyless")>();
return {
@@ -529,6 +542,47 @@ describe("authenticateUser", () => {
);
expect(auth.success).toBe(true);
+ // The override replaces the whole base × multiplier computation, so the
+ // Autumn multiplier is never fetched and a neutral 1 is passed instead.
+ expect(autumnService.getRateLimitMultiplier).not.toHaveBeenCalled();
+ expect(getAutumnRateLimiter).toHaveBeenCalledWith(
+ RateLimiterMode.Scrape,
+ 1,
+ flags,
+ );
+ });
+
+ it("still fetches the Autumn multiplier when no override covers the mode", async () => {
+ config.USE_DB_AUTHENTICATION = true;
+ vi.mocked(getValue).mockResolvedValue(null);
+ const flags = { rateLimitOverrides: { crawl: 42 } };
+ vi.mocked(authCreditUsageChunk).mockResolvedValue([
+ {
+ api_key: "00000000-0000-4000-8000-000000000000",
+ api_key_id: 1,
+ team_id: "team-1",
+ org_id: "org-1",
+ flags,
+ },
+ ]);
+ vi.mocked(redlock.using).mockImplementation(
+ async (_keys, _ttl, _options, fn) => fn({ aborted: false } as never),
+ );
+ vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(50);
+
+ const auth = await authenticateUser(
+ {
+ headers: {
+ authorization: "Bearer 00000000-0000-4000-8000-000000000000",
+ },
+ socket: { remoteAddress: "127.0.0.1" },
+ },
+ {},
+ RateLimiterMode.Scrape,
+ );
+
+ expect(auth.success).toBe(true);
+ expect(autumnService.getRateLimitMultiplier).toHaveBeenCalledTimes(1);
expect(getAutumnRateLimiter).toHaveBeenCalledWith(
RateLimiterMode.Scrape,
50,
diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts
index 0bb0f870d..780a426af 100644
--- a/apps/api/src/controllers/auth.ts
+++ b/apps/api/src/controllers/auth.ts
@@ -5,7 +5,11 @@ import { logger } from "../lib/logger";
import { parseApi } from "../lib/parseApi";
import { withAuth } from "../lib/withAuth";
import { getAgentSponsorStatus } from "../services/agent-sponsor";
-import { getRateLimiter, getAutumnRateLimiter } from "../services/rate-limiter";
+import {
+ getRateLimiter,
+ getAutumnRateLimiter,
+ getRateLimitOverride,
+} from "../services/rate-limiter";
import {
KEYLESS_FREE_TIER_LIMIT_MESSAGE,
consumeKeylessRequest,
@@ -576,7 +580,9 @@ export async function authenticateUser(
* can't diverge.
*
* The org flags carry the optional per-endpoint override, so they are passed
- * on to getAutumnRateLimiter.
+ * on to getAutumnRateLimiter, which stays the only place deciding the final
+ * limit. An override makes the multiplier irrelevant, so we skip fetching it
+ * from Autumn in that case rather than paying for a value that is discarded.
*/
async function buildAuthenticatedRateLimiter(
teamId: string,
@@ -584,7 +590,10 @@ async function buildAuthenticatedRateLimiter(
mode: RateLimiterMode,
flags: TeamFlags,
): Promise<RateLimiterRedis> {
- const multiplier = await autumnService.getRateLimitMultiplier(teamId, orgId);
+ const multiplier =
+ getRateLimitOverride(mode, flags?.rateLimitOverrides) !== undefined
+ ? 1
+ : await autumnService.getRateLimitMultiplier(teamId, orgId);
return getAutumnRateLimiter(mode, multiplier, flags);
}
diff --git a/apps/api/src/services/rate-limiter.ts b/apps/api/src/services/rate-limiter.ts
index 911496bb4..7abc68484 100644
--- a/apps/api/src/services/rate-limiter.ts
+++ b/apps/api/src/services/rate-limiter.ts
@@ -81,7 +81,7 @@ export function getRateLimiter(mode: RateLimiterMode): RateLimiterRedis {
* broken entry must never break authentication. Only a finite integer above
* zero counts.
*/
-function getRateLimitOverride(
+export function getRateLimitOverride(
mode: RateLimiterMode,
overrides: unknown,
): number | undefined {