Introduction
CLI-based code agents maintain internal session state that includes the conversation history, file context, and previous tool results. Agent-Ctrl exposes this session mechanism through two builder methods —continueSession() and resumeSession() — that work consistently across all supported agents.
Session continuity is valuable when a task spans multiple steps. Rather than starting fresh each time and re-establishing context, you can continue an existing session so the agent remembers what was discussed and what actions were taken previously.
How Sessions Work
Each agent manages sessions differently under the hood, but Agent-Ctrl normalizes the experience:-
Claude Code stores sessions internally and exposes a
session_idin its JSON stream output. Agent-Ctrl extracts this ID from the stream and makes it available viaAgentResponse::sessionId(). -
Codex uses thread-based conversations and returns a thread ID in its response. Agent-Ctrl normalizes this into an
AgentSessionId. - OpenCode maintains named sessions with their own session ID format. Agent-Ctrl extracts and normalizes these as well.
-
Pi maintains sessions with a session ID. It supports ephemeral mode (no session saved) and custom session directories. Agent-Ctrl normalizes the session ID into an
AgentSessionId. -
Gemini supports session resume by a session ID or the special value
'latest'for the most recent session. Agent-Ctrl normalizes these into anAgentSessionId.
Continuing the Most Recent Session
The simplest form of session continuity iscontinueSession(), which tells the agent to pick up where the last session left off. You do not need to know or store the session ID:
Resuming a Specific Session
When you need to resume a particular session — for example, after a delay, from a different process, or to branch from a specific point — useresumeSession() with the session ID:
resumeSession() method accepts a plain string. The AgentSessionId value object returned by sessionId() implements __toString(), so you can cast it directly.
Reading the Session ID
AgentResponse::sessionId() returns an AgentSessionId value object or null. A null value means the agent did not expose a session identifier in its output — this can happen if the agent’s CLI version does not support sessions or if the execution failed before session data was emitted.
AgentSessionId is an opaque value object (extending OpaqueExternalId) that wraps the raw string identifier. It provides type safety and prevents accidental mixing of session IDs with other string values.
Session Management by Agent
Each agent’s builder exposes the same two methods, but the underlying behavior varies:Claude Code
--continue or --resume <session_id> to the claude CLI. Session IDs are extracted from the session_id field in the JSON stream output.
Codex
OpenCode
Pi
continueSession(), resumeSession(), ephemeral() (no session saved), and withSessionDir() (custom session storage directory).
Gemini
continueSession() to resuming the 'latest' session internally. resumeSession() accepts a session ID or index.
Important Considerations
Session IDs are agent-specific. Do not attempt to resume a Claude Code session with the Codex bridge, or vice versa. Each agent’s session format is incompatible with the others. Session availability is not guaranteed. Some agent versions, configurations, or error scenarios may not produce a session ID. Always check fornull before storing or reusing a session ID.
Sessions persist on the agent’s side. Agent-Ctrl does not store or manage session state — it only passes session identifiers to the CLI. The actual session data (conversation history, file context, etc.) is managed by the agent’s own storage system.
continueSession() and resumeSession() are mutually exclusive in intent. If you call both on the same builder, the behavior depends on the agent’s CLI — typically, the explicit session ID from resumeSession() takes precedence. For clarity, use only one per execution.