Get the free coreGet core

OpenAI API key leaked: how to revoke it and check usage

Updated 6 min readSection 05 / Threats
SHORT ANSWER

If your OpenAI API key leaked, create a replacement key with the same permissions, switch your app to it, then revoke the old key. Revoking is the most important step. If the key is already being abused, revoke it first and accept that the app stops until the new key is in. Next, open the Usage page to see what the key did, then set spend alerts and a hard spend limit. Taking the key out of your code does not stop anyone from using it.

An OpenAI key is billed to your account, so a leak is a money problem as well as a privacy one. The steps below are short. They assume you can reach your OpenAI account and change the key in your app.

01What to do in the first minutes

  1. Create a new key on the API keys page of your OpenAI account. Give it the same permissions as the old one.
  2. Put the new key into your app and check that the app works.
  3. Revoke the old key in the same place.
  4. Open the Usage page of your OpenAI account and look at what the old key did.
  5. Set spend alerts and a hard spend limit.
  6. Find out where the key leaked from, and fix that.

The exception is a key you can see being abused right now. Then do step 3 first. The app will stop until step 2 is done, and that is the cheaper outcome. A leaked secret counts as compromised immediately, and removing it from the code does not undo that.

The API keys page is at platform.openai.com/settings/organization/api-keys.

02Two ways to order it

The right order depends on whether anyone is using the key against you right now.

Your situationOrder
Nothing suggests abuse and the app must stay upCreate the replacement, update the app, revoke the old key once the replacement works
The key is being used against youRevoke first. The app stops until the new key is in

In both cases revoking is the step that makes the leaked copy useless. The first row is the same sequence used for a routine rotation, so it is the one to practice before you need it. Whichever row fits, do the Usage check and the spend limit afterwards, because a replacement key does not tell you what the old one already did.

03Deleting the key from your code is not enough

Removing a secret from the codebase, pushing a new commit, or deleting and recreating the repository does not prevent it from being exploited. The leaked copy keeps working until the provider revokes it. If your OpenAI API key leaked through a repository, the steps for the repository itself are in API key exposed on GitHub.

04What a stolen key can cost you

A secret key works like account credentials, a username and password in one string. If an unauthorized party obtains it, they can make unauthorized charges, access customer data or disrupt your integration. For an AI provider the part that hurts first is usually the bill, which is why spend alerts and a hard limit belong next to key hygiene.

How long do you have? Automated scanners can locate publicly exposed secrets in minutes, and malicious actors can exploit them within hours. If the key was never public, you may have more time. You cannot know, so act as if you do not. One reported Gemini bill shows how fast a charge grows. It is told in what an API key is.

05Replace the key, then revoke the old one

Revoking is the most important step. The provider is the most reliable source on whether a key is still valid. Replacing first keeps your app running during the switch: create the replacement, update your applications to use it, and revoke the old key once you have verified that the replacement works.

The new key has a new value, so update every place that held the old one: your .env file, your CI settings and your teammates' machines. Anything still sending the old key will fail once it is revoked, and that failure tells you where you missed a copy.

06Check the Usage page

The Usage page is where key usage can be monitored once tracking is enabled. Look at the days since the key could have been seen by anyone. You are looking for requests you did not make: a spike, a model you never call, a time when you were asleep.

One caution on old keys. If the key was generated before Dec 20, 2023, tracking was not enabled by default. Then an empty Usage page does not prove the key was never used. You can turn tracking on going forward on the API key management dashboard. Look at your billing totals instead and compare them with what you expect.

07Cap the cost of the next leak

Alerts and limits are different tools. Spend alerts on the limits page of your OpenAI account send a notification when usage passes a dollar amount, and a hard spend limit enforces a monthly cap. Use both. The alert tells you, and the limit stops the bill. Alerts can arrive hours late, as one reported case showed.

08Find out where the OpenAI API key leaked

A fix that skips this step leaves the leak open for the next key. Three typical routes:

Where it leakedWhat to do
A public repositoryRevoke the key first, then clean up. Follow API key exposed on GitHub. Bad actors continuously scan public repositories for keys
A coding agent session or a .env file the agent could readRevoke, then move keys out of the agent's reach. Commands inside Claude Code's sandbox inherit its environment, secrets included. In auto mode, reading .env and sending credentials to the matching API is allowed by default. See Claude Code and .env files
A Mac that ran something maliciousMalicious packages search for .env files and SSH keys. Rotate from a different, trusted computer and go through the infostealer check

09Make the next key less dangerous

Small changes shrink the damage of a future leak:

10After the emergency

Write down which key leaked, where and when. Then widen the circle by one step. If the leak came from a file on your Mac, other keys in that file were exposed too, and a backdoor that searches the whole filesystem for credential and secret files does not stop at one. If you find one sensitive key, assume it has been exposed and compromised, and rotate the neighbors the same way as the first.

11Where to go from here

When your OpenAI API key leaked, replace it, revoke the old one and read the Usage page. Alerts and a hard limit protect the next key. The first-hour checklist covers any provider, and what an API key is explains why a key can cost money. If the leaked key was an AWS one, the steps differ: see AWS access key leaked.

FAQ

What happens if my API key is leaked?

Anyone who holds it can make requests on your account, at your cost. Automated scanners can find publicly exposed secrets in minutes, and malicious actors can exploit them within hours. So revoke the key first.

Why is exposing API keys bad?

A key works like account credentials, a username and password in one string. If it is exposed, treat it as compromised at once.

How do I get an OpenAI API secret key?

You get it on the API keys page of your OpenAI account. For an application, a project service account is recommended over an individual developer's key, and so is an expiration date on project keys.

Can I see what a leaked OpenAI key was used for?

Use the Usage page, where key usage can be monitored once tracking is enabled. Keys generated before Dec 20, 2023 do not have tracking on by default.

How do I limit what a stolen key can cost?

Set spend alerts on the limits page, and set a hard spend limit to enforce a monthly cap.

Is deleting the key from my code enough?

No. Removing a secret from the codebase or pushing a new commit does not prevent it from being exploited. Revoke it with the provider.

Sources

  1. OpenAI docs, production best practices
  2. GitHub docs, remediating a leaked secret
  3. Stripe docs, API key best practices
  4. Claude Code docs, permission modes
  5. Claude Code docs, sandboxing
  6. Cursor, extension security notice
  7. Artificial Immunity
Keys are read from the Mac where your agents work. Artificial Immunity watches program starts there and reports what it finds.Get the free core