Try free

Last updated 13 October 2026

Use multiple API keys with Claude Code

The friction of managing two digital identities

Context switching between personal and professional projects creates a significant overhead for developers. You might spend your morning working on a high priority repository for your employer and your evening building a side project. If you use Claude Code for both, you face a dilemma regarding billing and identity. Each session needs to be linked to the correct Anthropic API key to ensure costs are allocated properly and that you are not using corporate resources for private experiments.

Manually running /logout and then /login every time you switch projects is not a viable solution. This process requires you to open a browser, authenticate with a specific account, and then return to the terminal. It breaks your deep work and introduces the risk of human error. If you forget to switch back, you might accidentally bill a corporate client for personal tasks or expose private code to a shared work account. This interruption is exactly the kind of friction that stops developers from using agentic tools to their full potential.

The goal is to establish a workflow where your environment handles the authentication automatically based on the folder you are in. You want to reach a state where you can simply type the claude command and trust that the agent is using the right credentials. This requires moving away from global authentication and toward a system that respects local project context and shell-level configuration.

Environment variables take priority over global login

The most direct way to handle multiple keys is through terminal environment variables. When you run the claude command, the agent looks for an existing session but also checks the environment for specific configuration strings. If you set a specific API key in your current shell before starting a session, that key takes precedence over the global credentials stored during your last interactive login. This allows you to maintain different identities in different terminal tabs simultaneously.

To implement this, you can export the relevant key in your shell before you begin working. This key remains active for the duration of that terminal session. When the agent performs tasks like using Bash to run a test or using Read to examine a file, it does so under the authority of the key provided in that specific environment. This isolation ensures that your professional and personal lives do not interfere with each other even if you are working on the same machine.

This method also works well with the claude -p flag for one-off tasks. If you need to quickly ask a question about a snippet of code using your personal account while you are in the middle of a work project, you can run a single command with the specific key exported for just that execution. It provides a level of granular control that the standard login flow cannot match, as it bypasses the need for persistent session storage for every minor interaction.

Project level configuration with local settings

For a more permanent and less error prone setup, you should use local project settings. Claude Code looks for a directory named .claude in your project root. Inside this directory, you can create a file named settings.local.json. This file is intended for your own untracked overrides and should always be added to your .gitignore file to prevent sensitive keys from being committed to a shared repository. By specifying project specific configurations here, you can ensure that the agent always operates under the correct parameters for that specific directory.

Using .claude/settings.local.json is superior to relying on global settings in ~/.claude/settings.json because it keeps your configurations strictly separated within the file system. When you enter a directory and run the claude command, the agent automatically merges the local settings with your global preferences. This means you can have a global coding style defined in your user directory while maintaining unique API keys or permission sets for each individual repo you work on.

You can also use this local directory to manage specific tools and agents. If a professional project requires a custom subagent defined in .claude/agents, that agent will only be available when you are working within that repo. This prevents your personal agents from cluttering your work environment and vice versa. It creates a clean boundary that helps the agent stay focused on the specific requirements of the current account and project context.

Shell aliases for quick context switching

Shell aliases provide a fast way to launch the agent with the right context without manually typing out long environment variables every time. You can add lines to your shell profile that define short commands for different identities. For example, you could create an alias called work-claude that exports your corporate key and then executes the claude command. Another alias called home-claude could do the same for your personal credentials, ensuring you never have to think about which key is active.

These aliases can also include flags to make your workflow more efficient. You might want your work alias to always run claude --continue to pick up the most recent session in that folder, while your personal alias might start a fresh session every time. By embedding these preferences into the alias, you reduce the cognitive load required to start a task. You simply choose the command that matches your current intent and the environment takes care of the rest.

While in a session, you can use Shift+Tab to cycle through different permission modes such as plan or bypassPermissions. If you are using your personal key, you might be more comfortable with a mode that allows the agent to edit files more freely. For work projects, you might stick to the default mode or the acceptEdits mode to maintain tighter control. Using aliases to launch the agent ensures you are starting from a known state for each respective account.

Using directory managers for automatic key rotation

If your workflow involves jumping between many different folders, an environment manager is the most efficient choice for key management. These tools detect when you change directories in your terminal and can automatically set or unset variables based on the presence of a configuration file in that folder. When you enter your work directory, the tool automatically loads the corporate API key. When you navigate to your personal projects folder, it swaps to your private key.

This automation ensures that whenever you initiate a command like claude or /clear, the agent is already authenticated with the correct credentials for that specific location. It removes the need for custom aliases or manual exports entirely. This is particularly useful when you are working on multiple microservices that all belong to the same professional account, as you only need to configure the key once at the root of your workspace.

You should also consider the role of the ~/.claude/CLAUDE.md file in this setup. While your API keys change based on the directory, this global file holds project instructions that apply to everything you do on your machine. You can use it to define your universal preferences, such as your preferred naming conventions or your desire for the agent to always use the /compact command when the context window gets full. This ensures a consistent experience regardless of which account is paying for the tokens.

Visual separation with adhdTerminal

Managing multiple active sessions at once can become visually overwhelming in a standard terminal. If you have many tabs open, it is easy to lose track of which one is running a personal agent and which one is connected to your employer's account. This is a problem that adhdTerminal solves by providing a native wall of tiles on macOS and Windows. You can create separate tiles for every project you are working on, giving each one a distinct name and color.

Because adhdTerminal runs many Claude Code sessions side by side, you can dedicate specific tiles to your work projects and others to your personal ones. Each tile remembers its state and location on the wall even after a restart. This means you do not have to remember which terminal tab belongs to which account. The visual feedback tells you exactly which session is working, which is waiting for you, and which is idle. It removes the mental burden of tracking multiple identities in a flat list of terminal windows.

The app is made by MINDBALANCER OU in Estonia and is a separate product from Anthropic. It costs 99 dollars once for a key that runs on one computer at a time. It also includes a free iPhone app that mirrors your wall over an end to end encrypted link. This allows you to check on your work projects or send a quick task to your personal agent while you are away from your desk, all while maintaining the strict separation of accounts you have configured on your main machine.

Monitoring costs and usage across accounts

Verification is a critical step when you switch between multiple keys. You should use the /cost and /usage commands frequently during your first few sessions with a new setup. These commands show you the financial impact of the current conversation and the total tokens consumed. If the numbers do not match your expectations for the specific project, it is a sign that your environment variables or local settings might not be loading as intended.

Checking these figures early prevents unexpected bills at the end of the month. If you notice that your personal side project is generating costs that seem more in line with a heavy work project, you can use the /logout command to clear the current session and then verify your environment variables. It is always better to spend a few seconds running a diagnostic command than to discover a billing error after significant work has been done.

To keep costs down on your personal accounts, you can use the /compact command to summarize the session so far. This frees up space in the context window and reduces the number of tokens sent in subsequent prompts. The agent will still remember the key points of the conversation, but you will not be paying to send the full history of every Bash command and file Read operation every time you ask a new question.

Debugging configuration conflicts

If you suspect that your API keys are conflicting or that the agent is picking up the wrong identity, the claude doctor command is your best diagnostic tool. This utility checks the health of your installation and provides a report of your current environment. It can reveal if there are conflicting configurations between your user settings in ~/.claude/settings.json and your local project settings. It also confirms your connection status to the Anthropic servers.

Conflict often happens when a global setting is accidentally set to a fixed value that overrides your environment variables. If claude doctor shows that your authentication is pulling from a source you did not expect, you can check the .claude/settings.json file in your project or your user home directory. Removing the hardcoded key from these files and relying on environment variables or settings.local.json is usually the best way to restore a flexible multi-account workflow.

Another helpful command for cleaning up your environment is /clear. This starts the session over in the same folder without having to quit the agent. If you have just changed your environment variables and want to ensure the agent is now using the new key for a fresh start, running /clear is faster than restarting the process. It keeps you in the flow while ensuring your credentials and session state are synchronized with your current project needs.

Moving your setup between machines

For developers who work on multiple computers, keeping a multi-key setup consistent requires a bit of planning. Settings stored in your home directory are local to that specific machine, so you will need to replicate your environment variable strategy or your local settings files on every device you use. If you use a tool to sync your dotfiles, ensure that your secret keys are not included in the sync to keep them secure.

One of the benefits of adhdTerminal is that your license key can be moved between computers. This makes it easier to maintain your visual workflow even if you switch from a desktop to a laptop. Since the app shows the last exchanges of every conversation on its tiles, you can quickly see the context of your work and personal projects on any machine where you have your setup configured. The 7 day free trial also lets you test this multi-machine flow without an initial commitment.

When you are ready to update your installation on a new machine, use the claude update command to ensure you have the latest features and bug fixes. Keeping the agent updated across all your environments ensures that your configuration files and slash commands behave consistently. By combining these local maintenance habits with a visual management tool, you can create a reliable system for handling as many API keys and projects as your work requires.

Questions people ask

How can I verify which API key is being used in the current session?

You can run the /usage or /cost commands inside a session to see the current spend. While these do not show the key itself, you can compare the usage numbers with your account dashboard on the Anthropic website. Alternatively, use your shell to print the value of the environment variable you set before starting the agent.

Will using multiple keys interfere with the project instructions in CLAUDE.md?

No, the agent reads CLAUDE.md regardless of which API key is used for billing. The instructions in the markdown file control the behavior and coding standards of the agent, while the API key only handles authentication and costs. This allows you to have different personalities for different projects while using the same account, or vice versa.

Can I set a different default model for my personal and work accounts?

Yes, you can use the /model command inside a session to switch models, or specify a default in .claude/settings.local.json for each project. This is useful if you want to use a faster model for personal experiments and a more capable model for complex professional tasks. The setting will be saved for that specific project context.

Read next

More on when it goes wrong·All posts