All courses

Keeping secrets out of the agent's reach

Explain how secrets leak: the agent reads `.env`, prints keys in logs, commits them, or sends them to the model provider. Use ignore and deny rules, test keys and separate credentials so the agent never needs the real ones.

Builds on: What never to let an agent touch, What the agent can and cannot see

Read it card by card Start the whole course

The agent asks to look at your .env

You type: 'the weather lookup fails with error 401, fix it'. A few steps in, the agent shows: ⚙ Run: cat .env [Approve? yes / no / always] Your `.env` holds your real weather API key and your payment key. The agent only wants to check the settings, and it is your own laptop. Saying yes feels harmless. Is it?

Whatever the agent sees leaves your laptop

Everything the agent reads, and the output of every command it runs, becomes part of its context. That context is sent to the model provider's servers at every step and is kept in the session transcript. So approving `cat .env` ships your keys off your machine. Keys leak in three more ways. The agent prints them into logs or command output (`printenv`, debug lines). It writes them straight into code. Or it commits `.env`: git, the project's history tool, saves a snapshot of the file. That history can later be pushed (uploaded) to a site like GitHub, where others can see it.

Make real keys unreachable

1. **Separate credentials**: create a test-mode or dev-only key just for this project, with low limits and easy to revoke. Your real keys (the ones your live app or payments use) stay in your password manager. 2. Put only that test key in `.env`. List `.env` in `.gitignore`, the file that tells git never to save certain files. 3. Add **deny rules** in the agent's settings for `.env` and secret folders. 4. Launch the agent from a terminal with no real keys exported. 5. Answer no to `cat .env`, `env` and `printenv`. 6. Leaked anyway? Revoke the key and create a new one.

Fixing the 401 without showing the key

▸ You: The weather lookup returns 401. Don't read or print `.env`. The key is loaded as `WEATHER_KEY`. Check that the code reads that exact name. ⚙ Read: src/weather.py ⚙ Read: .env.example ⚙ Edit: src/weather.py ◂ Agent: The code reads `WEATHER_API_KEY`, but `.env.example` defines `WEATHER_KEY`. I changed the code to read `WEATHER_KEY`. Open `src/weather.py` and confirm the name. The agent needed only names, never values, and `.env.example` gave it those. Your prompt was a useful hint, not a lock. Had the agent slipped, `.env` held only a test key.

A project set up so keys stay out

`.gitignore` keeps `.env` out of git. `git check-ignore .env` confirms it: it prints the name only if the file is ignored. `.env.example` lists the variable names with placeholders, so the agent never needs the real file. The deny rules use Claude Code's format, where `Read(./secrets/**)` covers every file under `secrets/`. Other tools have their own, for example Cursor's `.cursorignore`. Deny rules block the agent's file-reading tool, but a shell command like `cat .env` can still get through, so keep answering no.
$ cat .gitignore
.env
$ cat .env.example
WEATHER_KEY=<your-test-key>
$ cat .claude/settings.json
{"permissions": {"deny": [
  "Read(./.env)",
  "Read(./secrets/**)"
]}}
$ git check-ignore .env
.env

Remember

Anything the agent reads, prints or commits can leave your machine. A 'please don't' in your prompt is a hint, not a lock. Give the project only test keys. Keep `.env` out of git with `.gitignore`, block the agent's reads with deny rules, and keep real credentials where the agent can't reach them. If a real key ever shows up in a session, log or commit, revoke it.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.