Skip to content

Memory flush softThresholdTokens doesn't scale with context window size #17034

@Moddy14

Description

@Moddy14

Bug Description

When switching between models with different context window sizes (e.g., 200K → 1M tokens or vice versa), the memoryFlush.softThresholdTokens value (default: 4000) becomes ineffective because it's an absolute value rather than relative to the context window.

Steps to Reproduce

  1. Start a session with a model that has a ~200K context window (e.g., Claude Sonnet)
  2. Switch to a model with a 1M context window (e.g., Claude Opus)
  3. Have a long conversation until auto-compaction triggers
  4. Observe that memoryFlushCompactionCount is never set — the memory flush never fires

Expected Behavior

Memory flush should trigger before compaction regardless of context window size, saving durable memories to disk before context is compacted.

Actual Behavior

With default softThresholdTokens: 4000:

  • 200K context: flush threshold = 200K - 20K - 4K = 176K ✅ (works fine)
  • 1M context: flush threshold = 1M - 20K - 4K = 976K ❌ (never reached)

Compaction is triggered reactively by API context overflow (e.g., a large tool result pushing past the limit), which typically happens around ~170K tokens. The flush threshold at 976K is never reached.

This also breaks in the reverse direction: switching from a large context model back to a smaller one doesn't recalibrate the threshold.

Root Cause

In src/auto-reply/reply/memory-flush.ts, shouldRunMemoryFlush() computes:

threshold = contextWindowTokens - reserveTokensFloor - softThresholdTokens

This threshold is absolute. The softThresholdTokens default of 4000 was presumably tuned for ~200K context windows and doesn't scale.

Suggested Fix

Consider one or more of:

  1. Make softThresholdTokens a percentage of the context window (e.g., softThresholdRatio: 0.15 → flush at 85% of context)
  2. Auto-scale the default based on context window size (e.g., max(4000, contextWindow * 0.15))
  3. Recalculate on model switch — when the active model changes, recompute the effective threshold
  4. Document the interaction — at minimum, warn users that switching to large-context models requires manual softThresholdTokens adjustment

Workaround

Manually set a large softThresholdTokens in openclaw.json:

{
  "agents": {
    "defaults": {
      "compaction": {
        "memoryFlush": {
          "enabled": true,
          "softThresholdTokens": 850000
        }
      }
    }
  }
}

This sets the flush threshold to ~130K tokens, which fires before typical compaction at ~170K.

Environment

  • OpenClaw v2026.2.13
  • Model: Claude Opus 4 (1M context) / Claude Sonnet 4.5 (200K context)
  • Compaction mode: safeguard
  • Session had 10 compactions with memoryFlushCompactionCount never set

Impact

Every user who switches to a large-context model (or starts with one) silently loses the pre-compaction memory flush protection, leading to context loss on compaction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleMarked as stale due to inactivity

    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