The idea
AI apps handle many secrets: model API keys, database passwords, third-party tokens for tools. Leaked model keys are abused quickly for free compute.
Keep secrets in a secret manager (AWS Secrets Manager, GCP Secret Manager, Vault, Doppler) or at minimum environment variables injected at runtime; never in Git, never in prompts, never in traces. Tools fetch credentials in code; the model only ever sees the tool name and arguments.
Add secret scanning to your repo and CI (GitHub secret scanning, gitleaks), and redact known secret patterns from logs.
A leaked key can cost thousands in hours and expose every system it touches.
Words to know
- Secret scanningAutomated search for keys in code and history.
Try it without code
Scan your repos
- Run gitleaks on one of your repositories.
- Check the whole Git history, not just current files.
Use it in code
Load secrets from the environment and keep them out of logs.
import logging, os, re
API_KEY = os.environ["ANTHROPIC_API_KEY"] # injected by the platform, never committed
class RedactSecrets(logging.Filter):
PATTERN = re.compile(r"(sk-[A-Za-z0-9_-]{10,}|AKIA[0-9A-Z]{16})")
def filter(self, record):
record.msg = self.PATTERN.sub("[REDACTED]", str(record.msg))
return True
log = logging.getLogger("app")
log.addFilter(RedactSecrets())
log.warning("calling API with sk-ant-abc123def456ghi") # logged as [REDACTED]Move every secret in your project into environment variables or a secret manager and add gitleaks to CI.
Common mistakes
- .env files committed "just once".
- Secrets visible in trace inputs.