Skip to content
Submit a tool
SPONSOR SLOT · OPENPut your tool in front of builders choosing one right now. One slot, every page, $50 a week.Your tool here, $50 a weekClaim it

Pick a tool 9 min readUpdated 19 Oct 2026

How to Keep Your API Keys Safe When Using AI Coding Tools

Linked an OpenAI or Anthropic key to an AI coding tool? How to revoke it, set spend limits, check usage and store keys safely after the OpenCode breach.

If a tool that holds your API key may have been breached, do three things now: revoke the key at the provider and issue a new one, set a spend limit, and check your usage for anything you don't recognize. Do this at the provider (OpenAI, Anthropic, Google and so on), not inside the tool, because the provider is where the key is actually switched off. After that, store keys so one leak can't do much damage: one key per tool, kept in environment variables or a secrets manager, with a spend limit on each.

What to do right now if your API key may be exposed

Every provider below documents the same basic order: replace the key, cut off the old one, and look at what it was used for. Links go to each provider's own pages, checked Oct 19, 2026.

OpenAI. Keys live on the API keys page. OpenAI's production guide recommends setting an expiration date on project keys and rotating them: create a replacement, update your apps, then revoke the old key once the new one works. For spending, OpenAI's spend limits guide separates spend alerts, which only notify you, from hard spend limits, which make requests fail with a 429 error once tracked spend reaches the cap. It also says enforcement is not instantaneous, so spend can slightly exceed the limit. Usage is on the Usage page. OpenAI's production best practices note that keys created before Dec 20, 2023 don't track usage by default, so older keys may show usage as "Untracked."

Anthropic. Anthropic's help article on suspected compromised keys recommends revoking the key immediately from the API keys page in the Claude Console, then creating a new one and keeping it in a secrets manager rather than version control. Its authentication docs explain that disabling a key can be undone, while deleting it is permanent, and that setting an expiration limits how long a leaked key stays usable. You can set your own monthly spend limit below your tier's cap on the Billing page, as described in the rate limits docs.

Google Gemini. The Gemini API key docs include a leak response checklist: generate a new key, deploy it, disable the old key once the new one works, then audit billing logs and API usage in the Google Cloud console. The billing docs say you can set project-level spend caps in AI Studio, but they label that feature experimental and warn of overages during a delay of around 10 minutes. Usage is shown in Google AI Studio.

OpenRouter. OpenRouter's Security settings page lists keys with no spend limit, keys that never expire and idle keys, and recommends setting a spend limit to cap what a leaked key can spend. Its key audit guide says disabling a key is reversible and deleting is not, and that you can't add an expiration to an existing key, so you replace it with a new, expiring one using the rotation guide.

xAI. xAI's security FAQ says that if you suspect a compromise, you should open the xAI Console, make sure you're viewing the right team (keys are tied to teams), and disable or delete the key from the API Keys section, then create a new one. xAI's billing management API reference describes a postpaid monthly spending limit and notes that it doesn't limit prepaid credit usage.

What happened with OpenCode Console

This guide exists because of a recent incident at OpenCode, the open-source coding agent. On Oct 17, OpenCode said on X that it had a security breach the night before, linked to a large-scale distillation attack, and that it affected 1.4% of OpenCode Console users. OpenCode said it emailed every affected user that morning, that users who didn't get an email weren't affected, and that the affected users were a subset who had external providers connected.

The next day, OpenCode's Dax Raad reported that attackers had been trying nonstop to get into OpenCode's systems for the past 48 hours. Tibo Sottiaux of OpenAI reported that OpenAI worked with OpenCode to get OpenCode users who are also OpenAI API customers running again, and to refund credits drawn in the attack.

That is everything these posts say. We haven't seen a full write-up from OpenCode, and we don't know which other providers were affected or what each provider is doing about it. OpenCode says users who didn't get an email weren't affected. If you used OpenCode Console with your own provider keys, rotating them anyway and checking usage takes a few minutes, so in our view it's worth doing.

How to store API keys securely

The providers' advice is consistent:

  • Use environment variables or a secrets manager. OpenAI, Anthropic, Google and xAI all say this in the docs linked above. Google's key docs also suggest Google Cloud Secret Manager for production.
  • Never put keys in code or commit them. Google's docs say never to check keys into source control. If you keep keys in a local .env file, add it to .gitignore before your first commit.
  • Keep keys out of the browser and mobile apps. Google warns that keys compiled into client-side code can be pulled out by users, and recommends calling the API from a backend server instead. Our pre-launch checklist for vibe-coded apps covers this alongside other launch checks.
  • Don't paste keys into chat. Anthropic's key best practices say not to share keys in forums, emails or support tickets, even with Anthropic.
  • Know where your tool keeps keys. OpenCode's provider docs say keys added through its /connect command are stored locally in ~/.local/share/opencode/auth.json. In our view, any file like that should be treated as a secret and never synced or committed.
  • Use one key per tool and set an expiration. Anthropic, OpenAI and OpenRouter all let you set expirations. Separate keys mean you can revoke one tool's access without breaking the others, and you can see which tool spent what.

For a wider look at app security, see our vibe coding security checklist.

Set spend limits and alerts at every provider you use

A spend limit won't stop a leak, but it caps the bill. Keep in mind the limits behave differently. OpenAI's alerts only notify, while its hard limit stops traffic. Anthropic's docs say usage pauses when you reach your tier's spend cap, and you can set your own lower limit. Google's project caps are experimental and can run over. xAI's postpaid limit doesn't cover prepaid credits. OpenRouter limits are set per key. In our view, set a limit at every provider you've linked to any tool, including ones you rarely use, since an unused key with no cap is easy to forget.

How to tell if your API key leaked

  • Unexpected usage. Check each provider's usage page above for spend, models or times you don't recognize.
  • Provider emails. Anthropic says it works with GitHub's secret scanning partner program: if a Claude API key turns up in a public GitHub repository, Anthropic deactivates it and emails the owner. xAI's security FAQ says it also takes part in GitHub's Secret Scanning program, disabling leaked keys and notifying you by email.
  • GitHub secret scanning. GitHub's secret scanning docs say it scans the full Git history of a repository, as well as issues, pull requests, discussions and wikis, for known secret types. It runs automatically and free on public repositories, and secrets from partner providers are reported directly to the provider.

Secret scanning only finds keys that ended up on GitHub. It won't tell you if a third-party service that held your key was breached, which is why usage checks matter.

Linking your own key vs using a tool's built-in plan

Many coding tools let you either bring your own provider key or pay for the tool's own plan. With your own key, you get the provider's billing, limits and usage pages, but the tool holds a credential that can spend your money. With a built-in plan, you don't hand over a provider key, though you rely on the tool's own account security and limits, which only that tool's docs can describe. Our usage limits comparison covers the subscription side for Claude Code and Codex. In our view, if you do link your own key, give that tool its own key with a low spend limit and an expiration.

FAQ

Is it safe to paste my API key into an AI coding tool? It means trusting that tool with your account. Anthropic's best-practices page says that uploading a key to a third-party tool gives its developer access to your Console account, so only do it with tools you trust. Use a dedicated key with a spend limit.

What happens if my API key leaks? Anyone holding it can make requests billed to you until you disable or delete it. Revoke it at the provider, issue a new one and check usage.

Will the provider refund misuse? We can only point to one case. Tibo Sottiaux of OpenAI reported that OpenAI refunded credits drawn in the OpenCode attack for OpenCode users who are also OpenAI API customers. We haven't found a general refund policy in any provider's docs, so contact support.

How do I check my usage? Use each provider's usage or billing page: OpenAI's Usage page, the Claude Console, Google AI Studio and the xAI Console.

Should I use one key per tool? In our view, yes. It limits what any single leak can affect and makes unusual usage easier to trace.

Sources

All checked Oct 19, 2026.