API Key Rotation for AI Applications: A Practical Guide
API Key Rotation for AI Applications
API key rotation for AI applications means systematically replacing the credentials your app uses to call LLM providers (OpenAI, Anthropic, etc.) on a regular schedule or in response to a suspected compromise, without breaking production traffic. For teams running Claude or GPT integrations, this is not optional hygiene — a leaked key tied to a pay-per-token model can generate thousands of euros in unauthorized usage before anyone notices.
The short answer: rotate keys on a fixed schedule (30–90 days), support multiple active keys at once so old ones can be phased out gracefully, automate the process so it doesn't depend on someone remembering, and separate keys per environment and per application so a leak in one place doesn't expose everything. The rest of this article covers how to actually implement that.
Why AI API keys are higher risk than typical secrets
Most API key rotation advice was written for database credentials or payment gateway keys, where a leak causes a fixed, bounded problem. AI provider keys are different:
- Usage-based billing with no hard ceiling by default. A leaked key can be hammered by a script until someone notices the invoice.
- Keys often get embedded client-side by mistake. Developers prototyping with
fetchcalls to an LLM API sometimes ship the key in a mobile app bundle or a public repo before anyone catches it. - Long-lived keys accumulate exposure. A key used in CI logs, support tickets, and three different
.envfiles for two years is a key that has been copy-pasted into places nobody can audit. - Downstream automation makes outages worse. If a rotated key breaks a production agent mid-conversation, you don't just lose an API call — you lose a user-facing feature.
This is why rotation needs to be a designed process, not a reactive one.
A rotation schedule that actually works
1. Pick a cadence based on exposure, not a arbitrary number.
- Internal tools with no external users: 90 days.
- Production customer-facing apps: 30–60 days.
- Anything that touches third-party integrations, contractors, or has been in a public repo even briefly: rotate immediately, then set a 30-day cadence going forward.
2. Use overlapping validity windows. Never have a single point in time where the old key is dead and the new key hasn't propagated. Issue the new key, deploy it everywhere, confirm it's working, and only then revoke the old one — with at least a 24-hour overlap in staging environments and a few hours in production.
3. Automate the handoff. Manual rotation fails because someone forgets a service, a serverless function with a cached environment variable, or a CI secret store. A rotation script or workflow should:
- Generate the new key via the provider's dashboard or API.
- Push it to your secrets manager (Vault, AWS Secrets Manager, Doppler, etc.).
- Trigger redeploys or hot-reloads of services that read the key.
- Verify with a test request before revoking the old key.
- Log the rotation event with a timestamp and who/what triggered it.
Zero-downtime rotation pattern
The core technique is dual-key support: your application layer should be able to validate requests against two active keys simultaneously during the transition window.
// Example: supporting two valid keys during rotation
const activeKeys = [process.env.AI_API_KEY_CURRENT, process.env.AI_API_KEY_PREVIOUS]
.filter(Boolean);
function isValidKey(key) {
return activeKeys.includes(key);
}
If you're calling a provider directly, this pattern applies to your own internal routing layer (if you proxy requests), not to the provider's key itself — most providers only let you use one key per request. The dual-key pattern matters most for your own application keys that front the AI provider, which is exactly the architecture an API gateway or wrapper like SubToAPI provides.
Where SubToAPI fits
If you're building on Claude directly, you're managing Anthropic credentials yourself — rotation, revocation, and audit trails are entirely on you. SubToAPI sits between your app and Claude: you generate application-level keys (sub_live_...) from a dashboard, scoped per app or per environment, and rotate them independently of your underlying Claude access. That means:
- Revoking a leaked
sub_live_key doesn't touch your Claude subscription or other apps' keys. - You can issue a new key, run both in parallel, and retire the old one on your own timeline.
- Usage metadata per key lets you spot anomalous spikes (a classic signal of a leaked key) before the bill does.
This is the same rotation discipline described above, just handled at the dashboard level instead of scripted against a provider console. See /docs/quickstart for how key generation works, or /pricing for plan details.
Detecting a key that needs emergency rotation
Don't wait for your scheduled cadence if you see:
- A sudden spike in request volume with no corresponding feature launch.
- Requests originating from IP ranges or regions your team doesn't operate in.
- Unexpected model usage (e.g., a key scoped for one app suddenly calling endpoints it's never used).
- A key appearing in a public commit, log dump, or shared screenshot — treat this as a confirmed compromise, not a maybe.
In any of these cases, revoke first and investigate second. The cost of a few minutes of downtime is almost always lower than the cost of continued unauthorized usage.
Practical checklist
- [ ] One key per environment (dev, staging, production) — never share across them.
- [ ] One key per application or service, not one shared key for everything.
- [ ] Rotation automated via CI/CD or a secrets manager, not a calendar reminder.
- [ ] Overlap window tested in staging before every production rotation.
- [ ] Usage dashboards checked weekly for anomalies, not just at renewal time.
- [ ] Revocation path documented and tested — know exactly how to kill a key in under five minutes.
questions
Do I need to rotate AI API keys if they're stored in a secrets manager? Yes. A secrets manager protects keys at rest and limits who can read them, but it doesn't reduce the blast radius if a key leaks through logs, a misconfigured client-side build, or a compromised CI pipeline. Rotation limits how long a leaked key stays valid.
How often should I rotate keys for a production AI application? 30–60 days for customer-facing production systems is a reasonable default. Rotate immediately and outside the normal cadence if you detect anomalous usage or confirm any exposure, regardless of when the last rotation happened.
Can I rotate keys without any downtime? Yes, using an overlap window where both the old and new key remain valid until you've confirmed every service has switched over. This requires either provider support for multiple active keys or an intermediary layer — like an API gateway — that manages key validity independently of the underlying provider credential.