Skip to content

Feature: Per-agent compaction configuration (thresholds, mode, reserveTokens) #57174

Description

@1052326311

Summary

Currently agents.defaults.compaction applies uniformly to all agents. In multi-agent deployments, agents have fundamentally different context-retention needs. An orchestrator agent needs long memory; a specialist agent should compact aggressively after task completion.

The Problem

We run 17 agents in a 4-tier hierarchy:

Tier Role Context Need Ideal Compaction
King (orchestrator) Dispatch, audit, decision Long — needs full dispatch/audit history 70% threshold, preserve decisions
Scene Leads (4) Project management, review Medium — needs project context + member interaction 65% threshold, preserve milestones
Specialists (12) Execute specific tasks Short — finish task, release context fast 55% threshold, aggressive compaction

But OpenClaw only supports:

{
  "agents": {
    "defaults": {
      "compaction": {
        "mode": "default",      // Same for ALL agents
        "reserveTokens": 12000  // Same for ALL agents
      }
    }
  }
}

Result

  • Orchestrator compacts too aggressively → loses dispatch history → forgets what it assigned to whom
  • Specialist agents compact too slowly → waste context window on completed tasks → eventually hit token overflow

Proposed Solution

Per-agent compaction override

{
  "agents": {
    "defaults": {
      "compaction": {
        "mode": "default",
        "reserveTokens": 12000
      }
    },
    "entries": {
      "main": {
        "compaction": {
          "mode": "safeguard",           // More conservative for King
          "reserveTokens": 20000,
          "thresholdPercent": 70          // Only compact at 70% full
        }
      },
      "dev-lead": {
        "compaction": {
          "thresholdPercent": 65
        }
      },
      "dev-frontend": {
        "compaction": {
          "mode": "default",
          "reserveTokens": 8000,
          "thresholdPercent": 55          // Compact early and often
        }
      }
    }
  }
}

Why This Matters

  1. Different agents have different memory needs — This is fundamental to any multi-agent system
  2. No workaround exists — We tried building external compaction via session-guardian skill, but compaction is a core engine feature; external tools can only trigger it, not configure it
  3. Prevents the fix: add @lid format support and allowFrom wildcard handling #1 cause of "agent amnesia" — Wrong compaction settings = lost context = repeated work = frustrated users
  4. Small implementation surface — The compaction config already exists at agents.defaults; extending it to agents.entries.<name>.compaction follows the existing override pattern

Our Workaround (Published on ClawHub)

We built session-guardian with a "smart compaction" layer:

  1. First-time install: LLM analyzes each agent's role → generates compaction-strategies.json
  2. Cron runs auto-compact.sh hourly → applies per-agent thresholds externally
  3. Zero ongoing token cost (config generated once, executed by scripts)

This works but is inherently limited — external tools can't control the actual compaction algorithm, only monitor and react.

Validation

We tested all 17 agents with personalized strategies. Results:

  • Token usage reduced ~30-50% across the fleet
  • Orchestrator (King) retained dispatch history through compaction
  • Specialists compacted faster, improving response speed
  • But: still can't control the actual compaction behavior, only the triggering threshold

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions