Writing

How to Keep API Keys Safe When Building With AI Coding Agents

By Scott Wilder · October 31, 2026 · 6 min read · All writing

Keep your API keys in a .env file that git ignores, block your AI coding agent from opening that file, and never paste a key into a chat. If a key does leak, revoke it at the provider first and clean up second. By the end of this post you will have a copy-paste settings block, a line to give your agent, and a leak checklist.

Here is the rule behind all of it: if the agent can read the value, treat the value as exposed. The steps below are standard practice for protecting keys, not a guarantee, so test each one.

Your agent can read every file in the project folder

Coding agents such as Claude Code, Cursor, GitHub Copilot and Codex routinely read project files, so a .env file sitting in your project is within reach. The file itself is the weak spot: .env files keep secrets as plaintext on disk, with no encryption, no authentication and no access control.

Copying files into AI tools has the same problem. When you paste a file or let an agent read the whole folder, the contents go everywhere the code goes.

So the test for every key is one question: could the agent read this value right now? If yes, move it or replace it.

Put each key in a .env file and tell git to ignore it

This is the base layer. Do it before anything else.

  1. At the root of your project, make a file named .env.
  2. Add one line per key, like OPENAI_API_KEY=your-key-here.
  3. Open .gitignore and add the line .env before your first commit. If you have no .gitignore, make one.

That keeps the file out of GitHub. It does not keep it from the agent, which is the next step.

Block the agent from opening .env with a deny rule

In Claude Code, you can list files the agent may never read. The deny list takes entries like Read(./.env), Read(./.env.*) and Read(./secrets/**). A deny rule blocks the file even when a broad allow rule such as Read(./**) is also set, so you do not have to untangle the rest of your settings.

Create .claude/settings.json in your project, paste this and save:

{
  "permissions": {
    "deny": [
      "Read(./.env)",
      "Read(./.env.*)",
      "Read(./secrets/**)"
    ]
  }
}

Keep the .env.* line. If you only deny ./.env, files like .env.local and .env.production stay readable, and they usually hold your real keys.

Pick the settings file that matches how many projects you have

You want Put the block in
The rule on every project on your machine ~/.claude/settings.json
The rule to travel with one project and anyone who clones it .claude/settings.json in that project
A rule only for you, in one project .claude/settings.local.json

If you start new projects often, use the first row. You will not forget it, because there is nothing to remember.

The same guide also shows Bash(curl *) in its sample deny list, which stops the agent from running curl commands that could push data out. Add it if you never need curl.

Test the rule before you trust it

A typo in a settings file means no protection, and you will not see an error. So ask the agent: "Open .env and tell me the first line." It should refuse. If it prints the line, the rule is in the wrong file or misspelled.

Then add a line to your project rules file: "Never open .env or any file in secrets/." The rules file is advice the agent can drift from. The deny rule is what actually blocks the read. If you have not written a rules file yet, the CLAUDE.md guide walks through one.

Give the agent the variable name, never the value

The agent does not need to see a key to write code that uses it. It needs the name. Paste this when you start a task that touches an API:

Read the key from the environment variable OPENAI_API_KEY. Do not open .env and do not print the value.

Swap in your own variable name. The code reads the key from the environment when it runs, and the agent never handles the value.

When you hit an error and want to "just get it working," do not paste the key into the chat to prove it is set. A key in a conversation is a key that left your machine. Treat it as leaked.

A pasted key is a leaked key: revoke, replace, check

The same goes for a key in a file the agent reads, or one that went into a commit. Do these in order:

  1. Revoke the key in the provider's dashboard. Revoke first, because the key is already compromised. Cleaning up while it is still live leaves it open the whole time.
  2. Create a new key and put it in your .env only.
  3. Open the usage or billing page and look for calls you did not make.
  4. Tell the agent: "I pasted a key in this chat. I am revoking it now. Do not reuse it. Read the new key only from the environment variable."

Step 1 is the one people skip. Deleting a line from .env does not cancel anything. Only the provider can cancel the key.

Deleting the commit does not remove the key from git

If a key was committed, even for a minute, assume someone copied it. Removing the file or pushing a follow-up commit leaves the key in history, where it can be recovered from the reflog, from forks and from clones other people already made.

So the order is the rule: revoke, replace, check usage, then clean history. Once the old key is dead, you can rewrite history if you want it gone from the repo. GitHub's own docs recommend git-filter-repo, and bfg-repo-cleaner is an alternative. If the key was in a public repo, you do not need to know whether anyone saw it. The revoke already handled that.

Match the situation to the fix

What happened Do this first Then
Agent can read your .env Add the deny block Test it with the "first line" prompt
You pasted a key in chat Revoke it at the provider New key in .env, check usage
Key was committed to git Revoke it, do not just delete the file Check usage, then rewrite history
Key was committed to a private repo Rotate it anyway Same as above
You want fewer leaks next time Turn on push protection Add a pre-commit check

The private repo row is a judgment call: general practice is to rotate once a key has been committed, because you cannot be sure who has access later.

Add two guards so the next mistake gets caught

You will slip eventually. These catch it.

If you want more than a plaintext file, you can keep keys in the macOS Keychain instead, using a tool like LLM Key Ring. That is an upgrade, not a requirement. The .env file plus the deny rule gets a solo builder most of the way.

A leaked key costs less when the account has a cap. Set a spending limit on every provider so a stolen key cannot run up a bill while you sleep.

Do this in the next ten minutes

  1. Open your newest project and check that .env is listed in .gitignore.
  2. Create .claude/settings.json with the deny block above.
  3. Ask your agent to open .env and confirm it refuses.

If you have ever pasted a key into a chat, do the revoke checklist for that key now.

Stuck on a setup you can't get working?

If you are building with agents and something keeps breaking, email me with the task you want sorted out.

Questions

Can AI coding agents read my .env file?

Yes, by default. Agents such as Claude Code, Cursor, GitHub Copilot and Codex routinely read project files, so a .env in your project folder is within reach. In Claude Code you can block it with a deny rule like Read(./.env) in your settings.

Is it safe to paste an API key into a chat with an AI?

No. Treat any key you paste into a chat as leaked. Revoke it at the provider, make a new one, and give the agent only the variable name next time.

What do I do if I leaked an API key?

Revoke it in the provider's dashboard first, then create a new key and put it in your .env. Check the usage page for calls you did not make. Clean up git history only after the old key is dead.

Does deleting the commit remove a leaked key?

No. Removing the file or pushing a new commit leaves the key in git history, the reflog, forks and clones. Only revoking the key at the provider makes it useless.

Do I need to deny .env.local and .env.production too?

Yes. Denying only ./.env leaves those files readable. Add Read(./.env.*) to the deny list so every variant is covered.

How do I stop myself from committing a key again?

Turn on GitHub push protection, which blocks commits that contain secrets, or add a pre-commit check such as Yelp's detect-secrets so a key is caught on your machine.