If you want to rotate AI API keys, the order matters more than the menus. When a provider reports a breach or you spot a key in a public repo, swap the key without breaking your app and cap what a stolen one could cost. This guide covers OpenAI, Anthropic and Google.
OpenCode says a Console breach it disclosed on October 17 affected about 1.4% of Console users. Our OpenCode breach story has the details.
When to rotate
- A key was pasted into code, a ticket, a chat or a public repository.
- A service you use reports a breach and you linked a provider key there.
- On a schedule. Anthropic suggests every 90 days, for example. OpenAI recommends setting an expiration date when you create a project key.
If you think a key is exposed, OpenAI says to revoke it promptly and replace it.
The safe order
- Create the new key.
- Put it into your app's environment variables or secret store and deploy.
- Check that requests are using the new key.
- Delete or revoke the old key.
OpenAI describes the same sequence: create a replacement, update your applications, and revoke the old key once the replacement works. Google's Gemini page adds the warning to keep the old key until the new one is fully active, so your app does not go down.
If a key is already leaked, do not wait on step 3. Revoke it sooner and accept a short outage.
OpenAI
Keys belong to projects, so rotate per project. Create a replacement, update your apps, then revoke the old key. OpenAI lets you set a key expiration date when you create a project key, which turns rotation into a routine.
For spend, OpenAI separates two controls. Spend alerts do not enforce a cap. A hard limit does:
- Organization: go to Organization limits, choose Edit spend limit under Spend, enter the monthly amount, turn on Enforce a hard limit, and save.
- Project: go to Project settings, then Limits, and follow the same steps.
After a hard limit is hit, API calls fail with a 429 error. Enforcement is not instant, so recorded spend can slightly exceed the amount you set.
Anthropic
Anthropic's guidance is to create new keys and deactivate old ones, and to use separate keys for separate purposes so you can disable one use case without touching the rest. Keys can be scoped to a single workspace.
If a key lands in a public GitHub repository, Anthropic says GitHub notifies it and it deactivates the key automatically.
Spend limits sit in two places:
- Organization: on the Console's Billing page, use Adjust limit (or Set limit) in the Spend limits section. Your limit cannot exceed your tier's monthly cap: $500 on Start, $1,000 on Build, $200,000 on Scale.
- Workspace: on the Workspaces page, select a workspace and click Spend limits to cap monthly spend and set alerts.
At a limit you set, Messages API requests return HTTP 400 (Claude Code workspace limits can return 429 instead). At the tier cap, they return HTTP 429 and usage pauses until the next calendar month.
For a Google Cloud API key, open the Credentials page, click the key, and choose Rotate key. Google creates a new key with the same restrictions, you update your apps, and then you delete the previous key from the Previous key section. Google also says unrestricted API keys are insecure, so keep restrictions on.
Google Cloud budgets come in two kinds. An alerts-only budget does not automatically cap usage or spending. A spend cap budget does: it is enforced once estimated usage costs exceed 100% of the amount, and usage of the covered service is paused for the rest of the budget period.
Requests already in flight finish and still accrue charges, and enforcement uses estimated costs, so the final bill can differ.
To set one, go to Budgets & alerts, click Create new budget, and choose Spend cap enforcement. Pick one project and the Gemini API; enter the monthly amount and click Finish. You cannot convert an existing alerts-only budget, so delete it and create a new one.
The Gemini API is on the list of supported services. A spend cap budget also emails at 50%, 80% and 100% of its amount. Only a spend cap budget pauses usage.
Keep keys out of code
- Read keys from environment variables or a secrets service, not from files in your repo. OpenAI and Anthropic both say this, and Google says not to commit keys to code repositories.
- Add
.envfiles to your ignore list. - Turn on secret scanning in your source host. Anthropic also names Gitleaks for scanning repositories.
- Never put a key in client-side code, a support ticket or a public chat.
Checklist
- Every key has a clear owner and purpose.
- Each project or workspace has its own key.
- Rotation follows the four-step order above.
- A hard cap or spend cap is on, not just an alert.
- Old and unused keys are deleted.
For more on AI tools and services, see our AI apps coverage.



