An agent that runs commands runs them as you. It inherits your environment variables, can read the files your user account can read, and can reach the network. That's ambient authority: nobody granted the agent your AWS keys, but nothing stopped it either. Most advice here stops at "don't put secrets in prompts." The real exposure is wider, and the fixes are more specific.
Treat context as disclosure
The working rule: once a secret appears in the agent's context, treat it as disclosed. It has been sent to the model provider, and it's now in a transcript on disk. Claude Code, for example, keeps session transcripts in plaintext under ~/.claude/projects/ for 30 days by default. OpenClaw keeps them in a per-agent database. From there, any tool the agent can use (a web request, a git push, an MCP call) is a way out, and prompt injection is how someone else steers it there.
So the goal isn't "the model will be careful." It's keeping the value out of reach, or making it worthless if it gets out.
How keys reach the agent
| Path | Example |
|---|---|
| Files it can read | .env, ~/.aws/credentials, ~/.ssh/, a tool's config file with a token in it |
| Inherited environment | GITHUB_TOKEN exported in your shell profile reaches every command the agent spawns |
| Tool output | env, a verbose curl echoing an Authorization header, a stack trace that prints a connection string |
| You | Pasting a key into chat "just this once" to debug |
The third path catches people. The agent never asked for the key; a command printed it, and the output went straight into context.
Deny rules are not a boundary
Tool-level rules stop accidents, not a determined path. In Claude Code, a Read(./.env) deny rule blocks the built-in file tools and commands it recognizes, like cat and head. Its docs are explicit about the gap: the rule doesn't apply to grep -r pattern . run from that folder, or to a Python or Node script that opens the file itself. Hermes works the same way: its file tools refuse to read project .env files, but that's the file tool, not the operating system.
Real enforcement happens at the OS layer: a sandbox that the process can't see around. Claude Code's sandbox is off by default, and even with it on, reads cover most of the machine, including ~/.ssh and ~/.aws/credentials, until you deny them. As its docs put it, there's no built-in credential deny list. Only what you list is protected.
Best: the agent never holds the real key
The strongest pattern is brokered credentials: the agent works with a placeholder, and something outside it swaps in the real value only on the way to an approved destination.
- Claude Code sandbox masking gives commands a per-session placeholder and has its proxy substitute the real token only on requests to hosts you allow. It needs TLS termination and an allowlisted destination, and it's honored only from trusted settings:
{
"sandbox": {
"enabled": true,
"network": {
"tlsTerminate": {},
"allowedDomains": ["*.github.com"]
},
"credentials": {
"envVars": [
{ "name": "GH_TOKEN", "mode": "mask", "injectHosts": ["api.github.com"] }
]
}
}
}
- OpenClaw protected secrets are write-only once saved, with no way to reveal them, and reach commands only as placeholders through its egress proxy, which is off by default. Its "agent-readable" secrets are the opposite: plaintext the agent can print or send.
- Managed connections work the same way at a higher level. Zapier MCP acts through accounts you connected in Zapier, and n8n workflows reference its credential store instead of holding keys in each step. The agent asks for an action, not a key.
Why this matters: a placeholder that leaks into a transcript, a log or an attacker's server is worthless. The real value only ever travels to the host it's meant for.
Strip what subprocesses inherit
If you can't broker a key, at least keep it out of the environment:
- Claude Code:
CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1strips credentials from Bash commands, hooks and local MCP servers. It deliberately leaves GitHub tokens in place, so scope those separately. Inside the sandbox,credentials.envVarswith"mode": "deny"unsets specific variables. - Hermes: code it writes and runs has variables with names like
KEY,TOKENorSECRETblocked by default. The Docker and SSH backends pass no host environment unless you forward it, and MCP servers only get the variables you configure for them. - Everywhere: don't export long-lived tokens in your shell profile. Load them per task, for the process that needs them.
Shrink the blast radius
Assume a key will leak eventually and limit what it can do:
- One key per agent or workflow, so you can revoke one without breaking everything else, and logs show which one was used.
- Narrowest scope available: read-only where possible, limited to the specific repo, folder or project.
- Short-lived over long-lived: an hour-long token that leaks is a much smaller problem than one valid until revoked.
- Spending limits on model and cloud API keys, set in the provider's dashboard.
When a key leaks
Rotate first, investigate second. Deleting a transcript doesn't un-send what went to the provider or out over the network. Then:
- Revoke the key and issue a new, narrower one.
- Check the provider's access logs for use you don't recognize.
- Find the copies: local transcripts, git history, CI logs, any file the agent wrote.
- Fix the path that leaked it, whether that's a file, an inherited variable or a command's output, so the new key doesn't follow.
Before your next agent session:
- No long-lived tokens exported in your shell profile
- Sandbox on, with
~/.ssh,~/.awsand project.envfiles denied - Network allowlist set, so a key can only reach the hosts that need it
- Brokered or managed connections for anything that supports them
- Each agent's keys scoped, separate and easy to revoke
Go deeper: Claude Code sandboxing · Claude Code permission rules · Prompt injection · Claude Code permission modes