n4nAI

Key rotation for multi-provider LLM routing setups

Practical guide to key rotation multi-provider llm routing: inventory keys, automate rotation with secrets managers, and avoid downtime during provider failovers.

n4n Team4 min read792 words

Audio narration

Coming soon — every post will get a voice note here.

Key rotation multi-provider llm routing is not just a security checkbox; it is an operational necessity when you spread traffic across OpenAI, Anthropic, Google, and smaller hosts. A single leaked key can drain credits or get your account suspended, and manual rotation breaks live traffic if you don’t plan for overlap.

Threat model: what breaks without rotation

A static API key in a GitHub repo or a long-lived env var is a ticking clock. Providers rate-limit per key, and some tier access by key age or usage pattern. When you route across multiple providers, a compromised key on one provider can expose your routing logic if you reuse identifiers.

The real risk is not just exfiltration. Provider-side key revocations happen during security incidents. If you have no automated rotation, you scramble to update configs while latency climbs.

Step 1: Map every provider and key scope

For effective key rotation multi-provider llm routing, start with a complete inventory. List each provider, the models you call, and the exact permissions on the key. Many teams use a single global key per provider; that is fine for starters but limits blast-radius control.

Create a table in your secrets manager:

{
  "openai": {
    "key_id": "prod-oa-1",
    "scopes": ["chat", "embeddings"],
    "rotation_days": 30,
    "overlap_days": 3
  },
  "anthropic": {
    "key_id": "prod-an-1",
    "scopes": ["claude-3"],
    "rotation_days": 45,
    "overlap_days": 5
  }
}

Treat this as code. Check it into a private repo as keyspec.json and version it.

Step 2: Choose rotation period and overlap window

Short rotations (7–14 days) reduce exposure but increase automation burden. For most setups, 30 days with a 3-day overlap is a sane default. Overlap means you issue the new key, verify it works, then revoke the old key only after the overlap expires.

Tradeoff: longer overlap means two valid keys exist simultaneously, widening the attack surface slightly. Keep overlap minimal but nonzero.

Step 3: Store keys in a secrets manager

Never put provider keys in plain env files or Docker images. Use AWS Secrets Manager, GCP Secret Manager, or Vault. Fetch at boot, cache in memory with TTL shorter than rotation period.

Example Python fetch with caching:

import boto3, time

class KeyCache:
    def __init__(self, secret_id, ttl=300):
        self.client = boto3.client("secretsmanager")
        self.secret_id = secret_id
        self.ttl = ttl
        self._val = None
        self._ts = 0

    def get(self):
        if time.time() - self._ts > self.ttl:
            resp = self.client.get_secret_value(SecretId=self.secret_id)
            self._val = resp["SecretString"]
            self._ts = time.time()
        return self._val

Step 4: Build a key selector with health checks

Your routing layer needs to pick a key that is live and not rate-limited. Maintain a pool per provider. Mark a key unhealthy after two 429s, rotate it out temporarily.

class KeyPool:
    def __init__(self, keys):
        self.keys = {k: {"healthy": True, "cooldown": 0} for k in keys}

    def pick(self):
        for k, meta in self.keys.items():
            if meta["healthy"] and meta["cooldown"] < time.time():
                return k
        raise RuntimeError("no healthy keys")

This is simplified; production needs metrics and alerting.

Step 5: Integrate rotation with routing logic

When the secrets manager issues a new version, your service should hot-reload without restart. Use a file watcher or a pub/sub notification.

If you use a unified inference gateway (n4n.ai, for instance, exposes one OpenAI-compatible endpoint across 240+ models with automatic fallback on provider degradation), you still must rotate the gateway’s own API token on a separate schedule from the underlying provider keys it holds.

A minimal TS middleware that swaps keys on SIGHUP:

process.on("SIGHUP", () => {
  reloadKeys().then(() => console.log("keys rotated"));
});

async function reloadKeys() {
  const res = await fetch("https://vault.internal/v1/keys");
  const data = await res.json();
  keyPool.replaceAll(data.keys);
}

Step 6: Handle fallback and degraded providers

Multi-provider routing implies you fail over when one provider returns 529 or 403. But if your rotated key is still propagating, a fallback may hit a dead key.

Implement a retry policy that distinguishes auth errors from capacity errors. On 401, immediately pull a fresh key from the pool; on 429, backoff.

def call_with_fallback(providers, prompt):
    for prov in providers:
        key = prov.pool.pick()
        try:
            return prov.complete(prompt, api_key=key)
        except AuthError:
            prov.pool.mark_dead(key)
        except RateLimit:
            prov.pool.cooldown(key, 10)
    raise AllProvidersDown()

Step 7: Audit usage and detect leaks early

Per-token metering is your early warning. If a key’s usage spikes at 3 AM from an unknown region, rotate immediately.

Most providers give usage endpoints; aggregate them. If you use a gateway with per-token usage metering, pipe those logs to your SIEM.

Set alarms:

# example CloudWatch alarm CLI (illustrative)
aws cloudwatch put-metric-alarm \
  --alarm-name "key-usage-spike" \
  --metric-name "TokenCount" \
  --namespace "LLMRouting" \
  --period 300 \
  --evaluation-periods 1 \
  --threshold 100000 \
  --comparison-operator GreaterThanThreshold

Common pitfalls and tradeoffs

Cold-start latency. Fetching keys at boot adds milliseconds. Cache aggressively but respect rotation TTL.

Secret manager rate limits. If you have 50 workers each fetching every 60 seconds, you can hit quotas. Use a sidecar or push model.

Over-rotation. Rotating daily gains little security but multiplies failure modes. Stick to 30-day cycles unless compliance demands otherwise.

Ignoring provider-specific cache hints. A frequent mistake in key rotation multi-provider llm routing is stripping cache-control headers during key injection. Some providers honor prompt prefix caching; if your layer drops those hints, you lose caching and blow up cost. Forward provider cache-control hints unchanged.

Single point of rotation. If your rotation script dies, keys go stale. Run it as a cron with alerting, not a one-off notebook.

Final checklist

  • Inventory all provider keys with scopes and rotation periods
  • Store in secrets manager, never in env files
  • Implement in-memory cache with TTL < rotation window
  • Hot-reload on secret version change
  • Health-check keys, isolate auth vs rate-limit failures
  • Alert on usage anomalies per token
  • Test rotation in staging by forcing overlap revocation

Key rotation multi-provider llm routing becomes boring once you automate it. The goal is to make a security-critical process invisible to application code and survivable during provider outages.

Tagsapi-keyskey-rotationmulti-providerrouting

Written by

n4n Team

The team building n4n — a single OpenAI-compatible API in front of 240+ models, with automatic fallback, load balancing and pay-per-token metering.

More from n4n Team →

All api key rotation & secrets management posts →