Skip to content

AiGatewaySpendingLimit

Source: src/Cloudflare/AiGateway/AiGatewaySpendingLimit.ts

The account-level Cloudflare AI Gateway spending limit — a hard dollar cap on cumulative spend across every gateway in the account’s Unified Billing.

This is a per-account singleton: Cloudflare stores a single limit per account (/accounts/{account_id}/ai-gateway/billing/spending-limit), so declaring more than one AiGatewaySpendingLimit against the same account will make them fight over the same remote object. Declare exactly one.

Monthly cap

import * as Cloudflare from "alchemy/Cloudflare";
// Cloudflare requires one manual credit top-up before a spending limit
// can be set. `topUp` reconciles that requirement: the provider observes
// the billing state and only charges the account's default payment
// method if the account has never topped up.
const cap = yield* Cloudflare.AiGatewaySpendingLimit("ai-spend-cap", {
amount: 250_00, // cents -> $250.00 (minimum 1_00 = $1.00)
duration: "monthly",
topUp: { amount: 10_00 }, // cents -> $10.00 (Cloudflare minimum)
});

Sliding daily window

const cap = yield* Cloudflare.AiGatewaySpendingLimit("ai-spend-cap", {
amount: 50_00, // cents -> $50.00
duration: "daily",
strategy: "sliding",
topUp: { amount: 10_00 },
});

Customize the recharge threshold

// Auto recharge is on by default: when the credit balance drops below
// `threshold`, Cloudflare recharges by `amount` automatically.
const cap = yield* Cloudflare.AiGatewaySpendingLimit("ai-spend-cap", {
amount: 250_00,
duration: "monthly",
topUp: { amount: 20_00, threshold: 10_00 }, // recharge $20 below $10
});

Disable auto recharge

const cap = yield* Cloudflare.AiGatewaySpendingLimit("ai-spend-cap", {
amount: 250_00,
duration: "monthly",
topUp: { amount: 10_00, autoRecharge: false }, // one-time bootstrap only
});