Guide · 2026-09-10 · 9 min read

LLM API Key Security: 12 Best Practices to Stop Inference Theft

Stolen API keys are the #1 way attackers run up AI bills. Twelve field-tested practices — from scoping and rotation to gateway enforcement and leak scanning — that keep your LLM credentials from becoming someone else's free compute.

TL;DR

  • A leaked LLM API key is a blank check — at frontier output prices, an uncapped key running flat-out burns hundreds of dollars a day.
  • Treat keys like production credentials, not config: scope them, cap them, rotate them, and never let them touch a repo or a client bundle.
  • The strongest posture is architectural: an AI gateway with machine authentication so raw provider keys exist in exactly one place.
  • Assume leakage will happen eventually and design so a leaked key expires fast and spends little.

1. Why API Key Theft Is a Pricing Problem

We track model pricing for a living, so we see the cost side of every breach. Attackers who steal a key don't optimize prompts, don't cache, and don't batch — they run the most expensive model at maximum concurrency, because it's not their money. The Claude token theft wave showed how quickly stolen access gets monetized through resale and abuse.

Every practice below does one of two things: shrinks the window a leaked key is useful, or shrinks the blast radius when it is.

2. The 12 Practices

Custody

1. Never hardcode keys. Not in source, not in client-side bundles, not in mobile apps. Anything shipped to a browser or device is public. Use a secrets manager or your platform's encrypted env storage.

2. Keep keys out of version control — and scan anyway. Add pre-commit hooks and CI scanning (gitleaks, trufflehog, or your provider's scanner). Scanning is the safety net for the day the policy fails.

3. One key per workload. A shared key across services means one leak exposes everything and revocation breaks everything. Per-service keys make rotation and incident response surgical.

Scoping and limits

4. Minimum permissions, always. If the provider supports scoped keys (specific models, no fine-tuning, no admin), take the least privilege available.

5. Hard spending caps and alerts. Configure strict budget caps and alerts on every provider account so anomalous usage surfaces in minutes, not on the monthly invoice. This is the single highest-leverage control.

6. Rate limits per key. A stolen key that can only make 60 requests/minute is a slower robbery.

Lifetime

7. Ban long-lived personal keys. Personal access tokens that live for a year are a year-long liability. Prefer workspace- or service-owned credentials.

8. Short-lived tokens with rotation. Migrate to short-term bearer credentials and rotate on a schedule — weekly for high-spend workloads. If a leak happens, the credential expires before it can be monetized.

9. Revoke on offboarding, immediately. Departures and vendor changes should trigger same-day key revocation, not a quarterly cleanup.

Architecture

10. Deploy an AI gateway. Route all LLM traffic through a gateway with machine authentication. Raw provider keys live in the gateway and nowhere else — no more keys in developer .env files, which is where a huge share of leaks originate.

11. Verify every request. Require authentication and validation on every individual API request rather than trusting a long-lived session. Session-level trust is what credential thieves exploit.

12. Log and alert on anomalies. A sudden burst from an unfamiliar ASN at 3 a.m., a model your app never calls, a 10x jump in output tokens — all should page someone automatically.

3. Priority Order

4. Response Playbook for a Leaked Key

1. Revoke immediately — rotate, don't just delete, so you can diff what breaks.

2. Pull usage logs for the key's full lifetime; assume the leak predates discovery.

3. Dispute with the provider — most will work with you on obviously fraudulent spikes if you report fast.

4. Post-mortem the custody path: how did the key get from your secrets store to wherever it leaked? Fix that path, not just the key.

Related Reading