Lesson 61.1 · 1 of 4 · 4 min read · Reviewed 24 Nov 2026
Resuming a session
By the end, you can
- Resume a session to continue earlier work.
- Know when resuming helps and when it hurts.
A session is a conversation with its history: messages, tool calls and results. Saving and resumingSession resumption: Continuing an earlier session by reloading its saved history or state. it lets work span hours or days.
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.: each run reports a
session_id. Pass it back with theresumeoption to continue. - Claude CodeClaude Code: Anthropic’s agentic coding tool that works in your terminal, IDE and CI, reading and editing code and running commands.:
claude --continueresumes the most recent session;claude --resumelets you pick one, or takes an id.
When to resume
- The task isn’t finished and the earlier findings still matter.
- A person answered a question the agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. asked.
- A long job was interrupted.
When to start fresh
- The task is new or unrelated.
- The old session is full of stale or wrong assumptions.
- In CIContinuous integration (CI): Automatically building and testing code on every change, usually in a hosted pipeline., where each job should be isolated.
If the history is long, consider starting fresh with a short summary or the notes file instead of resuming everything.
Key points
- Resuming loads a saved session's history so the agent continues where it stopped.
- In the Agent SDK pass resume with a session id; in Claude Code use --continue or --resume.
- Resume when the earlier context helps; start fresh when it's stale or unrelated.
Resume a saved agent session
Continue an earlier agent session by reloading its saved history instead of starting from zero.
You will have: A run() function with a resume option, and output showing the second day builds on the first.
Steps
- Run the starter and see that day 2 only knows one fact.
- Make run() load a copy of the stored messages when resume is a known session id.
- Raise a clear error for an unknown session id instead of silently starting fresh.
- Start one more run without resume and check it does not see the bug facts.
Done when
- Day 2 prints 'I know 2 fact(s)' and reuses session s-001.
- The fresh run gets a new id and knows only 1 fact.
- Resuming an unknown id raises an error.
Open in the Playground No API key needed.
Starter code (Python)
# Resume a session: a toy agent whose memory is a session store.
SESSIONS = {} # session_id -> list of messages
_next = [0]
def new_id():
_next[0] += 1
return f"s-{_next[0]:03d}"
def fake_model(history):
# The "model" can only use what is in the history it is given.
facts = [m["content"] for m in history if m["role"] == "user" and m["content"].startswith("Fact:")]
return f"I know {len(facts)} fact(s) about this bug."
def run(prompt, resume=None):
# TODO: if resume is a known session id, start from a copy of its messages
# TODO: otherwise create a new session id
session_id = new_id()
history = []
history.append({"role": "user", "content": prompt})
reply = fake_model(history)
history.append({"role": "assistant", "content": reply})
SESSIONS[session_id] = history
return session_id, reply
sid, r = run("Fact: the crash happens on empty carts.")
print("day 1:", sid, r)
sid2, r2 = run("Fact: it started after release 4.2.", resume=sid)
print("day 2:", sid2, r2)
Show a solution
SESSIONS = {}
_next = [0]
def new_id():
_next[0] += 1
return f"s-{_next[0]:03d}"
def fake_model(history):
facts = [m["content"] for m in history if m["role"] == "user" and m["content"].startswith("Fact:")]
return f"I know {len(facts)} fact(s) about this bug."
def run(prompt, resume=None):
if resume is not None:
if resume not in SESSIONS:
raise KeyError(f"unknown session {resume}")
session_id = resume
history = list(SESSIONS[resume])
else:
session_id = new_id()
history = []
history.append({"role": "user", "content": prompt})
reply = fake_model(history)
history.append({"role": "assistant", "content": reply})
SESSIONS[session_id] = history
return session_id, reply
sid, r = run("Fact: the crash happens on empty carts.")
print("day 1:", sid, r)
sid2, r2 = run("Fact: it started after release 4.2.", resume=sid)
print("day 2:", sid2, r2)
print(f"resumed {sid2} with {len(SESSIONS[sid2])} messages")
fresh, r3 = run("Fact: unrelated question about invoices.")
print("fresh:", fresh, r3)