[BUG] claude code doesn't use different API keys for different claude profiles
I'm trying to maintain two separate projects for claude code - one for work, one personal using different API keys.
I had configured that the following way:
alias ccwork="ANTHROPIC_API_KEY=\"$ANTHROPIC_API_KEY_WORK\" CLAUDE_CONFIG_DIR=\"/Users/pmihaylov/.claudework\" claude"
alias cchobby="ANTHROPIC_API_KEY=\"$ANTHROPIC_API_KEY_SIDE\" CLAUDE_CONFIG_DIR=\"/Users/pmihaylov/.claudehobby\" claude"
however, I recently found out that for a long time, claude had been using my personal API key in my work account.
I tried fixing this by relogging and setting up new API keys but it seems that's of no use. Regardless of which directory I point the tool to, it always uses whatever API key you last configured.
I also tried setting up the configuration for "use custom API key" but it doesn't seem to respect that at all.
How I tested this:
- login to work account + setup new API key
- login to hobby account + setup new API key
- disable hobby API key
- run ccwork - this results in Invalid API key because it's attempting to use the hobby API key (the last one I logged in with)
Same happens in the other direction, when logging in work account last + using cchobby.
The ANTHROPIC_API_KEY environment variable is only applicable to non-interactive mode, so this is working as expected for now. That said, it makes sense that you'd want a different auth session for work and personal projects on the same device. We'll keep this in mind for a future enhancements. Thanks for the report.
The ANTHROPIC_API_KEY approach I tried was a workaround because I expected CLAUDE_CONFIG_DIR to use different API keys with different directories, but it seems like the auth session is global
I ran into a similar issue. I tried configuring my .claude.json with the key I wanted to use, but that didn't work. Is there a way to specify the key to use? I'm being given an Anthropic key for my work project and need Claude Code to use that. How can I do this?
The
ANTHROPIC_API_KEYenvironment variable is only applicable to non-interactive mode, so this is working as expected for now. That said, it makes sense that you'd want a different auth session for work and personal projects on the same device. We'll keep this in mind for a future enhancements. Thanks for the report.
This link seems to be broken, there is no automate-ci-and-infra-workflows sectrion on this page and it never mentions either the key or interactive or non-interactive modes.