Skip to content

Session Management

A Session is a container for user-AI conversations. Each session independently manages its own message history and execution state. Users can freely create, switch, and rename sessions, and can also fork a new session based on any historical message.

StateMeaningAvailable Actions
🟢 IdleIdle, ready to send new messagesSend message, Close
🔵 ActiveTurn is executingStop turn, Close
⚫ ClosedClosed (archived), can view history or reopenView history, Fork, Reopen
OperationDescription
CreateAuto-created when sending the first message — no dedicated RPC; can also click the new button to create an empty session (purely frontend-local)
ListReverse chronological by update time, cursor-paginated (returns all active sessions by default)
SwitchPure frontend behavior, no RPC — loads message history from local IndexedDB
RenameModify the session title
ForkCopy history based on a specific message, replace that message’s content, and trigger a new AI flow
CloseMark as Closed and stop the currently executing turn
OpenRestore a Closed session back to Idle, allowing new messages (idempotent operation)

Forking is a signature feature of RTC Agent — unsatisfied with a previous answer? Edit the question and regenerate.

Fork process:

StepDescription
1. Select messageClick on any historical message
2. Click forkTrigger the fork operation
3. Copy historyCopy all messages before the selected message
4. Replace contentReplace the selected message with new content
5. Trigger AIAI reprocesses based on the new content
ConstraintDescription
Single Turn ExecutionOnly one turn can execute at a time per session
Ownership IsolationUsers can only operate on their own sessions
Closed = FrozenClosed sessions cannot accept new messages

RTC Agent adopts a Local-First data strategy — all operations are written locally first, then asynchronously synced to the server.

AdvantageDescription
⚡ Instant ResponseReturns after writing locally, no network wait
📴 Offline CapableOperations work offline, auto-sync on reconnection
🔄 Auto RetryFailed syncs marked as failed, manual retry supported

The server pushes session changes via WebSocket, and the frontend syncs automatically:

Pushed change types:

EventDescription
CreatedA new session was created (e.g., auto-created)
UpdatedSession title modified, state changed
ClosedSession was closed
OpenedA closed session was reopened

RTC Agent provides two session navigation methods: Session Tree (sidebar) and Session Tabs (tab bar), each suited for different use cases.

The session tree is a VS Code-style hierarchical structure, ideal for managing large numbers of sessions and viewing session relationships.

Components:

  • <rtc-session-tree> — Session tree container with refresh, new, and delete buttons
  • <rtc-session-tree-item> — Individual tree node with recursive child rendering

Features:

  • 📂 Hierarchical Structure: Supports parent-child session relationships (linked via rootClientSessionId)
  • 🔽 Expand/Collapse: Click arrow icons to expand or collapse child sessions
  • ⌨️ Keyboard Navigation: Full ARIA role="tree" support (arrow keys, Home, End, Enter)
  • 🎯 Selection Highlight: Currently selected session is highlighted

Events:

EventDescription
rtc-session-tree-selectUser clicks or keyboard-activates a session node
rtc-session-tree-toggleUser clicks expand/collapse arrow
rtc-session-tree-newUser clicks new session button
rtc-session-delete-requestedUser clicks delete button

⚠️ Breaking Change: The new session creation event has been renamed from rtc-new-session to rtc-session-tree-new for better naming consistency.

Session tabs are browser-style tabs for quick switching between sessions.

Components:

  • <rtc-session-tab-bar> — Tab bar container
  • <rtc-session-tab> — Individual tab

Features:

  • 🔄 Quick Switching: Click a tab to switch sessions
  • ❌ Close Tabs: Click the close button on a tab
  • 💾 State Persistence: Tab order and active state saved to localStorage
  • 📝 Unsaved Drafts: Supports temporary session tabs not yet persisted

<rtc-chat-layout> is the new main chat interface layout component, integrating session tree and tab bar:

Attributes:

AttributeTypeDefaultDescription
sessionTreeVisiblebooleantrueWhether to show the left session tree
theme'light' | 'dark' | 'system''system'Theme mode

Usage Example:

<!-- Default: show session tree -->
<rtc-chat-layout></rtc-chat-layout>
<!-- Hide session tree (show tab bar only) -->
<rtc-chat-layout session-tree-visible="false"></rtc-chat-layout>

Session tree and tabs are managed by two independent Controllers that collaborate through events:

  • SessionTreeController: Manages tree structure and expand state
  • SessionTabController: Manages tab state and persistence
  • Collaboration: Event-driven, with root component <rtc-agent> as the central orchestrator

Each Session automatically accumulates LLM token consumption and cost, pushed to the frontend in real-time via session.updated events.

FieldTypeDescription
total_tokensint64Cumulative total token count (all types)
total_input_tokensint64Cumulative pure input tokens (excluding cached read/write)
total_output_tokensint64Cumulative output token count
total_cached_read_tokensint64Cumulative cached read token count
total_cached_write_tokensint64Cumulative cached write token count
total_reasoning_tokensint64Cumulative reasoning token count
total_cost_usdfloat64Cumulative cost in USD
last_token_update_attimeLast token statistics update timestamp
FieldTypeDescription
current_context_tokensint64Actual current context token count (written back by server cumulativeTokenCounter, updated with real value after compression)
estimated_next_round_tokensint64EWMA-based prediction for next round tokens
compression_progressfloat64Compression progress (0-100), computed from current_context_tokens and compression_threshold
compression_thresholdint64Compression trigger threshold (contextTokensLimit - autoCompactBufferTokens)
rounds_until_compressionintRounds until compression (-1 means threshold exceeded)

💡 current_context_tokens replaces the old total_tokens as the baseline for compression progress display. After compression completes, this value is written back with the actual post-compression context size. For older sessions where this field is not yet initialized, the frontend falls back to total_tokens.

The system supports multi-dimensional cost calculation, automatically computed based on model pricing configuration:

DimensionDescription
Input TokensCost for non-cached pure input tokens
Output TokensCost for output tokens
Cached ReadCost for cache-hit tokens (typically lower)
Cached WriteCost for cache-write tokens
Reasoning TokensCost for reasoning tokens (e.g., thinking)

💡 Token statistics use a throttling mechanism (Throttle) to control the push frequency of session.updated events, avoiding event storms. The frontend displays this data via the rtc-token-usage component.

Users can export the current session’s conversation history as a standalone HTML file for offline reading, archiving, or sharing. Export is triggered via the /export command and runs entirely on the frontend.

FeatureDescription
Pure frontendNo server involvement; HTML file embeds all styles, viewable offline
Configurable optionsMessage limit, include tool calls, include thinking content
Markdown renderingUses marked + highlight.js + DOMPurify, consistent with component rendering
Token statisticsExported file includes input/output/cache read/cache write tokens and total cost
Smart file namingFormat: rtc-{title}-{YYYY-MM-DD}.html, illegal characters handled automatically

See Command System — /export for detailed export options and rules.

  • Messaging — Learn about message interactions within sessions
  • Remote Tool Calling — Learn about tool calling within turns
  • Command System — Learn about session-level commands (/compact, /export, /loop, /goal)