Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
fix(billing): raise Autumn timeout for historical usage queries
Claude594c8f6f8c8c
diff --git a/apps/api/src/services/autumn/__tests__/usage.test.ts b/apps/api/src/services/autumn/__tests__/usage.test.tsindex 4034a10ff..2c47da609 100644--- a/apps/api/src/services/autumn/__tests__/usage.test.ts+++ b/apps/api/src/services/autumn/__tests__/usage.test.ts@@ -1,9 +1,12 @@import { vi, beforeEach } from "vitest";-const mockAggregate = vi.fn<(args: any) => Promise<any>>();+const mockAggregate = vi.fn<(args: any, options?: any) => Promise<any>>();const mockEntitiesGet = vi.fn<(args: any) => Promise<any>>();const mockCustomersGetOrCreate = vi.fn<(args: any) => Promise<any>>();+// Historical aggregations override the Autumn client's global 2s timeout.+const EXPECTED_HISTORICAL_TIMEOUT_MS = 15000;+let autumnClientRef: {events: { aggregate: typeof mockAggregate };entities: { get: typeof mockEntitiesGet };@@ -779,6 +782,7 @@ describe("getTeamHistoricalUsage", () => {range: "90d",binSize: "day",}),+ expect.objectContaining({ timeoutMs: EXPECTED_HISTORICAL_TIMEOUT_MS }),);});@@ -802,6 +806,7 @@ describe("getTeamHistoricalUsage", () => {range: "90d",binSize: "day",}),+ expect.objectContaining({ timeoutMs: EXPECTED_HISTORICAL_TIMEOUT_MS }),);});@@ -895,6 +900,7 @@ describe("getTeamHistoricalUsageByApiKey", () => {binSize: "day",groupBy: "properties.apiKeyId",}),+ expect.objectContaining({ timeoutMs: EXPECTED_HISTORICAL_TIMEOUT_MS }),);});@@ -972,6 +978,50 @@ describe("getTeamHistoricalUsageByApiKey", () => {entityId: "team-1",groupBy: "properties.apiKeyId",}),+ expect.objectContaining({ timeoutMs: EXPECTED_HISTORICAL_TIMEOUT_MS }),);});});++// ---------------------------------------------------------------------------+// Per-call Autumn timeout for the historical aggregations+// ---------------------------------------------------------------------------++// These aggregations bin by day (and optionally group by API key), so Autumn+// walks raw events and the call cost scales with the team's event volume. The+// Autumn client's global 2s timeout is sized for the latency-sensitive balance+// checks, so each historical call must override it or high-volume teams get a+// timeout error surfaced as a 500.+describe("historical usage Autumn timeout override", () => {+ it("passes a 15s per-call timeout for the ungrouped aggregate", async () => {+ mockAggregate.mockResolvedValue({ list: [] });++ await getTeamHistoricalUsage("team-1");++ expect(mockAggregate).toHaveBeenCalledTimes(1);+ const [, options] = mockAggregate.mock.calls[0];+ expect(options).toEqual({ timeoutMs: 15000 });+ });++ it("passes a 15s per-call timeout for the grouped byApiKey aggregate", async () => {+ mockAggregate.mockResolvedValue({ list: [] });++ await getTeamHistoricalUsageByApiKey("team-1");++ expect(mockAggregate).toHaveBeenCalledTimes(1);+ const [, options] = mockAggregate.mock.calls[0];+ expect(options).toEqual({ timeoutMs: 15000 });+ });++ it("overrides the Autumn client's global 2s timeout with a larger value", async () => {+ mockAggregate.mockResolvedValue({ list: [] });++ await getTeamHistoricalUsage("team-1");+ await getTeamHistoricalUsageByApiKey("team-1");++ expect(mockAggregate).toHaveBeenCalledTimes(2);+ for (const [, options] of mockAggregate.mock.calls) {+ expect(options?.timeoutMs).toBeGreaterThan(2000);+ }+ });+});diff --git a/apps/api/src/services/autumn/usage.ts b/apps/api/src/services/autumn/usage.tsindex bab637746..42e1d08fa 100644--- a/apps/api/src/services/autumn/usage.ts+++ b/apps/api/src/services/autumn/usage.ts@@ -9,6 +9,14 @@ export const TOKENS_PER_CREDIT = 15;const HISTORICAL_RANGE = "90d";const HISTORICAL_BIN_SIZE = "day";+// The historical/analytics aggregations below are bucketed by day (and+// optionally grouped by API key), so Autumn has to walk raw events and their+// cost scales with the team's event volume. That routinely takes several+// seconds. The Autumn client's global 2s timeout is sized for the+// latency-sensitive balance checks on the request hot path, and it is far too+// tight here, so these calls override it per call.+const HISTORICAL_AGGREGATE_TIMEOUT_MS = 15000;+// ---------------------------------------------------------------------------// Types// ---------------------------------------------------------------------------@@ -384,13 +392,16 @@ export async function getTeamHistoricalUsage(let response: any;try {- response = await autumnClient.events.aggregate({- customerId: orgId,- entityId: teamId,- featureId: CREDITS_FEATURE_ID,- range: HISTORICAL_RANGE,- binSize: HISTORICAL_BIN_SIZE,- });+ response = await autumnClient.events.aggregate(+ {+ customerId: orgId,+ entityId: teamId,+ featureId: CREDITS_FEATURE_ID,+ range: HISTORICAL_RANGE,+ binSize: HISTORICAL_BIN_SIZE,+ },+ { timeoutMs: HISTORICAL_AGGREGATE_TIMEOUT_MS },+ );} catch (err: any) {const status = err?.statusCode ?? err?.status ?? err?.response?.status;if (status !== 404) throw err;@@ -420,14 +431,17 @@ export async function getTeamHistoricalUsageByApiKey(let response: any;try {- response = await autumnClient.events.aggregate({- customerId: orgId,- entityId: teamId,- featureId: CREDITS_FEATURE_ID,- range: HISTORICAL_RANGE,- binSize: HISTORICAL_BIN_SIZE,- groupBy: "properties.apiKeyId",- });+ response = await autumnClient.events.aggregate(+ {+ customerId: orgId,+ entityId: teamId,+ featureId: CREDITS_FEATURE_ID,+ range: HISTORICAL_RANGE,+ binSize: HISTORICAL_BIN_SIZE,+ groupBy: "properties.apiKeyId",+ },+ { timeoutMs: HISTORICAL_AGGREGATE_TIMEOUT_MS },+ );} catch (err: any) {const status = err?.statusCode ?? err?.status ?? err?.response?.status;if (status !== 404) throw err;