Cloudflare 推出支出管理:支出限额与告警

长期以来,在我们开发者平台上构建应用的开发者最希望获得的功能中,支出控制一直高居榜首。如今在 Cloudflare 上构建应用的开发者已超过 1000 万,这一点比以往任何时候都更加重要。
尽管我们为付费套餐提供用量通知和预算提醒已有一段时间,但这并不能彻底解决问题;告知并不等同于控制。
现在,你可以设置支出阈值,并选择在达到这些限额时让 Cloudflare 暂停相关活动,从而避免因计划外用量而产生意外账单。今天,我们推出 Beta 版的 Spend Management,面向所有按量付费(PayGo)客户,首批支持 Workers 和 Durable Objects。
将 Spend Management 与 AI Gateway 的支出限额配合使用,即可管理整个应用的计算和 AI 推理成本。AI Gateway 已支持针对 Workers AI 及其他 AI 提供商的按用户和按模型预算,并应用于通过你的网关路由的请求。
你可以通过仪表板、cf CLI 或 REST API 开始使用 Spend Management。
在仪表板中,前往 Manage Account > Billing > Spend limits,为你想控制支出的产品启用该功能。
为某个产品启用控制后,你可以定义支出限额、指定接收通知的邮箱,并设置一个或多个告警阈值。
如果你配置了接收通知的邮箱,当核算出的支出达到某个阈值或你的支出限额时,Cloudflare 就会发送通知。达到限额后,将强制执行阻断策略,你的产品会被自动暂停。
如果你打开仪表盘,就能近实时(10 分钟以内)看到每个受支持产品在当前计费周期的费用,并可在那里管理自己的设置和阈值。概览面板会告诉你哪些产品已配置阈值和上限。在这个示例中,你可以看到 Workers 在触及硬性上限之前触发了两次告警。
请注意,计费周期从付费订阅开始之时算起,而不是从你部署 Worker 或创建支出上限之时算起;10 月 10 日开始的订阅,其计费周期通常是 10 月 10 日至 11 月 10 日——而不是一个自然月。
仪表盘还会显示审计日志,因此你可以看到谁在何时修改了某个限制,或者上限是在何时被触及的。
与大多数 Cloudflare 服务一样,Spend Management 也是一个 API,你可以通过编程方式调用;相关文档可在我们的开发者文档中找到。下面是一个示例,不过用的是 API:
curl --fail-with-body --request POST \ "https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/billing/spend-policies" \ --header "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \ --header "Content-Type: application/json" \ --data '{ "name": "Workers spending cap", "product_id": "workers", "enabled": true, "threshold_cents": 10000, "action": "usage_block", "notification_email": "me@example.com" }'当然,你也可以使用 cf,即适用于整个 Cloudflare API 的 CLI:
cf billing spend-policies list cf billing spend-policies create cf billing spend-policies get cf billing spend-policies update cf billing spend-policies delete cf billing spend-policies audit list cf billing spend-policies activity list cf billing spend-policies create --body '{ "name": "Workers spending cap", "product_id": "workers", "enabled": true, "threshold_cents": 10000, "action": "usage_block", "notification_email": "me@example.com" }' 一旦上限被强制执行,该产品的新调用就会被拒绝。请注意,我们只会暂停会产生费用的客户代码调用——例如在 Workers 中,Cron Triggers 和 Queue consumer 调用会被暂停,但静态资源会继续提供服务。
要恢复服务,你可以:
就是这样。你今天就可以开始为 Workers 和 Durable Objects 使用 Spend Management,其他产品很快也会跟进。
继续阅读,了解我们如何构建该系统,以及接下来会发生什么。
尽管表面上看起来简单直接,但在 Cloudflare 的规模下构建这样的系统绝非易事。从我们分布式网络上任意位置的调用,到需要在全球范围内施加的阻断决策,构建 Spend Management 都需要与多个团队协作、修改现有系统中的代码,并从零开始创建新系统。
我们先从鸟瞰视角看一个受上限约束的可计费事件:
现在,让我们深入探究。
每次调用都会产生用量测量数据:请求、CPU 时间、执行时长或存储操作,具体取决于产品。但要把如此海量的原始数据从源头一路传输并处理到计费评级引擎,既无法扩展,也不高效。
因此,我们所做的是使用两阶段计量流水线,尽可能在靠近源头的位置收集并聚合测量数据,将事件合并为用量聚合数据,这样引擎看到的是累计用量,而不必逐个处理所有事件。
我们来看一张图。
第一阶段会在接近解析的位置将原始事件量缩减到一分钟窗口。每个计量解析器将原始事件转换为用量测量值,然后将匹配的测量值合并到通常为一分钟的桶中。不过,仍可能有若干条记录描述同一个账户和时间窗口。
在第二阶段,一个单独的服务使用相同的分组规则合并这些第一阶段的局部聚合数据,但通常是在五分钟窗口内,并在 Usage Engine 进行下游处理之前进一步缩减数据量。在这里,我们还会归档合并后的聚合数据;这为我们提供了一个持久数据集,用于后续的分析和对账。
这个两阶段流水线的好处在于,它很早就减少了原始事件量,因此下游引擎只需处理计算支出并据此采取行动所需的聚合用量。这正是让它在 Cloudflare 的规模下变得可行的原因。
Usage Engine 将聚合后的测量值转换为用量记录,这些是标准化记录,包含账户、用量窗口、指标、数量和相关维度。换句话说,它将产品特定的计量项映射为受支持的用量指标。
我们按账户对处理进行分区,因此一个账户的用量和订阅信息会在同一位置汇合。这让我们能够维护其累计用量,并应用包含的额度,而无需在每次更新时跨定价处理器进行协调。
如你所见,我们并行运行不同的分区,这样就能把工作分散到各个账户上,同时让每个账户的定价状态保持在一起。还要注意,每个分区既保留用于快速访问的本地状态,也在磁盘上保留持久化的预写日志(WAL)变更记录。这为我们带来:
最后,用量指标会被送入 Rating Engine。
Rating Engine 是 Spend Management 中最重要的模块;它把用量转化为货币价值,也正是它能够回答这个问题:“考虑到该账户已消费的量、其订阅以及定价,它到目前为止花了多少钱?”
它的工作方式如下。
本质上,Rating Engine 把三样东西汇聚到一起:
用量更新和订阅更新以相互独立的流到达,因此我们不能假设它们会按顺序抵达 Rating Engine。此外,我们还必须能够从数据处理故障,或者订阅信息缺失(比如 webhook 失败)中恢复。
如果用量在其定价所需的订阅信息之前到达,我们会将其保留为待处理状态,并在该信息可用时重新处理。这让我们能够容忍暂时的延迟,而不会把尚未定价的用量当作免费。
最后,我们计算并跟踪累计支出。Rating Engine 使用一个产品目录数据库,其中定义了用量指标,包括额度、费率和舍入规则,并结合账户的订阅信息。一百万 CPU 毫秒并不一定意味着额外收费:引擎会先应用账户包含的 CPU 额度,然后对超出部分定价。它对累计用量定价,而不是对每个请求单独舍入并计费。
一旦累计支出计算完成,接下来有两个步骤:
Rating Engine 并不处于请求路径之中,也不会直接拒绝请求。相反,策略服务会将上限发送给运行时配置服务以及 Quicksilver——这是我们内部开发的键值存储,用于在全球规模上实现快速复制和低延迟访问。随后,它会以异步方式将该上限传播到所有节点。
一旦该上限到达 Quicksilver,运行时会在此后的周期性刷新中读取它,并开始拒绝触及上限的调用或操作。
换言之,限额执行依赖本地状态,因此请求无需等待 Rating Engine。通过复用我们现有的运行时账户限额架构,我们让评级过程不进入请求路径,同时把上限分发到全球网络。
原文较长,译文为节选;译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。
查看英文原文(Introducing Spend Management: Spending limits and alerts for Cloudflare)
Spend controls have been at the top of the list of most requested features from developers building on our developer platform for quite some time now. With more than 10 million developers now building on Cloudflare, this matters more than ever.
Although we’ve had usage notifications and budget alerts available to paid plans for a while, this doesn’t solve the problem completely; informing is not the same as controlling.
You can now set spending thresholds and choose to have Cloudflare pause activity when those limits are reached, thus avoiding unexpected bills from unplanned usage. Today, we're introducing Spend Management in Beta, for any pay-as-you-go (PayGo) customer, starting with Workers and Durable Objects.
Use Spend Management alongside AI Gateway spend limits to manage compute and AI inference costs across your application. AI Gateway already supports per-user and per-model budgets for Workers AI and other AI providers, applied to requests routed through your gateway.
How it works
You can start using Spend Management using the dashboard, cf CLI, or REST API.
In the dashboard, head over to Manage Account > Billing > Spend limits and enable the products for which you want to control spending.
When you enable controls on a product, you can define a spending limit, specify a recipient email for notifications, and set one or more alert thresholds.
If you configure a recipient email, Cloudflare will send notifications when the evaluated spend reaches one of the thresholds or your spending limit. When the limit is reached, a blocking policy is enforced and your product is paused automatically.
If you go to the dashboard, you can see the current billing cycle costs for each supported product in near real time (under 10 minutes), and manage your settings and thresholds from there. The overview panel tells you which products have thresholds and limits configured. In this example, you can see that Workers triggered two alerts before hitting the hard limit.
Note that the billing cycle starts when the paid subscription begins, not when you deploy a Worker or create a spending cap; a subscription starting October 10 generally has a cycle of October 10-November 10 — not a calendar month.
The dashboard also shows an audit log, so you can see who changed a limit and when, or when a cap was reached.
As with most Cloudflare services, Spend Management is also an API that you can use programmatically; the documentation can be found in our Developer Documentation. Here’s an example, but using the API:
curl --fail-with-body --request POST \
"https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/billing/spend-policies" \
--header "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
--header "Content-Type: application/json" \
--data '{
"name": "Workers spending cap",
"product_id": "workers",
"enabled": true,
"threshold_cents": 10000,
"action": "usage_block",
"notification_email": "me@example.com"
}'And, of course, you can use cf, the CLI for the entire Cloudflare API:
cf billing spend-policies list
cf billing spend-policies create
cf billing spend-policies get
cf billing spend-policies update
cf billing spend-policies delete
cf billing spend-policies audit list
cf billing spend-policies activity list
cf billing spend-policies create --body '{
"name": "Workers spending cap",
"product_id": "workers",
"enabled": true,
"threshold_cents": 10000,
"action": "usage_block",
"notification_email": "me@example.com"
}'Once a cap is enforced, new invocations of that product are rejected. Note that we only pause customer-code invocations that incur costs — in Workers, for example, Cron Triggers and Queue consumer invocations are paused, but static assets continue to be served.
To resume service, you can:
- Raise the limit above the current cycle’s accumulated spend.
- Disable or delete the blocking policy.
- Wait until the next billing cycle, when the existing block expires.
That’s it. You can start using Spend Management for Workers and Durable Objects today, and other products will follow soon.
Read on to learn how we built the system, and what’s going to happen next.
How we built Spend Management
As easy and straightforward as it seems on the surface, building a system like this at Cloudflare scale was no small feat. From an invocation anywhere on our distributed network to a blocking decision that we need to apply globally, building Spend Management required working with multiple teams, changing code in existing systems, and creating new systems from scratch.
Let’s start by looking at the bird's-eye view of a capped billable event:
Now let's dig deeper.
Collecting and aggregating close to the source
Each invocation produces usage measurements: requests, CPU time, execution duration, or storage operations, depending on the product. But transporting and processing this volume of raw data all the way from the sources down to the rating engine would not scale or be efficient.
So what we do is we collect and aggregate the measurements as close to the source as possible using a two-stage metering pipeline that combines events into usage aggregates, so the engine sees accumulated usage rather than having to process all the events one by one.
Let's look at a diagram.
The first stage reduces raw-event volume close to parsing down to one-minute windows. Each metering parser converts raw events into usage measurements, then combines matching measurements into typically one-minute buckets. Still, several records can describe the same account and time window.
In the second stage, a separate service merges those first-stage partial aggregates using the same grouping rules, but over typically five-minute windows, and further reduces the volume before downstream processing by the Usage Engine. Here we also archive the consolidated aggregates; this gives us a durable dataset for later analytics and reconciliation.
The nice thing about this two-stage pipeline is that it reduces the raw event volume early, so the downstream engines process only the aggregated usage they need to calculate spend and act on it. That’s what makes this practical at Cloudflare’s scale.
Usage Engine: account-partitioned processing
The Usage Engine translates aggregated measurements into usage records, which are standardized records containing the account, usage window, metric, quantity, and relevant dimensions. In other words, it maps product-specific meters into supported usage metrics.
We partition processing by account, so an account’s usage and subscription information meet in the same place. That lets us maintain its accumulated usage and apply included allowances without coordinating across pricing processors on every update.
As you can see, we run different partitions in parallel, giving us a way to spread the work across accounts while keeping each account’s pricing state together. Also note that each partition keeps both a local state for fast access, and also a durable write-ahead logging (WAL) changelog on disk. This buys us:
- Less cross-partition coordination: each account’s usage and subscription state stay together.
- Consistent pricing: usage updates share the same allowance and accumulated-spend state.
- Parallel processing: different partitions calculate spend independently.
- Recoverability: durable state lets replacement processors rebuild and resume after failures, if necessary.
Then, finally, the usage metrics are fed into the Rating Engine.
The Rating Engine
The Rating Engine is the most important block of Spend Management; it turns usage into monetary value and it’s what’s able to answer the question: “Given what this account has consumed, its subscription, and its pricing, how much has it spent so far?”
Here’s how it works.
In essence, the Rating Engine brings together three things:
- Usage records: quantities such as requests and CPU milliseconds.
- Subscription information: the account’s billing period and plan changes.
- Pricing rules: rates, included allowances, and billing rounding.
Usage data and subscriptions are independent streams
Usage and subscription updates arrive as independent streams, so we can’t assume they reach the Rating Engine in order. Furthermore, we have to be able to recover from a data processing failure or missing subscription information from, say, a failed webhook.
If usage arrives before the subscription information needed to price it, we retain it as pending and revisit it when that information becomes available. This lets us tolerate temporary delays without treating unpriced usage as free.
Tracking accumulated spend
Finally, we calculate and track accumulated spend. The Rating Engine uses a product catalog database that defines usage metrics, including allowances, rates, and rounding rules, alongside the account’s subscription information. One million CPU milliseconds doesn’t necessarily mean an extra charge: the engine first applies the account’s included CPU allowance, then prices any excess. It prices accumulated usage rather than rounding and charging for each request independently.
Once the accumulated spend is calculated, two steps follow:
- Policy evaluation: decide whether one or more warning or blocking thresholds have been reached.
- Reporting: we show estimated usage charges in the dashboard via our APIs, and we send out email alerts (via Cloudflare Email Service) if necessary.
Asynchronous cap enforcement
The Rating Engine doesn’t sit in the request path or directly reject requests. Instead, the policy service sends the cap to our runtime configuration service and Quicksilver, our internal key-value store developed to enable fast global replication and low-latency access at planet scale. This propagates it to all our nodes asynchronously.
Once the cap reaches Quicksilver, the runtime picks it up during its periodic refresh and starts rejecting capped invocations or operations.
In other words, enforcement uses local state, so requests never have to wait on the Rating Engine. By reusing our existing runtime account-limit architecture, we keep rating out of the request path while distributing caps across the global network.
What’s next
Any pay-as-you-go customer can start using Spend Management today. While this is a major milestone in our roadmap, it’s also just the start. Let us share some of our plans for the next few weeks and months ahead.
More products
We are starting with caps for Workers and Durable Objects, but you’ll be able to measure and cap many other Cloudflare products and primitives in the near future. We’ve built the system in a way that makes it easy and familiar for our products to onboard, so expect the catalog to grow.
Closer to real time
While near real time under 10 minutes is a great benchmark, we’re not satisfied with it, and being the stubborn engineers we are, we have pretty good ideas on how to reduce that time even further. Expect us to shorten the gap between measurement and enforcement over time, hopefully to under a minute.
Agent and application-based budgets
Right now, we measure spend by products and primitives, but there are other useful dimensions to explore when controlling costs. For example, a developer might want to monitor the costs of a specific application — such as a Worker script that uses other primitives like R2 or Workers AI. How can we measure the aggregated cost of an entire application and pause that application, rather than disabling the underlying primitives across all customer applications?
Another compelling use case for cost controls is AI agents. We’ve all read horror stories about rogue agents entering unconstrained recursive reasoning loops to brute-force a problem — generating thousands of dollars in costs from high-frequency, expensive API calls, not to mention causing denial-of-service (DoS) issues on databases and other services. AI Gateway already lets you give an agent a budget for AI requests routed through it. A task can also consume compute, database, and storage resources. We want to make it possible to budget for that broader resource use across products.
You’ll be able to tag the calls from your agents and our pipeline will carry that metadata from the runtime through to the Rating Engine. That will let us attribute spend and pause an application or an agentic task across products, without interrupting other unrelated applications.
Get started
This launch is a first step toward making spend controls a normal part of building on Cloudflare, alongside deploying code and monitoring your applications.
Check out the billing developer documentation for more information. Start using Spend Management and tell us what works, what’s missing, or where more granular budgets would help.
Join us on Discord or our socials. We’re listening!
正文保留英文原文,标题与摘要由机器翻译。
原文发布于 Cloudflare Blog(Introducing Spend Management: Spending limits and alerts for Cloudflare),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。