API keys: a password that can cost you money
An API key is a secret code that identifies your account when a program calls a service such as OpenAI's or Anthropic's models. Anyone who has it can use the service in your name and at your expense, so treat it like a banking password: never share it and never publish it.
Data as of October 22, 2026
Key figures
| Level | Intermediate |
|---|
A simple definition
When a program calls an API, the provider needs to know who is asking, so it can apply the right limits and send the bill to the right account. That’s the API key’s job: a long string of characters, created in the provider’s developer console, that the program attaches to every request. At Anthropic, for instance, it goes in the request’s Authorization header.
OpenAI’s documentation is blunt: your API key is a secret; don’t share it with others or expose it in client-side code such as browsers or apps.
A concrete example
A developer builds a small assistant for her team and pastes her key straight into the code, “just for testing”. She then pushes the project to a public repository. Anyone can now read the key, copy it and use it, and the charges pile up on her account. OpenAI itself names keys committed to source code as a common way credentials get compromised. The right approach: store the key in an environment variable (such as OPENAI_API_KEY or ANTHROPIC_API_KEY, which the official SDKs read automatically) and never write it into a file you publish.
Security best practices
- Never share or publish it: not in an email, a screenshot, a forum post, a code repository or a web page.
- Keep it server-side. A key embedded in a mobile app or website can be extracted by any visitor.
- Use environment variables or a secrets manager, as both OpenAI and Anthropic recommend.
- One key per person or per use case, so you can revoke one without breaking everything.
- Set an expiration and rotate keys regularly; Anthropic lets you choose an expiration when you create a key.
- Cap spending in the console and monitor usage: it’s your safety net alongside rate limits.
- Revoke immediately any key you suspect has leaked.
These rules matter even more for autonomous agents: a broadly scoped key handed to a poorly supervised agent, or exposed to prompt injection, can lead to unwanted spending or actions. Our Can I trust an agent with this? tool helps you weigh those risks.
Common myths
- “My repository is private, so the key is safe.” A private repo can be made public, shared or cloned; the key doesn’t belong there.
- “I’ll delete the key later.” A published key can be copied very quickly, and a repository’s history keeps it even after you delete the file: revoke it right away.
Related terms: API, rate limit, price per million tokens, prompt injection, sandboxing.
Related terms
- API · An interface that lets one program send requests to a service, such as an AI model, and get its responses back automatically.
- Rate limit · A cap set by an AI provider on how many requests or tokens you may send within a given time window, often per minute.
- Price per million tokens · How AI APIs bill usage: one rate per million tokens the model reads as input, and another, usually higher, per million it generates.
- Prompt injection · An attack that slips hidden instructions into text an AI reads, to make it ignore its original instructions or act against its user.
- Sandboxing · Running an AI agent inside an isolated environment with restricted files, network and commands, so any damage stays contained.
Try it
Full glossary
Frequently asked questions
What should I do if my API key leaks?
Disable or delete it immediately in the provider's console, create a new one and swap it into your tools. Then review your usage history and billing. OpenAI's documentation says key revocations take effect within a few seconds.
Should I give my key to an extension or website that asks for it?
Only if you trust the developer and understand what they will do with it. A key handed to a third-party service can be used without limit at your expense. Use a dedicated key with a spending cap that you can revoke without affecting anything else.
Is an API key the same as my ChatGPT or Claude password?
No. Your password opens the consumer app; the API key unlocks the developer platform, which is billed separately by usage. Both are confidential, but the key is meant to be used by programs.
Can a team share one key?
Providers advise against it. OpenAI recommends a unique key for each team member; Anthropic recommends personal keys for your own work and service account keys for shared or automated workloads.
Data sources
Data as of October 22, 2026
Report an error
Does a figure, a date or a fact look wrong? Tell us: every report is checked.