HomeDocsBlogDownload

How to Prevent API Key Leaks in AI-Generated Code

AI coding agents like Claude, Cursor, and GitHub Copilot are extraordinary at generating functional code. They're also remarkably good at one thing developers dread: accidentally embedding API keys, database credentials, and authentication tokens directly into source files.

According to GitGuardian's 2025 State of Secrets Sprawl report, 12.8 million new secret occurrences were detected in public GitHub repositories last year โ€” a 28% increase from the prior year. With AI agents now writing a significant portion of development code, this problem is accelerating.

Here are five practical strategies to keep your secrets safe when working with AI coding assistants.

1. Never Put Real Keys in Your Prompt Context

The most common leak vector is surprisingly simple: developers paste real API keys into their editor or terminal while working with an AI agent, and the agent incorporates them into generated code.

๐Ÿ’ก Best Practice: Use placeholder values like YOUR_API_KEY_HERE or sk-test-xxxx when prompting AI agents. Replace with real values only in environment configuration files that are git-ignored.
# โŒ Don't do this โ€” the AI will hardcode the key
"Use my OpenAI key sk-abc123... to set up the API client"

# โœ… Do this instead
"Set up the OpenAI client reading the API key from
 the OPENAI_API_KEY environment variable"

2. Enforce .gitignore From Day One

Every project should have a comprehensive .gitignore before the first commit. AI agents frequently create configuration files, and without proper ignore rules, these files โ€” complete with credentials โ€” get committed.

# Essential entries for AI-assisted development
.env
.env.local
.env.*.local
*.pem
*.key
config/secrets.*
.claudearmor/vault/
*.sqlite
credentials.json

Use GitHub's gitignore templates as a starting point, then add entries specific to your AI tooling.

3. Use Pre-Commit Hooks for Secret Detection

Automated scanning at the commit stage catches secrets before they ever reach your repository. ClaudeArmor's KeyLeak Detection engine integrates with git hooks to scan every staged file:

# Install ClaudeArmor's pre-commit hook
claudearmor hooks install

# Now every commit is automatically scanned
git commit -m "Add payment integration"
# โ†’ ๐Ÿ” ClaudeArmor: Scanning 4 staged files...
# โ†’ โš ๏ธ  BLOCKED: Stripe secret key detected in payments.js:7
# โ†’ Run 'claudearmor fix payments.js' for remediation

The pre-commit hook approach is powerful because it creates a hard gate โ€” the commit literally cannot proceed until the secret is removed. This is far more reliable than relying on developers to remember to check.

4. Structure Your Environment Variables Properly

Create a clear separation between development and production credentials:

# .env.example (committed โ€” safe)
DATABASE_URL=postgresql://user:password@localhost:5432/myapp
STRIPE_KEY=sk_test_your_key_here
OPENAI_API_KEY=sk-your-key-here

# .env.ai (gitignored โ€” minimal AI agent access)
# Only non-sensitive vars the AI agent needs
NODE_ENV=development
API_BASE_URL=http://localhost:3000

5. Enable Real-Time Scanning With ClaudeArmor

The most comprehensive approach is continuous monitoring. ClaudeArmor's real-time file watcher scans every file modification โ€” including those made by AI agents โ€” and alerts you immediately when a secret pattern is detected.

# Start real-time monitoring
claudearmor watch --project ./my-app

# ClaudeArmor monitors file changes in real-time
# โ†’ ๐Ÿ‘€ Watching 847 files in ./my-app
# โ†’ โœ… server.js modified โ€” clean
# โ†’ โœ… routes/api.js modified โ€” clean
# โ†’ โš ๏ธ  lib/openai.js modified โ€” API key detected!

Unlike pre-commit hooks that only catch issues at commit time, real-time scanning catches them the moment the file is saved โ€” giving you faster feedback and reducing the chance of secrets lingering in your working directory.

Quick Checklist

Stop Secrets Before They Ship

ClaudeArmor catches API keys, tokens, and credentials before they reach your repository.

Download ClaudeArmor Free โ†’
๐Ÿ‘ฉโ€๐Ÿซ

Priya Sharma

Developer Advocate at ClaudeArmor. Helps engineers adopt security best practices. Previously a DevRel at Supabase and Auth0.