Joan Comadran
Field notes

AI & Dev Trends

Secure OpenAI, Claude and Gemini API keys

How to secure OpenAI, Claude and Gemini API keys: one key per tool, expiry where the provider has it, a hard spend cap, server-only calls, and the first hour after a leak.

October 19, 2026Updated November 27, 202610 min read

Keep an OpenAI, Claude or Gemini API key on a server you control, one key per tool, with an expiry where the provider offers one and a hard spend cap that stops traffic. The string is a bearer credential: anyone who copies it can spend on your account until that provider revokes it. Environment variables, a project or workspace of its own, and an IP allowlist are what limit the copies and what a copy can still do.

One place this already failed in public was OpenCode Console. On 17 October 2026 OpenCode posted on X that a breach affected 1.4% of Console users who had external providers linked. The Hacker News (19 October 2026) reports the email: keys saved in those workspaces may have been exposed. Lydia Hallie said Anthropic had revoked the Claude keys it believed were affected, and Evan Otero said Google had blocked the exposed Gemini keys. TechCrunch the same day reported an older Console system, not the open source app, and OpenAI refunding credits drawn in the attack. OpenCode has not published how it was reached. I am not going to invent that.

OpenAI API key safety best practices (official)

Best practices for API key safety, checked 27 November 2026:

  1. One key per person. Sharing keys breaks the Terms of Use. A user-owned key acts as that user. For an app, a cron job or CI, use a project service account. You can set permissions on a key.
  2. Expiry, then rotation. Project keys can expire. After that date, requests are rejected. Create the replacement, deploy it, verify it, then revoke the old one. Admins can cap lifetime for the org or a project. The project cap cannot exceed the org cap. Production best practices also lets admins allow only service-account keys, only user-owned keys, or no new keys. Keys that already exist are untouched.
  3. Server only. Not in a browser or a mobile app. The call goes through your backend.
  4. OPENAI_API_KEY in the environment. Do not commit it. A private repo still loses it in a breach.
  5. A secrets manager in production, so the key is encrypted outside the app.
  6. A hard monthly spend limit. The article says "Spend alerts alone don't stop API traffic." A hard limit returns 429 with organization_spend_limit_exceeded or project_spend_limit_exceeded (spend limits). Enforcement is not instant, so spend can slightly exceed the cap, and real traffic stops too.
  7. An IP allowlist, if egress is fixed (guide). Org or project, up to 50 addresses or CIDR ranges. A project list replaces the org list. Other IPs get 401 ip_not_authorized. Changes can take 15 minutes. Sign-in at platform.openai.com is not covered.

Workload identity federation swaps the stored key for a short-lived token. An Admin API key manages projects, keys, spend and audit logs, and cannot call the model. Costs (GET /organization/costs) groups by api_key_id.

LLM API key management

Same five controls. Each cell is from the pages linked under the table, read on 27 November 2026.

OpenAIAnthropic (Claude Console)Google (AI Studio / Gemini)
Per-project keysProject key. Service account belongs to one project.Personal or service account key, optional single workspace. Workspace keys are legacy.One Google Cloud project. Every key in it shares the spend cap.
ExpiryOptional. Admins can force a maximum lifetime.Fixed at creation: 3 hours, 1 day, 7 days, 30 days, custom, or Never.Not described in the key guides below.
Spend capsMonthly org or project limit. Hard stop returns 429. Not instant.Tier cap, plus a lower monthly org or workspace limit.Experimental monthly project cap, plus a billing-account tier cap.
IP allowlistOrg or project, 50 entries. Project list replaces the org list.Org-wide, beta, 1,000 entries. 403 permission_error.Per key, IP application restriction.
Admin and usage APIsAdmin API. Costs API groups by api_key_id.Admin API reports expires_at and status. Usage and Logs pages.API Keys API. Usage in AI Studio. Costs in Cloud Billing.

OpenAI sources are in the section above. Anthropic, checked 27 November 2026: authentication, IP allowlisting, rate limits, API key best practices (help page dated 19 October 2026). Google, same day: Using Gemini API keys, billing, API keys.

Anthropic. Never is hidden if the org sets a maximum. Email is 7 days ahead for a lifetime of at least 14 days, 1 day ahead for at least 7, and none shorter. Expired keys return 401 authentication_error and can only be deleted. Disable reverses. Delete does not. A personal key is archived when its owner leaves. Tier caps: Start $500, Build $1,000, Scale $200,000 a month. Custom has none on that page. Your lower limit is HTTP 400. The tier cap is HTTP 429 enforced_spend_limit_reached. No default-workspace limit on Claude Models on Google Cloud (Anthropic managed) or Claude Platform on AWS. On the default workspace a limit may miss playground, Managed Agents, and Claude Code or Desktop on a Console login. The org limit still covers that. The IP allowlist also blocks the Console. It does not apply to Bedrock, Claude Platform on AWS, Claude on Google Cloud, or Microsoft Foundry.

Google. Since 28 May 2026, new AI Studio keys are authorization keys, bound to a service account and restricted to the Gemini API. Unrestricted standard keys are rejected. Since 7 May 2026, dormant unrestricted keys are blocked. Those calls are missing from service account usage metrics. Project caps are experimental: about 10 minutes of lag, and batch jobs and agent sessions can pass them. Prepay at $0 returns HTTP 402 for every key on that billing account, same lag. The monthly auto-charge limit only stops automatic reloads. Invoiced accounts have no spend caps. Libraries read GEMINI_API_KEY, unless GOOGLE_API_KEY is also set, which wins. An IP allowlist is serverKeyRestrictions.allowedIps on the API Keys API.

None of the three stop on the exact dollar. Claient reserves budget before a turn on the platform key. The run cost behind that cap is in AI agent development cost.

Keys stay on the server

OpenAI and Google both forbid the key in client code. In Next.js, NEXT_PUBLIC_ is copied into the browser bundle at build time. An LLM key with that prefix is already public.

ts
import 'server-only'; // importing this from a Client Component fails the build
import Anthropic from '@anthropic-ai/sdk';

export const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

A public route that proxies the key is still your key. The anon key versus the server secret is the split in the Next.js + Supabase pre-launch checklist. An SSH private key is a separate secret. OpenAI's article does not cover it, and it does not belong in the prompt, the repo, or the browser. Lightframes runs the model in the tab, so there is no key.

Pasting a key into a hosted tool gives that developer your provider account. Anthropic's help center says so for Claude Console. Use a separate key, a low cap and an expiry. Deleting it in the tool does not revoke it. Claient stores a customer's Claude key in Supabase Vault, decrypts it only as the webhook's service_role, and falls back to the platform key if that key is revoked. A compromised server still has the string.

Can Salt Security protect OpenAI API keys?

Salt's platform discovers your agents, MCP servers and APIs, then looks at exposure, source, configuration and runtime. Discovery inventories APIs nobody documented, including third-party ones your app calls. Posture is policy on that estate. Salt Protect blocks runtime abuse of those APIs. The platform tour shows a hard-coded key in source as a finding. Checked 27 November 2026.

That is APIs you run, and traffic a collector can see. It does not issue, expire or revoke a provider key, and a copied key used elsewhere never passes Salt. Revoke at the provider.

What to do if your OpenAI, Claude or Gemini API key leaked

If money is already moving, revoke first. Create-then-revoke is for a rotation you planned.

  1. OpenAI. Rotate on the API keys page, update production, read Usage or Costs by key. The safety article sends billing questions to help.openai.com.
  2. Anthropic. API keys, three-dot menu, Disable or Delete. Check Usage and Logs, and lower the limit on Spend limits. The help article treats Billing auto-reload limits as the leaked-key safeguard.
  3. Gemini. New key in Google AI Studio or the Cloud console, deploy it, then disable the old one. Fast-acting leaked key enforcement on auth keys is not a reason to leave it enabled. Then read Cloud Billing.

Git history, CI, preview env and the tool you pasted it into still have copies. A later commit that deletes the file leaves the blob. Revoke order, git-filter-repo and .gitignore are in Leaked an API key on GitHub? How to fix it.

FAQ

How do I secure my OpenAI API key?

Keep it in a server environment variable or a secrets manager, never in client code or git. OpenAI's key safety article, checked 27 November 2026: one key per person, project keys with an expiration, calls only through your backend, a monthly spend limit with hard enforcement, and an IP allowlist if egress IPs are fixed. Alerts do not stop traffic.

How do I rotate an Anthropic API key?

Create a new Claude Console key with an expiration, deploy it, confirm it, then disable the old key (reversible) or delete it (permanent). Expiration is fixed at creation: 3 hours, 1 day, 7 days, 30 days, custom, or Never. If the key is being abused, disable or delete it first.

Do spend alerts protect me if my API key leaks?

No. An alert does not stop requests. OpenAI hard limits return 429 once enforcement catches up, and spend can slightly exceed the cap. Anthropic lets you set a monthly limit under the tier cap. Google AI Studio project caps are experimental, about 10 minutes behind, and batch jobs and agent sessions can overshoot.

Is it safe to paste my API key into a third-party tool?

Only if you trust that vendor with the provider account. Anthropic's help center says uploading a key gives that developer access to your Claude Console account. Give the tool its own key, project or workspace, low cap and expiry. Deleting it inside the tool does not revoke it.

Can Salt Security protect OpenAI API keys?

Salt discovers and monitors your own APIs, MCP servers and agents, including runtime abuse on traffic it can see (salt.security, 27 November 2026). It does not issue, expire or revoke OpenAI, Claude or Gemini keys, and a copied key used off your network never passes it. Revoke at the provider.

What should I do if my OpenAI, Claude or Gemini API key leaked?

Revoke it at the provider before you edit git. OpenAI: rotate on the API keys page. Anthropic: Disable or Delete in the Claude Console. Gemini: create a replacement, deploy it, then disable the old key. Check usage back to the earliest possible leak, lower the spend cap, and clean every copy. Deleting the file on GitHub is not enough.

I'm Joan Comadran, an industrial engineer (UPC) turned AI engineer and full stack developer in Barcelona. I am a problem-solving engineer: the agents I ship call these providers from a server I control, with a cap in front of the key. The controls above are from OpenAI, Anthropic, Google and Salt Security's pages, checked 27 November 2026. Putting that boundary on a product is the work I take on.

Work with me

Want this in your product?

I build AI agents, apps and websites for companies.

Let's talk →

Keep reading

03