Phase 7 / Ep 35: Session In-depth Analysis —— Lifecycle of Dialogue
🎯 Learning Objective: Understand the complete lifecycle of a Session and context window management.
1. What is a Session?
A Session = a complete record of a continuous conversation. From your first message until the end of the conversation, all content is stored within a Session.
2. Session Lifecycle
stateDiagram-v2
[*] --> Active: User initiates conversation
Active --> Active: Ongoing conversation
Active --> Compacting: Context window nearing full
Compacting --> Active: Compaction complete\nCore info retained
Active --> Idle: User stops conversation\n(over 30 minutes)
Idle --> Active: User returns to continue
Idle --> Archived: Over 24 hours\nAutomatically archived
Archived --> [*]3. Context Window Management
LLMs have context window limits (e.g., Claude 200K Token). As the conversation grows longer, the Session automatically triggers Compaction:
graph LR
A["Original Conversation\n1000 messages\n180K Token"] -->|"Compaction"| B["Retained:\nLast 50 original messages\n+ Historical summary 5K Token\nTotal 55K Token"]Compaction Strategies
| Strategy | Description |
|---|---|
| Retain Recent | Last N messages kept as-is |
| Summarize History | Older messages generate a summary |
| Retain Key | Messages with tool calls are prioritized |
| Discard Chit-chat | Pure conversational messages are prioritized for discarding |
4. Session Storage Format
sessions.json (Index)
[
{
"id": "session-abc123",
"startedAt": "2026-04-06T08:00:00Z",
"lastMessageAt": "2026-04-06T10:30:00Z",
"status": "archived",
"messageCount": 47,
"summary": "Discussed the architectural design of the OpenClaw tutorial"
}
]
session-xxx.jsonl (Full Record)
One message per line, JSON Lines format:
{"role":"user","content":"Check the weather for me","timestamp":"2026-04-06T08:00:00Z"}
{"role":"assistant","content":"Beijing is sunny today, 28°C","timestamp":"2026-04-06T08:00:03Z","tools_used":["web-search"]}
5. Management Commands
openclaw sessions list # List all Sessions
openclaw sessions show <id> # View details
openclaw sessions clear # Clear current active Session
openclaw sessions archive <id> # Manually archive
openclaw sessions compact # Manually trigger compaction
openclaw sessions export <id> > out.json # Export
Next Up: Ep 36, Memory System—MEMORY.md and Hierarchical Memory Architecture. How Agents remember things across Sessions.