# Secure your stack: keep AI coding agents away from production keys

Instructions for an AI coding agent, from Charles Cushing's guide [How to keep AI coding agents away from your production keys](https://collider.so/writing/keep-ai-agents-away-from-production-keys/). Your human shared this link with you. Your job is to help them get to the end state below, one step at a time, with their approval along the way.

## The end state

- Every secret lives in one secrets manager. Each app has its own project with a `dev` and a `prod` environment.
- Production values reach each platform (Cloudflare, Vercel, AWS, and so on) by an automatic sync from the secrets manager, never by hand.
- No provider key is shared between apps, or between dev and prod. Each key is scoped down, capped where the provider allows, and expires within a year.
- Keys that were exposed under the old setup have been rotated, and old keys have been revoked.
- Agents (including you) log in with their own identity that can read `dev` values and see `prod` secret names, but never `prod` values, and can't write anything. The human's own CLI login is logged out.
- MCP servers authenticate with OAuth, not tokens in config files. Tools that send, share or delete ask the human first.

## Ground rules (follow these at every step)

1. **Never read, print, log, paste or commit a secret value.** Work with names only. If a check needs a value, write a script that uses the value in memory and prints only a status ("PASS", "HTTP 200", "401"), and ask the human to run it.
2. **The human handles every value.** They create keys at providers and paste them into the secrets manager. You can prepare forms, write commands, and tell them exactly where to click. You stop before a key is shown on screen.
3. **Ask before anything irreversible:** deleting or revoking a key, deleting a project or environment, production deploys, DNS changes, and anything that sends or publishes. Prefer the reversible option first: disable before delete, schedule deletion before deleting now.
4. **Change production only through the secrets manager.** Never `wrangler secret put`, `vercel env add`, `aws ssm put-parameter` or similar for production values.
5. **One app at a time.** Start with the app that has the fewest secrets. Run its smoke test before moving on.
6. **Say what you can't verify.** If you can't confirm a step worked, say so and give the human a check to run.

## Step 0: Inventory (names only)

Produce a table for the human: app × environment × secret name × where it lives today (`.env` files, platform dashboards, a shared vault, CI secrets) × which provider it belongs to.

- Search repos for env var names, `.env*` files, platform configs (`wrangler.toml`/`wrangler.jsonc`, `vercel.json`, CI workflows) and scripts that fetch secrets.
- List platform secret **names** with the platforms' CLIs or APIs (for example `wrangler secret list`, or Vercel's project env API, names only).
- Flag: the same key used by several apps, prod values stored somewhere agents can read, unused keys, and keys with no expiry.

Do not open `.env` files to read values. Listing their variable names is fine; when in doubt, ask.

## Step 1: One project per app, synced to where it runs

1. Help the human pick a secrets manager. Charles uses **Infisical**: per-app projects, environments, secret syncs to Cloudflare Workers and Pages, Vercel and many other platforms, and machine identities. **Doppler** also has per-app projects, read-only tokens per environment and a native Vercel sync; its Cloudflare Workers support is a CLI pipe rather than an automatic sync.
2. Create one project per app with `dev` and `prod` environments. Add a `.infisical.json` (or the tool's equivalent) to each repo with `defaultEnvironment: dev`.
3. The human puts values in. Where an old key was shared between apps, they mint a new per-app key instead of copying the old one (see step 2).
4. Create a sync from `prod` to each platform. **Turn on "prevent secret deletion" for every new sync**, so a mistake in the secrets manager can't wipe a live secret.
5. After the sync runs, compare names: the platform's secret names should match the project's exactly. When they do, the human can turn deletion protection off, so the secrets manager becomes the full record.
6. If an app fetches secrets at runtime with its own machine credential, replace that with a sync where possible. One fewer credential on a server.
7. Update each repo's agent instructions (CLAUDE.md, AGENTS.md or similar) with a short "Secrets" section: where values live, that production changes only happen in the secrets manager, and that agents use `dev` and names only.

## Step 2: One key per app and environment

For each provider key the human mints:

- **One key per app per environment.** Name it so it's readable at the provider: `appname-prod-202611` (app, environment, year and month).
- **Standard variable names** in the app (`ANTHROPIC_API_KEY`, `RESEND_API_KEY`), not app-suffixed ones. The project already says which app it's for.
- **Scope it down:** email keys that can only send, from one domain; storage tokens for one bucket; deploy tokens for one app.
- **Cap the spend:** on AI model providers, a workspace or project per app with a monthly limit where the provider supports one.
- **Expire it within a year,** and add a reminder to the human's task list a month before.

## Step 3: Find what was exposed, rotate it, revoke the rest

Assume everything an agent could read under the old setup was exposed. Sort it before rotating:

1. Write a status-only script for the human to run. For each old value: does the provider still accept it (a harmless read call, or the provider's token-verify endpoint), and does the same value appear in any new project? Print names and statuses only.
2. **Dead keys:** nothing to do except delete the old copies.
3. **Dev keys that moved to their new project:** low risk, since agents are meant to read dev.
4. **Production secrets that sat anywhere agents could read: rotate these first.** Watch for less obvious ones too, such as webhook signing secrets, SMTP passwords and service tokens.
5. Revoke old keys only after their replacement has been working for a day or two. Where the provider allows it, disable first and delete a week later.

## Step 4: Give agents their own login

1. Create a machine identity (in Infisical: Universal Auth) with no organization permissions.
2. Add it to every project with no base access, then grant: `dev` = read, `prod` = list (names only). In Infisical on the Pro plan, use **folder access**, because custom roles aren't available. Grant every folder, because a grant doesn't reach subfolders.
3. Create one client secret per machine. The human stores it in the OS keychain (macOS Keychain) or a mode-600 file on Linux. A lost machine then means revoking one secret.
4. At session start, trade the client secret for a short-lived access token (a day is a good default) and export it as `INFISICAL_TOKEN` for the session. In Claude Code this is a `SessionStart` hook that appends `export INFISICAL_TOKEN=…` to the file in `$CLAUDE_ENV_FILE`. Other harnesses: any session-start script that sets the variable.
5. The human logs their own CLI out (`infisical logout`). For production work they use the web dashboard, or log in, do the job, and log out.
6. Test, and report statuses only:
   - reading a `dev` secret works
   - listing `prod` names works
   - reading `prod` values is refused (403)
   - writing anywhere is refused
   - a project the identity isn't in is refused

## Step 5: Close the other doors

- **MCP servers:** switch any server that uses a bearer token in a config file to OAuth where the server supports it, then rotate the old token.
- **Chat app connectors** (for example claude.ai): for email send, forward and reply, Slack send, and file share and trash, choose "Always ask" (or "Blocked" for business mailboxes). "Needs approval" can still let the model act without asking in auto mode.
- **Coding harness permissions:** add an "ask" rule for every tool that sends, shares or deletes. Remove blanket allow rules for network tools such as `curl`. Narrow any "deploy to production without asking" rule to the main checkout only.

## Gotchas

- **The human's CLI login is shared by every process on the machine.** It's stored per OS user, not per terminal. An agent can use it even if the session has its own token. That's why step 4 logs the human out.
- **Infisical Pro has no custom roles or additional privileges.** Use folder access. It covers only the folder you name, so a project that keeps secrets in folders needs a grant on each, and every new folder needs a new grant.
- **Names-only access has to ask for names only.** Infisical's `GET /api/v3/secrets/raw` returns 403 under a list-only grant unless you pass `viewSecretValue=false`.
- **Under a machine token, the Infisical CLI ignores `.infisical.json`.** It wants `--projectId`, or `INFISICAL_PROJECT_ID` in the environment. A small shell wrapper can read the project ID from the nearest `.infisical.json` and set it.
- **Deletion protection keeps platform-only secrets, and also keeps secrets you deleted.** While it's on, deleting a secret in the secrets manager doesn't remove it from the platform. Remove it there too, then turn protection off once names match.
- **Commands run without a keyboard take the default answer.** If the human runs a command through the agent (for example with `!` in Claude Code), a `[Y/n]` prompt gets no input and takes its default, which may be "yes". Preview with `echo n | <command>` first.
- **Rotated secrets on Cloudflare Workers can linger for a few seconds.** Right after a rotation, the old value may still be accepted briefly. Recheck after a minute before treating it as a failure.
- **Vercel "sensitive" variables come back empty when imported.** Don't import them into the secrets manager; the human pastes those values in by hand.
- **Usage data at providers can lag by a day.** Before deleting a disabled secret, wait until "last used" data covers the time since it was disabled.
- **Some provider consoles don't respond to automated clicks.** If you're driving a browser and a dropdown won't open, hand the form to the human with exact values instead of retrying.

## Checklist

- [ ] Inventory done: names, locations and owners, no values
- [ ] One project per app, with `dev` and `prod`
- [ ] A sync from `prod` to each platform, with deletion protection on until names match
- [ ] No hand-set production secrets on any platform
- [ ] One key per app per environment, named `app-env-yyyymm`, scoped down, expiring within a year, with a reminder before it does
- [ ] A spending cap per app on each AI model provider
- [ ] Old keys checked; exposed production secrets rotated; the rest revoked (disabled first where possible)
- [ ] A machine identity for agents: `dev` read, `prod` names only, nothing else
- [ ] One client secret per machine, in the keychain
- [ ] Agent sessions log in as that identity; the human's CLI is logged out
- [ ] MCP servers on OAuth; no tokens in config files
- [ ] Send, share and delete tools set to ask first
- [ ] Each repo's agent instructions have a "Secrets" section

When you finish, give the human a short report: what's done, what's left, and which checks they should run themselves.
