Why agents leak keys

The usual way to hand a key to a program is an environment variable. OWASP notes these "are generally accessible to all processes and may be included in logs or system dumps" (1), and an agent runs a lot of processes. Claude Code's shell sandbox, for example, passes on its environment "including any secrets in its environment" (5).

Telling the model not to reveal a key is not a control. OWASP says such restrictions "may not always be honored and could be bypassed via prompt injection or other methods" (2). If the agent also reads untrusted content and can send data out, an attacker "can easily trick it into accessing your private data and sending it to that attacker" (6); see prompt injection.

One key per agent, with only the access it needs

Do not reuse your personal key. OpenAI states "The sharing of API keys is against the Terms of Use" (7), and a separate key per agent means you can revoke one without breaking everything else. OpenAI keys can be "Restricted: Choose scopes for specific resources or endpoints" (8); an Anthropic key covers "Either a single workspace or the workspaces where your role allows API use" (9), so a workspace per agent is the way to narrow it. Google Cloud's advice is the same: restrictions limit "the impact of a compromised API key" (10).

Give keys an end date. OpenAI "strongly recommend[s] creating keys with an expiration date and establishing a regular key rotation process" (11). Where the agent runs on a cloud platform that supports it, workload identity federation replaces stored keys with short-lived tokens, so "There are no static secrets to mint, store in CI, rotate, or leak" (12)(13).

Cap the bill, not just the alerts

A leaked model key is mostly a money problem. OpenAI recommends you "Set monthly spend limits for your organization or projects, and enable hard enforcement" (14), and adds that "Spend alerts alone don't stop API traffic" (3). Anthropic lets you "Cap monthly spending for a workspace" (15), with one catch: "You cannot set spend limits on the Default Workspace" (16), so put the agent's key in a workspace of its own.

The same applies to the machine. Some sandbox providers can stop a runaway agent at a fixed amount; the Hard spending cap page lists which.

Keep the real key out of the agent's reach

The strongest pattern is a proxy that holds the key: "Programs receive a placeholder; the proxy substitutes the real value for allowed destinations" (4), which is how Codex cloud environments handle network secrets. Claude Code can do something similar for credentials it masks: "The command and anything it logs never hold the real credential, but its requests still authenticate" (17). It can also "strip credentials from the environments of the subprocesses Claude Code starts" (18), which "reduces what a prompt injection attack can read" (19), though "The Claude Code process keeps the credentials for its own API calls" (20).

Store the real keys properly: Anthropic says to "Store API keys in a secrets manager, rotate them periodically, and disable or delete any key you suspect has leaked" (21), and OpenAI says "Never commit your key to your repository" (22). If you build MCP servers, the MCP spec calls forwarding a client's token to another API "an anti-pattern" (23).

On a hosted machine, the provider decides how much of this you get. The table below shows which providers can store secrets, hide them from the agent, and issue limited keys.

Which providers help

From the capability checks; each links to every provider's verdict and its catch

CapabilityYesWith a catch
Secrets the agent cannot read
Keys are added to outgoing requests by the provider, so the agent never sees them.
Yes Cloudflare Sandbox, Daytona, E2B, InstaBox, Runtime, Vercel Sandbox, boxdWith a catch Blaxel, Boat, DigitalOcean, Modal, Sprites
Stored secrets
Save API keys with the provider and have them appear in the machine as environment variables.
Yes Agent 37, Blaxel, Boat, Daytona, E2B, InstaBox, Lizard, Modal, Morph, Runtime, Sandboxer, boxdWith a catch Cloudflare Sandbox, DigitalOcean, Vercel Sandbox
Limited API keys
Create API keys that can do only some things, or only in one project.
Yes Boat, Cloudflare Sandbox, Daytona, DigitalOcean, InstaBox, Lizard, Morph, Runtime, Sandboxer, TensorlakeWith a catch Agent 37, Blaxel, E2B, Modal, Vercel Sandbox, boxd
Hard spending cap
Set a limit the bill cannot go over; machines stop instead.
Yes E2B, InstaBox, Modal, RuntimeWith a catch Boat, Daytona, Sandboxer, Tensorlake, Vercel Sandbox
Allow only some sites
Limit which internet addresses the machine can reach.
Yes Agent 37, Cloudflare Sandbox, Daytona, DigitalOcean, E2B, Modal, Runtime, Sprites, Tensorlake, Vercel Sandbox, boxdWith a catch Blaxel, Boat

Sources

Each quote was read on the page linked, on the date shown

  1. OWASP Secrets Management Cheat Sheet (cheatsheetseries.owasp.org, 29 October 2026) third-party
    “environment variables are generally accessible to all processes and may be included in logs or system dumps.”
  2. OWASP Top 10 for LLM Applications: LLM02 Sensitive Information Disclosure (genai.owasp.org, 29 October 2026) third-party
    “such restrictions may not always be honored and could be bypassed via prompt injection or other methods”
  3. OpenAI Help: best practices for API key safety (help.openai.com, 29 October 2026) docs-confirmed
    “Spend alerts alone don't stop API traffic.”
  4. OpenAI Codex docs: cloud environments (learn.chatgpt.com, 29 October 2026) docs-confirmed
    “Programs receive a placeholder; the proxy substitutes the real value for allowed destinations.”
  5. Claude Code docs: sandboxing (code.claude.com, 29 October 2026) docs-confirmed
    “Inherited from Claude Code, including any secrets in its environment”
  6. Simon Willison: The lethal trifecta for AI agents (simonwillison.net, 29 October 2026) third-party
    “If your agent combines these three features, an attacker can easily trick it into accessing your private data and sending it to that attacker.”
  7. OpenAI Help: best practices for API key safety (help.openai.com, 29 October 2026) docs-confirmed
    “The sharing of API keys is against the Terms of Use.”
  8. OpenAI Help: managing projects in the API platform (help.openai.com, 29 October 2026) docs-confirmed
    “Restricted: Choose scopes for specific resources or endpoints.”
  9. Claude API docs: authentication (platform.claude.com, 29 October 2026) docs-confirmed
    “Either a single workspace or the workspaces where your role allows API use, chosen when the key is created”
  10. Google Cloud: best practices for API keys (cloud.google.com, 29 October 2026) docs-confirmed
    “By adding restrictions, you can limit the ways an API key can be used, reducing the impact of a compromised API key.”
  11. OpenAI Help: best practices for API key safety (help.openai.com, 29 October 2026) docs-confirmed
    “We strongly recommend creating keys with an expiration date and establishing a regular key rotation process.”
  12. Claude API docs: workload identity federation (platform.claude.com, 29 October 2026) docs-confirmed
    “There are no static secrets to mint, store in CI, rotate, or leak.”
  13. OpenAI Help: best practices for API key safety (help.openai.com, 29 October 2026) docs-confirmed
    “use workload identity federation instead of storing a long-lived OpenAI API key”
  14. OpenAI Help: best practices for API key safety (help.openai.com, 29 October 2026) docs-confirmed
    “Set monthly spend limits for your organization or projects, and enable hard enforcement to help limit unexpected costs from a leaked key.”
  15. Claude API docs: workspaces (platform.claude.com, 29 October 2026) docs-confirmed
    “Cap monthly spending for a workspace.”
  16. Claude API docs: workspaces (platform.claude.com, 29 October 2026) docs-confirmed
    “You cannot set spend limits on the Default Workspace.”
  17. Claude Code docs: sandboxing (code.claude.com, 29 October 2026) docs-confirmed
    “The command and anything it logs never hold the real credential, but its requests still authenticate.”
  18. Claude Code docs: environment variables (code.claude.com, 29 October 2026) docs-confirmed
    “strip credentials from the environments of the subprocesses Claude Code starts, such as Bash commands, hooks, and stdio MCP servers”
  19. Claude Code docs: environment variables (code.claude.com, 29 October 2026) docs-confirmed
    “This reduces what a prompt injection attack can read.”
  20. Claude Code docs: environment variables (code.claude.com, 29 October 2026) docs-confirmed
    “The Claude Code process keeps the credentials for its own API calls.”
  21. Claude API docs: authentication (platform.claude.com, 29 October 2026) docs-confirmed
    “Store API keys in a secrets manager, rotate them periodically, and disable or delete any key you suspect has leaked.”
  22. OpenAI Help: best practices for API key safety (help.openai.com, 29 October 2026) docs-confirmed
    “Never commit your key to your repository”
  23. Model Context Protocol: security best practices (modelcontextprotocol.io, 29 October 2026) docs-confirmed
    “"Token passthrough" is an anti-pattern where an MCP server accepts tokens from an MCP client without validating that the tokens were properly issued to the MCP server and passes them through to the downstream API.”