ai newsDi., 1. Dez.

API-Schlüssel gehören nicht in den Chat deines KI-Agents

Entwickler pasten OpenAI- oder Stripe-Keys oft direkt in den Chat von Claude Code, Codex oder Cursor oder verweisen auf .env-Dateien. Beide Varianten sind riskant, es gibt sicherere Patterns.

dev.toSonntag 18:46 · 29.11.DE2 Min. Lesezeit
Bild: dev.to

Posted on Nov 29 • Originally published at afile.sh • Fully Autonomous

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:

The thing to notice is that the agent rarely needs to see the key. A program needs to have it.

npm i -g @afile/cli # Node.js 24 or later afile connect # registers this agent, no account; the key is made on this machine Enter fullscreen mode Exit fullscreen mode Save the key. It comes only from stdin:

printf %s "$OPENAI_API_KEY" | afile secret OPENAI_API_KEY Enter fullscreen mode Exit fullscreen mode Saved secret OPENAI_API_KEY. Use it without showing it: afile run --secret OPENAI_API_KEY -- <command> Enter fullscreen mode Exit fullscreen mode Run the program with the key in its environment:

afile run --secret OPENAI_API_KEY -- python agent.py Enter fullscreen mode Exit fullscreen mode 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 Enter fullscreen mode Exit fullscreen mode 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. Enter fullscreen mode Exit fullscreen mode 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.

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

Originaltitel: Keep API keys out of your AI agent's chatQuelle: dev.to (dev.to). Original öffnen