[BUG]Title: claude -p fails with "OAuth session expired and could not be refreshed" while interactive mode works fine
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?
Version: Claude Code v2.1.220 (confirmed up to date via claude update)
Environment: macOS, zsh
Description:
Non-interactive (-p / print mode) invocations fail to authenticate, while a normal interactive session in the same terminal, same directory, authenticates successfully.
Steps to reproduce:
Run claude -p "say hello" --permission-mode bypassPermissions
→ Fails immediately with:
Failed to authenticate: OAuth session expired and could not be refreshed
(An earlier attempt returned a slightly different message: Failed to authenticate. API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"OAuth access token has been revoked."},"request_id":null})
Run claude (interactive, no -p) in the same terminal, same directory
→ Succeeds immediately, no login prompt needed:
Claude Code v2.1.119
Sonnet 4.6 with high effort · Claude Pro
(Note: interactive banner shows v2.1.119 while claude update reports the installed version as 2.1.220 — possibly related, possibly a red herring.)
Exit the interactive session and retry claude -p ... → same authentication failure persists.
Ran claude update → already up to date (2.1.220). Retried -p again → same failure.
Expected: -p mode should authenticate using the same valid session as interactive mode, since I'm logged in and interactive mode works without any re-login prompt.
Actual: -p mode consistently fails to authenticate, blocking any headless/scripted/cron-based use of Claude Code, while interactive use is unaffected.
What Should Happen?
Expected: -p mode should authenticate using the same valid session as interactive mode, since I'm logged in and interactive mode works without any re-login prompt.
Actual: -p mode consistently fails to authenticate, blocking any headless/scripted/cron-based use of Claude Code, while interactive use is unaffected.
Error Messages/Logs
Failed to authenticate: OAuth session expired and could not be refreshed
Steps to Reproduce
Steps to reproduce:
Run claude -p "say hello" --permission-mode bypassPermissions
→ Fails immediately with:
Failed to authenticate: OAuth session expired and could not be refreshed
(An earlier attempt returned a slightly different message: Failed to authenticate. API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"OAuth access token has been revoked."},"request_id":null})
Run claude (interactive, no -p) in the same terminal, same directory
→ Succeeds immediately, no login prompt needed:
Claude Code v2.1.119
Sonnet 4.6 with high effort · Claude Pro
(Note: interactive banner shows v2.1.119 while claude update reports the installed version as 2.1.220 — possibly related, possibly a red herring.)
Exit the interactive session and retry claude -p ... → same authentication failure persists.
Ran claude update → already up to date (2.1.220). Retried -p again → same failure.
Claude Model
Sonnet (default)
Is this a regression?
No, this never worked
Last Working Version
_No response_
Claude Code Version
2.1.220
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Terminal.app (macOS)
Additional Information
N/A
4 Comments
I just got this error when trying to use Claude (maybe the first time using it?) in T3 Code.
Run
/loginwithin Claude (interactive) to fix it.@vitor-belim-ovyo your recommendation is far from helpful.
We use
claude -pfor small automations in our local machine - filling in gaps that neither Claude Desktop nor Claude Code could provide us. The reality is that if we need to constantly re-authenticate every day (for no reason), then my local jobs will be broken and my workflow will be disrupted.I can only see 2 ways out of this:
I am running into this at least once every 2-3 days. It's really annoying