Omniscio

OmniscioGuidesSessions in the Agent SDK

Sessions in the Claude Agent SDK

A single query() call already takes as many turns as the task needs; sessions are about what happens between calls, after a process restart, or when you want to try a different direction without losing the first. Running one agent at all is the Agent SDK guide.

Last checked against Anthropic's documentation on November 24, 2026. By the Omniscio team. Updated .

What a session is, and what it is not

A session is the conversation history the SDK accumulates while your agent works: your prompt, every tool call the agent made, every tool result, and every response. The SDK writes it to disk automatically. Returning to a session means the agent has the earlier context again — files it already read, analysis it already performed, decisions it already made.

It is a record of the conversation, not of your files. A session does not restore the state of the working tree, so a resumed agent remembers what it changed but the files themselves are whatever they are now. Snapshotting and reverting file changes is file checkpointing, a separate feature.

Which of the three fits your app

Anthropic frames this as a question about your application's shape, not a preference. If the task is one-shot, the answer is nothing extra: one query() call handles it.

What you are buildingWhat to use
Multi-turn chat inside one processClaudeSDKClient in Python; in TypeScript continue: true, which takes the most recent session in the directory, so it holds while that is the session you want
Pick up where you left off after a restartcontinue_conversation=True or continue: true, which resumes the most recent session in the directory with no ID
Return to a specific past sessionCapture the session ID and pass it to resume
Try an alternative without losing the originalFork the session
A stateless task with nothing written to diskpersistSession: false in TypeScript; in Python, CLAUDE_CODE_SKIP_PROMPT_HISTORY in the env option

Continue and resume are the same operation with a different way of finding the session: continue takes the most recent session in the current directory, resume takes the ID you kept. Fork is genuinely different — it creates a new session that starts with a copy of the original's history, and the original stays unchanged. That guarantee covers the conversation, not the disk: a forked agent's file edits are real and visible to any session in the same directory.

Letting the SDK track it for you

In Python, ClaudeSDKClient holds the session for you: every client.query() continues the same session, and client.receive_response() iterates the messages for that query. Use it as an async context manager so connection setup and teardown are handled, or call connect() and disconnect() yourself.

async with ClaudeSDKClient(options=options) as client:
    await client.query("Analyze the auth module")
    async for message in client.receive_response():
        print_response(message)

    await client.query("Now refactor it to use JWT")
    async for message in client.receive_response():
        print_response(message)

The second query has the first one's context without any ID handling. TypeScript has no session-holding client object: pass continue: true on each subsequent query() call and the SDK picks up the most recent session in the current directory.

Capturing the ID, and what happens when a run fails

Resume and fork both need an ID, and the place to read it is the session_id field on the result message, which is present on every result whether the run succeeded or errored. In TypeScript the ID is also available earlier, as a field on the init system message; in Python it is nested inside SystemMessage.data.

There is an asymmetry worth designing around. A single-shot query() raises after it has yielded an error result, so the loop already captured the ID before the exception. A connection or process failure yields no result message at all, so the ID is never set — which is why the capture belongs in a variable you can print, not only in a success branch.

ClaudeAgentOptions(resume=session_id)                    # Python
{ resume: sessionId }                                    # TypeScript
ClaudeAgentOptions(resume=session_id, fork_session=True) # Python, forked copy
{ resume: sessionId, forkSession: true }                 # TypeScript, forked copy

Resume earns its keep in three situations Anthropic names: following up on a completed analysis without re-reading files, recovering from a run that ended at a turn or budget limit by resuming with a higher one, and restoring a conversation after your process restarted.

Resuming on a different machine

Session files are local to the host that created them, so CI workers, ephemeral containers and serverless functions start with nothing to resume. Anthropic gives three answers and says to pick the one that fits, noting only that the third — capturing results as application state — is often more robust than shipping transcript files around.

  • Attach a session store. A sessionStore or session_store adapter mirrors transcripts to your own backend so another host can resume them. Its lookup key derives from the working directory, so resume from a cwd that matches the original run's.
  • Move the session file. Persist ~/.claude/projects/<encoded-cwd>/<session-id>.jsonl from the first run and restore it under ~/.claude/projects/ on the new host before resuming. That is the file lookup, which is separate from the store's key: from v2.1.223 Claude Code searches beyond the current project directory to find an ID, while before that the lookup was scoped to the project directory and its worktrees, and an SDK version bundling an older CLI still behaves that way.
  • Do not rely on resume. Capture the results you need — analysis output, decisions, file diffs — as application state and pass them into a fresh session's prompt. Anthropic's own wording is that this is often more robust than shipping transcript files around.

Both SDKs also expose the pieces needed to build a session picker of your own: listSessions() and getSessionMessages() in TypeScript, list_sessions() and get_session_messages() in Python, plus getSessionInfo(), renameSession() and tagSession() — get_session_info(), rename_session() and tag_session() in Python — for one session at a time.

Where these answers come from

  • Work with sessions, read November 24, 2026: what a session holds, the choose-an-approach table, continue versus resume versus fork, ClaudeSDKClient and continue: true, capturing the ID, and resuming across hosts.
  • File checkpointing: the separate feature that snapshots and reverts the files an agent changed.
  • Session storage: the adapter that lets another host resume a transcript.

Common questions

What is the difference between continue and resume in the SDK?

Both pick up an existing session and add to it, and they differ only in how they find it. Continue takes the most recent session in the current directory, with nothing for you to track. Resume takes a specific session ID you captured, which you need when one process holds several conversations, such as one session per user.

Where do I get the session ID to resume with?

Read the session_id field on the result message, which is present on every result whether the run succeeded or errored. In TypeScript the ID also appears earlier on the init system message. Capture it before the process ends, because a single-shot query() raises after yielding an error result and a connection failure yields no result message at all.

Can I resume a session on a different machine?

Not by default: session files are local to the machine that created them. Either attach a sessionStore or session_store adapter so transcripts are mirrored to your backend, or move the session's jsonl file from ~/.claude/projects on the first host to a directory under ~/.claude/projects on the new one before resuming. Anthropic also suggests capturing the results you need as application state instead, which it calls often more robust.

Does a session store the files the agent changed?

No. A session persists the conversation, not the filesystem, so resuming gives the agent its earlier context rather than restoring the files. To snapshot and revert the files an agent changed, Anthropic points at file checkpointing, which is a separate feature.