Per-Dir API keys
How to easily setup per directory API keys to, for example, scope different AI subscriptions in different folders. This will use the CLI direnv to accomplish that.
direnv Setup
- Install direnv: https://direnv.net/docs/installation.html
- Use
brew install direnvfor macOS.
- Use
- Hook direnv into your shell: https://direnv.net/docs/hook.html
- Add
eval "$(direnv hook zsh)"in your~/.zshrcfile on macOS.
- Add
- For shells that have launched without using
cd(like in agent-storm), you'll need to immediately triggerdirenv.- On macOS, add
_direnv_hookaftereval "$(direnv hook zsh)"in your~/.zshrcfile.
- On macOS, add
- Open a new terminal tab or source your shell profile to load the change.
- Run
source ~/.zshrcon macOS.
- Run
Per-Dir Setup
- Create a
.envrcfile in the dir. - Export a variable in that file:
export ANTHROPIC_API_KEY="<something>";. - Navigate to the folder that contains that
.envrc.
Now when you cd into a dir that is an ancestor of folder containing the .envrc file (or multiple nested .envrc files), your env will be populated with those variables.
If you get a Run direnv allow to approve its content error, just run direnv allow (only needed once per folder).
Setup worktree root
If you use git worktrees frequently, you'll need to allow direnv on every new worktree. This fixes that.
- Create a
direnvconfig file:mkdir -p ~/.config/direnvtouch ~/.config/direnv/direnv.toml
- Trust the root of your worktrees but adding the following to that
direnv.tomlfile:[whitelist]
prefix = ["<replace-with-absolute-path-to-worktree-root>"] - Make sure to replace
replace-with-absolute-path-to-worktree-rootabove with the correct path.