03/12/2026 10:00am

Set a Spending Cap on Cloudflare Workers and Durable Objects With Spend Policies to Avoid Surprise Bills
#Cloudflare Workers
#Spend Policies
#Durable Objects
#Cloud cost
#usage_block
Imagine you ask an AI to help write a small Worker and deploy it. That night a loop calls itself without stopping and nobody notices, and in the morning there is an invoice you did not expect. That fear is why anyone on pay-as-you-go services should know about setting a spending cap.
Cloudflare's API documentation has a resource called spend policies that applies to Workers and Durable Objects. Today Superdev Academy reads the documentation for creating a spend policy to see what it can do, which fields are required, and what to think about before setting the kind that cuts real usage. We read from the documentation only and did not try it on a real account.
There are two kinds of spend policy
According to the documentation, both workers and durable_objects support two actions, and each product has its own balance and policies.
notification: when spend reaches
threshold_cents, Cloudflare sends an email tonotification_emailwithout blocking usage. You can set any number of them.usage_block: when spend reaches the threshold, further use of that product is blocked until the billing period ends. Raising the threshold above the current balance, disabling the policy or deleting it lifts the block early. An account can have only one enabled usage_block per product; creating a second returns a 409.
The documentation we read does not say whether this feature is in beta or when it launched, so we will not guess. We suggest checking Cloudflare's billing page or changelog for the latest status before you depend on it.
Required fields
The request body for creating a policy has these fields according to the documentation.
actionis"notification"or"usage_block"enabledis a booleannameis 1 to 200 bytes after trimming whitespaceproduct_idis"workers"or"durable_objects"threshold_centsis an integer with a minimum of 1, in centsnotification_emailis optional in general but required whenactionisnotification, and must be a bare email address
Here is an example request body that notifies you when Workers spend reaches 20 dollars (2,000 cents). The values and name are ones we made up; the field names come from the documentation.
{"action": "notification","enabled": true,"name": "workers-alert-20-usd","product_id": "workers","threshold_cents": 2000,"notification_email": "you@example.com"}For the API route and how to authenticate, see the source documentation page, because we did not verify those details in this article. The documentation requires account_id as a path parameter.
What the response tells you
The result includes id, version and a read-only current_state that contains balance_cents (the current spend), billing_period_start and billing_period_end, enforcement, status and evaluated_at, which let you check when the policy was last evaluated and what state it is in.
What to think about before setting a usage_block
A block is a real stop of the service until the end of the billing period. The documentation says further use of that product is blocked, which in practice may make a website or API that depends on that Worker stop working (that part is our own conclusion). So we recommend two layers: a notification at a lower threshold so you find out early, and a usage_block at a higher threshold as the last line. Decide in advance who will lift the block if it really happens and how, for example by raising the threshold or disabling the policy.
This policy is a safety net, not a substitute for reviewing code. An endless loop should be caught at review time. If an AI wrote that code, use the checklist in How far can you trust AI-written code? A 6-point checklist before you commit (a Thai article) as well.
Summary: notify first, block later, and know who lifts it
Cloudflare's spend policies apply to Workers and Durable Objects and come in two kinds, notification and usage_block. Only one usage_block can be enabled per product, and it stops usage until the billing period ends or it is lifted. Always set a notification first, add a usage_block only when you are ready for the effect of a service stop, and check the feature's status on Cloudflare's pages again before relying on it.
If you want to start learning to program from the basics, see the courses from Superdev Academy at learn.superdevacademy.com/courses. There are currently the Go Cheat Sheet and Git & GitHub Essentials courses, and neither covers Cloudflare.
🎯 Follow dev knowledge from Superdev Academy:
🔵 Facebook: Superdev Academy Thailand
🎬 YouTube: Superdev Academy Channel
📸 Instagram: @superdevacademy
🎬 TikTok: @superdevacademy
FAQ: Frequently Asked Questions about This Article
A collection of questions and answers to help you better understand the content of this article.