Tips

Protecting API keys in AI apps: 7 mistakes that blow up your bill overnight

TL;DR

Never put API keys in the frontend, the repo or screenshots. Keep keys on the server or in a secret manager, call models through your backend, set spend limits, separate keys per environment and rotate them. Key leaked → revoke first, investigate second.

7 common mistakes

MistakeFix
Key in frontend / mobile app codeCall through a backend or serverless function
Committing .env to GitHub.gitignore + secret scanning / push protection
One key for every environmentSeparate dev / staging / prod keys with clear names
No spend limitSet limits and alerts in the Console
Pasting keys in chats, tickets, screenshotsUse a secret manager, share via a vault
Agents can read key filesDeny reading .env with permissions or hooks
Never rotatingRotate regularly and whenever someone leaves

Block agents from secrets (Claude Code)

// .claude/settings.json
{
  "permissions": {
    "deny": ["Read(./.env)", "Read(./.env.*)", "Read(./secrets/**)"]
  }
}

When a key leaks

  1. Revoke it in the Console immediately.
  2. Create a new key, update the secret manager.
  3. Review usage logs to assess damage.
  4. Purge it from git history (and treat the old key as gone forever).