Skip to main content

Sessions

Sessions provide conversation context isolation for each sender. Every DM, group chat, or thread gets its own session with persistent message history, model settings, and conversation state.

What is a Session?

A session is a conversation context that includes:
  • Message History: Full conversation transcript
  • Agent State: Current model, settings, and configuration
  • Metadata: Sender info, channel, timestamps
  • Delivery Context: Last known route for replies
  • Usage Tracking: Token counts and model information

Session Keys

Session Key Format

Each session has a unique key that identifies the conversation:
Examples:
Location: src/routing/session-key.ts:142

Session Key Components

Location: src/routing/session-key.ts:90

DM Session Scoping

Direct messages can share or separate sessions:
DM Scope Options:
  • main: All DMs share one session (default)
  • per-peer: Separate session per sender
  • per-channel-peer: Separate per sender+channel
  • per-account-channel-peer: Separate per account+channel+sender
Example with main (shared):
Example with per-peer:
Example with per-channel-peer:
Location: src/routing/session-key.ts:149, src/config/types.base.ts:6 Link identities across platforms to share sessions:
Now Alice’s messages from Telegram, Discord, and WhatsApp all use:
Location: src/routing/session-key.ts:190, src/config/types.base.ts:92

Session Storage

Storage Location

Sessions are stored as JSONL files:
Location: src/config/sessions/paths.ts:1

Session Transcript Format

Each .jsonl file contains one message per line:
Line Format:
Location: src/config/sessions/transcript.ts:1, src/gateway/session-utils.fs.ts:69

Session Metadata

Session metadata is stored in sessions.json:
Key Fields:
  • sessionId: Unique UUID for this session
  • updatedAt: Last message timestamp (ms)
  • label: Display name for UI
  • channel: Originating channel
  • chatType: direct, group, channel, or thread
  • model: Current AI model
  • compactionCount: Number of compactions performed
  • lastChannel, lastTo: Delivery context for replies
Location: src/config/sessions/types.ts:25, src/config/sessions/store.ts:1

Session Lifecycle

Session Creation

Sessions are created automatically on first message:
  1. User sends first message
  2. Router generates session key based on agent, channel, and sender
  3. Gateway creates session with empty history
  4. Agent processes message and generates response
  5. Session saved to disk with metadata
Location: src/config/sessions/store.ts:1

Session Reset

Sessions can be reset manually or automatically: Manual Reset:
Configuration:
Reset Modes:
  • daily: Reset at specific hour each day
  • idle: Reset after N minutes of inactivity
Location: src/config/sessions/reset.ts:1, src/config/types.base.ts:71

Session Compaction

When a session grows too large, OpenClaw compacts it:
  1. Trigger: Session exceeds token limit (e.g., 100k tokens)
  2. Compaction LLM: Summarizes conversation history
  3. Summary Injected: Replaces old messages with summary
  4. Recent Messages Kept: Last N messages preserved
  5. Transcript Updated: Compaction event logged
Compaction Entry:
Location: src/agents/pi-embedded-subscribe.handlers.compaction.ts:1
Compaction preserves conversation continuity while reducing token usage and cost.

Session Configuration

Basic Settings

Location: src/config/types.base.ts:87

Reset Configuration

Location: src/config/types.base.ts:71

Maintenance Configuration

Maintenance Options:
  • mode: warn (log only) or enforce (apply limits)
  • pruneAfter: Remove sessions older than duration (e.g., 30d, 12h)
  • maxEntries: Maximum session count in sessions.json
  • rotateBytes: Rotate sessions.json when size exceeds limit
Location: src/config/types.base.ts:113

Session Pruning

Automatic Pruning

Old sessions are pruned automatically: Default Behavior:
  • Sessions older than 30 days are warned
  • Mode defaults to warn (no automatic deletion)
Enable Enforcement:
Location: src/config/sessions/store.ts:1

Manual Pruning

Location: src/commands/sessions.ts:1

Archival

Before pruning, sessions are archived:
Location: src/gateway/session-utils.fs.ts:1

Session Management

List Sessions

Output:
Location: src/commands/sessions.ts:1

Inspect Session

Location: src/commands/sessions.ts:1

Reset Session

Location: src/commands/sessions.ts:1

Session Persistence

Write Lock

Session writes are protected by a lock to prevent corruption:
Location: src/agents/session-write-lock.ts:4

Atomic Writes

Session updates use atomic write-then-rename:
  1. Write to .tmp file
  2. Verify write succeeded
  3. Rename to final path
Location: src/config/sessions/store.ts:1

Cache Management

Session metadata is cached in memory:
  • TTL: 45 seconds (default)
  • Invalidation: On write
  • Size Limit: 5000 entries
Location: src/config/sessions/store.ts:39

Advanced Features

Thread Sessions

Threaded conversations create sub-sessions:
Location: src/routing/session-key.ts:254

Group History Keys

Group chats use a shared history key:
Location: src/routing/session-key.ts:236

Session Send Policy

Control which sessions can send outbound messages:
Location: src/config/types.base.ts:50

Troubleshooting

Session Not Loading

Session Corruption

High Token Usage

Session files contain full conversation history. Back up regularly and secure access.

Next Steps

Message Routing

Configure multi-agent routing rules

Agent Configuration

Customize agent behavior and models

Channel Setup

Connect messaging platforms

Session Commands

CLI reference for session management