blog / security
Keep your API keys away from your AI coding agent
How API keys leak through AI coding agents via prompts, .env and MCP configs, the habits that stop it, and what to do when a key gets out.

An AI coding agent reads your files, runs your commands and sends what it sees to a model. That is exactly why it is useful, and exactly why an API key sitting in the wrong place does not stay secret for long. This guide covers how keys leak through agents, the habits that stop it, and what to do when one gets out anyway.
How secrets end up in an agent's context
An agent does not need to be malicious to leak a key. It only needs to see it. The usual paths:
- Pasted prompts. "Here's my Stripe key, wire up the webhook" puts the key into the conversation, the session transcript and the provider's request.
.envfiles. Agents read files to understand a project. Ask one to "debug the config" andcat .envis a perfectly reasonable step from its point of view.- MCP config files. A
.mcp.jsonorconfig.tomlwith a token written in plaintext is just another file in the repo. It gets read, quoted in an answer, and sometimes committed. - Command output and errors. A failed request often prints the full URL, including
?token=...or anAuthorizationheader. That output lands in the agent's context and in your scrollback. - Commits. An agent that runs
git add -Awill happily stage the file you forgot to ignore.
Once a key has been in a model request, a log or a pushed commit, treat it as exposed. You cannot reliably pull it back.

Rules that actually prevent leaks
Never paste keys into chat. Tell the agent the name of the variable instead: "the key is in STRIPE_SECRET_KEY". Code reads it from the environment at runtime; the value never appears in the prompt.
Ignore secret files before the first commit. Put this in .gitignore on day one:
.env
.env.*
!.env.example
*.pem
*.key
Commit an .env.example with empty values so the agent (and your teammates) know which variables exist without seeing any of them.
Scan before every commit. .gitignore only helps with files you thought of. A pre-commit secret scanner catches a key pasted into a source file or a test fixture. gitleaks is a well-known open-source option:
gitleaks protect --staged # check what is about to be committed
gitleaks detect # scan the whole repository history
Subcommand names have shifted between releases, so check gitleaks help for yours.
Wire it into a pre-commit hook so neither you nor an agent can commit around it by accident.
Use environment-variable indirection in MCP configs. Most MCP clients can expand variables in their config, so the file holds a reference, not the token. In Claude Code's .mcp.json, for example:
{
"mcpServers": {
"tickets": {
"type": "http",
"url": "https://mcp.example.com/mcp",
"headers": { "Authorization": "Bearer ${TICKETS_TOKEN}" }
}
}
}
The file can now be read, shared or even committed without giving anything away. Check your own client's docs for its exact syntax.
Give agents least-privilege tokens. If the agent only needs to read issues, create a token that can only read issues. Prefer read-only database roles, scoped API keys and short expiry times. A leaked read-only token for one project is an incident; a leaked admin key for the whole account is a very bad week.
When a key leaks anyway
- Rotate it first. Revoke the key at the provider and issue a new one. Deleting the file or rewriting history does not un-leak it.
- Find out where else it went. Search the history, not just the working tree:
git log -p -S "sk_live_" --all # every commit that added or removed the string
git log -p --all | grep -n "API_KEY"
- Clean history if the repo is shared, using a tool built for it, then tell collaborators to re-clone. This is hygiene, not a fix; the rotation was the fix.
- Check the provider's logs for usage you do not recognise.
How Bettr's MCP Vault handles it
Bettr is a terminal for running AI coding agents side by side, and its Vault is built around one idea: the agent should be able to use a credential without ever holding it.
- Secrets stay in Bettr's main process. You add an MCP server or HTTP API once (from the catalog, pasted JSON, a curl command or by hand); keys are encrypted with your OS's secure storage (Keychain, DPAPI or Secret Service). Credentials never leave main.
- Attach at runtime, not in config. Bettr itself is the MCP server,
bettr-vault, registered once with Claude Code, Codex or OpenCode. Each pane gets its ownBETTR_VAULT_TOKEN, so one gateway serves a different tool list per pane. Right-click a pane and pick Tools… to attach a server, with no restart and no token written into.mcp.json. - Redaction. Every string that reaches the model or the audit log passes through redaction, because upstream errors love to quote
?token=URLs. - Policies per pane. Policy for this pane offers Full, Read-only and Ask before writes (a native dialog asks for your OK before a write runs), plus a Rate limit. Individual tools can be put on a deny-list, and a denied tool stays denied even when a kit grows.
- Recent calls. Each pane keeps an audit of its calls, newest first. Arguments are never recorded.
If you still need a plain config file, Export config… writes the entry into the CLI's config and auto-gitignores the files it drops. The CLI vault does the same for command-line tools: a profile's credentials are injected into that pane's CLI without touching your global login.
Frequently asked questions
Is it safe to let an agent read my .env file? Assume anything the agent reads can end up in a model request and in logs. Keep real values out of files the agent works in, reference variables by name, and keep an .env.example with empty values for context.
Does .gitignore remove a secret I already committed? No. .gitignore only stops untracked files from being added. A key that is already in history stays there; rotate it, then clean history if the repository is shared.
Are read-only tokens really worth the extra setup? Yes. Agents make mistakes and follow instructions found in files and web pages. A read-only, narrowly scoped token limits what a mistake or a leak can cost to the smallest possible blast radius.


