Guide

How to give AI coding agents API access safely

Give an AI coding agent API access by limiting what the credential can do, not by trusting the agent to be careful. Use a scoped, short-lived, revocable credential for a non-production environment, keep it out of files and environment the agent can read, require approval for anything destructive, and prefer designs where the agent is not handed the key at all.

Why is this different from giving a person a key?

A person decides what to read. An agent reads whatever is in its context, including text written by strangers: a README, an issue, a web page, a dependency. If that text contains instructions, the agent may follow them. This is prompt injection, and the dataset records disclosed cases where it 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).

So design as if the agent could be talked into using any credential it can reach, in any way that credential allows. The question is then how much harm that allows.

How do you give an agent less than full power?

  1. Create a dedicated credential for the agent. Never reuse your own key. A separate key can be revoked without touching anything else.
  2. Scope it to the narrowest permissions. Stripe has restricted keys carrying only the permissions you grant; GitHub has fine-grained tokens limited to chosen repositories and per-permission levels, where a classic token reaches every repository you can access (/keys/stripe, /keys/github). See least privilege.
  3. Prefer read-only. If the task is to inspect, a read-only credential removes most of the risk.
  4. Point it at test or sandbox data. Use test-mode keys, a staging project or a throwaway account.
  5. Make it expire. Choose a lifetime at creation where the provider allows it, such as with Anthropic keys, and plan to delete it after the task (/keys/anthropic).
  6. Restrict where it works. Stripe lets you attach an access policy with IP ranges to a key. Network and resource limits turn a stolen key into a mostly useless one.
  7. Use temporary credentials where the provider offers them. AWS recommends IAM roles for temporary credentials over long-lived user keys (/keys/aws).

Providers are starting to treat agents as a distinct case. Stripe lets you tag a restricted key as an agent key, which subjects it to Stripe approval rules for sensitive actions. The Stripe row also notes that from November 30, 2026, Stripe’s MCP server stops accepting full-access secret keys and restricted keys without the Agent tag, in favor of OAuth or an agent API key. Check Stripe’s MCP docs for the current rules.

How do you hand the credential to the agent’s tools?

If the agent calls APIs through an MCP server, the server needs the key, but the agent usually does not.

  1. Use environment variable references, not literals, in the tool’s config. Claude Code expands ${VAR} in .mcp.json (see /agents/claude-code); Cursor resolves ${env:NAME} in mcp.json (/agents/cursor); VS Code can prompt for a secret with an inputs entry marked as a password (/agents/github-copilot).
  2. Know who else inherits the variable. Claude Code’s documentation says a stdio MCP server inherits your shell environment unless CLAUDE_CODE_MCP_ALLOWLIST_ENV is set. In Codex CLI, the documented default does not strip variables with names containing KEY, SECRET or TOKEN from commands it runs, so set inherit = "none" or add exclude filters (/agents/codex). Gemini CLI, by contrast, redacts such host variables before starting an MCP server by default.
  3. Check project-level MCP config before opening a repository. Interactive Claude Code sessions ask before using servers from a repository’s .mcp.json, but in claude -p and Agent SDK runs they load without a prompt unless blocked.
  4. Use the provider’s official MCP server or CLI where one exists, and prefer its OAuth flow to a pasted key. Each provider page under /keys lists the official MCP and CLI if there is one.

See the Model Context Protocol entry for background.

What controls belong around the agent?

  1. Keep secrets out of files it can read. Add deny or ignore rules and keep real keys out of the project folder: keeping env files away from AI agents. Remember vendors state these are partial controls.
  2. Run it in a sandbox or a container with no access to your home directory, SSH keys or cloud config.
  3. Require approval for risky actions. Deleting, spending money, sending messages, changing permissions and publishing all deserve a human click.
  4. Limit egress. An agent that cannot reach arbitrary hosts cannot easily send data to them.
  5. Log what it does, and watch the provider’s request logs for the agent’s credential.
  6. Review the agent’s instructions and config files like code. Disclosed issues involved files that came with a repository, such as the CVE-2025-61260 problem in older Codex CLI releases.

Can the agent use the key without holding it?

Yes, and it is the strongest design. Put something between the agent and the secret: a proxy, a command wrapper or a credential broker that attaches the key to approved requests, keeps it out of the agent’s context, and returns only the result. The agent can then be wrong, confused or compromised without handing over the key, because it never had it. The trade-off is that you must trust and secure the broker, and it should check where each credential is allowed to be sent. This is the approach described in why agents should never see your API keys and intents, not values. A credential broker is the general term.

What should you do after a task?

  1. Revoke or let the agent’s credential expire.
  2. Check request logs for anything unexpected.
  3. If the agent could have read a production key, rotate it: how to rotate API keys.
  4. If you suspect a key escaped, follow what to do when an API key leaks.

Where does BotPassword fit?

BotPassword is the broker described above: an agent asks for an outcome, such as running a command or making an authenticated request, BotPassword checks the grant you approved in its own interface, performs the secret-bearing step itself, and returns redacted output rather than the value. An agent can also request key rotation without seeing either key. Planned: bpw agent install will set up Claude Code, Codex, Cursor and others in one step, and each tool’s behavior will be tested per tool; today, add the bpw mcp entry by hand. The scoped-credential practices above apply with or without BotPassword.

Frequently asked questions

Should I give an AI coding agent my API key?

Avoid giving it a powerful key. If it needs access, use a narrowly scoped, short-lived, revocable credential for a test or development environment, and keep production keys out of its reach.

Is it safe to put an API key in an MCP server config?

Prefer referencing an environment variable over a literal value, and keep the config file out of version control if it holds anything sensitive. Claude Code, Cursor and others support variable references in MCP config.

What is the biggest risk when an agent has API access?

Prompt injection: text in a file, page or issue that the agent reads can steer it to misuse or leak the credentials it can reach. Limiting what the credential can do limits the damage.

Do agents need to see the key to use an API?

No. A tool or proxy outside the agent can attach the credential to requests, so the agent only sees the response.

More guides

Give agents access. Keep the keys.

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