openclaw - 💡(How to fix) Fix Feature: Proactive auto-compaction with chained summaries [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#60944Fetched 2026-04-08 02:45:19
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0

Automatically compact session history when approaching context limits, using a summary-chaining strategy that preserves deep context across multiple compactions.

Error Message

  • On prompt_too_long API error: compact and retry (once)
  • If compact + retry fails: surface error to user with context

Root Cause

Automatically compact session history when approaching context limits, using a summary-chaining strategy that preserves deep context across multiple compactions.

Code Example

- Previously compacted context:
  - Scope: 15 earlier messages compacted
  - Tools mentioned: exec, read, edit
  - Key files: src/main.ts, config.yaml
- Newly compacted context:
  - Scope: 8 messages compacted  
  - Recent user requests: [...]
  - Current work: [...]

---

compaction:
  auto: true
  warningThreshold: 0.85
  compactThreshold: 0.90
  preserveRecentMessages: 6
  chainSummaries: true
RAW_BUFFERClick to expand / collapse

Summary

Automatically compact session history when approaching context limits, using a summary-chaining strategy that preserves deep context across multiple compactions.

Motivation

Currently, sessions hit context limits unexpectedly, requiring manual /compact. Claude Code's architecture reveals a robust multi-strategy approach:

  1. Proactive compaction at ~85% context capacity (before it's a problem)
  2. Reactive compaction on prompt_too_long API errors (emergency recovery)
  3. Summary chaining — when compacting a session that was already compacted, the old summary is preserved alongside the new one

The summary chaining is the key insight. Without it, each compaction destroys context from previous compactions. With it, you get layered summaries:

- Previously compacted context:
  - Scope: 15 earlier messages compacted
  - Tools mentioned: exec, read, edit
  - Key files: src/main.ts, config.yaml
- Newly compacted context:
  - Scope: 8 messages compacted  
  - Recent user requests: [...]
  - Current work: [...]

Proposed Behavior

Proactive Compaction

  • Track estimated token usage per session
  • At 85% capacity: log a warning
  • At 90% capacity: auto-trigger compaction
  • Preserve last N messages verbatim (configurable, default 4-6)
  • Generate structured summary of older messages

Reactive Compaction

  • On prompt_too_long API error: compact and retry (once)
  • If compact + retry fails: surface error to user with context

Summary Chaining

When compacting a session that already has a compacted summary:

  1. Extract the existing summary
  2. Generate new summary from messages since last compaction
  3. Merge: Previously compacted context + Newly compacted context
  4. Include continuation instruction: "Resume directly — do not acknowledge the summary"

Structured Summary Extraction

Summaries should include:

  • Scope (message counts by role)
  • Tools mentioned
  • Recent user requests (last 3)
  • Pending/incomplete work items
  • Key files referenced
  • Current work state

Configuration

compaction:
  auto: true
  warningThreshold: 0.85
  compactThreshold: 0.90
  preserveRecentMessages: 6
  chainSummaries: true

Prior Art

  • Claude Code: src/services/compact/autoCompact.ts, reactiveCompact.ts
  • Claw Code (Rust): rust/crates/runtime/src/compact.rs — clean implementation of summary chaining with merge_compact_summaries()

extent analysis

TL;DR

Implementing automatic session compaction with summary chaining can help prevent context limits from being reached unexpectedly.

Guidance

  • To implement proactive compaction, track estimated token usage per session and auto-trigger compaction at 90% capacity, preserving the last N messages verbatim and generating a structured summary of older messages.
  • For reactive compaction, compact and retry once on prompt_too_long API errors, and surface the error to the user with context if the retry fails.
  • When compacting a session with an existing summary, extract the existing summary, generate a new summary from messages since the last compaction, and merge them to preserve deep context.
  • Configure the compaction settings using the provided YAML configuration, such as setting auto to true and adjusting the warningThreshold and compactThreshold values.

Example

compaction:
  auto: true
  warningThreshold: 0.85
  compactThreshold: 0.90
  preserveRecentMessages: 6
  chainSummaries: true

Notes

The implementation of summary chaining is crucial to preserving context across multiple compactions. The provided configuration settings can be adjusted to fine-tune the compaction behavior.

Recommendation

Apply the proposed behavior with automatic compaction and summary chaining to prevent context limits from being reached unexpectedly, as it provides a robust approach to managing session history.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING