Blog

Why AI agents should never see your API keys

An agent that holds a raw API key can leak it through logs, prompts and injected instructions. Here is the case for keeping values out of the agent's context entirely.

BotPassword team5 min read

An AI agent should never see your API keys because everything an agent sees can end up somewhere you did not intend: a transcript, a log, a model provider’s servers, or the output of a command an attacker talked it into running. The safest key, from the agent’s point of view, is the one it never held.

BotPassword is a password manager for people and AI agents. This post explains the reasoning behind its central design rule: agents get outcomes, not values.

What actually happens to a key an agent can read?

A key the agent can read becomes part of its working context. From there it can move in several ways, and each one is documented for real tools.

  • It is sent to the model provider. Aider, for example, documents that it sends the files you add to the chat, plus a map of the repository, to the configured model. A secret sitting in one of those files leaves your machine.
  • It sits in the process environment. Codex CLI’s shell_environment_policy setting defaults to not stripping variables whose names contain KEY, SECRET or TOKEN, so an exported API key is visible to anything the agent runs, unless you change the policy.
  • It is reachable through tools that ignore your ignore file. Cursor’s own documentation says .cursorignore cannot block the terminal or MCP tools the Agent uses, so a secret in a file or the environment can still be reached with a command.

None of this is a flaw unique to one product. It is what happens when a tool is built to be helpful with the files and processes on your machine and you put a secret in that machine’s reach. See /agents/codex and /agents/cursor for the sourced details.

How do attackers use an agent’s access to a key?

Through prompt injection. An agent reads text from places you do not fully control: a README, an issue, a web page, a dependency’s source. If that text contains instructions, the agent may follow them. The dataset records several disclosed cases where this led toward credential theft:

  • Tracebit reported that a malicious README.md or GEMINI.md could make Gemini CLI run a command disguised as an allowlisted one and send environment variables to an external server. Google fixed it in July 2025 (/agents/gemini-cli).
  • Mindgard reported that instructions hidden in a source file could make Cline read environment variables, including API keys, and send them out through a ping command that needed no approval (/agents/cline).

If the key is in the environment or on disk where the agent can reach it, the attack only has to convince the agent to read it. If the agent never had a path to the value, the same attack finds nothing.

The malware side points the same way. In the Nx “s1ngularity” incident, a malicious package collected GitHub and npm tokens, SSH keys and .env files from developer machines. Local files and environment variables are the first place anything hostile looks.

Why not just tell the agent to be careful?

Instructions are not a boundary. A line in CLAUDE.md or AGENTS.md saying “never print secrets” is a request to a model that can be argued out of it, and it does nothing about the key traveling to the model provider in the first place. Even vendor controls are described by their authors as best-effort: Anthropic describes Read deny rules in Claude Code as a best-effort application to built-in tools and says a Bash deny rule “isn’t a security boundary” (Claude Code permissions).

Deny rules and ignore files are worth using, and the guide to keeping env files away from agents covers them. But they reduce exposure; they do not remove the secret from the machine. The robust fix is structural: do not put the value where the agent can reach it.

What is the design alternative?

Separate the agent from the secret-bearing step. The agent says what it wants done, and a trusted component that holds the key does it and reports the result.

BotPassword is built around this split (ADR 0011 records the decision):

  1. The agent calls an intent such as “run this command with the deploy credential” or “make this authenticated request”.
  2. A separate local process, the key custodian, checks that the user has granted that agent that action for that credential.
  3. The custodian performs the step itself, injecting the credential where it is needed.
  4. The agent receives an exit code and output with known secret values scrubbed, not the credential.

The design documents state a hard rule: no intent returns a secret, and the only reveal path is the human interface, which the agent endpoint has no code path to reach. Approval is given in BotPassword’s own interface, never in chat, so an agent cannot talk its way into a grant.

Does withholding the key make agents less useful?

The design argues it makes them more useful. Agents are often reluctant to type passwords or paste keys, which is reasonable, and it creates friction for exactly the tasks people want help with: deploying, signing in, rotating a token. The architecture notes that harness safety policies allow one pattern: hand the secret-bearing step to the user’s password manager, with the user’s approval given there. If the agent is told plainly that the value will never reach it and approval already happened, it has no reason to refuse or to ask again in chat.

What are the honest limits?

This approach removes the agent from the credential’s path; it does not make every residual risk disappear.

  • Another process running as the same user can still try to impersonate an agent client. The design records this as a limit, not something solved.
  • If an agent drives a browser to sign in, the filled field is technically readable in that browser. An extension-only sealed mode for credentials where that matters is planned, not built yet.
  • Output scrubbing catches the injected value and its common encodings, but a hostile program that transforms a secret in an unusual way is a harder problem than a well-behaved one.

What should you do today?

Until tools like this exist, keep secrets out of files the agent can read, use the ignore and deny mechanisms your tool offers, scope keys narrowly and rotate them. The guide to giving coding agents API access safely walks through the options. For where each kind of key lives, see /tools/api-key-identifier.

More from the blog

Give agents access. Keep the keys.

BotPassword is in early access. Request an invite and we'll email you when your spot opens.