[Claude Code] Using Multiple Accounts on One PC (Auto-Switching by Directory)

2026-06-18 hit count image

Learn how to separate work and personal Claude Code accounts on a single PC. Split your settings with CLAUDE_CONFIG_DIR, solve the macOS Keychain sharing issue with CLAUDE_CODE_OAUTH_TOKEN, and configure .zshrc so the account switches automatically based on your working directory.

generative_ai

Overview

Sometimes you need to use both a work account and a personal account on the same PC. For example, you might want to use your company subscription in work directories, and your personal subscription in directories for your blog or side projects.

The biggest concern here is accidentally working under the wrong account — doing personal work under your company account, or vice versa. In this post, we’ll separate the two accounts safely, in the following order:

  1. Separate config directories per account with CLAUDE_CONFIG_DIR
  2. Solve the macOS credential-sharing issue with CLAUDE_CODE_OAUTH_TOKEN
  3. Configure .zshrc so the account switches automatically based on the working directory

In the examples, the work account stays as the default (~/.claude), and we switch to the personal account only under ~/personal. Adjust the paths to fit your own environment.

Can You Use Multiple Accounts on One PC?

The short answer is yes. Claude Code doesn’t have a dedicated “account switch” feature, but you can use one of the following two approaches.

  • Sharing the same config directory: Run /logout and then log in again with a different account. Only one account is active at a time.
  • Separating config directories: Use the CLAUDE_CONFIG_DIR environment variable to point each account at a different directory, fully isolating credentials and settings.

Repeatedly logging out and back in is tedious and error-prone, so this article uses the latter approach — separate directories.

Is settings.json Shared Across Accounts?

settings.json is tied to a directory, not an account. Regardless of which account you’re logged in as, the following locations are used as-is.

FileScopeLocation
User-global settingsAll projects~/.claude/settings.json
Project settingsRepository (team-shared).claude/settings.json
Local settingsPersonal (gitignored).claude/settings.local.json

So by default, multiple accounts share the same ~/.claude/settings.json. If you want different global settings per account, you need to separate the config directory itself.

Separating Config Directories with CLAUDE_CONFIG_DIR

When you set the CLAUDE_CONFIG_DIR environment variable, Claude Code uses that directory as its config root. settings.json, account metadata (.claude.json), MCP settings, caches, and history are all stored under it.

# Run as the personal account
export CLAUDE_CONFIG_DIR="$HOME/.claude-personal"
claude

With this, the personal account uses ~/.claude-personal/settings.json while the work account uses the default ~/.claude/settings.json, keeping the settings separate.

The directory is created automatically on first run/login, so there’s no need to create it in advance.

The macOS Pitfall — Credentials Share the Keychain

Here, macOS users hit a pitfall. According to the official docs, separating .credentials.json via CLAUDE_CONFIG_DIR only works on Linux and Windows.

On macOS, credentials are always stored in a single Keychain entry (Claude Code-credentials), unaffected by CLAUDE_CONFIG_DIR. If you inspect the Keychain, you’ll find only one entry.

security find-generic-password -s "Claude Code-credentials" | grep -E "svce|acct"
#     "acct"<blob>="your-username"
#     "svce"<blob>="Claude Code-credentials"

In other words, separating only the config directory splits settings.json and account metadata, but the actual login token is still shared. The moment you log in with a second account, it overwrites the first account’s token, leaving you needing to re-login every time you change directories.

Separating Accounts with CLAUDE_CODE_OAUTH_TOKEN

The official way to solve this on macOS is the CLAUDE_CODE_OAUTH_TOKEN environment variable. When this variable is set, it takes precedence over the Keychain, so you can separate accounts without token conflicts or re-logins.

The strategy is as follows.

  • Work account: Keep using the default ~/.claude and the existing Keychain login (no separate token needed)
  • Personal account: Authenticate with ~/.claude-personal plus CLAUDE_CODE_OAUTH_TOKEN

Because CLAUDE_CODE_OAUTH_TOKEN takes precedence over the Keychain, it does not conflict with the work account’s Keychain login.

First, generate a long-lived token for the personal account. The command below walks you through a browser login and prints a one-year OAuth token.

CLAUDE_CONFIG_DIR="$HOME/.claude-personal" claude setup-token

Writing the token into .zshrc in plain text is a security risk, so we store it as a separate entry in the macOS Keychain and read it when needed.

security add-generic-password -U -a "$USER" -s claude-token-personal -w '<the generated token>'

Does Using the Token Incur API Charges?

The token created by claude setup-token is an OAuth token tied to your subscription plan (Pro/Max/Team/Enterprise). It works the same way as logging in via /login, and is not billed per token like the API.

Auth methodBillingSubscription required
CLAUDE_CODE_OAUTH_TOKEN (setup-token)Subscription usage limits (no extra charge)Pro/Max or higher
/login (interactive)Subscription usage limitsPro/Max or higher
ANTHROPIC_API_KEYMetered (per-token)Not required (Console credits)

Note that setup-token requires an active paid subscription of Pro/Max or higher. It does not work with a free account.

Switching Accounts Automatically by Working Directory

Now for the key part. Instead of setting environment variables manually every time, you can make the account get selected automatically based on the current directory, which prevents working under the wrong account.

zsh has a chpwd hook that runs every time you change directories. Using it, we automatically swap the environment variables so that the personal account is used under ~/personal and the work account everywhere else.

.zshrc Configuration

Add the following to your ~/.zshrc.

# Claude Code - separate accounts automatically by working directory
#   personal/* -> personal account (~/.claude-personal, Keychain: claude-token-personal)
#   otherwise  -> work/default account (~/.claude, existing Keychain login)
_claude_pick_account() {
  case "$PWD/" in
    "$HOME/personal/"*)
      export CLAUDE_CONFIG_DIR="$HOME/.claude-personal"
      local tok; tok=$(security find-generic-password -s claude-token-personal -w 2>/dev/null)
      if [ -n "$tok" ]; then
        export CLAUDE_CODE_OAUTH_TOKEN="$tok"
      else
        unset CLAUDE_CODE_OAUTH_TOKEN   # fall back to shared Keychain login if no token
      fi
      ;;
    *)
      # otherwise -> work/default account: default ~/.claude + existing Keychain login
      export CLAUDE_CONFIG_DIR="$HOME/.claude"
      unset CLAUDE_CODE_OAUTH_TOKEN
      ;;
  esac
}
autoload -U add-zsh-hook
add-zsh-hook chpwd _claude_pick_account
_claude_pick_account

The reason we call _claude_pick_account directly on the last line is that the chpwd hook only fires when you change directories. Running it once at shell startup ensures the correct account is selected even when a new terminal starts inside a specific directory.

If you haven’t registered the personal account token in the Keychain yet, it safely falls back to the work account (Keychain login) even under ~/personal.

Showing the Active Account on Launch

Auto-switching alone is enough, but it’s even more reassuring if you can see which account is active at a glance. Add a wrapper function around the claude command that prints the active account right before launch.

# Show the active account + token status when launching claude
claude() {
  local label dir="${CLAUDE_CONFIG_DIR:-$HOME/.claude}"
  case "$PWD/" in
    "$HOME/personal/"*) label="personal" ;;
    *)                  label="work (default)" ;;
  esac
  local auth
  if [ -n "$CLAUDE_CODE_OAUTH_TOKEN" ]; then
    auth="%F{green}personal token applied%f"
  else
    auth="%F{green}Keychain login (work)%f"
  fi
  print -P "%F{cyan}▶ Claude account: %B${label}%b%f  [${auth}%F{cyan}]  (CLAUDE_CONFIG_DIR=${dir})%f"
  command claude "$@"
}

Now when you run claude, the active account is displayed like this.

▶ Claude account: work (default)  [Keychain login (work)]  (CLAUDE_CONFIG_DIR=/Users/username/.claude)
▶ Claude account: personal  [personal token applied]  (CLAUDE_CONFIG_DIR=/Users/username/.claude-personal)

Caution — the Account Is Fixed When claude Launches

There’s one important point. CLAUDE_CODE_OAUTH_TOKEN and CLAUDE_CONFIG_DIR are read only once, when the claude process starts. So the account is determined by the directory at the moment you launch claude, and stays fixed until the session ends.

For example, suppose you launch Claude Code in a work directory (work account), and mid-session you ask it to “write a personal blog post.” What happens?

The personal blog work is performed under the work account. Even if Claude edits files in a different directory during the session, the account of the already-running process does not change. The chpwd hook only affects the interactive shell — it has no bearing on the running claude process.

Therefore, work and personal tasks must be run as separate sessions (separate terminals), each from its own directory.

What you want to doWhat you should do
Work tasksLaunch claude from a work directory
Personal blog/side projectsMove to ~/personal in a new terminal, then launch claude

The launch-time notice we added earlier serves as the safety net that catches this very mistake.

Applying the Configuration

Once you’ve finished the setup, apply and verify it in the following order.

# 1) (Personal account) generate a token and store it in the Keychain
CLAUDE_CONFIG_DIR="$HOME/.claude-personal" claude setup-token
security add-generic-password -U -a "$USER" -s claude-token-personal -w '<the generated token>'

# 2) Apply the configuration
source ~/.zshrc

# 3) Verify per directory
cd ~/personal && claude   # ▶ personal
cd ~        && claude      # ▶ work (default)

If the intended account is displayed when you run claude in each directory, everything is working.

Conclusion

We’ve now covered how to separate work and personal Claude Code accounts on a single PC, and configure them to switch automatically based on the working directory. To summarize the key points:

  • settings.json is tied to a directory (CLAUDE_CONFIG_DIR), not an account.
  • On macOS, credentials share the Keychain, so separate the personal account with CLAUDE_CODE_OAUTH_TOKEN.
  • Use the chpwd hook to switch accounts by directory, and a launch-time notice to prevent mistakes.
  • The account is fixed when claude launches, so run work and personal tasks as separate sessions.

If you’ve ever committed or worked under the wrong account while juggling multiple accounts, setting this up once brings great peace of mind — give it a try.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.



SHARE
Twitter Facebook RSS