Skip to content

[Feature] Automatic session compaction and context length management #31787

@qiuyuemartin-max

Description

@qiuyuemartin-max

Problem

Long-running agent sessions accumulate too many messages, leading to:

  • HTTP 400: invalid_request_error: context is too long errors
  • Model API failures
  • Manual intervention required to clear sessions
  • No automatic cleanup mechanism

Current Behavior

  • Sessions grow indefinitely
  • compaction.mode: "safeguard" is too conservative
  • No built-in way to reset sessions without deleting files
  • Token usage is not visible to users

Impact

This affects:

  • Long-running group chats
  • Personal assistants with continuous conversation
  • Multi-agent deployments with high message volume

Users experience unexpected failures and must manually delete session files.

Proposed Improvements

1. Enhanced Auto-Compaction

{
  "agents": {
    "defaults": {
      "compaction": {
        "mode": "auto",
        "strategy": "sliding-window",
        "maxTokens": 150000,
        "targetTokens": 100000,
        "preserveRecent": 10,
        "preserveTools": true,
        "summarizeOlder": true
      }
    }
  }
}

Features:

  • Automatically compact when approaching token limit
  • Preserve recent messages for context
  • Keep tool call history for continuity
  • Optionally summarize older conversations

2. Session Reset Command

Add built-in /reset command to clear session history:

{
  "commands": {
    "native": "auto",
    "custom": {
      "/reset": {
        "description": "Clear session history",
        "action": "reset-session",
        "confirmRequired": false
      }
    }
  }
}

User can type /reset in conversation to start fresh without deleting files manually.

3. Automatic Session Archival

  • Archive sessions older than N days
  • Compress old sessions to reduce disk usage (.jsonl.gz)
  • Option to auto-delete very old sessions
{
  "agents": {
    "defaults": {
      "sessions": {
        "archival": {
          "enabled": true,
          "archiveAfterDays": 7,
          "deleteAfterDays": 30,
          "compress": true
        }
      }
    }
  }
}

4. Context Length Warning and Visibility

  • Warn user when session is approaching token limit (e.g., at 80%)
  • Show current token usage in session status
  • Suggest compaction or reset proactively

Example output:

⚠️ Session is using 160K/200K tokens (80%). Consider /reset to free up context.

Use Cases

  1. Group chat agent: Conversations can span days/weeks
  2. Personal assistant: Continuous daily interactions
  3. Customer support: Long support threads
  4. Development agent: Extended debugging sessions

Benefits

  • Prevents "context too long" errors automatically
  • Reduces manual maintenance burden
  • Improves system reliability
  • Better user experience with proactive warnings

Additional Context

Current workaround requires users to:

rm -rf ~/.openclaw/agents/*/sessions/*.jsonl
echo '{}' > ~/.openclaw/agents/*/sessions/sessions.json
openclaw gateway restart

This is not user-friendly and loses all conversation history.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions