Use spend alerts to track monthly API costs. To stop traffic after tracked spend reaches a configured amount, enforce a hard spend limit for your organization, an individual project, or an API key.
Hard spend limits can interrupt production traffic. When tracked spend reaches
an applicable hard limit, affected API requests return a 429 error with the
organization_spend_limit_exceeded, project_spend_limit_exceeded, or
api_key_spend_limit_exceeded code. Enforcement is not instantaneous, so
recorded spend can slightly exceed the configured amount.
Choose a spend control
Spend alerts and hard spend limits have different effects:
| Control | What happens at the configured amount | Use it when you want to |
|---|---|---|
| Spend alert | Sends a notification; API traffic continues | Track spend without interrupting traffic |
| Hard spend limit | Affected API requests return a 429 error |
Enforce a monthly organization, project, or API key cap |
Spend alerts do not enforce a cap. They remain active when you add a hard spend limit, so you can use alerts for notification before a hard limit interrupts traffic.
OpenAI also assigns your organization an approved monthly usage limit based on its usage tier. This OpenAI-approved usage limit is separate from the spend limits you configure.
Configure a spend limit
You need permission to manage the applicable organization or project settings. For details, see API Platform permissions.
- Go to Organization limits.
- In Spend, select Edit spend limit.
- Enter the Monthly spend limit.
- To make API responses fail after the organization reaches the limit, turn on Enforce a hard limit.
- Select Save.
- Go to Project settings.
- Select Limits.
- In Spend, select Edit spend limit.
- Enter the Monthly spend limit.
- To make API responses fail after the project reaches the limit, turn on Enforce a hard limit.
- Select Save.
- Go to API keys.
- Edit the API key you want to limit.
- Under Monthly spend limit, select Set hard limit.
- Enter the amount in USD.
- Select Save.
To remove the limit, edit the key, select No limit, and save. You can also set a monthly limit when creating an API key.
To automate key limits with an Admin API key, update an API key using POST /v1/organization/projects/{project_id}/api_keys/{api_key_id}. Set spend_limit to an object such as {"threshold_amount": 10000, "currency": "USD", "interval": "month"} for a $100 monthly hard limit, or set it to null to remove the limit.
You can also provide spend_limit when creating a service account with an initial key or issuing a service account API key. For a configured limit, returned keys include spend_limit.enforcement.status: enforcing while the limit blocks requests and inactive otherwise.
Understand hard-limit behavior
Organization, project, and API key hard limits can all apply to a request:
- An organization hard limit applies to API traffic across all projects in the organization.
- A project hard limit applies only to API traffic billed to that project.
- An API key hard limit applies only to requests made with that key. Other keys can continue unless another applicable limit blocks them.
- Reaching an organization hard limit returns a
429error with theorganization_spend_limit_exceededcode. - Reaching a project hard limit returns a
429error with theproject_spend_limit_exceededcode. - Reaching an API key hard limit returns a
429error with theapi_key_spend_limit_exceededcode. - Raising or removing the reached limit allows traffic to resume after the update propagates. Otherwise, the limit resets with the next monthly cycle.
Enforcement is not instantaneous. The API Platform can process a small amount of extra usage while the limit state propagates, so recorded spend can slightly exceed the configured amount.
Spend alerts
Use spend alerts to get notified before spend reaches a hard limit. Add alerts at thresholds that allow time to adjust usage, raise the limit, or investigate unexpected traffic.
Restore API traffic
If requests fail because of a billing-related limit or credit balance:
- Check
error.codeto identify the reached spend or usage limit, or determine whether the organization exhausted its prepaid credits. - For
organization_spend_limit_exceeded,project_spend_limit_exceeded, orapi_key_spend_limit_exceeded, compare current usage with your spend limits. Raise or remove the reached limit if traffic should resume before the monthly reset. - For
organization_usage_limit_exceeded, request a higher approved usage limit. - For
credit_balance_exhausted, add credits. - If the error reports a request or token rate limit, follow the rate limit guide.