---
title: "OpenAI API Spending Limit: Budgets, Alerts, Prepaid Credits & Script"
description: "Cap OpenAI API spend in four layers: budgets and alerts, project limits, prepaid credits, and a daily usage script that limits damage from leaks or loops."
url: https://cdnping.org/en/blog/openai-api-set-spending-limit
language: en
published: 2026-11-14
category: "Developer tools"
tags: ["OpenAI API","spending limit","usage alerts","budget","API key security"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# OpenAI API Spending Limit: Budgets, Alerts, Prepaid Credits & Script

Setting a spending limit on the OpenAI API isn't a single switch. It takes several layers stacked together: an organization-level budget with email alerts, project-level limits, prepaid credit balance, and a usage check you write yourself. Any one layer can lag or have gaps. Stacked, they can keep the damage from a leaked key or a runaway loop within a number you can live with.

One caveat up front: console menu names, and how each limit behaves (alert only, or actually reject requests), change as OpenAI updates the platform. The locations below are only a general guide. Go by OpenAI's official documentation and the interface you see in your own account.

## Why isn't a single budget enough?

Runaway bills usually come from one of two situations:

- A leaked key that someone else is using to burn through your quota.
- A bug in your own program, such as unlimited retries after failures, repeated calls inside a loop, or an agent stuck talking to itself.

Both are fast and noticed late. Usage stats are themselves delayed, and email alerts only fire after a threshold is crossed. So you need to limit how much money *can* be spent, not just get reminded after it has been spent.

## Layer 1: organization budget and alert emails

In the API platform's organization settings, find the Billing and Limits pages. You can usually set:

1. A monthly budget.
2. Email alert thresholds that trigger at a given amount.
3. Notification recipients.

Recommendations:

- Set alerts at 50% and 80% of the budget, not just 100%.
- Use an address that someone actually reads, not an old personal inbox nobody checks.
- Check the docs or the UI to confirm whether hitting the budget only sends a notification or also rejects further requests. The rules have differed over time, so go by the official description and don't assume it will hard-stop.

## Layer 2: project-level limits

If you run several applications in one organization, create a separate Project for each and set limits and model permissions per project. That way one application going wrong doesn't drain the whole organization's quota. For details, see [How to use OpenAI project API keys: permissions and limits per project](/en/blog/openai-project-api-keys-permissions).

For teams, combine this with one key per person and a split budget. See [How teams should manage API keys: one key per person, budget splits and rotation](/en/blog/team-shared-api-key-management).

## Layer 3: use prepaid credits as a hard cap

How OpenAI API billing works (prepaid or postpaid) depends on your account and the official docs. If your account is in prepaid credits mode, requests fail once the balance runs out, which is the most direct hard cap.

- Choose the top-up amount based on the maximum loss you can absorb, not on your expected usage.
- If auto recharge is on, set both the trigger threshold and the amount per recharge. For personal projects and test environments, my recommendation is to turn it off and top up manually.
- A low balance makes production requests fail, so production needs a balance alert and a failure-handling plan. See [How to build LLM API failover](/en/blog/llm-multi-provider-failover-circuit-breaker).

Your account's usage tier also relates to rate and usage limits (exact values are in the official docs), which indirectly bounds the worst-case loss as well. See [What are OpenAI API usage tiers and how to upgrade](/en/blog/openai-api-usage-tiers-explained).

## Layer 4: a script that checks usage daily

The console's stats lag, so pull the numbers yourself on a schedule. OpenAI provides organization-level usage and cost endpoints (the Usage API and Costs API). They require an Admin key, and the Admin key should never go into application code. Below is an example that queries daily costs for the last 7 days; check the official API docs for parameters:

```bash
START=$(date -d '7 days ago' +%s)   # on macOS use date -v-7d +%s
curl -s "https://api.openai.com/v1/organization/costs?start_time=$START&bucket_width=1d" \
  -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \
  -H "Content-Type: application/json"
```

The response is costs bucketed by day. You can total it with `jq` and send a notification if it goes over your daily budget:

```bash
TOTAL=$(curl -s "https://api.openai.com/v1/organization/costs?start_time=$START&bucket_width=1d" \
  -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \
  | jq '[.data[].results[].amount.value] | add')
echo "Cost over the last 7 days: $TOTAL"
```

The field paths depend on the JSON you actually receive, so print the full response once before writing the jq filter. Put the script in cron to run once a day, and email or push a notification when the limit is exceeded. For the push side, see [Subscribe to status page RSS: push OpenAI and Claude outages to Feishu, DingTalk and WeCom](/en/blog/subscribe-ai-status-rss-to-feishu-dingtalk) (Feishu, DingTalk and WeCom are the team chat apps widely used in China; the same approach works with Slack webhooks). If you'd rather compute usage from your own app's responses and logs, see [How to track API usage yourself](/en/blog/track-ai-api-usage-yourself).

## Limit things in code too

Platform limits are the last line of defense. Your code should stop most problems first:

- Set `max_tokens` (or the equivalent parameter) and a timeout on every request.
- Cap the number of retries, and distinguish errors that should not be retried.
- Set a maximum number of turns for agent loops.
- Log the token usage of every call so you can reconcile later.

## Checklist

| Item | Done? |
| --- | --- |
| Organization budget + 50%/80% alert emails | |
| Confirmed actual behavior once the budget is reached (per official docs) | |
| Separate Project and limits for each application | |
| Top-up amount set to the "maximum tolerable loss" | |
| Auto recharge evaluated, off in test environments | |
| Daily usage script + over-limit notification | |
| max_tokens, timeouts and retry caps in code | |
| Known steps to take after a key leak | |

The last item matters. If you see abnormal usage, revoke the key immediately and check the logs. The process is in [OpenAI API key leaked: revoke it, check usage, prevent a repeat](/en/blog/openai-api-key-leaked-response).

## No overseas credit card, or want less budget babysitting?

The OpenAI API requires an account, a payment method in a supported region, and compliance with its terms of use; check the official documentation for these. If you mainly use models inside tools like Claude Code and Codex, you can also look at aggregation gateways that offer monthly subscriptions payable in RMB. AGI Plan is one of them: one subscription lets you switch between multiple capability groups, no overseas credit card is needed, and Claude Code and Codex only need a single endpoint URL changed. AGI Plan states that it is not an officially authorized channel, and some models are available only outside China; see [agiplan.dev/models](https://agiplan.dev/models) for specifics. AGI Plan is a commercial partner of cdnping. If you want to see how to connect, start at [agiplan.dev/startup](https://agiplan.dev/startup).

Whichever route you use, the budgeting logic is the same: decide the maximum loss first, then decide the usage.

## Next steps

1. Today, set up the organization budget and 50%/80% alerts in the console.
2. Create a separate Project for each application.
3. Run the script above once to confirm you can read the cost data, then put it in cron.
4. To check whether the API itself is healthy, see [OpenAI status](/en/status/openai). If latency is high from within China, see [How to read AI API latency in China](/en/learn/ai-api-latency-china).
