Claude Code uses ~/.claude/ as its config directory by default, which includes your login credentials in .claude.json. You can override this with the CLAUDE_CONFIG_DIR environment variable to point at a different config directory, enabling automatic account switching based on where you run claude.
1. Create a separate config directory for your second account:
mkdir ~/.claude-personal2. Log in to the second account in that config directory:
CLAUDE_CONFIG_DIR="$HOME/.claude-personal" claude login3. Add a wrapper function to your ~/.zshrc (or ~/.bashrc):
claude() {
if [[ "$PWD" == "$HOME/code/personal"* ]]; then
CLAUDE_CONFIG_DIR="$HOME/.claude-personal" command claude "$@"
else
command claude "$@"
fi
}Adjust the path pattern to match whichever directories should use the alternate account. You can add multiple conditions:
claude() {
if [[ "$PWD" == "$HOME/code/personal"* || "$PWD" == "$HOME/side-projects"* ]]; then
CLAUDE_CONFIG_DIR="$HOME/.claude-personal" command claude "$@"
else
command claude "$@"
fi
}4. Reload your shell:
source ~/.zshrcCLAUDE_CONFIG_DIRtells Claude Code where to find its config, credentials, session data, and settings- The wrapper function checks your current directory and sets the env var inline before invoking
claude command claudebypasses the function itself to call the realclaudebinary, avoiding infinite recursion- Each config directory is fully independent — separate login, settings, history, and MCP config
Run claude from each directory type and check which account is active. You can also verify by checking which .claude.json is being read:
# Should use team account
cd ~/code/work && claude --version
# Should use personal account
cd ~/code/personal && claude --version