KeyBark

OpenAI spend limit API: set hard limits and alerts in code

If you create a project per agent, customer or environment, clicking through Project settings → Limits for each one doesn't scale. OpenAI's Admin API can now set the same hard spend limits and spend alerts. Here are the endpoints, the units, a per-agent setup you can script, and what the API still can't do.

Last reviewed 14 Nov 2026

The short version

  • Organization cap: POST /v1/organization/spend_limit creates or replaces the monthly hard limit for the whole organization.
  • Project cap: POST /v1/organization/projects/{project_id}/spend_limit does the same for one project. GET reads it, DELETE removes it.
  • Alerts: /v1/organization/spend_alerts and /v1/organization/projects/{project_id}/spend_alerts send an email at a threshold. Traffic keeps flowing.
  • Units: threshold_amount is in cents, currency must be USD, interval must be month.
  • Auth: an Admin API key. The reference lists it as the auth for every one of these endpoints; keep it out of your app.
  • Still no per-key cap, and enforcement isn't instantaneous, so spend can slightly pass the limit.
Want a heads-up before the bill does?

KeyBark checks your OpenAI, Anthropic or OpenRouter spend about every 15 minutes during the beta and emails you when it crosses a daily or monthly threshold you set, or jumps above its 7-day average. It doesn't set or enforce limits for you. Free for one key, no card.

Hard limit or alert: which endpoint does what

EndpointScopeAt the threshold
POST /organization/spend_limitAll projectsRequests fail with 429 organization_spend_limit_exceeded
POST /organization/projects/{id}/spend_limitOne projectRequests fail with 429 project_spend_limit_exceeded
POST /organization/spend_alertsAll projectsEmail to the recipients you list; traffic continues
POST /organization/projects/{id}/spend_alertsOne projectEmail to the recipients you list; traffic continues

This matches the dashboard: per OpenAI's spend limits guide, a spend alert “sends a notification; API traffic continues”, and a hard limit makes affected requests return 429. Both organization and project hard limits can apply to the same request. OpenAI's own approved monthly usage limit for your tier is separate from all of these (it fails with organization_usage_limit_exceeded).

1. Set a project hard limit

From the Update project spend limit reference (read 14 Nov 2026), a $100 monthly cap on one project:

curl -X POST "https://api.openai.com/v1/organization/projects/$PROJECT_ID/spend_limit" \
  -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{"threshold_amount": 10000, "currency": "USD", "interval": "month"}'
  • It's create or replace: posting again overwrites the amount, so the same call raises or lowers the cap.
  • threshold_amount is an integer in cents with a minimum of 1. Sending 100 when you meant $100 gives you a $1 cap, and that project's traffic stops almost at once.
  • The response is a project.spend_limit object with the amount, currency, interval and enforcement.status, which the reference lists as inactive or enforcing and describes only as “whether the hard spend limit is currently enforcing”. The docs don't explain more than that, so don't build alerting on that field alone.
  • Read it back with GET on the same path; DELETE removes the cap and returns {"object": "project.spend_limit.deleted", "deleted": true}.

The organization version (reference) is the same body on /v1/organization/spend_limit. The Admin APIs guide uses it for a $100 organization cap.

2. Add alerts below the cap

A hard limit with no warning means the first sign of trouble is your app failing. Add spend alerts at, say, 50% and 80% of the cap (Create project spend alert):

curl -X POST "https://api.openai.com/v1/organization/projects/$PROJECT_ID/spend_alerts" \
  -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{"threshold_amount": 8000, "currency": "USD", "interval": "month",
       "notification_channel": {"type": "email", "recipients": ["oncall@example.com"],
                                "subject_prefix": "OpenAI agent-a"}}'
  • Email is the only notification_channel.type the reference lists. There's no webhook, so a script can't react to the alert directly.
  • Each alert gets an id; list, update and delete them under the same path. The organization-wide version is /v1/organization/spend_alerts.
  • Alerts stay active when a hard limit is also set, per the spend limits guide.

3. A per-agent setup you can script

OpenAI has no per-key spend limit, so the unit of control is the project. For each agent, customer or environment that needs its own ceiling:

  1. Create a project: POST /v1/organization/projects with a name you'll recognise in reports (reference). Projects can be archived but not deleted.
  2. Give it its own key: POST /v1/organization/projects/{id}/service_accounts returns a service account with an api_key.value you store once (reference). Set expires_in_seconds if you want it to expire.
  3. Cap it: POST …/spend_limit with the monthly amount in cents.
  4. Warn before the cap: one or two POST …/spend_alerts below it.
  5. Check it took: GET …/spend_limit, and keep the project ID next to the key in your own records.

To see which project is spending, the Costs API groups daily cost by project_id (and by api_key_id, see OpenAI cost per API key).

What the spend limit API can't do

  • No per-key or per-day cap. Limits are monthly and attach to the organization or a project. A leaked key can spend the whole project's month in one day.
  • Not exact. The guide says enforcement is not instantaneous and recorded spend can slightly exceed the amount. Set the cap with that headroom in mind.
  • Not a key kill switch. A hit limit blocks the project's traffic until you raise or remove it or the month resets; it doesn't revoke the key. For a leaked key, delete it first: OpenAI API key leaked.
  • USD and monthly only for now, per the reference.
  • The Admin key is powerful. It can raise or delete every cap. Keep it in your provisioning job only, never in app servers, and give it an expiry (expires_in_seconds when you create it with the API).

If your caps are set but requests still went through, see OpenAI spend limit not working. If requests fail with credit_balance_exhausted instead, that's your prepaid balance, not a spend limit: OpenAI credit_balance_exhausted.

Checklist

  1. One project per agent, customer or environment that needs its own ceiling.
  2. A project hard limit on each, in cents, with headroom for enforcement lag.
  3. Spend alerts at about 50% and 80% of each cap, sent to an inbox someone reads.
  4. An organization hard limit above the sum you expect, as a last stop.
  5. The Admin key only in the provisioning job, with an expiry.
  6. A weekly Costs API check by project_id, and something that tells you the same day when spend jumps.

Not sure your OpenAI (and Anthropic / OpenRouter) limits would stop a leaked key or a runaway loop? The free AI key limit audit walks through them in a few minutes (no sign-up, no key needed). Related: runaway AI agent cost, Anthropic workspace spend limits via the API, OpenAI project model allowlist.

Where KeyBark fits (and where it doesn't)

KeyBark covers the "tell you the same day" item. With an OpenAI Admin key you connect, it reads your organization's costs from our servers about every 15 minutes during the beta and emails you when a daily or monthly threshold you set is crossed, or when today's spend jumps above its 7-day average, including days well below your monthly cap.

Limits, honestly: KeyBark does not set or change OpenAI spend limits or alerts for you, and it watches the organization total, not each project or key. OpenAI's cost data comes in daily buckets and checks run on a schedule, so an alert can arrive after money has been spent. KeyBark is not a proxy and can't block a request; the project hard limits above are what stop traffic.

FAQ

Can I set an OpenAI spend limit with the API?

Yes. With an Admin API key, POST /v1/organization/spend_limit creates or replaces the organization's monthly hard spend limit, and POST /v1/organization/projects/{project_id}/spend_limit does the same for one project. The body is threshold_amount in cents, currency USD and interval month; those are the only currency and interval the reference lists today.

Is threshold_amount in dollars or cents?

Cents. 10000 means $100 a month. The reference gives a minimum of 1 for hard limits and 0 for spend alerts.

Can I set a spend limit on a single OpenAI API key?

No. Hard spend limits exist for the organization and for projects only. To cap one agent or customer, create a project for it, give it its own key (for example a service account key), and set a project spend limit.

Does the hard limit stop spending exactly at the amount?

No. OpenAI's spend limits guide says enforcement is not instantaneous and recorded spend can slightly exceed the configured amount. Requests over the limit get HTTP 429 with organization_spend_limit_exceeded or project_spend_limit_exceeded until you raise or remove the limit or the monthly cycle resets.

Sources (read 14 Nov 2026): OpenAI Spend limits guide, Admin APIs guide, API changelog (hard spend limits 22 Jul 2026; spend alert Admin API 26 May 2026), API reference: organization spend limit, project spend limit, project spend alerts, organization spend alerts, create project, project service accounts, Costs API. KeyBark is not affiliated with OpenAI.

Want a second line of defence?

KeyBark watches your OpenAI, Anthropic and OpenRouter spend from the server and flags a crossed threshold or spike by email and on your dashboard. Free for one key.