How to rotate a free LLM API key without breaking a running app
A free LLM API key can be rotated without downtime only if its provider lets old and replacement keys overlap. ChinaGenAI site keys do not overlap: replacing one immediately revokes the previous key and neither resets allowance nor frees an in-flight slot, according to the current site console. For other providers, create a replacement, put it in the approved secret store, test it from a fresh process, move each worker and scheduled job, then revoke the old key. If a key may be exposed, revoke it immediately and restore service with a replacement; availability does not justify leaving a compromised key active.
First check whether overlap is possible
“Rotate” can mean two different operations:
- Planned replacement: the provider accepts two active keys temporarily. You can move clients in stages, observe the change, and revoke the old key after they have switched.
- Emergency revocation: the old key is disabled at once. Some in-flight or scheduled work may fail, but the credential is no longer available for new requests.
Do not assume that every free LLM API supports two live keys, that keys have independent quotas, or that creating a key resets a free limit. Check the provider's current key-management and quota rules before planning a maintenance window.
OpenAI's API-key guidance recommends creating a replacement, updating applications, verifying the replacement, and then revoking the old key. Its compromise guidance says to delete an exposed key and review usage. Those are separate paths: a normal rotation can use a short overlap when supported; a suspected leak should be revoked promptly. The exact controls differ by provider. (Key safety; account security, checked 2026-11-19.)
ChinaGenAI site keys need a direct cutover
A ChinaGenAI site key is separate from a key issued by an upstream provider. The current site console says keys expire after 48 hours; replacing one revokes the previous key immediately and does not reset the shared allowance or free an in-flight slot. Plan a cutover: copy the replacement into the approved secret store, update and restart each consumer, then verify a short request. Do not expect the old and new site keys to overlap or a replacement to restore quota. This rule applies to ChinaGenAI site keys; other provider keys follow their own controls.
A low-risk planned rotation
Use this sequence when the old key is not suspected to be compromised.
- List every consumer. Include web workers, background queues, notebooks, CI workflows, local development, and scheduled jobs. Record where each one reads its secret and how it reloads configuration. Never paste the key into this inventory.
- Check overlap and scope. Confirm whether the provider allows a second key, whether the new key has the required permissions, and whether both keys use the same free-plan limits. Prefer the narrowest available permissions.
- Create and store the replacement. Put it in the approved secret manager. Do not add it to a source file, shell command, screenshot, ticket, or log.
- Update one canary consumer. Start a fresh process or deployment that reads the new secret. Send one minimal request to the intended free route and verify the expected response. Log the HTTP status and a request ID if available, never the Authorization header or key value.
- Move the remaining consumers. Roll workers gradually if the platform supports it. Start a new CI run after updating its secret. A changed secret store does not prove that an already-running process has reloaded its environment.
- Observe before revoking. Check provider usage or non-secret key identifiers if available. Exercise delayed jobs and less-frequent paths, not only the web request that happens to be online.
- Revoke and verify. Once all consumers have moved and the overlap window has ended, revoke the old key. Run one final canary and confirm that the old credential no longer appears in any configured consumer.
Synthetic rehearsal: two workers and one nightly job
This is a planning example, not a live API observation or request. Assume one application runs two web workers and a nightly summarizer.
| Consumer | State after the secret changes | Evidence to accept | Rotation decision |
|---|---|---|---|
| Worker A | Restarted with the replacement key | One small request succeeds from the new process; logs contain no credential | Count this worker as migrated |
| Worker B | Still running with its startup-time value | A healthy page check does not prove which key it loaded | Restart or redeploy, then make a fresh request |
| Nightly summarizer | Secret store is updated, but the job has not run | A fresh scheduled run or a safe manual run uses the new credential | Keep this consumer pending |
If the provider allows key overlap, leave the old key active until every row has evidence, including the scheduled path. If replacement revokes the old key immediately, drain or pause work that can wait, perform the cutover, restart consumers, run a canary, and resume queued work. A single successful worker request is not evidence that the other two consumers have switched.
If there is no overlap window
A provider may only let you delete the old key and create a new one, or it may invalidate the old key as soon as you change credentials. In that case, zero downtime is not something the client can guarantee. Choose a controlled cutover:
- Pause or drain work that can safely wait.
- Create the replacement and update the secret store.
- Restart or redeploy every consumer that snapshots environment variables at startup.
- Send one small canary request.
- Resume queued work and watch authentication failures.
If the provider supports staged versions, use that documented mechanism. For example, Cloudflare Workers documents that wrangler secret put creates and deploys a new Worker version; when using gradual deployments, a versioned secret can instead be deployed with the version. Treat this as a platform-specific release step, not a universal API-key behavior. (Cloudflare Workers secrets, checked 2026-11-19.)
GitHub Actions exposes a configured secret to a workflow when the workflow passes it through the secrets context or as an environment variable. After updating a repository or environment secret, run a fresh workflow that tests the replacement. Do not print the value to confirm it; a successful request from the new run is stronger evidence. (GitHub Actions secrets, checked 2026-11-19.)
If the key may have leaked
Do not wait for every consumer to be ready before revoking a credential that is publicly exposed. Revoke it through the provider's supported control, review usage for unfamiliar requests, create a replacement, and update consumers. Expect some requests to fail while the new secret propagates. If a public repository or client bundle contained the key, deleting the text from the latest commit or renaming the environment variable does not make the old credential safe again.
Keep incident evidence that does not contain secrets: the provider's key label or ID, revocation time, affected deployment names, request IDs, and usage changes. Do not store the key itself in the incident record.
Rotation checklist
| Check | Evidence to record |
|---|---|
| Provider permits a temporary overlap | Current provider documentation or dashboard setting |
| Every consumer is inventoried | Service/job names and reload action, without secret values |
| Replacement works | Fresh-process canary status and timestamp |
| Long-lived and scheduled consumers moved | New deployment/run evidence |
| Old credential is no longer needed | Non-secret key ID usage or successful consumer inventory |
| Revocation is complete | Provider confirmation and final canary |
Common questions
Will changing an environment variable update a process that is already running?
Do not assume so. A process may have read the value only when it started. Restart or redeploy it, then test a request from that new process.
Can I rotate a free API key to get more quota?
Do not assume that. A provider may apply limits to an account, project, organization, or shared free pool rather than to an individual key. Follow its published quota rules; rotation is a credential-maintenance action, not a quota workaround.
Should I wait for a quiet period before revoking a leaked key?
No. If exposure is credible, revoke promptly and restore service with a replacement. A planned overlap is for routine migration, not for keeping a compromised credential active.
How can I prove the new key worked without exposing it?
Run one small request from a fresh process and record the time, status, and non-secret request ID. Keep request headers and secret values out of console output and logs.