Lesson 61.2 · 2 of 4 · 4 min read · Reviewed 24 Nov 2026
Forking with fork_session
By the end, you can
- Fork a session to try alternatives from the same starting point.
- Know how forking differs from resuming.
ResumingSession resumption: Continuing an earlier session by reloading its saved history or state. continues a session. Forking copies it into a new one.
analysis (session A)
/ \
fork -> session B fork -> session C
approach 1 approach 2
Why fork
- Try alternatives from the same starting knowledge, such as two refactoring strategies or two drafts.
- Protect the original: experiments don’t change session A.
- Parallel work: run branches at the same time, then compare.
How
- Agent SDKClaude Agent SDK: Anthropic’s library for building agents on the same loop that powers Claude Code, with tools, hooks, subagents and MCP.: resume a session id with
forkSession: true(TypeScript) orfork_session=True(Python). You get a new session id; the original stays as it was. - Claude CodeClaude Code: Anthropic’s agentic coding tool that works in your terminal, IDE and CI, reading and editing code and running commands.:
--resume <id> --fork-session.
Costs
Each branch carries the full history, so tokens add up. CompactCompaction: Summarising older conversation history so a long session fits in the context window while keeping what matters. or summarise before forking if the history is long.
Key points
- Forking creates a new session that starts with a copy of an existing session's history.
- The original session is left unchanged, so you can compare approaches side by side.
- In the Agent SDK set forkSession (fork_session in Python) when resuming; Claude Code has --fork-session.
Fork a session into two branches
Fork one analysed session into two independent branches and prove the original is left unchanged.
You will have: A fork() function plus output showing A, B and C message counts and that no branch leaked into another.
Steps
- Run the starter: the counts show every branch writing into the same list.
- Change fork() to make a deep copy of the history under a new id.
- Refuse to fork onto an id that already exists.
- Print a rough token count to see how each fork carries the full history.
Done when
- The output shows A: 2 B: 3 C: 3.
- 'original unchanged: True' and 'branches mixed: False' are printed.
Open in the Playground No API key needed.
Starter code (Python)
import copy
# Fork a session: two branches share the analysis but must not mix.
SESSIONS = {"A": [
{"role": "user", "content": "Analyse the payments module."},
{"role": "assistant", "content": "It has 3 services and one shared retry helper."},
]}
def resume(session_id, prompt):
SESSIONS[session_id].append({"role": "user", "content": prompt})
return session_id
def fork(session_id, new_id):
# TODO: create SESSIONS[new_id] as an independent copy of SESSIONS[session_id]
# TODO: make sure changing the fork never changes the original
SESSIONS[new_id] = SESSIONS[session_id] # bug: this shares one list
return new_id
before = len(SESSIONS["A"])
b = fork("A", "B"); resume(b, "Approach 1: wrap the helper in an adapter.")
c = fork("A", "C"); resume(c, "Approach 2: rewrite the helper.")
print("A:", len(SESSIONS["A"]), "B:", len(SESSIONS["B"]), "C:", len(SESSIONS["C"]))
print("original unchanged:", len(SESSIONS["A"]) == before)
Show a solution
import copy
SESSIONS = {"A": [
{"role": "user", "content": "Analyse the payments module."},
{"role": "assistant", "content": "It has 3 services and one shared retry helper."},
]}
def resume(session_id, prompt):
SESSIONS[session_id].append({"role": "user", "content": prompt})
return session_id
def fork(session_id, new_id):
if new_id in SESSIONS:
raise ValueError("fork id already used")
SESSIONS[new_id] = copy.deepcopy(SESSIONS[session_id])
return new_id
before = len(SESSIONS["A"])
b = fork("A", "B"); resume(b, "Approach 1: wrap the helper in an adapter.")
c = fork("A", "C"); resume(c, "Approach 2: rewrite the helper.")
print("A:", len(SESSIONS["A"]), "B:", len(SESSIONS["B"]), "C:", len(SESSIONS["C"]))
print("original unchanged:", len(SESSIONS["A"]) == before)
print("branches mixed:", any("rewrite" in m["content"] for m in SESSIONS["B"]))
tokens = sum(len(m["content"]) // 4 for s in SESSIONS.values() for m in s)
print("rough tokens across all sessions:", tokens)