Rotating and scoping AI API keys means giving each workload its own credential with only the permissions it needs, replacing that credential on a predictable schedule, and revoking the old one only after the replacement is verified. Done well, a leaked key costs you one narrow integration and a short cleanup instead of your entire account balance.
This guide is for practitioners who wire AI APIs into automation — scheduled jobs, WordPress plugins, local model gateways, CI pipelines — and who want a rotation process they can actually run. It covers what rotation and scoping mean, an ordered procedure with verification checks, the places keys usually leak, and the tradeoffs that make some rotation plans fail.
Rotation and scoping, defined
Most AI APIs authenticate with a bearer credential: whoever holds the key can spend the associated credit until it is revoked. Safeguard’s key management documentation states this directly — an inference key is a bearer credential, and whoever holds it can spend the workspace’s credit until the key is revoked. That single property drives every recommendation below.
Scoping answers “what can this key do, and where is it allowed to be used?” It covers permission sets, the API surfaces a key is accepted by, and how many systems share the same secret. Rotation answers “how long does this key live?” It is the process of issuing a replacement, moving every caller onto it, and then revoking the predecessor.
The two work together. A key scoped to a single job is cheap to rotate, because there is only one caller to update. A key shared by six services is expensive to rotate, so teams postpone it — which is exactly how long-lived, over-privileged credentials accumulate.
What good looks like: one key per workload, per environment
Both sources converge on the same shape of key inventory:
- Unique keys per person. OpenAI’s API key safety guidance says each team member should have their own key, that sharing keys is against the Terms of Use, and that new members should be invited from the Members page so they receive their own key. Individual keys also make revocation surgical: you can disable one person’s access without breaking everyone else’s automation.
- Unique keys per app and per environment. Safeguard’s guidance recommends a key per application and per environment, so a staging experiment never carries production spend or production permissions.
- Scoped permissions. OpenAI supports assigning permissions to individual API keys, and its project keys can be created with an expiration date. Safeguard’s inference keys carry exactly one scope,
inference, and are not accepted by its other APIs — a useful reminder that “scope” often means “this key only works with this one service,” not a rich permission matrix. - Service identity, not personal identity. A Safeguard key acts for the member who created it and stops working if that member is deactivated or removed. Plan for automation to be owned by a durable service account or dedicated identity, not by whoever happened to click “create” first.
Before you rotate: build the inventory
Rotation fails most often because nobody knows which systems use the key. Do this once, then keep it current:
- List every AI API key in the account, with its creation date, owner, and expiration (if any).
- Map each key to the workload that uses it: which site, which cron job, which plugin, which container, which CI secret.
- Note where each key is stored. If the answer is “in a file in the repo,” that is a finding, not a detail.
- Confirm you have a staging or canary path where a new key can be tested before it reaches production.
- Confirm the secret store you will write the new key into — a secret manager or a runtime environment variable, following Safeguard’s guidance to give the key to the application at runtime rather than embedding it in source or configuration files.
The rotation procedure
OpenAI’s guidance describes the core sequence: before a key expires, create a replacement, update your applications to use it, and revoke the old key once you have verified the replacement works. Expanded into operational steps:
- Create the replacement key with the narrowest scope that still works, and with an expiration date where the provider supports it. OpenAI notes administrators can enforce a maximum key lifetime at the organization or project level, and that project limits cannot exceed the organization limit.
- Store the new key as a secret. Copy it directly into your secret manager. Safeguard’s documentation notes the full key is shown only once at creation and only a SHA-256 hash is stored afterwards, so a key you fail to capture has to be revoked and reissued.
- Deploy to a canary. Point one non-critical caller at the new key first. This is where you discover that the key is missing from a config map, or that a proxy strips the
Authorizationheader. - Verify with a real request, not a ping. Run the actual job — a generation call, an embedding batch, a scheduled summary — and confirm a successful response plus the expected usage in your provider dashboard.
- Roll out to the remaining callers in the same order you inventoried them, restarting services so they pick up the new value.
- Watch for traffic on the old key. If you have request logs, filter for the old key’s masked form. Any hit means a caller you missed.
- Revoke the old key once the replacement is verified and the old key is quiet. Revocation is the step that actually reduces risk; creating a new key alone changes nothing.
- Record the rotation — date, key name, owner, next rotation date — in the same inventory from the previous section.
If the provider exposes key lifetimes, set an expiration shorter than your intended rotation interval so an expired key fails loudly rather than lingering silently. If it does not, rotation is a process commitment rather than a technical control: Safeguard notes, for example, that keys created in its Console have no expiry date and work until revoked.
Where keys leak, and the habits that prevent it
- Client-side code. OpenAI’s guidance is unambiguous: never deploy a key in client-side environments such as browsers or mobile apps. A key shipped to a browser is public.
- Source code and configuration files. Safeguard’s guidance is to keep keys out of source code and config files and pass them at runtime from a secret manager or an environment variable such as
SAFEGUARD_INFERENCE_KEY. - Logs. If you log outgoing requests, redact the
Authorizationheader. Bearer tokens sitting in log aggregation systems are one of the most common quiet leaks. - Tickets, chat, and screenshots. When reporting a problem, quote the key’s name or its masked form — Safeguard displays keys masked as a prefix and suffix, for example
sg-api-1a2b3c4d…9f8e— never the full value. - Shared secrets across environments. A staging key that also carries production spend turns a test into an incident.
Illustrative example: rotating a key in a WordPress automation
The following is a proposed pattern, not a tested deployment; adapt it to your own plugin and hosting.
Suppose a WordPress site runs a scheduled job that calls an AI API to summarise new posts. The key lives in an environment variable read at runtime, never in the plugin source:
// mu-plugin, illustrative only
$key = getenv( 'AI_SUMMARY_API_KEY' );
if ( empty( $key ) ) {
error_log( 'AI summary job: AI_SUMMARY_API_KEY is not set' );
return;
}The rotation runbook then looks like this:
- Create a new key scoped to this one integration.
- Add it to the hosting environment (or your secret manager) as
AI_SUMMARY_API_KEYon staging, then run the scheduled event manually — for examplewp cron event run ai_summary_daily— to confirm it succeeds. - Update the production environment variable and re-run the job once.
- Watch the logs for authentication failures on the next scheduled run.
- Revoke the old key and note the rotation date.
Two failure modes are worth pre-empting. First, WordPress cron is request-triggered on many hosts, so a job may not run for hours after you change the variable; force a run rather than waiting. Second, if the same key is also used by a page builder, a form plugin, or a separate “AI assistant” plugin, that caller will keep working until you revoke — and then break. The inventory step is what catches this.
Local inference endpoints need the same discipline
Self-hosted model servers are often run on localhost with authentication disabled, which is reasonable while nothing else can reach them. The moment an endpoint is exposed to a LAN, a container network, or a tunnel, it needs per-client tokens, a reverse proxy that enforces them, and the same rotation discipline described above. Check your server’s documentation for its authentication options rather than assuming a default; many local runtimes offer either no authentication or a single shared token, and a single shared token has exactly the rotation problem this article is about.
Workload identity federation, when it is available
OpenAI’s guidance recommends that supported workloads use workload identity federation instead of storing a long-lived API key: the workload exchanges an identity from a trusted provider for a short-lived access token, with a dedicated service account holding only the permissions the workload needs. Where a provider offers this, it removes the rotation problem rather than managing it, because there is no long-lived secret to rotate. The tradeoff is setup complexity and provider support, so treat it as the preferred target for new integrations and a migration candidate for existing ones.
Limits and tradeoffs to plan around
- Overlap windows. Rotation requires both keys to be valid briefly. That window is a real exposure period; keep it as short as your deployment allows.
- Scoping granularity varies. Some providers offer per-key permissions and expiration; others offer a single scope and no expiry. Your rotation cadence has to match what the provider actually enforces.
- More keys means more inventory. One key per workload improves blast radius but multiplies bookkeeping. Automate the inventory or it will rot.
- Revocation breaks things. That is the point, but it means revocation should always follow a verified rollout, never precede it.
Your next action
Pick one production key that is shared by more than one system. Create a scoped replacement for the single most important caller, move that caller over, verify a real request, and revoke the old key. That one rotation will tell you more about your inventory gaps than any policy document. Then schedule the next one before you forget.
If you run into a specific failure — a 401 after rotation, a cron job that never picks up the new variable, a provider whose keys never expire — share your environment, the exact error, and what you already checked in the Ai-Forum.org community so others can compare notes.
FAQ
How often should I rotate an AI API key?
Neither source prescribes a fixed interval. The practical rule is to rotate on a schedule you can sustain and immediately after any suspected exposure. Where the provider supports expiration dates or an enforced maximum key lifetime, set the expiry shorter than your rotation interval so a missed rotation fails visibly.
Can my team share one API key to simplify setup?
OpenAI's guidance says no: each team member should have a unique key, sharing keys is against the Terms of Use, and new members should be invited from the Members page so they receive their own key. Individual keys also let you revoke one person's access without disrupting everyone else.
What if the provider's keys never expire?
Then rotation depends on process rather than enforcement. Safeguard notes that keys created in its Console have no expiry date and work until revoked, which means the inventory and rotation schedule in this guide are the only thing standing between a leaked key and ongoing spend.
What happens to an automation key when its creator leaves?
It can stop working. A Safeguard key belongs to the workspace and acts for the member who created it, so it stops functioning if that member is deactivated or removed. Create automation keys under a durable service identity rather than a personal account.