[BUG] claude -m sonnet -p "plan this project" style prompt usually hanging on an error
Environment
- Platform (select one):
- [ ] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [ x ] Other: Claude Max
- Claude CLI version: 1.0.16
- Operating System: macOS 15.4.1
- Terminal: iTerm2
Bug Description
I have a script that calls claude-p to plan the next task, then again to do dev, then a third time to review the code. The first part, the planner, has been hanging most of the time lately (not always) on a particular error. I don't know what it's doing at that time; it could be running a build or something?
==================================================================
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
Error: Expected content to be an assistant message, but got "user"
at yS2 (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:2342:9744)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async bE5 (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:2356:8265)
Steps to Reproduce
- run claude -m sonnet -p 'do planning'
- try again if it doesn't fail
- <!-- And so on... -->
Expected Behavior
do work
Actual Behavior
error displayed, and then it never completes
13 Comments
I reset my .claude.json, and it's still happening, most of the time (but not all). This is a blank config, so no MCP's.
It also made it through my planner prompt, then stalled again with the reviewer prompt ... so it's not the prompt. also one of the runs had --dangerously-skip-permissions set and another one didn't.
factors eliminated, then: the config, the prompt, the MCP's, the --dangerously-skip-permissions arg
When you add
--output-format "stream-json" --verbosedoes that give any more details on what it is doing when failing? You could try piping this toclaude -p --output-format "stream-json" --verbose >> test.logAnd do you need the -m sonnet call? I never add that and without it is always defaulting to sonnet 4.
I'm getting the same error - yesterday occasionally, but now its every time - running an async subprocess calling
claude, it seems to error at the end of an interaction. my typical workflow runs for ~3mins and it runs through the todos, completes all of them, and then fails. heres the tail of the output:Have you tried going into the session to inspect what's going on? Probably wont help but may be good to check:
claude -r e5668ab2-1a8f-4ef5-be45-c40c82d38fd9
Are your stderr's (coppied below) from your process logging or directly from claude?
When claude has an error it is not piped to stdout but to stderr so make sure you are capturing that. But looking at it, this log makes me think it is from claude not your logs:
_Error: Expected content to be an assistant message, but got "user"_
I've checked a few of sessions with the command
claude -r e5668ab2-1a8f-4ef5-be45-c40c82d38fd9and each time the todo list is complete. For example:It seems like an issue exiting gracefully - probably something wrong with the way I'm doing the async call.
Yeah it may be the way you are doing the async call. Do you know if the error I mentiond above is from your code or claude?
_Error: Expected content to be an assistant message, but got "user"_
That looks interesting to me, and curious where that message is coming from. If not your code then that is helpful. Are you using those variables (user, assistant, message) in your code? If yes then it may be in how you are handling the subprocess and async.
Also do you know if you are capturing the stderr of the claude process? For example when I am wrapping claude with async processes I store the session id, so that I can continue them later on (this is actually a really cool feature and also helpul for debugging). However claude occasionally cleans these up so when I would call an async process with an old session id, I was gettting failures without any error message. Once I started to also pipe out stderr it became clear what the issue was.
Just mentioning this as there are not many docs or forums of people doing stuff like this. Are you streaming out the logs anywhere that you can review outside of the process with verbose turned on? This has also helped me quite a bit with tracing back issues.
I believe this is likely the same issue as #473, just showing a different error message in
-pmode.I'm not specifically referencing any user/assistant roles so I'm pretty sure thats coming from claude code, but its not present in the session when i reopen, so I could be wrong.
Heres my async call:
I'm wondering if I should be doing the
cmd -p --verbose > claude.logtype instead?My observation has been that when I downgrade the big model to haiku it doesn't seem to happen as much. I definately was seeing it nearly everytime when I had some async timeout at 180s. I removed that and it makes it rarer but still present.
The errors are coming in stderr. One thing I've noticed with
claude -ris a ton of sessions with content in them but the description complains about being authenticated. a) I don't know yet why there are so many sessions when it's running more like 4 per hour, not 40, and b) this is on a Claude Max subscription, and I'm not getting these auth errors in any interactive session. Is there a problem with auth handling, maybe related to sub-agent tool use or something?All of this activity relates to a shell script running claude -p a few times per loop, and like I said, approx loops approx once an hour. Nothing is backgrounded.
Modified Created # Messages Summary
❯ 1. 1m ago 7m ago 61 You are an expert megathinker static code rev...
I tried changing my ANTHROPIC_API_KEY in my environment to something else, and apparently it was an old key. Here's the problem: I'm running claude on a Claude Max account, so it shouldn't be touching the API key at all. I feel like maybe this is related?
Invalid API key · Fix external API key
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.