[BUG] Resuming sessions doesn't work in non-interactive mode (Claude Code SDK)

Status Fixed / completed
Maintainer reply ✓ Yes — blois
Activity 7 comments · opened Jul 19, 2025 · closed Aug 4, 2025
💡 Likely answer: A maintainer (blois, collaborator) responded on this thread — see the highlighted reply below.

I'm running in MacOS and run the following commands:

$ claude -p --session-id 0f096bd6-56cd-4fc4-aaa2-331954a6d83e "hello"
Hello! I'm Claude Code, ready to help you with software engineering tasks. What would you like me to work on?

$ claude -p --resume 0f096bd6-56cd-4fc4-aaa2-331954a6d83e "what is 2+2"
4

$ claude -p --resume 0f096bd6-56cd-4fc4-aaa2-331954a6d83e "what did i jsut ask"
You asked "hello" - which was a greeting.

Notice how claude is not aware of my previous conversations in this session. It seems that --resume doesn't work at all in non-interactive mode.

This prevents a lot of potential SDK integrations from being built.

View original on GitHub ↗

7 Comments

hesreallyhim · 1 year ago

@presmihaylov

Notice how claude is not aware of my previous conversations in this session. It seems that --resume doesn't work at all in non-interactive mode.

This prevents a lot of potential SDK integrations from being built.

This is what I see:

  • you start a session
  • you resume either some other session or possibly the result of the first query, not sure.
  • you resume the first session, which does not contain the question about "2 + 2" - so this not a 1 -> 2 -> 3 interaction. If you want to resume from after your second message, use the session ID that is returned from THAT message, or use --continue.

I think this is the expected behavior, what do you think?

presmihaylov · 1 year ago

Hey @hesreallyhim apologies, I actually messed up my example because the second UUID is different, but the issue is still legit - here is me reproducing it:
<img width="998" height="251" alt="Image" src="https://github.com/user-attachments/assets/ccfb4ae8-3a63-4afe-845a-7ac3d4b6d79d" />

Snippet version:

$ claude -p --session-id 4e934b1f-30c6-470c-9b66-db27d56c80ac "hello"
Hello! I'm here to help you with your software engineering tasks. What would you like to work on today?

$ claude -p --resume 4e934b1f-30c6-470c-9b66-db27d56c80ac "what is 2+2"
4

$ claude -p --resume 4e934b1f-30c6-470c-9b66-db27d56c80ac "what did i just ask?"
You asked "hello" - which was a greeting.

And here is the example working when I'm not using session IDs, instead just continuing last conversation:
<img width="586" height="320" alt="Image" src="https://github.com/user-attachments/assets/965a5308-130e-4c46-b379-28ad6fbd2f96" />

Snippet:

$ claude -p "hello"
Hello! I'm Claude Code, ready to help you with your software engineering tasks. I can see you're working in the `claudecontrol/ccagent` repository with Go backend services, CLI tools, and WebSocket implementations.

What would you like to work on today?

$ claude -p --continue "what is 2+2"
4

$ claude -p --continue "what did i just ask?"
You asked "what is 2+2"
hesreallyhim · 1 year ago

it's just the weird way that sessions work in p-mode... you should examine ~/.claude/projects/ if you're curious... and also get the output in json to see more (--output-format json)... every message is assigned a new session ID, but if it's a "continuation"/resume, the entire conversation from the previous log file where the whole chat is stored will be copied into the new file. That's why it suggests on the doc site to store the session ID somewhere, e.g. see here. Sessions are kind of "virtual" in p-mode, they should probably be clearer about this.

presmihaylov · 1 year ago

I'm afraid this still doesn't work :/
<img width="762" height="320" alt="Image" src="https://github.com/user-attachments/assets/10b2cb43-4da2-466f-888b-77b71ee2455d" />

$ claude -p "Hello" --output-format json | jq -r '.session_id' > session.txt

$ claude -p --resume "$(cat session.txt)" "what is 2+2?"
4

$ claude -p --resume "$(cat session.txt)" "what did i just ask?"

You asked "what did i just ask?" - which is asking me to repeat your previous question. Before that, you said "Hello" as a greeting.
hesreallyhim · 1 year ago

OK. so. The "What did I just ask?" thing is annoying cuz it doesn't interpret it the way you mean (like what did I ask before the current exchange lol). But look at the code and see what you're doing... You capture the session ID from line 1 and then resume it. OK nice. Then in the third line, you use the session ID in session.txt... but you didn't update it. The session ID output of the line 2 response is _different_. You're "resuming" from line 1 again. You would need to capture the new session ID each time and resume with THAT. They should really really probably give a more complete example, but you might write a helper function that receives the json payload, (a) captures the session ID (which like I said changes every turn); (b) prints the response. But please take a moment and use "-p --output-format json" and then keep doing that with"--continue" and you'll see that each response has a different session ID but it's the "same convo".

hesreallyhim@Doctors-MacBook-Pro-4 ~ % claude -p --output-format json "Hello I'm Fred" | jq '[.session_id, .result]'
[
  "65117b0d-6b77-44a6-9acb-88225fd089b4",
  "Hello Fred! Nice to meet you. How can I help you today?"
]

hesreallyhim@Doctors-MacBook-Pro-4 ~ % claude -p --output-format json --resume 65117b0d-6b77-44a6-9acb-88225fd089b4 "I want to learn claude CLI" | jq '[.session_id, .result]'
[
  "8bbf4a01-6190-4076-ba5b-af61d26db92a",
  "Let me help you get started with Claude CLI! Here are the essentials:\n\n**What is Claude CLI?**\n- An interactive command-line tool for working with Claude on software engineering tasks\n- Helps with coding, debugging, refactoring, and understanding codebases\n\n**Key features:**\n- File operations (read, write, edit)\n- Code search and analysis\n- Running bash commands\n- Web search and fetching\n- Task management with todo lists\n- Git integration\n\n**Getting started:**\n1. Install: `npm install -g @anthropic/claude-code`\n2. Authenticate: `claude-code auth`\n3. Start a session: `claude-code`\n\n**Common commands:**\n- `/help` - Get help\n- `/settings` - Configure preferences\n- Use natural language to ask for coding help\n\nWant to try something specific or learn about a particular feature?"
]

hesreallyhim@Doctors-MacBook-Pro-4 ~ % claude -p --output-format json --resume 8bbf4a01-6190-4076-ba5b-af61d26db92a "Do you remember my name?" | jq '[.session_id, .result]'
[
  "009b6a17-704a-4295-a174-d981c93dddab",
  "Yes, your name is Fred."
]

hesreallyhim@Doctors-MacBook-Pro-4 ~ % claude -p --output-format json --resume 009b6a17-704a-4295-a174-d981c93dddab "Print every message in our conversation so far." | jq '[.session_id, .result]'
[
  "19ce6b63-30fc-463c-8318-d560cfa42018",
  "Here's our conversation so far:\n\n1. **You:** Hello I'm Fred\n2. **Me:** Hello Fred! Nice to meet you. How can I help you today?\n3. **You:** I want to learn claude CLI\n4. **Me:** [Provided information about Claude CLI including features, installation, and common commands]\n5. **You:** Do you remember my name?\n6. **Me:** Yes, your name is Fred.\n7. **You:** Print every message in our conversation so far."
]

So that make sense?

hesreallyhim@Doctors-MacBook-Pro-4 ~ % claude -p "print one a one-line bash function I can use to capture the sesion_id from every response you send me, print the response text, and then on the next message I send use "--resume" with the session_id that had your reply"

claude_chat() { local r=$(claude-code "$@" 2>&1); echo "$r"; export CLAUDE_SESSION_ID=$(echo "$r" | grep -oE 'session_id: [a-f0-9-]+' | cut -d' ' -f2 | tail -1); }


Then use: `claude_chat "your message"` and next time: `claude_chat --resume $CLAUDE_SESSION_ID "next message"`

That's not an elegant solution and I haven't tried it, but you can give that a go if you want. Anyway, you can figure this all out if you study the conversation flow with "--continue" and the full json output.

blois collaborator · 1 year ago

There was a recent issue where the user input was mistakenly being lost from resumed sessions. This should be fixed now.

Example:

claude --output-format=json -p "x = 2. Respond only with OK"

Followed by:

claude --output-format=json --resume 25016971-ac0b-4eaa-996e-78eab4f4c9c9 -p "what is the value of x?"
github-actions[bot] · 1 year 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.