I expected my .env files to be the problem. They weren't.
I ran a key scanner over my whole home folder: 803,628 files. It found 179 real API keys, plus 5 that looked like examples. Between them, those 179 keys had 1,338 plain text copies on my disk. One key alone had 173 copies spread over 105 folders.
Here is where they were. A key can be in more than one place, so the numbers add up to more than 179.
| Where | Keys with a copy there |
|---|---|
Coding agent folders (~/.claude, ~/.codex, ~/.cursor, .mcp.json...) | 119 |
| Other files (notes, configs, logs) | 53 |
.env files | 32 |
| Source code | 22 |
| Shell history | 4 |
And 16 of the keys sat in files that git tracks.
The agent folders were the surprise. Of those 119 keys, 115 had a copy in Claude Code's session transcripts under ~/.claude/projects. Five were in other files under ~/.claude, two in ~/.codex and two in .gemini.
Why agent folders fill up with keys
A coding agent keeps a record of its sessions. Claude Code's docs say it stores session transcripts "locally in plaintext under ~/.claude/projects/ for 30 days by default". Transcripts from Claude Desktop and Cowork sessions are exempt from that 30-day limit by default.
A transcript holds what the agent read and what its commands printed. So a key ends up there when:
- the agent reads a
.envfile to debug something - a command prints the environment, like
envorprintenv - a failing
curl -vor an HTTP client logs theAuthorizationheader - you paste a key into the chat, or into a config the agent manages, like an MCP server's
envblock
None of this is a bug. The agent did what it was asked, and the record kept the key.
Check your own machine in five minutes
Count the files in agent folders that hold something shaped like a key. This prints file counts, not keys:
grep -rlE 'sk-(proj|ant)-[A-Za-z0-9_-]{20}|ghp_[A-Za-z0-9]{36}|AKIA[0-9A-Z]{16}' ~/.claude ~/.codex ~/.cursor 2>/dev/null | wc -l For a proper scan with hundreds of rules, point gitleaks at the folder:
gitleaks dir ~/.claude --redact --redact keeps the keys out of the output.
What I changed
1. Rotate anything real. A key that sat in a transcript, a synced folder or a git-tracked file has to be treated as leaked. Deleting the file doesn't un-leak it. Make a new key at the provider and revoke the old one.
2. Shorten how long transcripts live. Set cleanupPeriodDays in ~/.claude/settings.json to fewer days if you don't resume old sessions.
3. Add deny rules, and know where they stop.
{ "permissions": { "deny": ["Read(./.env)", "Read(./.env.*)"] } } Per Claude Code's permission docs, these rules cover its own file tools and the shell commands it recognizes, like cat, head, tail and sed. They don't cover a command that reads without naming the file, like grep -r from the project root, or a script that opens the file itself. So node -e 'require("dotenv").config(); console.log(process.env)' still prints everything.
4. Don't start the agent from a shell with keys exported. Every command the agent runs inherits that environment, and one env puts it all in the transcript.
5. Keep the value out of files. If a .env file holds a reference instead of the key, a transcript that reads the file gets the reference. The real value goes to your app at run time. op run (1Password) and doppler run work this way, and dotenvx keeps the values encrypted in the file instead. The limit: the process you start still has the key, and so does anything it spawns, including an agent.
6. Use short-lived, scoped keys with spend limits wherever the provider allows it. A leaked key that expired or can only spend $5 is a small problem.
Disclosure
The scanner I used is kj, part of KeyJar, which I'm building. It finds keys like this, keeps them in a local encrypted vault, and swaps .env values for kj:// references. The scan is free and changes nothing. Everything above works without it.
Your turn
If you run the grep or gitleaks on your own agent folders, I'd like to know: did your numbers look like mine?
*I wrote this with help from Claude. The numbers come from a scan of my own laptop, and the quotes come from the Claude Code docs.