Set an API spending limit before you paste your key into any tool
Updated First published
In this guide (7)
The facts on this page come from OpenAI’s Best Practices for API Key Safety, Anthropic’s API key best practices, rate limits and spend limits, API credits for subscribers and Why is Claude API usage billed separately, OpenCode’s Console docs on budgets and billing, and Lovable’s usage limits and alerts, all checked October 21, 2026. Menus and names change, so check the provider’s page if something looks different. None of the links on this page are affiliate links.
What is an API key, and why does a limit matter?
An API key is a password-like code that lets a program use an AI model, like Claude or GPT, and bill your account for it. Anthropic’s help page puts it simply: much like a credit card number, if someone gets your key and uses it, they run up charges on your behalf.
That’s why the limit comes before you paste the key anywhere. Once a key leaks, from a tool, a screenshot or code you shared, it can keep working and spending until you delete it. A spending limit doesn’t stop the leak, but it caps how much the key can cost you.
A limit is not an alert
These two sound alike, but they do different things:
- An alert sends you an email or a notification when spending reaches an amount. The requests keep going.
- A limit with blocking (OpenAI calls it hard enforcement) stops new requests once the amount is reached.
OpenAI says it plainly: “Spend alerts alone don’t stop API traffic.” It also warns that hard enforcement can block legitimate requests, and that spend may slightly exceed the limit because enforcement isn’t instantaneous. Lovable’s credits work the same way: its docs say low-balance alerts never block usage.
So set a real limit. Alerts are a nice extra, not protection. A blocking limit also caps the damage when an agent gets stuck retrying the same fix: see AI agent stuck in a loop and burning credits.
Step by step, per provider
OpenAI
From OpenAI’s best practices:
- Set a monthly spend limit for your organization or for each project.
- Turn on hard enforcement, so the limit actually blocks requests.
- Check the Usage page from time to time, to see whether the usage matches your own work.
- Optional: project keys can have an expiration date.
Claude Console (Anthropic)
Anthropic’s docs say each usage tier (Start, Build and Scale) carries a monthly spend cap, and you can set your own, lower limit:
- In the Claude Console, open the Billing page (platform.claude.com/settings/billing).
- In the Spend limits section, click Set limit (or Adjust limit if you already have one).
- Enter the amount. It can’t be higher than your tier’s cap.
One workspace per project. You can set custom spend limits per workspace, except on the default workspace, where you can’t set one. Anthropic’s advice: “To cap spending on a project, give it its own workspace and set that workspace’s spend limit.”
When you reach your limit, requests fail with an error that starts “You have reached your specified API usage limits” (or “You have reached your specified workspace API usage limits” for a workspace limit), and it says when access resumes. Anthropic’s spend caps reset on the first of each calendar month. To get access back sooner, raise or remove the limit.
To keep an eye on things, Anthropic recommends regularly reviewing the Logs and Usage pages.
OpenCode Console
OpenCode Console uses budgets. Owners and admins set them in the Budget limits panel, on the Members and Keys pages:
- Open Members or Keys.
- In Budget limits, click Monthly workspace budget.
- Enter a Workspace limit in US dollars and click Save.
You can also set a Monthly default user budget for every member, and a custom limit for one member. A request has to fit under both the workspace budget and the person’s own budget. OpenCode’s docs are clear about the default: “Without any budget, spend is unlimited.”
When a budget is reached, new requests fail with HTTP 429 and the message Upstream request failed: Account budget exceeded, until the budget resets or an admin raises it. Budgets reset on the 1st of each month at 00:00 UTC.
Lovable (credits, not your own key)
Lovable bills you in credits, not through your own API key, but the same idea applies. In Workspace settings → Usage limits & alerts, you can set a credit limit for the workspace, a project, a member, a group or an access token, and choose to block usage when it’s reached. It’s available on all paid plans, and only workspace owners and admins can set it. A low-balance alert, on the other hand, only notifies you.
Auto-reload and auto-recharge
Many providers can buy more credits for you automatically when your balance runs low. It’s convenient, and it’s also how a leaked key keeps spending.
- Claude Console: Anthropic’s help page describes auto-reload as a threshold at which your account automatically charges the card on file. It says: “Carefully consider auto-reload limits”, because they also act as a safeguard against unexpected high usage from leaked keys or mistakes in your code.
- OpenCode Console: auto-recharge runs on a rule you set (the balance that triggers a charge and the amount to add), and it charges your default payment method. If a charge fails, auto-recharge turns off until you set it up again.
- Claude Code: if you choose to use API credits in Claude Code and auto-reload is on in your Console account, Anthropic says more credits are added automatically when your balance runs low.
While you’re learning, keep auto-reload off, or set it low enough that you’d be fine losing that amount.
A 5-minute checklist before you paste a key
- One key per tool. Anthropic recommends separate keys for different purposes, so if one leaks, you disable just that one.
- A hard limit is set at the provider (and in the tool too, if it has budgets), and it blocks requests.
- Auto-reload or auto-recharge is off, or set low.
- The key isn’t in browser or mobile app code. OpenAI says never to deploy your key in browsers or mobile apps, and to route requests through your own backend.
- The key isn’t in your repository. OpenAI says never to commit your key. Use environment variables instead.
- .env is in .gitignore. Anthropic says that if you keep keys in a .env file, you must add it to your .gitignore so it’s never shared by accident.
- You trust the tool. Anthropic says that when you give your key to a third-party tool, you give that tool’s developer access to your Claude Console account. If you don’t trust their reputation, don’t trust them with your key.
To check your app for keys and other data it might be leaking, see Could my app be exposing personal data?. For the rest of the habits, like keeping keys out of browser code, the AI chat and git, see How to keep API keys safe in a vibe-coded project.
Monthly plan or API key?
People mix these up, so here’s what the sources say.
A monthly plan, like Claude Pro or Max, is a subscription. An API key belongs to an API account, which is billed separately. Anthropic says paid Claude plans and the Claude Console are separate products, and “the Claude API is billed separately”. When you sign in to a tool with your plan, for example Claude Code with your Pro account, you’re not using an API key at all. And the monthly API credits that come with Max go to a Claude Console organization; Anthropic says that when they run out, “usage is never charged to your Claude plan”.
One thing to watch: if an ANTHROPIC_API_KEY environment variable is set on your computer, Claude Code uses that key instead of your plan and you pay API rates. More in Claude Code for beginners: which plan to pick. If you’re thinking of moving Claude Code to an API key because your plan’s limit runs out fast, first see what is using it: Claude Code usage limit draining fast?
What if a key leaks?
Delete it at the provider right away, because that’s what stops it. Then create a new one and put it in the tool:
- Claude Console: open the API keys page, click the three dots next to the key and select Delete API key.
- OpenAI: rotate the key immediately from the API Keys page, then check the Usage page. For more help, contact help.openai.com.
For a full walkthrough, including Gemini keys, see OpenCode breach: what to do with your AI API key.
Questions people ask
Does a spend alert stop charges?
No. OpenAI says spend alerts alone don't stop API traffic. To stop requests, set a monthly spend limit with hard enforcement. Lovable's docs say the same kind of thing about its credits, where low-balance alerts never block usage and only a usage limit with blocking turned on does.
Can a limit be exceeded?
Slightly, yes. OpenAI says spend may slightly exceed the limit because enforcement isn't instantaneous. OpenCode says budgets are checked when a request starts and requests already in progress finish, so spend can end slightly above the limit. A limit still keeps a leaked key from spending without end.
Where do I set a spend limit in the Claude Console?
On the Billing page (platform.claude.com/settings/billing). In the Spend limits section, click Set limit, or Adjust limit if you already have one, and enter a value. It can't be higher than your tier's monthly cap. You can also set a spend limit for each workspace, except the default workspace.
If I only pay a monthly plan, can a leaked API key charge it?
An API key bills the API account it belongs to, not your monthly plan. Anthropic says paid Claude plans and the Claude Console are separate products and the Claude API is billed separately, and for the API credits that come with Max, "usage is never charged to your Claude plan". A leaked key can still spend whatever its API account allows, which is why the hard limit matters.
Should I turn off auto-reload?
While you're learning, keep it off or set it low. Auto-reload and auto-recharge charge your card or payment method automatically when your balance runs low, so with a leaked key they keep the spending going. Anthropic says to carefully consider auto-reload limits, because they also act as a safeguard against unexpected usage from leaked keys.
New guides by email. An email when we publish a new guide, error fix or chapter, in plain English. Subscribe