[BUG] Interactive Mode Ignores ANTHROPIC_API_KEY — Requires /login Despite Valid Auth

Status Closed — not planned
Reported on v2.1.50
Maintainer reply None cached
Activity 11 comments · opened Feb 23, 2026 · closed May 2, 2026

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_

View original on GitHub ↗

11 Comments

github-actions[bot] · 6 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/25069
  2. https://github.com/anthropics/claude-code/issues/5167
  3. https://github.com/anthropics/claude-code/issues/23753

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

sstklen · 6 months ago

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 via claude config set apiKey $ANTHROPIC_API_KEY so interactive mode finds it in its config store without needing OAuth. For CI/headless environments, the config approach is most reliable.

# Option 1: Explicit flag (per-session)
claude --api-key "$ANTHROPIC_API_KEY"

# Option 2: Persist to config (permanent fix)
claude config set apiKey "$ANTHROPIC_API_KEY"

# Then just run normally:
claude

Hope this helps! Let me know if it doesn't match your case — happy to dig deeper. 🦞

_Disclosure: This analysis is from Confucius Debug, an AI-powered community KB for agent bugs. Please verify before applying._

---
<sub>🦞 Confucius Debug — community knowledge base for AI agent bugs. Free to search via MCP.</sub>

BMR59920 · 6 months ago

Hey. Hi. Not trying to hijack this or simply chime in me too... but I guess I'm going both.

`` # Option 1: Explicit flag (per-session) claude --api-key "$ANTHROPIC_API_KEY" # Option 2: Persist to config (permanent fix) claude config set apiKey "$ANTHROPIC_API_KEY" # Then just run normally: claude `` Hope this helps! Let me know if it doesn't match your case — happy to dig deeper. 🦞
:~$ claude --api-key "$ANTHROPIC_API_KEY"
error: unknown option '--api-key'

:~$ claude config set apiKey "$ANTHROPIC_API_KEY"
Welcome to Claude Code v2.1.61
...
THEME >
...
Login Method:
1. Claude account with subscription · Pro, Max, Team, or Enterprise
2. Anthropic Console account · API usage billing
3. 3rd-party platform · Amazon Bedrock, Microsoft Foundry, or Vertex AI

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.

BMR59920 · 6 months ago

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:

 ⚠Auth conflict: Both a token (ANTHROPIC_AUTH_TOKEN) and an API key (ANTHROPIC_API_KEY) are set. This may lead to
  unexpected behavior.
    • Trying to use ANTHROPIC_AUTH_TOKEN? Unset the ANTHROPIC_API_KEY environment variable, or claude /logout then
    say "No" to the API key approval before login.
    • Trying to use ANTHROPIC_API_KEY? Unset the ANTHROPIC_AUTH_TOKEN environment variable.
sstklen · 6 months ago

@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:

# Step 1: Set dummy to bypass the login gate
export ANTHROPIC_AUTH_TOKEN=dummy
export ANTHROPIC_API_KEY="your-actual-key"
claude   # → Claude prompts "Use API key?" → select Yes

# Step 2: Clean up the dummy
unset ANTHROPIC_AUTH_TOKEN

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_KEY alone keeps interactive mode running.

Hope that closes the loop\! 🦞

praiaBarryTolnas · 5 months ago
@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: # Step 1: Set dummy to bypass the login gate export ANTHROPIC_AUTH_TOKEN=dummy export ANTHROPIC_API_KEY="your-actual-key" claude # → Claude prompts "Use API key?" → select Yes

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

mjnovice · 5 months ago

Any updates on this one ?

Cs4r · 5 months ago
@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: # Step 1: Set dummy to bypass the login gate export ANTHROPIC_AUTH_TOKEN=dummy export ANTHROPIC_API_KEY="your-actual-key" claude # → Claude prompts "Use API key?" → select Yes # Step 2: Clean up the dummy unset ANTHROPIC_AUTH_TOKEN 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_KEY alone keeps interactive mode running. Hope that closes the loop! 🦞

this worked for me

LingyvKong · 5 months ago
> @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: > # Step 1: Set dummy to bypass the login gate > export ANTHROPIC_AUTH_TOKEN=dummy > export ANTHROPIC_API_KEY="your-actual-key" > claude # → Claude prompts "Use API key?" → select Yes > # Step 2: Clean up the dummy > unset ANTHROPIC_AUTH_TOKEN > 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_KEY alone keeps interactive mode running. > Hope that closes the loop! 🦞 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.

github-actions[bot] · 3 months ago

Closing for now — inactive for too long. Please open a new issue if this is still relevant.

github-actions[bot] · 2 months ago

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.