Overview
Agent Sessions enable persistent, multi-turn conversations with CLI agents. Think of them likeHttpSession β 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 viaAgentSessionRegistry.create() β never instantiated directly.
AgentSessionRegistry
Factory and lifecycle manager for sessions. Analogous toSessionRepository in Spring Session.
AgentSessionStatus
Usage
Create a Registry and Session
Multi-Turn Conversation
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 toDEAD. 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
- 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:
ClaudeAgentSessionRegistrystores sessions in aConcurrentHashMap. Sessions do not survive application restarts β useresume()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.
Related
- Agent Client Overview β high-level project documentation
- Claude Agent SDK Tutorial: Session Resume β lower-level SDK session resume
- Claude Agent SDK Tutorial: Session Fork β lower-level SDK session fork