In a multi-step `claude -p` script, why capture `session_id` and pass `--resume` rather than chaining calls with `--continue`?
answer
- most recent, in this directory
- two jobs, one checkout
- the ID comes from json output
- found from any project on the machine
- unsaved sessions cannot come back
basics
~20 s--continue picks the most recent conversation in the current directory, so concurrent jobs can grab each other's session; --resume with the session_id read from --output-format json targets exactly one conversation, from any directory on the machine.
solid answer
~40 s`claude -p --continue` loads the most recent conversation in the current directory, and in print mode that includes `-p`, SDK and `/loop` sessions. That is fine for a single sequential script, but two jobs sharing a checkout, or a developer working there, can make "most recent" someone else's session. The deterministic form captures the ID from the first call, `session_id=$(claude -p "Start a review" --output-format json | jq -r '.session_id')`, then calls `claude -p "Continue that review" --resume "$session_id"`. Claude Code finds the ID in any project on the machine, and `--resume` also accepts the absolute path to a session's `.jsonl` transcript. A run started with `--no-session-persistence` is not saved, so it cannot be resumed at all.
code
bash · 3 linessession_id=$(claude -p "Triage the failures in lint.log" --output-format json | jq -r '.session_id')
echo "$session_id" > triage-session.txt
claude -p "Now propose fixes for the top three rules" --resume "$session_id" --output-format json > fixes.jsongo deeper
Recall that --continue picks up the most recent conversation and --resume takes a session ID, and that json output carries session_id.
Explain why most recent in a directory is ambiguous for concurrent jobs and how capturing session_id and passing --resume removes the ambiguity.
Show the pipeline details: resuming across directories, transcript paths, cost totals on resumed calls, the recorded system prompt, and where --no-session-persistence fits.
Decide whether long multi-step automations should chain claude -p calls by session ID at all, or move to the Agent SDK where sessions are held in code.
## Why scripts continue conversations at all A `claude -p` call ends when Claude produces its final response. Many jobs are naturally several steps: review the code, then focus on one area, then produce a summary. Each step could start fresh, but then Claude would re-read everything and lose what it already concluded. Continuing the same conversation keeps that context. Interactive resuming, the session picker and branching a conversation are taught with session management; this answer covers the print-mode forms a script uses. ## Two ways to continue | Form | What it targets | Good for | | --- | --- | --- | | `claude -p "..." --continue` | The most recent conversation in the current directory | One sequential script with nothing else running there | | `claude -p "..." --resume <id>` | Exactly the session with that ID (or transcript path) | Anything concurrent, or steps run from different directories | ### `--continue` `--continue` loads the **most recent conversation in the current directory**. In print mode it includes sessions created with `-p`, the Agent SDK and `/loop`; the interactive `claude --continue` skips those. So: ```bash claude -p "Review this codebase for performance issues" claude -p "Now focus on the database queries" --continue claude -p "Generate a summary of all issues found" --continue ``` works as long as this script is the only thing creating conversations in that directory. ### Why "most recent" breaks - **Concurrent jobs**: two nightly jobs in the same checkout each continue whichever conversation was written last, which can be the other job's. - **People**: a developer starting a session in the same directory between two steps changes what "most recent" means. - **Background sessions**: a finished background session in that directory can also be the one `--continue` opens. The failure is quiet: the step succeeds, but it answers in the wrong conversation. ## The deterministic pattern 1. Run the first step with `--output-format json` and read **`session_id`** from the result. 2. Pass that value to **`--resume`** on every later step. 3. Optionally keep the ID in a file or job artifact so a retry continues the same conversation. ```bash session_id=$(claude -p "Start a review" --output-format json | jq -r '.session_id') claude -p "Continue that review" --resume "$session_id" ``` Claude Code finds a session **by its ID in any project on this machine**, so the second command may run from a different directory. In place of an ID, `--resume` also accepts the **absolute path to a session's `.jsonl` transcript**. If you want to fix the ID before the first call, `--session-id` takes a UUID of your choosing. ## Things that carry over, and things that don't - **Cost**: each resumed call's `total_cost_usd` reports the conversation's whole total, earlier runs included, so read the latest value instead of summing; a resumed run's `--max-budget-usd` counts only its own new spend. - **System prompt text**: Claude Code records the system prompt on the conversation's first request and reuses it until the conversation is compacted, so changing `--append-system-prompt` on a later `--resume` call takes effect only after compaction, unless you pass `--system-prompt-snapshot off`. - **Permission mode and launch flags**: a `claude -p --resume` run starts in the permission mode a new `-p` run would start in (a session that ended in plan mode is the exception), and flags such as `--mcp-config`, `--settings`, `--plugin-dir` and `--add-dir` are not restored. Pass the mode, the tool rules and those flags on every step. ## When not to keep sessions A one-shot job that will never be continued can pass **`--no-session-persistence`** (print mode only): the session is not saved to disk and **cannot be resumed**. That keeps transcripts of throwaway runs off the runner, but it is incompatible with any later `--resume` step, so use it only on the final or only call. ## Choosing - Single, sequential, nothing else in the directory: `--continue` is acceptable. - Anything else: capture `session_id` and `--resume` it. - No follow-up ever: consider `--no-session-persistence`. Whichever you choose, store the session ID alongside the job's other artifacts. When a nightly run goes wrong, that ID is what lets someone open the exact conversation the next morning and see what Claude read, decided and was refused.
- You change `--append-system-prompt` on the second `claude -p --resume` call and nothing changes; why?Claude Code records the system prompt on the conversation's first request and reuses it, including after `--resume` or `--continue`, until the conversation is compacted. New flag text takes effect after compaction or in a new conversation; pass `--system-prompt-snapshot off` to rebuild the prompt on every request while you iterate.
- What does `--no-session-persistence` cost a pipeline?The session is never saved to disk, so neither `--resume` nor `--continue` can reach it later. Use it for one-shot jobs whose transcript you don't want kept on the runner, never on a step that a later step continues.
- claude -p --continue skips sessions created with -p, just like interactive -c
- --resume only finds sessions started in the current directory
- The exit status of the first call is the session ID to resume
- --no-session-persistence still lets a later step resume by ID
- --continue is safe even when several jobs share one checkout