Using macOS Keychain for AI Agent Credential Access
Using macOS Keychain for AI Agent Credential Access
I put my passwords in macOS Keychain specifically for Claude to use. It beats having .env files scattered across every repo or pasting tokens into every new session.
The .env File Problem
Most developers manage credentials through .env files. For AI agent workflows, this creates several issues:
- Duplication - the same API key lives in 10 different
.envfiles across 10 repos - Rotation pain - when you rotate a key, you have to update every copy
- Accidental commits - despite
.gitignore,.envfiles get committed to repos regularly - Session friction - every new agent session needs tokens pasted in or environment configured
Why Keychain Is Better
macOS Keychain is an encrypted credential store built into the OS. Using it for AI agent credentials means:
- Single source of truth - one entry per credential, accessible from any project
- OS-level encryption - credentials are encrypted at rest, protected by your login password
- No file exposure - nothing to accidentally commit, no plaintext files on disk
- Programmatic access -
security find-generic-passwordretrieves credentials from the command line
Setting It Up
Store a credential:
security add-generic-password -a "your-account" -s "openai-api-key" -w "sk-..." -T ""
Retrieve it in your agent's context:
security find-generic-password -s "openai-api-key" -w
Your AI agent can call this directly when it needs a token, without you pasting anything.
Security Considerations
Keychain access still requires thought:
- Scope credentials narrowly - give the agent access only to the keys it needs
- Use app-specific passwords - do not store your primary email password for agent use
- Monitor access - Keychain Access app shows which processes have read your items
- TCC protection - macOS prompts before granting Keychain access to new applications
The Workflow Improvement
Before Keychain: start a session, dig through notes for the right API key, paste it in, hope you grabbed the right one.
After Keychain: the agent retrieves what it needs automatically. Zero friction, better security.
Fazm is an open source macOS AI agent. Open source on GitHub.