Cloudflare OS includes an opt-in billing system that gives every user a free daily AI allowance and — once that runs out — routes further usage through the user’s own Cloudflare account so their credits are billed directly. The platform operator is never charged for funded users. WhenDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/cloudflare/cloudflare-os/llms.txt
Use this file to discover all available pages before exploring further.
ENABLE_CLOUDFLARE_LIMITS is unset (the default), all usage is unlimited and no billing logic runs, which is ideal for self-hosted or internal deployments.
How it works
Each user’s AI usage is tracked on theirUserDurableObject via a daily counter (consumeDailyLlmCall / checkDailyLlmCount) that resets at midnight UTC. Before every user-initiated agent turn, the overseer calls checkUsageAndBalance to determine how to route the request:
Connected, balance ≥ $2
Routed through the user’s own Cloudflare account. Usage bills their AI Gateway credits. The platform is never charged. The user’s free-tier counter is left untouched.
Within free tier
Routed through the platform’s AI Gateway (
CF_AI_GATEWAY). This includes users with a connected account whose balance is below $2.Free tier exhausted, no account
Blocked. The user sees a prompt to connect their Cloudflare account.
Free tier exhausted, balance < $2
Blocked. The user sees a prompt to add credits to their Cloudflare account.
/ai-gateway-billing/credit_balance) and cached for 5 minutes. The platform never holds any money — topping up means adding credits directly in the Cloudflare dashboard.
Connecting Cloudflare
Billing is tied to the Cloudflare gatekeeper. OAuth tokens for accessing the Cloudflare billing API live in the gatekeeper’s connected account, and the billing flow obtains a usable token viagetUsableAccessToken().
A user connects Cloudflare in one of two ways:
- Sign in with Cloudflare — if
cloudflareis inAUTH_GATEKEEPERS, the user can sign in via Cloudflare. After sign-in they’ll be prompted to connect the full gatekeeper to enable billing. - Connect via the Connectors panel — if the user signed in another way, they can connect Cloudflare from the Connections tab using
AuthenticatedApi.connectAccount("cloudflare"). This requests the full scopes and persists the connection.
Configuration
Enable billing by settingENABLE_CLOUDFLARE_LIMITS=true along with the AI Gateway variables. Set these in .dev.vars for local development or as wrangler secrets/vars in production.
The
CF_AI_GATEWAY_API_TOKEN needs AI Gateway Run and AI Gateway Read permissions. The Run permission lets Gadgets execute models through the gateway; the Read permission lets them retrieve each request’s cost for user-visible accounting. The gateway may live in the worker’s own account or a separate one — the CF_AI_GATEWAY_ACCOUNT_ID specifies which account owns it.Workers AI routing options
Workers AI is handled separately from other providers because it can run via a binding (in the wrangler process) or via the REST API (over HTTPS). You have three options:| Variable | Effect |
|---|---|
CF_AI_GATEWAY (default) | Workers AI uses the same gateway ID as other providers. |
CF_AI_GATEWAY_WAI=your-wai-gateway | Route Workers AI through a different AI Gateway in the same account. |
CF_AI_GATEWAY_WAI_DIRECT=true | Bypass the gateway entirely for Workers AI and call the Workers AI REST endpoint directly using CF_AI_GATEWAY_ACCOUNT_ID / CF_AI_GATEWAY_API_TOKEN. Requests produce no cost logs. |
Topping up credits
When a user’s free tier is exhausted and their balance is below $2, they are shown a prompt with a link to add credits. Credits are added directly in the Cloudflare AI Gateway dashboard — the platform never intermediates payments: dash.cloudflare.com → AI → AI Gateway → Credits The balance is cached for 5 minutes on the user’sUserDurableObject, so after a top-up the user may need to wait up to 5 minutes before the new balance is reflected.