API Key Rotation Best Practices for LLM Apps
Why key rotation matters more for LLM apps
API key rotation is the practice of periodically replacing credentials before they're compromised, rather than waiting for a breach to force the issue. For LLM applications specifically, this matters more than for typical SaaS integrations because a leaked key can generate real financial damage fast: a single automated script can burn through thousands of dollars in model usage in minutes, and the key often has access to sensitive prompts, customer data, and tool-calling permissions.
The short answer to "how should I rotate LLM API keys": rotate on a fixed schedule (30–90 days for production keys), rotate immediately on any suspected exposure, use short-lived or scoped keys per service instead of one shared key, and always support overlapping validity windows so rotation doesn't cause downtime. The rest of this article breaks down how to actually implement that without breaking your app.
The core risks specific to LLM keys
Before setting a policy, understand what you're protecting against:
- Cost exposure: A compromised key can run thousands of concurrent requests against expensive models before anyone notices.
- Data exposure: Prompts often contain customer PII, internal documents, or proprietary code. A leaked key can expose months of request history if your provider logs usage.
- Tool access: If your key is tied to function/tool calling against internal systems, a leak isn't just a billing problem — it's a potential system compromise.
- Long-lived secrets in client code: LLM keys that accidentally ship in frontend bundles, mobile apps, or public repos are a recurring failure mode. Rotation is your safety net when prevention fails.
A practical rotation schedule
Treat rotation frequency as a function of key exposure, not a single global number.
| Key type | Suggested rotation interval | |---|---| | Server-side production key, never leaves your infra | 90 days | | Key used in CI/CD pipelines | 60 days | | Key used by third-party contractors/agencies | 30 days | | Key suspected of exposure | Immediately | | Per-customer or per-project key in a multi-tenant app | 90 days, or on customer offboarding |
Fixed calendar rotation works, but event-driven rotation should always override it. Any of these events should trigger an immediate rotation regardless of schedule: an employee with key access leaves, a key appears in a public repo scan, you see anomalous usage spikes, or a third-party vendor has a security incident.
Zero-downtime rotation: the overlap window
The single most common rotation mistake is revoking the old key the instant the new one is issued. This breaks any in-flight requests, cached deployments, or background workers still holding the old credential. Instead:
- Generate the new key while the old one is still active.
- Deploy the new key to all services and confirm it's working with real traffic.
- Monitor for a window (commonly 24–72 hours) during which both keys work.
- Check logs to confirm zero requests are still using the old key.
- Revoke the old key.
If your provider doesn't support two active keys simultaneously, you can't do a clean rotation — you're forced into a hard cutover, which guarantees some failed requests during the switch. This is a real feature gap worth checking before you pick an LLM access layer. SubToAPI's dashboard supports issuing a new sub_live_... key and revoking the old one independently, so you can run the overlap pattern above without touching application code mid-rotation — see /docs for key management details.
Scope keys instead of rotating one master key
Rotation is painful when you have one key doing everything. It's much safer and easier to rotate narrow, purpose-specific keys:
- One key per service (billing worker, chatbot backend, internal admin tool).
- One key per environment (staging vs. production — never share these).
- One key per customer or project in multi-tenant setups, so a single compromised key has a blast radius of one customer, not your whole user base.
When keys are scoped, rotating one doesn't require redeploying unrelated services, and revoking a compromised key doesn't take down your entire product. If you're building on top of Claude, SubToAPI lets you issue separate application keys per project or environment from one dashboard, which makes this scoping pattern straightforward without managing multiple Anthropic accounts — see /docs/quickstart.
Automate rotation, don't rely on calendar reminders
Manual rotation reliably fails because it depends on someone remembering. At minimum, automate these three things:
- Expiry alerts: a scheduled job that flags keys approaching their rotation date.
- Secret injection: pull keys from a secrets manager (AWS Secrets Manager, HashiCorp Vault, Doppler) at runtime instead of hardcoding them in
.envfiles that get copied around. - Revocation audit: log every key creation and revocation event with a timestamp and actor, so you can answer "who issued this key and when" during an incident review.
# Example: fetching a current key from a secrets manager at deploy time
export SUBTOAPI_KEY=$(vault kv get -field=key secret/subtoapi/production)
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "content-type: application/json" \
-d '{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"ping"}]}'
Never commit the key itself to source control, even in example files — commit the retrieval command instead.
Detect misuse, not just scheduled expiry
Rotation on a schedule catches slow-moving risk. Usage anomalies catch active breaches faster than any calendar policy. Watch for:
- A sudden spike in request volume from a key that's normally quiet.
- Requests originating from unexpected IP ranges or geographies.
- Usage outside normal business hours for internal tools.
A usage dashboard that breaks down requests and spend per key — not just per account — makes this detection realistic instead of theoretical. If you're managing several application keys across projects, check /docs/messages for how request metadata is surfaced per key.
A minimal rotation checklist
- [ ] Every key has an owner and a documented purpose
- [ ] Rotation interval is set based on exposure level, not a single default
- [ ] Overlap window is supported and tested before going live
- [ ] Keys are scoped per service/environment/customer, not shared
- [ ] Secrets are injected at runtime, never hardcoded
- [ ] Anomalous usage triggers immediate rotation regardless of schedule
- [ ] Revocation events are logged for audit purposes
If you're setting this up for a Claude-based product, start with a trial at /signup and compare plans at /pricing — Solo, Team, and Scale tiers all include per-key dashboard management so rotation is a few clicks, not a deployment event.
Questions
How often should I rotate LLM API keys? 90 days for low-exposure production keys, 30–60 days for keys used by third parties or CI/CD, and immediately for any suspected exposure — schedule rotation by risk level, not a single fixed interval.
Does rotating an API key cause downtime? Only if your provider doesn't support two active keys at once. With an overlap window where both old and new keys work simultaneously, you can migrate traffic and revoke the old key with zero failed requests.
Should every service share one API key? No. Use separate scoped keys per service, environment, or customer so a single compromised key has a limited blast radius and rotating it doesn't require touching unrelated systems.