Skip to content

Session Management

Sessions are the fundamental unit of message context in OpenClaw. Every conversation takes place within a session, and sessions determine the AI's memory scope, tool permissions, and behavioral configuration.

Session Model

Session Types

TypeDescriptionTool Permissions
main1:1 direct message sessionExecute tools directly on the host machine
groupGroup/channel sessionOptional Docker sandbox isolation
channelPer-channel isolationInherits channel configuration

Session Key

Source: src/gateway/session-utils.ts

Each session is uniquely identified by a sessionKey, derived from the channel type and account ID:

typescript
// src/config/sessions.ts (simplified)
function deriveSessionKey(channel: string, accountId: string): string {
  return `${channel}:${normalizeAccountId(accountId)}`;
}

function normalizeAccountId(accountId: string): string {
  // Normalize E164 phone numbers, WhatsApp JIDs, etc.
}

Session Storage

Source: src/config/sessions.ts

Session state is persisted to the file system:

typescript
// src/config/sessions.ts (simplified)
function resolveStorePath(sessionKey: string): string {
  // ~/.openclaw/sessions/{sessionKey}/
}

function loadSessionStore(sessionKey: string): SessionStore {
  // Load from file system
}

function saveSessionStore(sessionKey: string, store: SessionStore): void {
  // Persist to file system
}

Storage location: ~/.openclaw/sessions/{sessionKey}/

Session Patching

Source: src/gateway/sessions-patch.ts

Session configuration can be dynamically modified through the sessions.patch method:

Patchable properties include:

PropertyTypeDescription
modelstringAI model selection
thinkingLevelstringThinking level (on/off/stream)
sandboxModebooleanSandbox mode
queueModestringMessage queue mode
activationModestringActivation method (mention/always)

Session Resolution

Source: src/gateway/sessions-resolve.ts

When a message arrives, the Gateway needs to resolve the target session:

typescript
// src/gateway/sessions-resolve.ts (simplified)
function resolveSession(
  channel: string,
  chatType: "direct" | "group" | "channel",
  accountId: string
): ResolvedSession {
  // 1. Derive session key
  const sessionKey = deriveSessionKey(channel, accountId);

  // 2. Check if session exists
  // 3. Apply channel defaults
  // 4. Merge per-session overrides

  return { sessionKey, config };
}

Activation Modes

Whether the AI responds to a message depends on the activation mode:

ModeDescriptionTypical Use Case
alwaysRespond to all messages1:1 direct messages
mentionRespond only when @mentionedGroup chats
commandRespond only to commandsChannels

File System Session Utilities

Source: src/gateway/session-utils.fs.ts

Session file system operations include transcript management and mirrored persistence:

typescript
// src/gateway/session-utils.fs.ts (simplified)
// Session transcript persistence
// Mirrored transcript management
// Session cleanup and maintenance

Session Type Definitions

Source: src/gateway/session-utils.types.ts

typescript
// src/gateway/session-utils.types.ts (simplified)
interface SessionConfig {
  model?: string;
  thinkingLevel?: "on" | "off" | "stream";
  sandboxMode?: boolean;
  queueMode?: "fifo" | "fanout";
  activationMode?: "always" | "mention" | "command";
  // ...
}

Summary

  • Sessions are uniquely identified by sessionKey, derived from channel + account ID
  • Three session types: main (1:1), group (group-based), channel (channel-level)
  • Session state is persisted to the ~/.openclaw/sessions/ directory
  • sessions.patch enables dynamic configuration updates (model, thinking level, sandbox, etc.)
  • Activation mode controls AI responsiveness: always / mention / command

Next: Configuration System

OpenClaw Source Code Tutorial