Sub topic 13
Rotating and Revoking Keys
This chapter assumes you can use a command line and Git, read short Python snippets, and know that you load keys from environment variables or a git-ignored .env file. It covers the two actions that end a key's useful life: revoking it and replacing it by rotation.
Revoke and rotate are different actions
- Revoking a key tells the provider to reject it from now on. Any request that uses it fails, usually with a 401 status code.
- Rotating a key means replacing it with a new one on a schedule or after an event, and then revoking the old one.
Revocation is the emergency tool. Rotation is routine upkeep. Rotating limits how long a stolen key stays useful, because an old key that nobody uses can't be abused.
Why rotate on a schedule
Keys leak in ways you can't always see: an old laptop, a log archive, a former teammate's notes. A key that never changes stays valid through all of those. Rotating every few months, and whenever someone with access leaves the team, caps the exposure window.
Rotation also gives you practice. If replacing a key takes five minutes because you have done it before, you will act quickly when a leak happens.
A rotation without downtime
If you revoke the old key first, your app breaks until the new key is deployed. Create the new key first and overlap the two.
The steps in detail:
- In the provider console, create a new key. Give it a name that includes the date, such as
backend-prod-2026-11. Copy it when it is shown, because most consoles display it only once. - Update every place that holds the key: your local
.env, the CI secret, and the hosting environment. - Restart or redeploy the app. Environment variables are read when a process starts, so a running process keeps using the old key until it restarts.
- Check that requests succeed. Look at the status code, not the key.
- Revoke the old key in the console.
Check which key is in use
A quick check helps you confirm the new key is active before you revoke the old one. This script prints only the last four characters, enough to tell two keys apart without exposing either.
import os
api_key = os.environ.get("LLM_API_KEY")
if not api_key:
raise RuntimeError("LLM_API_KEY is not set")
print(f"Using key ending in ...{api_key[-4:]}")Compare that suffix with the one the console shows next to each key. Many consoles show the last few characters in the key list.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| 401 after rotation | The app still holds the old, revoked key | Restart the process and check each place the key is stored |
| Works locally, fails in CI | The CI secret was not updated | Update the secret, then re-run the job |
| New key ignored | A real environment variable overrides `.env` | Unset the old variable in your shell |
| Old key still used | A forgotten server, cron job, or teammate's machine | Look at the console's usage per key before revoking |
The last row is why you should revoke only after the new key works. If revoking the old key breaks something, the failure tells you where a copy was hiding. Fix that place and continue.
Revoking after a leak
If a key was exposed, skip the careful overlap. Revoke it first, then create a replacement. A short outage costs less than leaving a live key in public. The next chapter covers the full response to a leak, including checking usage and cleaning up Git history.
Exercise
You rotate a key on a schedule. You create the new key, update CI and production, and redeploy. Should you revoke the old key before or after confirming that requests succeed with the new one, and why?
Show answer
After. Revoking last means that if a deployment missed the new key, the app keeps working on the old one while you fix it. The exception is a leaked key, which you revoke immediately because the risk of abuse outweighs a short outage.