The only reliable defense is to never let the LLM see the secret in the first place. "Tell it not to share" is not a defense.
The layered strategy
| Layer | What it does | How |
|---|---|---|
| 1. Don't put secrets in files the agent reads | If it's not in context, it can't be repeated | Use OS keychain, secrets manager, or runtime env vars from outside the repo |
| 2. Ignore-files | Belt-and-suspenders for #1 | .cursorignore, .aiignore, .aiderignore, etc. |
| 3. Use placeholders in committed configs | Real values exist only in the running process | .env.example with API_KEY=<FROM_KEYCHAIN> |
| 4. Rotate aggressively | Limits blast radius if leaked | OPENAI / Anthropic / GitHub keys: rotate quarterly minimum |
| 5. Scoped tokens | Even leaked tokens have small blast radius | Read-only DB users, repo-scoped GitHub PATs |