Skip to main content

AgentSession

The AgentSession manages conversation persistence. Every agent has a session that stores conversation history and metadata on disk automatically.

Creating Sessions

Session Structure

Each session creates a directory:

Metadata

Session Metadata

Accessing Metadata

Custom Metadata

Store application-specific data:
When an agent is running, always update metadata through the AgentHandle to avoid race conditions. The handle provides thread-safe access to the session.

Conversation History

Message Storage

Messages are stored as JSON:

Reading History

Message Types

Messages can contain various content types:

Write-Through Persistence

Automatic Saving

Messages are written to disk immediately after each turn: This ensures conversation state survives crashes.

Manual Saving

Force a save (rarely needed):
Useful if you’ve modified metadata directly.

Listing Sessions

List All Sessions

List Top-Level Only

Filter out subagent sessions:

List with Metadata

More efficient when you need metadata:

Loading Sessions

Load Existing Session

Check Existence

Parent-Child Relationships

Subagent Sessions

When agents spawn subagents, sessions are linked:

Use Cases

Parent-child relationships enable:
  • Hierarchical agents: Parent delegates to specialized children
  • Conversation trees: Track conversation branches
  • Cleanup: Delete parent and all children together
  • Tracing: Follow execution across agents

Conversation Naming

Automatic Naming

StandardAgent automatically generates conversation names after the first turn:
Disable automatic naming:

Manual Naming

Set conversation name explicitly:

Custom Naming LLM

Use a cheaper/faster model for naming:

Session Management

Deleting Sessions

Deleting a parent session does NOT automatically delete child sessions. Delete children first if needed.

Updating Metadata (Running Agents)

When an agent is running, use the handle:

Updating Metadata (Stopped Agents)

When agent is not running, direct access is safe:

Custom Storage Location

Change Storage Directory

Storage Interface

Implement custom storage backends:
Could implement:
  • Database storage (PostgreSQL, SQLite)
  • Cloud storage (S3, GCS)
  • In-memory storage (testing)
  • Encrypted storage

Best Practices

1. Unique Session IDs

2. Meaningful Names

3. Clean Up Old Sessions

4. Backup Sessions

5. Monitor Disk Usage

Advanced Patterns

Session Migrations

Session Templates

Session Cloning

Next Steps

Message Flow

Understand input/output communication patterns

Streaming & History

Critical dual-channel architecture

AgentSession API

Complete session API reference

Custom Storage

Implement custom storage backends