[BUG] Claude CLI print mode reads from .env

Resolved 💬 4 comments Opened Jun 11, 2025 by AltTheAi Closed Jan 6, 2026

Environment

  • Platform (select one):
  • [ ] Anthropic API
  • [x] Other: Claude CLI (claude code)
  • Claude CLI version: 1.0.18
  • Operating System: Debian GNU/Linux 12 (bookworm) x86_64, Kernel 6.1.0-34-amd64
  • Terminal: /dev/pts/10 (zsh 5.9)

Bug Description

Claude CLI uses different authentication methods for interactive mode vs print mode, causing confusion and authentication failures. Interactive mode (claude) uses OAuth authentication, while prompt mode (claude -p "prompt") attempts to read API keys from environment variables/.env files, leading to "Invalid API key" errors when no valid environment variable is set.

Steps to Reproduce

  1. Set up Claude CLI with OAuth authentication (works in interactive mode)
  2. Run claude - OAuth login works correctly
  3. Run claude -p "prompt" - fails with "Invalid API key" error
  4. This behavior is inconsistent between users (works for root, fails for other users depending on .env file presence/content)

Expected Behavior

Both interactive and print modes should use the same authentication method (OAuth) for consistency, OR there should be clear documentation explaining the different authentication methods and how to configure them properly.

Actual Behavior

  • Interactive mode: Uses OAuth authentication successfully
  • Print mode: Attempts to read API key from environment variables/.env file, fails when not properly configured
  • No clear indication or documentation that different modes use different authentication methods
  • Users are confused why one mode works while the other fails with the same setup

Additional Context

  • The CLI appears to check for ANTHROPIC_API_KEY in environment variables/.env files for print mode
  • This behavior difference is not documented or communicated to users
  • Causes confusion as users expect consistent authentication across all CLI modes
  • Consider either unifying authentication methods or providing clearer documentation about the differences

View original on GitHub ↗

This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗