How to hide API keys from AI agents in terminal output

Run commands through a tool that knows which secrets it injected and replaces those exact values in the output before the agent reads it. 1Password's op run, psst, dotenvx with --redact and fidelius's accio all do this, and they catch accidental prints but can't stop an agent that sets out to get a value.

Why output is the leak nobody plans for

You can keep keys out of files and out of your shell profile and still leak them through output. An agent runs your tests, a library logs its full config at debug level, an HTTP client prints request headers, or the agent runs printenv to debug a missing variable. Whatever the command prints lands in the agent's context, its transcript, and maybe a script it writes next. When we had Claude Code and Codex debug programs that print their keys, a key reached the agent in 41 of 42 runs, and in none of 54 through accio, our masking runner (the full test).

How masking works

A runner that injects secrets knows their exact values. It sits between the command and whoever reads the output, and replaces each value with a placeholder as the text streams past:

# masking demo, with a test key$ FIDELIUS_MASK=1 accio myapp printenv STRIPE_KEY
<concealed by fidelius: STRIPE_KEY>

The command itself still gets the real value. Only what comes back out is changed.

Tools that do it

ToolWhen it masksTurn it off
1Password op runBy default--no-masking
psstBy default, prints [REDACTED]--no-mask
dotenvxWith --redact, or values marked in an EnvfileLeave them off
fidelius accioWhen Claude Code or Codex runs it, or with FIDELIUS_MASK=1--no-mask

What masking can't catch

Masking compares output with the known value and, depending on the tool, some encoded forms of it. accio also matches the value in base64, hex, percent-encoding and JSON escaping. Other changes get past it:

  • a hash of the value, or the value encoded inside a larger blob or compressed
  • a value printed in pieces, reversed or with characters in between
  • a value sent somewhere instead of printed, such as a curl to another server
  • a value written to a file and read back later by a tool that doesn't mask

So treat masking as protection against accidental exposure: a process that holds a key can still transform it or send it somewhere. Against an agent that has been told, by you or by a prompt injection, to get a key, the defenses are elsewhere: scoped keys, network limits, sandboxing and approval prompts.

How accio decides

fidelius's accio masks when it recognizes an agent from its environment (Claude Code or Codex today) or when FIDELIUS_MASK=1 is set, Automatic masking applies when output goes to a pipe, a socket or a file, which is how agents capture it. FIDELIUS_MASK=1 masks terminal output too. A normal run in your own terminal shows real values, because you're the one reading them.

Some details worth knowing:

  • Values under 6 bytes aren't masked, because short strings match ordinary output too often. Setting "Hide in agent output" to Always for a key masks it from 3 bytes.
  • Plain settings, like a port or a region, can have "Hide in agent output" set to Never so agents can read them. accio list myapp shows which variables aren't masked and why.
  • Binary output and MCP servers' JSON on stdout pass through untouched. Put --no-mask in MCP server configs to make that explicit.
  • When a run replaced at least one value, accio says so on stderr at the end, so masking never changes output silently. To write a real key into a file, add --no-mask.

Masking is the second line

The first line is not having keys where agents look: no .env in the project, no exports in your shell profile, keys handed to one command at a time. Masking then catches what slips through. For the first line, see how to keep secrets out of Claude Code, Codex and Gemini CLI and can Claude Code read your .env file?