What you will be able to do
- Use fork_session to branch independent explorations from a shared analysis baseline
- Explain why tool results in a resumed session can be stale after code changes
- Tell a resumed session which files changed so it re-analyzes only those
- Choose between resuming, resuming from a summary, and a fresh session with injected state
1.fork_session: branching from a shared baseline
Suppose an agent has spent a session analyzing a codebase, and you now want to try two different approaches from that same starting point, such as two testing strategies or two refactoring plans. If you resumed the session twice in a row, the second attempt would inherit everything the first one did. A fork solves this. In the SDK you pass resume with the baseline session's ID together with fork_session=True (forkSession: true in TypeScript). The run then starts with the full baseline history but writes to a new session that gets its own ID.
Checkpoint 1 of 5· Fill the gap
Which option makes this call branch into a new session instead of appending to session_id?
# Fork: branch from session_id into a new session
forked_id = None
try:
async for message in query(
prompt="Instead of JWT, outline how OAuth2 would work for the auth module",
options=ClaudeAgentOptions(
resume=session_id,
? =True,
max_turns=5,
),
):resume picks the baseline history, and fork_session=True writes the new turns to a separate session with its own ID instead of the original.
Source: code.claude.com # Original session is untouched; resuming it continues the JWT thread
try:
async for message in query(
prompt="Continue with the JWT approach",
options=ClaudeAgentOptions(resume=session_id),
):Behind the scenes, forking copies the transcript into a new file and remaps the message IDs, leaving the original alone. The session-browser cookbook also offers a standalone fork_session() function that writes the fork to disk without running the agent; you start the fork later by passing its ID to resume. The function takes up_to_message_id, which branches from a chosen point in the conversation instead of the end. Because each fork is a separate session, you can explore extract-service in one branch and strangler-fig in another, and each starts from the same analysis without seeing the other.
Checkpoint 2 of 5· Exam question
An application serves many concurrent users, each with their own long-running investigation conversation against the same repository directory. A backend service needs to resume a specific user's conversation on demand, potentially hours after the user's last message, while other users' sessions remain active in the same directory. Which session option should the service use?
Correct answer: A — Track each user's captured session ID and pass it to resume when that user sends a follow-up
- A. Correct. With multiple concurrent sessions sharing a directory, resume with a tracked session ID is required to return to a specific user's conversation rather than whichever one was most recently active.
- B. Incorrect: continue always targets the most recently active session in the directory, which would route different users into whichever conversation last had activity, not their own.
- C. Incorrect: fork_session branches from an already-selected session; without first resuming the correct session ID, it would just branch off the wrong user's latest activity.
- D. Incorrect: the interactive session picker is a human-facing terminal feature and is not a programmatic mechanism a backend service can use to route requests.
2.Stale tool results after code changes
Resuming is meant to save work. The agent picks up its earlier analysis without re-reading files, and that is exactly the risk. A resumed Claude Code session reloads the full history, tool results included. So if the agent read config.ts last Tuesday and someone has rewritten it since, the old file contents are still in its context, and nothing in the history says they are out of date. The agent will reason from the version it saw.
Checkpoint 3 of 5· Check yourself
A session analyzed billing.py yesterday. Today a teammate refactored that file, and you resume the session with no further context. Why might the agent work from the old code?
Resuming replays the recorded tool results instead of re-running the tools, so the old file contents sit in the context exactly as they were read.
“the full history, including tool calls and results”Source: code.claude.com
The fix uses the one new input a resume accepts: the prompt you send with it. The SDK example shows resume=session_id together with a new instruction, and the CLI claude -r "<session>" "query" form works the same way. If you name the files that changed and what changed in them, the agent can re-read just those files and keep the rest of its analysis, instead of exploring the whole codebase again. The documentation doesn't describe a dedicated flag or automatic step for this. It is a matter of writing the right prompt, and that follows from the fact that the replayed history can't announce its own staleness.
3.Resume, resume from summary, or start fresh
So the choice depends on how much of the earlier context is still true. If most of it holds, resume and point out the few changes. If much of it is outdated, start a new session and inject a structured summary. You capture the conclusions that are still valid (analysis output, decisions, diffs) as application state and pass them into the new session's prompt. The model then works from accurate, compact context instead of a long history full of outdated tool output. Length matters too, because context behaves like a finite resource. Anthropic's context-engineering guidance notes that recall gets worse as the token count grows.
| Strategy | Use when | What the model starts with |
|---|---|---|
| Resume (resume / --resume) | Prior context is mostly valid; changes are few and known | Full prior history, old tool results included, plus your new prompt |
| Fork (resume + fork_session=True) | You want divergent explorations from one valid baseline | A copy of the baseline history under a new session ID |
| Fresh session with injected summary | Prior tool results are stale or the history is long | Only the results you captured and passed into the prompt |
Claude Code also has a middle option. When you resume a long session, a dialog can offer Resume from summary. That runs /compact immediately and replaces the history with a summary, your most recent exchanges, and up to five recently read files. The alternative, Resume full session as-is, loads everything unchanged.
Checkpoint 4 of 5· Exam question
An architect ran a two-hour investigation into a flaky payment-retry bug yesterday afternoon and named the session 'payment-retry-bug' before stopping for the day. This morning, a teammate wants the architect to pick the investigation back up with full context of everything already read and concluded, without hunting through the interactive session picker. Which approach satisfies this most directly?
Correct answer: A — Run claude --resume payment-retry-bug to resume that exact named session directly
- A. Correct. Passing the session name to --resume resolves an exact match directly, restoring the full prior conversation without going through the picker.
- B. Incorrect for this case: --continue always targets the most recently active session in the directory, which may not be 'payment-retry-bug' if other sessions ran afterward.
- C. Incorrect: scrollback is not the same as the persisted conversation history, and this discards the tool call and file-read context that resumption preserves automatically.
- D. Incorrect: this works but requires manual searching; naming let the architect skip the picker entirely by resuming the exact name.
Checkpoint 5 of 5· Check yourself
A week-old investigation session contains dozens of file reads, and most of those files have since been rewritten. The conclusions about the architecture still hold. What is the most reliable way to continue?
When most tool results are stale, resuming or forking carries them forward. A fresh session seeded with the captured conclusions keeps the reasoning and leaves the outdated reads behind.
“Capture the results you need (analysis output, decisions, file diffs) as application state”Source: code.claude.com
Exam traps
Each one states something that sounds right. Open it to see what is actually true.
1.Work done in a forked session also changes the original session's history.Why is that wrong?
A fork is a separate session with its own ID. The original stays as it was and can be resumed on its own thread.
2.Resuming is always the safest choice because it keeps every earlier tool result.Why is that wrong?
Keeping every tool result is the problem once files have changed. A fresh session seeded with the results you captured avoids carrying stale reads forward.
Covered in Resume, resume from summary, or start fresh
Practise it for real
Run one analysis, then compare two follow-up approaches from it using the Agent SDK, without either one affecting the baseline.
1.Run query() with allowed_tools=["Read", "Glob", "Grep"] and the prompt "Analyze the auth module and suggest improvements", saving message.session_id from the ResultMessage.
Why: You need the baseline session's ID before you can resume it or fork from it.
You should see: The run prints a session ID along with the analysis result.
2.Run a second query() with resume=session_id, fork_session=True, max_turns=5 and the prompt "Instead of JWT, outline how OAuth2 would work for the auth module", saving its session_id as forked_id.
Why: fork_session starts a new session from the baseline history instead of adding to it.
You should see: forked_id is different from session_id.
3.Run a third query() with only resume=session_id and the prompt "Continue with the JWT approach".
Why: This shows the original thread is still intact after the fork.
You should see: The agent continues the JWT plan and makes no mention of the OAuth2 outline.
Stuck? Get a nudge
If both runs come back with the same session ID, check that fork_session=True was set on the second call.
Sources
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.https://code.claude.com/docs/en/agent-sdk/sessionsOfficial docs
“Original session is untouched; resuming it continues the JWT thread”
↩︎ fork_session: branching from a shared baseline“The agent already analyzed something; now you want it to act on that analysis without re-reading files.”
↩︎ Stale tool results after code changes“Capture the results you need (analysis output, decisions, file diffs) as application state”
↩︎ Resume, resume from summary, or start fresh“Original session is untouched; resuming it continues the JWT thread”
↩︎ Exam trap 1“Capture the results you need (analysis output, decisions, file diffs) as application state”
↩︎ Exam trap 2 - 2.https://code.claude.com/docs/en/agent-sdk/pythonOfficial docs
“fork_session: bool = False”
↩︎ fork_session: branching from a shared baseline - 3.
“into a new file with freshly remapped message IDs. The original stays untouched.”
↩︎ fork_session: branching from a shared baseline“pass up_to_message_id. The fork will contain the source transcript up to and including that message.”
↩︎ fork_session: branching from a shared baseline - 4.https://code.claude.com/docs/en/sessionsOfficial docs
“the full history, including tool calls and results”
↩︎ Stale tool results after code changes“replaces the history with the summary, your most recent exchanges, and up to five recently read files”
↩︎ Resume, resume from summary, or start fresh - 5.
“Context, therefore, must be treated as a finite resource with diminishing marginal returns.”
↩︎ Resume, resume from summary, or start fresh