Claude API Key Rotation Automation Guide
Rotating Claude API keys on a schedule — rather than only when something breaks — is the single biggest improvement most teams can make to their key hygiene. This guide covers how to automate that rotation so it happens without downtime, without manual Slack messages, and without someone forgetting to revoke the old key.
The short answer: generate a new key, deploy it alongside the old one, cut traffic over gradually or atomically, confirm no requests are using the old key, then revoke it. Automate each of those steps with a script or CI job tied to a schedule (e.g. every 90 days) and to incident triggers (e.g. a key leaking in a git commit). The rest of this article breaks down the mechanics.
Why rotate keys at all
API keys for any LLM provider, Claude included, are long-lived bearer credentials. If one leaks — in a client-side bundle, a log file, a support ticket, a public repo — anyone holding it can run inference on your account until it's revoked. Rotation limits the blast radius of a leak you don't even know happened yet, and it's a baseline requirement for SOC 2, ISO 27001, and most enterprise security questionnaires.
The problem isn't deciding that you should rotate keys. It's building a rotation process that doesn't risk an outage every time you do it.
The core rotation pattern
Regardless of provider, safe key rotation follows the same sequence:
- Issue a new key without touching the old one.
- Deploy the new key to all consumers (servers, workers, CI secrets).
- Verify traffic has shifted — check logs or usage metadata for calls still using the old key.
- Revoke the old key.
- Record the rotation (who, when, why) for audit purposes.
Skipping step 3 is the most common cause of rotation-induced outages: someone revokes the old key while a background job, a cached config, or a forgotten microservice is still using it.
Automating with a script
A minimal rotation script for a system with its own key-management API looks like this:
#!/usr/bin/env bash
set -euo pipefail
OLD_KEY_ID=$(cat .current_key_id)
NEW_KEY=$(curl -s -X POST https://api.example.com/v1/keys \
-H "Authorization: Bearer $ADMIN_TOKEN" \
-d '{"name":"rotated-'"$(date +%Y%m%d)"'"}' | jq -r '.key')
# push new key to secret manager
aws secretsmanager put-secret-value \
--secret-id prod/llm-key \
--secret-string "$NEW_KEY"
# wait for deploy to pick up new secret
sleep 120
# check old key usage before revoking
USAGE=$(curl -s https://api.example.com/v1/keys/$OLD_KEY_ID/usage \
-H "Authorization: Bearer $ADMIN_TOKEN" | jq '.requests_last_hour')
if [ "$USAGE" -eq 0 ]; then
curl -s -X DELETE https://api.example.com/v1/keys/$OLD_KEY_ID \
-H "Authorization: Bearer $ADMIN_TOKEN"
echo "Rotated. Old key revoked."
else
echo "Old key still in use ($USAGE req/hr) — not revoking."
exit 1
fi
Run this on a cron schedule (monthly or quarterly) and on-demand from an incident runbook when a leak is suspected. The key design decision is the usage check before revocation — it turns "hope nothing breaks" into a verified gate.
Zero-downtime rollover for distributed systems
If your Claude-calling code runs across multiple services, don't rotate all of them at once:
- Dual-key window: keep both old and new keys valid for a set period (if your provider supports multiple active keys, or multiple keys per project). Deploy the new key everywhere first, then revoke the old one only after confirming zero traffic.
- Secret manager, not env files: store keys in AWS Secrets Manager, HashiCorp Vault, or similar, and have services pull at startup or via a short-TTL cache. This avoids redeploying every service manually when a key changes.
- Canary rollout: push the new key to one instance or region first, watch error rates for a few minutes, then roll out everywhere.
Where this gets harder with raw provider keys
Anthropic's own API key management is intentionally simple — it doesn't give you per-application sub-keys, team-level revocation, or usage breakdowns by consumer out of the box. If you're running Claude behind five different internal services, you're either sharing one key across all of them (bad for blast radius) or manually managing a pile of keys with no central dashboard.
This is one of the gaps SubToAPI is built to close. It sits in front of your Claude access and issues scoped application keys (sub_live_...) per project or team, each with its own usage metadata. That means rotation becomes a per-application operation: you can regenerate a key for one service without touching the others, see exactly which key made which requests, and revoke individual keys from the dashboard instead of rotating a single shared secret company-wide. Check the quickstart to see how keys are issued, and the docs for the full API reference including messages, streaming, and tool use.
Building rotation into CI/CD
Treat key rotation like a dependency upgrade: scheduled, automated, and reviewable.
# .github/workflows/rotate-key.yml
on:
schedule:
- cron: '0 3 1 * *' # monthly
jobs:
rotate:
runs-on: ubuntu-latest
steps:
- name: Rotate Claude key
run: ./scripts/rotate-claude-key.sh
env:
ADMIN_TOKEN: ${{ secrets.ADMIN_TOKEN }}
Log every rotation to an append-only audit trail (a database table or even a committed ROTATIONS.md with timestamps) so compliance reviews don't require digging through chat history.
A rotation checklist worth pinning somewhere
- New key generated and stored in secret manager, not in code or
.envcommitted to git - All consuming services redeployed or restarted to pick up the new key
- Old key usage confirmed at zero before revocation
- Revocation executed and logged with timestamp and reason
- Alerting configured for 401/403 spikes immediately after rotation, so a missed service is caught in minutes, not days
FAQ
How often should I rotate a Claude API key? Every 60–90 days for routine hygiene, plus immediately any time a key may have been exposed — in a log, a public repo, a shared screenshot, or a former employee's laptop.
Can I rotate a key without downtime? Yes, if you support a brief window where both old and new keys work. Deploy the new key everywhere, confirm traffic has shifted via usage logs, then revoke the old one — never revoke before confirming.
Does SubToAPI help with key rotation across multiple apps? Yes — SubToAPI issues separate scoped keys per application, so you can rotate or revoke one app's key independently without affecting others, with usage metadata to confirm the cutover before revoking the old key.