[BUG] 401 "Invalid bearer token" caused by ANTHROPIC_AUTH_TOKEN environment variable
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
After /login (which reports success), every prompt fails with 401 "Invalid bearer token". This happens on both the Windows CLI (v2.1.76) and the Claude Desktop App. The root cause is the environment variable ANTHROPIC_AUTH_TOKEN which silently overrides the valid OAuth token. In my case it was set to "ollama" from a previous configuration. Removing it fixes the issue immediately.
What Should Happen?
Claude Code should either ignore ANTHROPIC_AUTH_TOKEN when OAuth login is used, or display a clear warning like: "⚠️ ANTHROPIC_AUTH_TOKEN environment variable detected. This overrides your subscription login. Remove it to use your Max/Pro plan."
Error Messages/Logs
Steps to Reproduce
- Set environment variable
ANTHROPIC_AUTH_TOKENto any value (e.g.ollama) - Open Claude Code CLI or Claude Desktop App (Codice tab)
- Run
/login— login reports success - Send any message (e.g. "ciao")
- Error: 401 "Invalid bearer token"
The ANTHROPIC_AUTH_TOKEN env var silently overrides the valid OAuth token from login, causing all requests to fail. Removing it fixes the issue:
[System.Environment]::SetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", $null, "User")
Claude Code should warn when this variable is set and conflicts with OAuth credentials.
Claude Model
None
Is this a regression?
Yes, this worked in a previous version
Last Working Version
_No response_
Claude Code Version
2.1.76
Platform
Anthropic API
Operating System
Windows
Terminal/Shell
PowerShell
Additional Information
_No response_
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗