Skip to content

1.7 Session State and Resumption

A session is one conversation with an agent that you can return to. Its conversation history holds the agent’s reasoning and every tool result. A tool result is the output of a tool call, such as a file’s contents when the agent read it. Long-running tasks include debugging a complex system, reviewing a large codebase and multi-day research. In these tasks, the history fills with tool results, file analyses and reasoning chains. Session management determines how an agent keeps continuity across work sessions. Task Statement 1.7 covers how to manage that accumulated state: when to continue it, when to branch it and when to start fresh. A tool result is stale when the file it describes has changed since the agent read it.

This lesson uses one made-up example throughout: a shoe repair shop whose owner uses Claude Code to review the shop’s files. The files are the price list, the repair-ticket template and the supplier order sheet. On day one the agent reads the files and finds three pricing errors. Overnight the owner fixes all three by editing those three files. On day two the owner wants to continue.

The Agent SDK and Claude Code give you three approaches to session management. Each does a different job.

Resume continues a specific named session from the point where it stopped. It restores the entire conversation history, including every tool result, analysis and line of reasoning. Use it when the prior context is mostly still valid. The files have not changed significantly since the last session, and you want to continue from exactly where you stopped.

Do not resume when the earlier tool results are stale. This happens when files have changed since the last session, and much of what the history says about them no longer matches the codebase. If most of the context is still valid, resume and tell the agent which files changed. Otherwise the stale context problem occurs: the agent reasons from tool results that no longer reflect the current files. Resume would suit the shop after a night when nobody changed the files. Resume does not suit day two, because the day-one analysis was based on three files, and those three files have changed.

Fork creates an independent branch from a shared analysis baseline. The baseline is the analysis in the session so far, and both branches start from it. After the fork, each branch operates independently. Changes in one branch do not affect the other, and neither branch can see the other’s results.

Current state (checked 6 October 2026): in the SDK you set fork_session beside resume, not instead of it. resume picks the session, and fork_session: true starts a new session from a copy of that history instead of appending to it. The CLI pairs --fork-session with --resume the same way. (Source: the Agent SDK sessions guide. Lesson 1.3 has the full note.)

Use fork when you have completed an initial analysis and want to explore divergent approaches from that shared starting point. For example, after analysing a codebase, you fork to compare two refactoring strategies. Each fork starts from the same initial understanding and then follows a different approach. Do not fork when you simply want to continue the same line of investigation. Fork is for divergence. If you are not comparing alternatives, use resume. After day one, the owner could fork to compare two pricing fixes: a higher resoling price, or a charge for rush jobs. Both branches start from the same reading of the files, and neither branch sees what the other concludes.

Option 3: Fresh start with summary injection

Section titled “Option 3: Fresh start with summary injection”

Start a completely new session and inject a structured summary of the prior session’s findings into its initial context. The new session has no stale tool results. It has only the curated summary that you provide. Use a fresh start when tool results from the prior session are stale because files have changed, APIs have been updated or dependencies have changed. Use a fresh start too when context has degraded over a long session. In that case, too many irrelevant tool results clutter the history. In both cases you need a new session without stale or irrelevant tool results, and you need to keep the prior knowledge. If the prior context is still valid and you want the full conversation history, resume is more efficient. Fresh start is the right choice for the shop on day two.

The stale context problem is the central concept of this task statement. It occurs when an agent resumes a session after code or other files have changed. The agent then reasons from cached tool results that no longer reflect the current state of those files.

When you resume a session, the entire conversation history is restored, including every tool result from the previous session. Suppose the agent read a file in the previous session, and the file has changed since then. The file’s old contents stay in the conversation as a tool result. The model reasons from that stale data alongside any new data, and the two contradict each other. The symptom is contradictory advice about the changed files. The agent recommends changes that were already made, or refers to code that no longer exists.

On day two the owner resumes the session, and the agent recommends fixing the three errors that the owner fixed overnight. The tool results showing the old price list are still in the history. When the agent is asked about the current price list, it gives contradictory answers. Sometimes it quotes the old version from the stale tool result, and sometimes it quotes the new version from a fresh read.

The obvious fix is to resume and ask the agent to re-read the changed files. This fix is better than nothing, but the stale tool results remain in the conversation history. The model may still refer to old information from earlier in the context. The model is most likely to do this for tangential decisions that do not directly involve the changed files.

The correct fix is to start a fresh session with a structured summary of prior findings. Specify which files have changed, so the agent can run a targeted re-analysis of those files. The fresh session has no stale tool results. The injected summary keeps what the prior session learned, without the outdated data. For the shop, the new session reads the current files, verifies the three fixes and gives consistent advice.

Targeted Re-Analysis vs Full Re-Exploration

Section titled “Targeted Re-Analysis vs Full Re-Exploration”

When files have changed, the agent does not need to re-analyse everything. Re-reading 50 files because 3 of them changed is wasted time. Targeted re-analysis means telling the agent which files changed and letting it re-analyse only those. The summary from the prior session covers everything that has not changed.

On day two, the shop’s targeted re-analysis runs in four steps:

  1. Start a fresh session.
  2. Inject a structured summary: “Prior analysis found three pricing errors. All three have been fixed in these 3 files: the price list, the repair-ticket template and the supplier order sheet. Re-analyse only these files to verify the fixes and check for any new issues the changes introduced.”
  3. The agent re-reads and re-analyses only the 3 changed files.
  4. It combines the fresh analysis of the changed files with the preserved summary of the unchanged files.

This is faster than full re-exploration and more reliable than resuming with stale context.

Scenario Best Option Reasoning
Continuing work from yesterday, no files changed --resume Prior context is valid, full history is useful
Comparing two refactoring approaches fork_session Divergent exploration from shared baseline
Resuming after modifying the 3 core files most of the earlier analysis relied on Fresh start + summary Stale tool results for modified files would cause contradictions
Long session with cluttered history Fresh start + summary Degraded context benefits from a clean baseline
Exploring a testing strategy vs a documentation strategy fork_session Two independent approaches from the same analysis
Resuming after dependency updates Fresh start + summary Multiple files may have changed indirectly

The exam tests whether you can select the right option for each scenario. Resume the session when prior context is mostly valid. Start fresh with an injected summary when prior tool results are stale. A typical question describes a session that was resumed after files changed, and an agent that gives contradictory advice. The right answer is a fresh session with a structured summary, plus targeted re-analysis of the changed files. Starting a new session with a structured summary is more reliable than resuming with stale tool results.

Five exam-style multiple-choice questions on Session State and Resumption. Pick an answer, then open the explanation.

A developer resumes a Claude Code session after modifying 3 files in a 50-file codebase. The agent gives contradictory advice — recommending changes that were already made and referencing code that no longer exists. What is the most appropriate approach?

  • A. Start a completely new session and re-analyse the entire 50-file codebase from scratch
  • B. Resume the session and ask the agent to re-read only the 3 modified files to update its understanding
  • C. Use fork_session to create a new branch that can incorporate the file changes independently
  • D. Start a fresh session with a summary of prior findings and the 3 changed files listed
Answer & explanation

Correct: D

  • D is correct because a fresh session with summary injection avoids stale tool results entirely. The injected summary preserves knowledge from the prior session. Specifying the 3 changed files enables targeted re-analysis without re-exploring the entire codebase.
  • A is wrong because re-analysing all 50 files is wasteful when only 3 changed. The prior analysis of the other 47 files is still valid. Targeted re-analysis is more efficient.
  • B is wrong because resuming preserves the stale tool results in conversation history. Even after re-reading the 3 files, the old file contents remain in context and can still influence reasoning for related decisions.
  • C is wrong because fork_session creates a branch from the existing session, which still contains the stale tool results. The fork inherits the same stale context that caused the contradictions. Fork is for divergent exploration, not for resolving stale data.

After analysing a codebase, a developer wants to compare two different refactoring strategies: one focusing on performance and another on readability. Which session management approach is most appropriate?

  • A. Start two completely new sessions, one for each strategy
  • B. Use –resume to continue the same session and try both strategies in sequence on one branch
  • C. Use fork_session to create two independent branches from the shared analysis baseline
  • D. Start a fresh session with summary injection for each strategy
Answer & explanation

Correct: C

  • C is correct because fork_session creates independent branches from a shared analysis baseline. Both branches build on the same initial codebase understanding but explore different directions independently. This is the exact use case for fork.
  • A is wrong because starting completely new sessions loses the initial analysis. Both strategies benefit from the shared baseline understanding of the codebase.
  • B is wrong because trying both strategies in the same session contaminates the exploration. The second strategy attempt is influenced by the first strategy’s analysis and decisions.
  • D is wrong because while summary injection preserves some knowledge, fork_session preserves the complete analysis context and is the purpose-built mechanism for divergent exploration.

A developer completed a codebase analysis yesterday. No files have changed overnight. They want to continue where they left off today. Which approach is best?

  • A. Use --resume <session-name> to continue the named session
  • B. Start a fresh session with summary injection
  • C. Use fork_session to create a branch from yesterday’s analysis
  • D. Start a completely new session and re-analyse the whole module from scratch
Answer & explanation

Correct: A

  • A is correct because no files have changed, so the prior context is still valid. –resume restores the entire conversation history and lets the developer pick up exactly where they stopped. This is the ideal scenario for resume.
  • B is wrong because summary injection loses detail from the full conversation history. When context is still valid, resume preserves more information than a summary can capture.
  • C is wrong because fork is for divergent exploration, not for simple continuation. The developer wants to continue the same line of investigation, not explore alternatives.
  • D is wrong because re-analysing everything is wasteful when the prior analysis is still valid. Resume is more efficient.

Why is resuming a session after file modifications more problematic than simply asking the agent to re-read the changed files?

  • A. The agent cannot re-read files during a resumed session
  • B. Stale tool results stay in history and still drive the agent’s reasoning
  • C. Resumed sessions have a reduced context window that cannot accommodate new file contents
  • D. The agent will refuse to provide analysis if it detects file modifications
Answer & explanation

Correct: B

  • B is correct because resuming preserves the entire conversation history, including old tool results showing the pre-modification file contents. Even after re-reading changed files, the old contents remain in context. The agent may reference stale data from earlier in the conversation, especially for tangential decisions that do not directly involve the modified files.
  • A is wrong because agents can re-read files during resumed sessions. The issue is not capability but context contamination from stale data.
  • C is wrong because resumed sessions do not have reduced context windows. The issue is stale data in the existing history, not context capacity.
  • D is wrong because agents do not detect or refuse analysis based on file modifications. They simply reason from whatever data is in their context, including stale results.

A developer uses fork_session to handle stale context after modifying files. Why is this approach flawed?

  • A. fork_session is not available after file modifications
  • B. fork_session deletes the original session, losing all prior analysis
  • C. The fork inherits the same stale tool results
  • D. fork_session only works for comparing testing strategies, not for general use
Answer & explanation

Correct: C

  • C is correct because fork_session branches from the current session state, including all stale tool results. The fork inherits the same contaminated context that caused the contradictory advice. Fork does not create a clean slate — it creates a copy of the current state. For stale context, a fresh start with summary injection is the correct approach.
  • A is wrong because fork_session is available regardless of file modifications. The issue is not availability but the inheritance of stale data.
  • B is wrong because fork_session does not delete the original session. It creates an independent branch while preserving the original.
  • D is wrong because fork_session can be used for any divergent exploration scenario. However, it is specifically wrong for handling stale context because it inherits the stale data.