Claude Code stores authentication, settings, conversation history, and project metadata in ~/.claude. If you use two accounts — say, a personal Max subscription and a work team account — both compete for the same config directory. You need a way to keep them separate.
The solution has two parts:
-
CLAUDE_CONFIG_DIR— an environment variable that tells Claude Code where to read its config from. Point it at~/.claude-workand Claude Code uses your work account; point it at~/.claude-personaland it uses your personal account. -
direnv — a shell extension that automatically loads and unloads environment variables when you
cdinto a directory, so you never have to manually switch accounts.
Together, they give you automatic account switching based on which directory you are working in.
flowchart LR
A["cd ~/work/some-repo"] --> B["direnv detects .envrc"]
B --> C["sources account-map.sh"]
C --> D["matches ~/work/* \n sets ~/.claude-work"]
D --> E["CLAUDE_CONFIG_DIR \n = ~/.claude-work"]
E --> F["claude runs with \n work account"]
flowchart LR
A["run cc-work alias"] --> B["CLAUDE_CONFIG_DIR \n = ~/.claude-work"]
B --> C["claude runs with \n work account"]
Launch Claude Code once per account, pointing it at a dedicated config directory. Log in, then exit.
# Personal account
CLAUDE_CONFIG_DIR=~/.claude-personal claude
# Log in, then exit
# Work account
CLAUDE_CONFIG_DIR=~/.claude-work claude
# Log in, then exitAfter this step, you have two independent config directories, each authenticated to a different account.
Create a clear separation between personal and work projects:
~/personal/ # Personal repos and projects
~/work/ # Work repos and projects
If you already have projects scattered elsewhere, symlink or move them into these directories.
brew install direnvAdd the hook to your shell config (~/.zshrc):
eval "$(direnv hook zsh)"Reload your shell:
source ~/.zshrcCreate ~/.claude/account-map.sh:
#!/bin/bash
case "$PWD" in
"$HOME"/personal|"$HOME"/personal/*) echo "$HOME/.claude-personal" ;;
"$HOME"/work|"$HOME"/work/*) echo "$HOME/.claude-work" ;;
*) echo "$HOME/.claude" ;;
esacMake it executable:
chmod +x ~/.claude/account-map.sh# ~/personal/.envrc
export CLAUDE_CONFIG_DIR=$(source ~/.claude/account-map.sh)# ~/work/.envrc
export CLAUDE_CONFIG_DIR=$(source ~/.claude/account-map.sh)direnv requires explicit approval for each .envrc file:
direnv allow ~/personal
direnv allow ~/workcd ~/work/some-repo
echo $CLAUDE_CONFIG_DIR
# Expected: /Users/<you>/.claude-work
cd ~/personal/my-project
echo $CLAUDE_CONFIG_DIR
# Expected: /Users/<you>/.claude-personalThat is it -- you are set up. The sections below are optional extras for power users.
Add these to your ~/.zshrc for quick switching anywhere, regardless of direnv:
alias cc-who='echo "Claude config: ${CLAUDE_CONFIG_DIR:-~/.claude (default)}"'
alias cc-personal='export CLAUDE_CONFIG_DIR=~/.claude-personal && echo "Switched to: personal"'
alias cc-work='export CLAUDE_CONFIG_DIR=~/.claude-work && echo "Switched to: work"'
alias cc-default='export CLAUDE_CONFIG_DIR=~/.claude && echo "Switched to: default"'Usage:
cc-who # Check which account is active
cc-work # Force work account in the current shell
cc-personal # Force personal account in the current shell
cc-default # Reset to default configNote: manual overrides apply to the current shell session only. Opening a new terminal or cd-ing into a direnv-managed directory will revert to automatic switching.
Each config directory maintains its own:
- Authentication — separate accounts and subscriptions
- Conversation history — work and personal histories stay separate
- Settings and plugins — independent
settings.jsonper account - Project metadata — per-account project tracking
- CLAUDE.md rules — per-account global instructions (e.g.,
~/.claude-work/CLAUDE.mdvs~/.claude-personal/CLAUDE.md)
-
Different CLAUDE.md per account. Your work account might enforce strict linting and commit conventions, while your personal account might be more relaxed. Place different
CLAUDE.mdfiles in each config directory. -
Different plugins and MCP servers per account. Each config directory has its own
settings.json, so you can configure separate MCP servers, API keys, and plugin settings for work vs personal. -
Easy to extend. Adding a third account (e.g., freelance) is just another case in
account-map.sh, another config directory, and another.envrc. -
Pair with SSH host aliases. If you use separate GitHub accounts per identity, configure SSH host aliases (
github.com-work,github.com-personal) so thatgitandclaudeboth route through the correct credentials automatically.