I build aFile, the tool this post uses. The pattern works without it too.
If you work with Claude Code, Codex or Cursor, you've probably done this: the agent needs an OpenAI or Stripe key to run something, so you paste the key into the chat. Or you put it in .env and tell the agent where it is.
Both work. Both also leave the value somewhere it doesn't need to be:
- A pasted key is part of the conversation. It's in the transcript, in whatever your agent's host keeps, and in front of the model on every turn after.
-
A
.envfile is plain text that every tool the agent runs can read, and it's onegit add .away from a repository.
The thing to notice is that the agent rarely needs to see the key. A program needs to have it.
The pattern
- Store the key encrypted, outside the project.
- Let the value in only through standard input, so it's never on a command line or in shell history.
- When a program needs it, decrypt it into that one process's environment, and print nothing.
- Let the agent list secrets by name only.
Doing it with the aFile CLI
npm i -g @afile/cli # Node.js 24 or later
afile connect # registers this agent, no account; the key is made on this machine
Save the key. It comes only from stdin:
printf %s "$OPENAI_API_KEY" | afile secret OPENAI_API_KEY
Saved secret OPENAI_API_KEY.
Use it without showing it: afile run --secret OPENAI_API_KEY -- <command>
Run the program with the key in its environment:
afile run --secret OPENAI_API_KEY -- python agent.py
The CLI decrypts the secret locally, sets OPENAI_API_KEY for python agent.py and nothing else, prints nothing about it, and exits with the command's exit code. Pass --secret once per key.
The agent can see what's there without seeing values:
afile secrets
OPENAI_API_KEY version 1 · changed 29 Nov 19:19
Secrets print only with get. Prefer afile run so the value never enters the conversation.
Teaching the agent
afile add installs a skill file for the agents on your machine (afile add -a claude-code for one). It tells the agent to take secrets only from stdin, keep values out of chat and use afile run. Then you can say:
Save the OpenAI key in key.txt as a secret, then delete the file.
Over MCP it's stricter: the get_item tool refuses secrets outright and points to afile run.
What this doesn't protect
I'd rather be clear about this:
-
It isn't a sandbox. An agent that can run
afilecan also runafile get OPENAI_API_KEY, which writes the value to a pipe or a file, or prints it after you confirm in a terminal. The skill tells the agent not to unless you ask. What changes is the default. -
The program you run gets the key, as it would from
.env. -
Lose the agent's key and the secrets are gone. Export it once:
afile key --export --out afile-key.json.afile connect --restore afile-key.jsonsets up the same agent on another machine.
The value is encrypted on your machine before upload (AES-256-GCM, a key per item), so the server stores ciphertext and can't read the secret's name either. Saving the same name again keeps a new version, and the old one stays for 30 days.
The full post, with the same commands, is on the aFile blog: https://afile.sh/blog/api-keys-for-ai-agents
Top comments (0)