Manage API keys safely across projects and providers

  • API keys
  • Security
  • Environment variables
  • Developer workflow

When you collect a handful of free AI offers, dumping ten export XXX_API_KEY=... lines into ~/.zshrc or ~/.bashrc feels convenient—until three things go wrong:

  1. Env-var collisions: tools fight over shared names like OPENAI_API_KEY or ANTHROPIC_BASE_URL.
  2. Accidental leaks: a screen recording, crash dump, or env printout exposes plaintext keys.
  3. Committed secrets: a project .env or opencode.json slips into git add . and lands in a public repo.

Isolating keys per process or project takes five minutes and prevents all three.

Four ways to isolate credentials

1. Per-provider shell functions (best for switching quickly)

Instead of exporting global variables, wrap each provider in a small launcher function so its settings only exist inside that child process. On macOS, pull the secret straight from Keychain:

claude-a() {
  env -u ANTHROPIC_API_KEY \
    ANTHROPIC_BASE_URL="https://provider-a.example.com/api" \
    ANTHROPIC_AUTH_TOKEN="$(security find-generic-password -a "$USER" -s "cybereggs-provider-a" -w)" \
    claude "$@"
}

When you exit claude, your shell stays clean, and no plaintext secret ever sits in ~/.zshrc. On Linux, swap security for secret-tool. Keep debug/trace flags off when recording or sharing terminal output.

2. Project-level isolation with direnv

If you juggle work repos (internal gateway) and side projects (free credits), direnv loads environment variables automatically when you cd into a folder and unloads them when you leave. Add .envrc to your global .gitignore first, pull secrets from a keychain/vault rather than hardcoding them when possible, and review .envrc before running direnv allow.

3. Let your coding tool manage the secret

Most agents now separate config from credentials:

  • Codex CLI: reference the variable name via env_key in ~/.codex/config.toml instead of pasting the secret.
  • OpenCode: use {env:VAR_NAME} in opencode.json, or run opencode auth login to save keys in its local auth store.
  • Pi: use "$VAR_NAME" inside ~/.pi/agent/models.json.
  • DeepSeek Harness: keys saved in the Web UI live separately in $DSH_HOME/.credentials.yaml.

4. A local proxy gateway (for heavy multi-provider setups)

If you rotate through five or more providers and want automatic fallback without reconfiguring every client, run a local gateway like LiteLLM Proxy on http://127.0.0.1:4000/v1. Point all your tools at that one local address and let the proxy route upstream. Because the proxy sees every prompt and API key, bind it to localhost only and protect its config file.

Track quotas without storing keys in your notes

Once you have more than three providers, keep a simple four-column table in your notes—just quotas and dates, no secrets:

Provider Allowance and reset Expires Role
Provider A $25 one-time 2026-12-15 Priority—burn first
Provider B $5/day, resets 00:00 UTC Ongoing Daily driver
Provider C Ongoing free small models Ongoing Translation and scripts

Every week or two, compare your list against the free AI offers directory. Delete dead providers from your config and revoke unused keys in the provider dashboard.

If a key leaks

  1. Revoke it first. Log into the provider console and delete or rotate the exposed key immediately. Deleting a file in a new commit does nothing—bots scrape Git history continuously.
  2. Check usage and billing. Especially if a payment card is attached, inspect recent usage and set a hard spending cap.
  3. Clean Git history if needed. For a private repo that hasn’t spread, rewrite history with git filter-repo. If the repo was public, treat that key as permanently burned and never re-enable it.
  4. Rotate only what leaked. Because each provider has its own key, you only have to replace that one credential and update your keychain.