AI News HubLIVE
站内改写2 min read

Cloudflare AI Gateway now supports spend limits

Cloudflare AI Gateway introduces spend limits to control costs by setting budgets per model, provider, or custom metadata. Requests exceeding the limit are blocked or can fall back to cheaper models.

SourceHacker News AIAuthor: tjek

Spend limits

Spend limits let you set cost-based budgets on your AI Gateway. When cumulative spend reaches the limit within a time window, AI Gateway blocks further requests with a 429 response until the window resets.

Unlike rate limiting, which caps the number of requests, spend limits track actual dollar cost per request based on model pricing. You can scope limits to any combination of model, provider, or custom metadata dimensions like user ID, team, or application.

Spend limits apply to both Unified Billing requests and BYOK requests for models with known pricing.

How it works

Each spend limit rule defines a budget (in dollars) over a rolling or fixed time window. AI Gateway calculates the cost of each request based on token usage and model pricing, then tracks cumulative spend against the limit in real time.

Before sending a request to the provider, AI Gateway evaluates all applicable spend limit rules at once. If any individual rule is over budget, the request is blocked with a 429 response.

Spend limits are eventually consistent. The current request's cost is recorded after completion, so a burst of concurrent requests can briefly exceed the limit before enforcement catches up.

Scoping with dimensions

Each rule can be scoped by model, provider, or custom metadata dimensions. Each dimension can be configured in one of two modes:

ModeBehaviorExample

Split by valueEach distinct value gets its own independent budget bucket.Splitting by metadata.user_id gives every user their own budget.

Filter by valueThe rule applies only when the dimension equals a specific value.Filtering metadata.team to engineering limits only requests from the engineering team.

If a dimension is not configured on a rule, all values share one budget bucket. For example, a rule without a provider dimension tracks spend across all providers together.

Dimension examples

Given a request with model=openai/gpt-5.5 and metadata.user_id=u_42:

ScenarioDimensionsBudget bucket

Global budget for everyoneNoneOne shared bucket

Per-user budgetmetadata.user_id: split by valueSeparate bucket per user

Per-provider, per-usermetadata.user_id: split by value, provider: split by valueSeparate bucket per user+provider combination

Specific model onlymodel: filter by value openai/gpt-5.5Only applies to openai/gpt-5.5 requests

Per-user, per-modelmetadata.user_id: split by value, model: split by valueSeparate bucket per user+model combination

Configure spend limits

Spend limits are configured on the gateway via the dashboard or the API. You can define up to 20 rules per gateway.

To scope spend limits by custom dimensions like user ID or team, attach custom metadata to your requests.

Behavior when a limit is reached

When a spend limit is exceeded, AI Gateway returns a 429 Too Many Requests response. You have two options:

Block requests (default) - The request is rejected until the budget window resets.

Fall back to a cheaper model - Create a Dynamic Route with a primary model and a fallback (for example, anthropic/claude-opus-4.7 with a fallback to @cf/moonshotai/kimi-k2.6). Then set a spend limit on the primary model using this feature. When the primary model's budget is exceeded, AI Gateway automatically routes requests to the fallback model instead of blocking them.

Monitoring spend

You can track your spend per model, provider, or any custom metadata attribute on the Analytics dashboard. Use this to understand usage patterns and set informed budgets.

Limitations

Cost tracking is a best-effort estimation based on token counts and model pricing. Refer to your provider's dashboard for exact billing amounts.

A maximum of 20 spend limit rules can be configured per gateway.