What is an AI API key: 4 ways to protect your billing

What is an AI API key? A code that links your app to an AI model and bills your account. Follow 4 ways to keep the key safe and stop surprise charges.

16/11/2026 · 8 min read
What is an AI API key: 4 ways to protect your billing
In this article (5)
  1. What is an AI API key
  2. 4 ways to protect your billing
  3. When a key may have leaked, act at once
  4. Conclusion
  5. Sources

What is an AI API key? It is a unique code that lets your app call an AI model, and every call bills your account. Whoever holds the key can spend your quota, so a leaked key means leaked money. This article explains what a key is, where to get one, and 4 ways to keep it safe.

What is an AI API key

Close-up of code on a computer screen
Close-up of code on a screen, where an AI API key is used to call a model. Photo: Abdul Kayum, Pexels 10845119.

An API key is the code your app sends with every call to an AI model. OpenAI describes a key as a unique code that identifies your requests to the API. With that code, the provider knows which account to bill and whose usage limit has been reached. A key is not your login password. Your password opens your account on the website. A key opens the API path from inside code, and code runs on its own, so keys usually sit ready in a server, an environment variable, or a config file.

Where do you get a key? With OpenAI, you create one on the API Keys page of the platform. With Anthropic, keys live in the Claude Console, on the API keys page. With Google Cloud, you create a key on the Credentials page of the console. The flow is the same at its core: you name the key, pick its permissions and an expiry date if offered, then copy the key right away. Anthropic says the full key shows only once, at creation, starts with sk-ant-, and if you lose it you cannot view it again, so you must create a new key instead.

Why is a key tied to money? Every call to an AI model costs computing power, and the provider bills by usage. The key is what links that call to your account. A person holding your key can call the API on your quota without knowing your password. OpenAI warns that a compromised key lets someone gain access to your account quota, and the result can be data loss, unexpected charges, a depleted monthly quota, and interrupted API access.

The risk grows when an app calls the API for you many times. An agent tool that works in many steps calls the model again and again, so a leaked agent key burns money faster than a key used for one question and answer. When you write what a prompt is and how to write one for an app that calls the API, the key travels with every call, so guarding the key matters as much as writing a good prompt. Knowing what an AI API key is comes first, and the four ways below are the daily work.

4 ways to protect your billing

A laptop with coding software and a coffee mug on a desk
A laptop running coding software, the everyday place where an AI API key gets used. Photo: Daniil Komov, Pexels 34803973.

OpenAI lists 8 best practices for API key safety, Google Cloud has its own page on managing keys, and Anthropic has a guide for getting and using keys. The four ways below gather the most important points shared by all three sources, in the order you should act on them.

Way 1: Give each person their own key, never share one

OpenAI is direct: always use a unique API key for each team member on your account, and sharing API keys is against the Terms of Use. When your team grows, invite new members to your account from the Members page, and they receive their own unique key upon sign-in. You can also assign permissions to individual API keys.

Anthropic splits keys by the kind of work. A personal key acts as you, and stops working if you leave an organization. A service account key represents a service account, and fits workloads such as CI pipelines, production services, or agents. Use a personal key for your own development, and a service account key for anything shared. A workspace key is a legacy key without an owner, and Anthropic prefers personal or service account keys instead.

A shared key hurts in two ways. First, you cannot tell who leaked it when something goes wrong. Second, revoking a shared key stops the whole team at once. With one key per person, revoking affects only that person, and the usage log shows clearly who called what.

Way 2: Keep the key out of source code, use environment variables

OpenAI warns never to commit your key to your repository. This is a common way for keys to leak. With a public repository, you share your key with the internet without knowing it. With a private repository, a data breach can still expose your keys. The safe way is to use environment variables instead of a key written into code. OpenAI suggests setting the variable name to OPENAI_API_KEY, and keeping that name the same across your team, so everyone can commit and share code without the risk of exposing the key.

Anthropic follows the same line. Its client SDK reads the ANTHROPIC_API_KEY variable on its own, and direct HTTP requests send the key as an Authorization Bearer header. Either way, the key stays out of source code. Store the key in a secrets manager, since Anthropic says plainly: copy the key and store it somewhere safe, such as a secrets manager.

For apps running in production, OpenAI suggests looking at a key management service. These tools control access to your keys and keep them encrypted in a separate location, so if your app is breached, your keys are not exposed with it.

Way 3: Limit permissions, set an expiry date, rotate keys on a schedule

The longer a key lives and the more it can do, the worse a leak gets. OpenAI suggests creating keys with an expiration date and building a regular rotation process. Before a key expires, create a replacement, update your apps to use it, and revoke the old key once you have checked that the replacement works. Admins can also enforce a maximum key lifetime at the organization or project level, so new keys cannot stay valid forever.

Google Cloud puts it bluntly: unrestricted API keys are insecure. You can restrict a key in two ways. API restrictions limit the key to a specific set of APIs. Application restrictions limit the key to specific websites, IP addresses, or apps. Google Cloud recommends setting both kinds of restrictions, and when you create a key in the console you must add at least one API restriction.

OpenAI adds one more layer with IP allowlisting. When you turn it on, only requests from configured IP addresses or ranges can reach the API, and all others are rejected, even if they carry a valid key. That layer makes sure your key only works from infrastructure you trust, such as your backend servers or your cloud environment.

Way 4: Watch spending and revoke at the first sign of a leak

Even a well-guarded key needs someone watching it. OpenAI suggests setting monthly spend limits for your organization or projects, and turning on hard enforcement to limit surprise costs from a leaked key. Remember this: spend alerts alone do not stop API traffic, only hard enforcement blocks requests over the limit. Your team usage is tracked on the Usage page, and you should check whether usage matches the work your team actually did.

When you believe a key has leaked, OpenAI says to rotate your key at once from the API Keys page. For apps in production, you must update the key value everywhere it is used. After rotating, contact OpenAI through its help center for further investigation. Do not wait to see strange charges first, because whoever holds the key can burn through your quota in a short time.

One way to cut the risk at its root is to use fewer API keys for work that can run on your own machine. Speech to text is one example: Faster Whisper transcribes audio on your own machine, with no cloud upload, so that job needs no API key at all. Fewer keys stored means fewer places for a leak.

When a key may have leaked, act at once

A person typing on a computer keyboard
A person typing on a keyboard, checking a key that may have leaked. Photo: Soumil Kumar, Pexels 735911.

Treat a suspected leak as a real leak. Revoke the suspect key at once and create a new one, then move your apps to the new key before you remove the old key from every place it lived. Check the Usage page for odd requests, odd hours, or odd addresses. If usage jumps while your team did not call that much, someone else is using your key.

Then search every place the key has ever appeared: repositories, screenshots, chat logs, config files, and backups. Deleting one commit is not enough when a key sat in commit history, because the old key still sits in that history, so you must still revoke it. Last, set fresh spend limits and API restrictions on the new key, and note the rotation date so the next rotation happens on time.

Conclusion

An AI API key is the key to your billing account, so guarding the key is guarding your money. The four ways fit in one short list: one key per person, no key in source code, limited permissions with regular rotation, and spending watched with fast revocation at the first sign of a leak. Do all four and even a leaked key causes only small harm. Open your key management page today and check which keys have lived too long or never had a limit set.

Sources (7)

Linh SEO Agent

The complete guide for this topic: What Is Claude Code? Pricing, Setup and Who It's For

Found it useful? Share it:
Facebook

More reading

Latest news

The ten most recent, scrolling sideways. Hover or tap to pause.