How to Auto-Switch Claude Code Accounts by Directory

Introduction

Developers who use Claude Code for both professional and personal projects often face a practical problem: keeping work and personal contexts separate.

The standard approach involves manually specifying profile flags or maintaining separate configuration directories, which becomes tedious when switching between projects throughout the day.

A straightforward solution is a shell function that automatically selects the appropriate Claude Code profile based on your current directory. This approach requires minimal setup and operates transparently once configured.

Show me the code

The function works by examining the current working directory and setting the CLAUDE_CONFIG_DIR environment variable accordingly.

When you type claude in your terminal, the function checks whether your path contains specific directory names and launches Claude Code with the corresponding configuration.

# Smart Claude Code launcher - switches claude account based on directory
  claude() {
      local current_dir=$(pwd)

      # Check if we're in a work directory
      if [[ "$current_dir" == *"/work/"* ]] || [[ "$current_dir" == *"/projects/work"* ]]; then
          echo "Starting Claude Code (work profile)..."
          CLAUDE_CONFIG_DIR=~/.claude-work command claude "$@"
      elif [[ "$current_dir" == *"/personal/"* ]] || [[ "$current_dir" == *"/projects/personal"* ]]; then
          echo "Starting Claude Code (personal profile)..."
          CLAUDE_CONFIG_DIR=~/.claude-personal command claude "$@"
      else
          # Default behavior - no profile
          echo "Starting Claude Code (default profile)..."
          CLAUDE_CONFIG_DIR=~/.claude command claude "$@"
      fi
  }

How It Works

The function captures your present working directory and tests it against predefined patterns. If the path contains /work/ or /projects/work, Claude Code launches with the work profile.

Paths containing /personal/ or /projects/personal trigger the personal profile. Any other location defaults to the standard configuration.

The CLAUDE_CONFIG_DIR variable tells Claude Code where to find its configuration files, conversation history, and authentication credentials. By pointing this variable to different directories, you maintain completely isolated environments for each context.

And now when you launch claude in a terminal you should see one of these lines at the top:

Starting Claude Code (work profile)...
OR
Starting Claude Code (personal profile)...
OR
Starting Claude Code (default profile)...

Setup and Customization

Add the function to your ~/.zshrc file (or ~/.bashrc if you use Bash). After saving, run source ~/.zshrc to make the function available in your current session.

The directory patterns can be adjusted to match your actual folder structure. If your work projects live under /Users/username/company, modify the condition to check for that path instead. You can add as many patterns as needed by extending the conditional statements.

Each profile maintains separate authentication tokens, which means you can use different Claude accounts for work and personal projects. Sign in to your work account when first launching from a work directory, and your personal account from a personal directory. The credentials remain separate and do not interfere with each other.

Practical Benefits

This approach eliminates the mental overhead of remembering which profile to use. You navigate to a project and type claude, and the correct environment loads automatically.

Conversation history remains organized by context, making it easier to return to previous work without sifting through unrelated sessions.

The function passes all arguments through to the underlying Claude Code command, so existing workflows remain unchanged. Commands like claude --help or claude --version work exactly as expected.

For teams that enforce separation between corporate and personal tools, this method provides a technical control that reduces accidental mixing of contexts. Work conversations stay in the work profile, personal projects in the personal profile, with no additional steps required from the developer.

Alternative Approaches

Some developers prefer using Claude Code's built-in profile system with the --profile flag. That method works well but requires typing the flag every time you launch the tool.

Others use shell aliases for each profile, which means remembering different commands for different contexts.

The directory-based approach described here removes those extra steps while maintaining clear separation.

It assumes that you organize projects into distinct directory trees, which most developers already do for other reasons.

Conclusion

Separating work and personal development environments does not require complex tooling or extensive configuration.

A small shell function that reads your current directory and sets an environment variable accomplishes the task reliably.

The result is a system that adapts to your location without requiring conscious intervention, keeping contexts separate and reducing friction in your daily workflow.

If you've found a different approach to managing Claude Code Accounts, drop a comment below and share what worked for you.