[BUG] Interactive Mode Ignores ANTHROPIC_API_KEY — Requires /login Despite Valid Auth
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?
Title
claude interactive mode says "Not logged in" despite ANTHROPIC_API_KEY being set and claude -p working correctly
Description
Claude Code interactive mode refuses to use ANTHROPIC_API_KEY environment variable for authentication, always requiring /login (OAuth flow). However, claude -p (print mode) works perfectly with the same API key.
Environment
Claude Code version: 2.1.50
OS: Ubuntu 22.04 (GCP VM, headless/SSH)
Shell: bash
Installation method: curl -fsSL https://claude.ai/install.sh | bash
Workaround
claude -p "message" works for non-interactive use, but loses the interactive features (multi-turn conversation context, tool approval UI, etc.).
Suggested Fix
Interactive mode should work with ANTHROPIC_API_KEY alone, without requiring a prior OAuth /login. The behavior should match claude -p, which already works correctly with just the API key.
What Should Happen?
Expected Behavior
Interactive mode should use ANTHROPIC_API_KEY for API calls, just like claude -p does. claude auth status confirms the key is recognized and valid.
Actual Behavior
Interactive mode blocks all messages with Not logged in · Please run /login, even though:
claude auth status shows "loggedIn": true
claude -p works perfectly with the same API key
The API key is confirmed valid via direct curl calls
Error Messages/Logs
Steps to Reproduce
Steps to Reproduce
Set ANTHROPIC_API_KEY in environment or in ~/.claude/settings.json:
bashCopyexport ANTHROPIC_API_KEY="sk-ant-api03-..."
jsonCopy// ~/.claude/settings.json
{
"env": {
"ANTHROPIC_API_KEY": "sk-ant-api03-..."
},
"model": "opus"
}
Verify auth status:
bashCopy$ claude auth status
{
"loggedIn": true,
"authMethod": "api_key",
"apiProvider": "firstParty",
"apiKeySource": "ANTHROPIC_API_KEY"
}
Verify API key works via print mode:
bashCopy$ claude -p "hello"
Hello! How can I help you today? # ✅ Works
Verify API key works via curl:
bashCopy$ curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "content-type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{"model": "claude-sonnet-4-20250514", "max_tokens": 50, "messages": [{"role": "user", "content": "say hi"}]}'
✅ Returns valid response
Run interactive mode:
bashCopy$ claude
Type any message:
Copy❯ hi
⎿ Not logged in · Please run /login
Additional Context
Running /login inside interactive mode forces an OAuth flow that connects to a different workspace/organization — one that may have different billing/limits than the workspace the API key belongs to.
On a macOS laptop with the same API key AND a prior OAuth login (authMethod: "claude.ai"), interactive mode works — it shows "Auth conflict: Using ANTHROPIC_API_KEY instead of Anthropic Console key" and uses the API key. This suggests interactive mode requires an OAuth login to exist first, then optionally overrides it with ANTHROPIC_API_KEY.
On a headless Linux VM without a prior OAuth login, there is no way to make interactive mode work with just an API key.
The OAuth token is stored in the system keyring (secret-service/gnome-keyring), not in ~/.claude/ files, making it difficult to manage on headless servers.
Claude Model
Opus
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.1.50
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Terminal.app (macOS)
Additional Information
_No response_
11 Comments
Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
Hey! I ran into a similar pattern in our bug knowledge base and thought this might help.
What's happening: Interactive mode auth check prioritizes OAuth session lookup over ANTHROPIC_API_KEY env var. On headless systems without a browser-based OAuth session, it reports 'Not logged in' even when the env var is set. Print mode (-p) has a separate code path that correctly falls back to the env var.
What worked for us:
Pass the API key explicitly via CLI flag to bypass the OAuth-first auth check:
claude --api-key $ANTHROPIC_API_KEY. Alternatively, persist it viaclaude config set apiKey $ANTHROPIC_API_KEYso interactive mode finds it in its config store without needing OAuth. For CI/headless environments, the config approach is most reliable.Hope this helps! Let me know if it doesn't match your case — happy to dig deeper. 🦞
---
<sub>🦞 Confucius Debug — community knowledge base for AI agent bugs. Free to search via MCP.</sub>
Hey. Hi. Not trying to hijack this or simply chime in me too... but I guess I'm going both.
It doesn't matter if you choose 1 or 2 - the cool interactive terminal window will ALWAYS prompt you to connect back to Anthropic OAuth via a browser link. Running headless seems to be part of this problem. Copying the link from the Linux box and pasting it in the Mac browser always yields OAuth Errors.
I could not make your suggested method work.
I found a workaround for my case: https://github.com/anthropics/claude-code/issues/1084#issuecomment-3223079288
Setting both got past the mandatory URL stage. It does produce this output though:
@BMR59920 You're one step away from a clean setup\! The auth conflict warning happens because you're running both at the same time — here's the full sequence from #1084 that makes it stick:
After selecting Yes, Claude uses your API key for the session. The dummy token was only needed to reach that prompt — once you've confirmed, just
ANTHROPIC_API_KEYalone keeps interactive mode running.Hope that closes the loop\! 🦞
Using Claude 2.1.74, that gets me past the login prompt and /status reports it is using the API key, but sending a prompt results in:
⎿ API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"Invalid bearer token"},"request_id":"req_011CYzKuoxMCe6rTVP7Fyjr9"} · Please run /login
Any updates on this one ?
this worked for me
this worked for me, too.
By the way, if you don't get this dialog: "claude # → Claude prompts 'Use API key?' → select Yes"
Type "/config" in claude, then find the "Use custom API key: xxx false" item, use the spacebar to toggle it, and make sure it is set to true.
Closing for now — inactive for too long. Please open a new issue if this is still relevant.
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.