[
    {
        "role": "user",
        "content": "typically%2C%20I%20rely%20on%20ANTHROPIC_API_KEY%20environment%20variable%20to%20use%20my%20claude%20API%20account.%20However%2C%20some%20features%20require%20claude.ai%20login.%20Howerver%2C%20when%20I%20login%2C%20it%20is%20lost%20the%20next%20time%20I%20run%20claude%2C%20perhaps%20conflicting%20with%20ANTHROPIC_API_KEY."
    },
    {
        "role": "assistant",
        "content": "You're seeing expected behavior: Claude Code (the CLI) gives priority to certain authentication methods over others. If you have `ANTHROPIC_API_KEY` set in your environment, it will **override** your Claude.ai login session in many cases\u2014even if you've logged in through `/login`. This can lead to seeing features disappear or switching into a different billing mode (e.g. API billing rather than subscription included usage) after login. ([support.anthropic.com](https://support.anthropic.com/en/articles/12304248-managing-api-key-environment-variables-in-claude-code?utm_source=openai))\n\nHere\u2019s a breakdown of what's happening, and how you can manage it cleanly so you get the behavior you want.\n\n---\n\n## How Claude Code decides which credentials to use\n\nThe authentication \"precedence\" in Claude Code works like this: ([code.claude.com](https://code.claude.com/docs/en/authentication?utm_source=openai))\n\n1. Credentials for cloud providers (if relevant)  \n2. `ANTHROPIC_AUTH_TOKEN` (Bearer-type token in environment variable)  \n3. `ANTHROPIC_API_KEY` environment variable (direct API key, sent in an `X-Api-Key` header)  \n4. Output from an `apiKeyHelper` script\u2014useful for rotating credentials or secrets vaults  \n5. Your subscription login via `/login` (OAuth) \u2014 used when none of the above are active\n\nBecause `ANTHROPIC_API_KEY` is higher priority than `/login`, if it's set\u2014even if you login\u2014it often wins and determines what features are available, how you\u2019re billed, etc. ([code.claude.com](https://code.claude.com/docs/en/authentication?utm_source=openai))\n\n---\n\n## What you\u2019re observing (and why)\n\n- You set `ANTHROPIC_API_KEY`, and then run `claude` \u2192 it uses the API key, not the subscription login.  \n- You login with `/login` \u2192 you see access to subscription-based features for that session, **unless** the API key is present and \u201capproved\u201d by Claude Code.  \n- Next time you run `claude` or restart the terminal/session: the environment variable is still there, so the API key authentication is still active.  \n- Features that require subscription login (OAuth) disappear if they detect the API key is active.  \n\n---\n\n## How to use login (OAuth) without interference\n\nIf you want Claude Code to use your subscription login (Pro, Max, Team, etc.) instead of the API key, here are the clear steps to make sure that happens:\n\n1. **Unset `ANTHROPIC_API_KEY`** in your current shell session  \n   - On Linux/macOS:\n     ```bash\n     unset ANTHROPIC_API_KEY\n     ```\n   - On Windows CMD:\n     ```\n     set ANTHROPIC_API_KEY=\n     ```\n   - On PowerShell:\n     ```powershell\n     Remove-Item Env:ANTHROPIC_API_KEY\n     ```\n\n2. **Remove it from your shell startup files** (e.g. `~/.bashrc`, `~/.bash_profile`, `~/.zshrc`) so it doesn\u2019t get set automatically next session. ([code.claude.com](https://code.claude.com/docs/en/troubleshooting?utm_source=openai))\n\n3. **Run `/status`** inside Claude Code to see which credential is currently active. This tells you whether you're using API key auth or your subscription login. ([support.anthropic.com](https://support.anthropic.com/en/articles/12304248-managing-api-key-environment-variables-in-claude-code?utm_source=openai))\n\n4. If needed, **run `/logout` then `/login`** to clear and regenerate your OAuth credentials and ensure you're fully in login-mode. ([code.claude.com](https://code.claude.com/docs/en/errors?utm_source=openai))\n\n---\n\n## When `ANTHROPIC_API_KEY` is useful\n\nThere *are* situations where using the API key is the better choice:\n\n- You want to use Claude Code in non-interactive or automated contexts (e.g. CI/CD scripts, scheduled tasks) where browser OAuth isn't practical. ([code.claude.com](https://code.claude.com/docs/en/errors?utm_source=openai))  \n- You need billing tied to a specific pay-as-you-go API account rather than your subscription.  \n- You're rotating keys or using a helper script (`apiKeyHelper`) to manage credentials. ([code.claude.com](https://code.claude.com/docs/en/authentication?utm_source=openai))\n\nIn those cases, the recommendation is to temporarily set `ANTHROPIC_API_KEY` (or use `apiKeyHelper`), then unset it when you're done, so you don\u2019t accidentally switch out of your subscription login mode.\n\n---\n\n## TL;DR / Checklist\n\n| Desired Behavior | Action Required |\n|-------------------|------------------|\n| **Use subscription features (login via `/login`)** | Unset `ANTHROPIC_API_KEY`, remove from env files, confirm with `/status`, re-login if needed |\n| **Use API key / API billing** | Set `ANTHROPIC_API_KEY` env var, accept/prompts from Claude Code, ensure your key has access and is active |\n\n---\n\nIf you want, I can help you figure out exactly what your CLI is doing right now (which credential it's using) with the commands to run. Do you want me to walk you through that?"
    }
]