Area 06 · AI Security · Concept 11 of 20

Secrets Management

Keys live in a vault or environment variables. Never in prompts, code or logs.

11–20 · Production levelPhase 7 · Weeks 24–26≈ 45 min

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.

Why it matters

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

  1. Run gitleaks on one of your repositories.
  2. Check the whole Git history, not just current files.

Use it in code

Load secrets from the environment and keep them out of logs.

python
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]
$ gitleaks detect --source . --log-opts="--all"
PRACTICE · TICK IT AFTER THIS

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.

Go deeper