Skip to main content

Overview

Agent Sessions enable persistent, multi-turn conversations with CLI agents. Think of them like HttpSession β€” the session maintains state across prompts so you can have iterative dialogues with an agent instead of fire-and-forget single tasks. Without sessions, each AgentClient.goal().run() call starts a fresh conversation. With sessions, you can:
  • Send follow-up prompts that build on previous context
  • Resume conversations after transport failures
  • Manage session lifecycle with automatic stale cleanup
Available since version 0.10.0. Currently implemented for the Claude agent provider. Other providers will follow.

Core Interfaces

AgentSession

A single persistent conversation. Created via AgentSessionRegistry.create() β€” never instantiated directly.
MethodDescription
getSessionId()Unique session ID, assigned eagerly at creation
getWorkingDirectory()Immutable working directory the session operates in
getStatus()Current lifecycle status (ACTIVE, DEAD, or RESUMED)
prompt(message)Send a follow-up in the same conversation context
resume()Resurrect a DEAD session β€” restores conversation history
fork()Branch the conversation (not yet implemented)
close()Close the session and release resources

AgentSessionRegistry

Factory and lifecycle manager for sessions. Analogous to SessionRepository in Spring Session.
MethodDescription
create(path)Start a new session β€” eagerly connects to CLI and captures session ID
find(id)Look up an existing session
evict(id)Remove and close a session
evictStale(duration)Evict sessions inactive longer than the threshold

AgentSessionStatus

Usage

Create a Registry and Session

Multi-Turn Conversation

Each prompt() call continues the same conversation β€” the agent sees the full history of what it built in earlier turns.

Resuming a Dead Session

If the CLI process dies (crash, timeout, network issue), the session transitions to DEAD. You can resurrect it:

Finding an Existing Session

Session Lifecycle

Spring Integration

Bean Configuration

Stale Session Cleanup

Use @Scheduled to periodically evict inactive sessions and prevent resource leaks:

Startup Health Probe

Use a disposable session to verify CLI availability at startup:

Limitations

fork() is not yet implemented β€” calling it throws UnsupportedOperationException. This will be added in a future release.
  • Claude-only: Sessions are currently only implemented for the Claude agent provider. Other providers will be added as their CLIs support persistent sessions.
  • In-memory registry: ClaudeAgentSessionRegistry stores sessions in a ConcurrentHashMap. Sessions do not survive application restarts β€” use resume() with a persisted session ID if you need cross-restart continuity.
  • One working directory per session: A session is anchored to the working directory specified at creation time. It cannot be changed.
  • No context pruning: Resumed sessions include the full conversation history. You cannot trim earlier turns to reduce token usage.