Skip to content

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

fix(api): skip the agent gate overage probe for unlimited balances (#5328)

Rakshith Ramprakash23bf398d024d
diff --git a/apps/api/src/controllers/v2/__tests__/agent-credit-gate.test.ts b/apps/api/src/controllers/v2/__tests__/agent-credit-gate.test.ts
index 0ceab23c4..c2df6ae01 100644
--- a/apps/api/src/controllers/v2/__tests__/agent-credit-gate.test.ts
+++ b/apps/api/src/controllers/v2/__tests__/agent-credit-gate.test.ts
@@ -46,6 +46,32 @@ describe("gateAgentCredits", () => {
);
});
+ it.each([9.223372036854776e18, Number.MAX_SAFE_INTEGER])(
+ "runs a team with an unlimited balance (%d) as requested without probing past it",
+ async remaining => {
+ checkCredits.mockResolvedValueOnce({ allowed: true, remaining });
+
+ expect(await gate(5000)).toEqual(asRequested(5000));
+ expect(checkCredits).toHaveBeenCalledTimes(1);
+ expect(hasAutoTopUp).not.toHaveBeenCalled();
+ },
+ );
+
+ it("still probes past a large but finite balance", async () => {
+ checkCredits
+ .mockResolvedValueOnce({
+ allowed: true,
+ remaining: Number.MAX_SAFE_INTEGER - 1,
+ })
+ .mockResolvedValueOnce({
+ allowed: true,
+ remaining: Number.MAX_SAFE_INTEGER - 1,
+ });
+
+ expect(await gate()).toEqual(asRequested(undefined));
+ expect(checkCredits).toHaveBeenCalledTimes(2);
+ });
+
it("blocks a team below the minimum and reports its balance", async () => {
checkCredits.mockResolvedValueOnce({ allowed: false, remaining: 12 });
diff --git a/apps/api/src/controllers/v2/agent-credit-gate.ts b/apps/api/src/controllers/v2/agent-credit-gate.ts
index b2d4421eb..5627192b4 100644
--- a/apps/api/src/controllers/v2/agent-credit-gate.ts
+++ b/apps/api/src/controllers/v2/agent-credit-gate.ts
@@ -22,7 +22,8 @@ export type AgentCreditGate =
* asking for a little more than that balance:
*
* - below the minimum: refused, as before.
- * - can spend past the balance (overage, unlimited): runs as requested.
+ * - unlimited balance: runs as requested, without probing past it.
+ * - can spend past the balance (overage): runs as requested.
* - cannot, but has an auto top-up: runs as requested, as before.
* - otherwise: `maxCredits` is capped at the balance, and the agent service
* is told to stop the run if the balance runs out mid-flight.
@@ -60,6 +61,9 @@ export async function gateAgentCredits({
if (minimum === null) return asRequested;
if (!minimum.allowed) return { allowed: false, balance: minimum.remaining };
+ // An unlimited balance comes back as int64 max. Probing past it overflows
+ // the billing service's integer, so treat it as unlimited and skip the probe.
+ if (minimum.remaining >= Number.MAX_SAFE_INTEGER) return asRequested;
const balance = Math.floor(minimum.remaining);
if (!Number.isFinite(balance)) return asRequested;
const pastBalance = await check(