openclaw - 💡(How to fix) Fix ToolResultCompactor for Agent Context Management [1 participants]

Official PRs (…)
ON THIS PAGE

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#52702Fetched 2026-04-08 01:20:07
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Author
Participants

Code Example

{
  "compaction": {
    "token_threshold": 8000,
    "overlap_raw": 10,
    "summarizer_model": "step-3.5-flash:free",
    "disk_ttl_days": 7
  }
}
RAW_BUFFERClick to expand / collapse

Problem

During multi-tool workflows, raw tool results accumulate in agent context, consuming tokens and eventually exceeding context window. Current pre-compaction flush helps but not sufficient for long-running sessions.

Proposed Solution: ToolResultCompactor

Design

  • Threshold: 8 KB raw text per tool result (configurable).
  • When total tool history size exceeds threshold, truncate oldest messages beyond threshold but keep last N messages raw (overlap = 10).
  • Summarize truncated messages to a condensed form (tokenizer: step-3.5-flash:free) and store to disk for audit.
  • Disk persistence: memory/tool_results/<session_id>/<timestamp>.json with 7-day TTL cleanup.

Integration Hook

Executed before each sessions_spawn that includes tool calls. Check accumulated tool_results array size in context; if over threshold, invoke compactor.

Configuration

{
  "compaction": {
    "token_threshold": 8000,
    "overlap_raw": 10,
    "summarizer_model": "step-3.5-flash:free",
    "disk_ttl_days": 7
  }
}

Benefits

  • Prevents context overflow in multi-tool sequences.
  • Maintains continuity via overlap window.
  • Audit trail retained on disk.

Alternatives Considered

  • Global context summarization (more aggressive) — risk losing important details; tool-specific safer.
  • No compaction — leads to eventual failures.

Request for Feedback

  • Is 8 KB the right threshold? Should it be dynamic based on session age?
  • Should compaction be also triggered by message count rather than bytes?
  • Need to ensure disk storage does not grow unbounded; TTL cleanup must be reliable.

extent analysis

Fix Plan

To implement the ToolResultCompactor, follow these steps:

  • Configure the compaction settings in your JSON configuration file:
{
  "compaction": {
    "token_threshold": 8000,
    "overlap_raw": 10,
    "summarizer_model": "step-3.5-flash:free",
    "disk_ttl_days": 7
  }
}
  • Create a function to check the accumulated tool results size and invoke the compactor when the threshold is exceeded:
def check_compaction_threshold(context):
    if len(context['tool_results']) > config['compaction']['token_threshold']:
        invoke_compactor(context)
  • Implement the compactor function to truncate oldest messages, summarize, and store to disk:
import json
import os

def invoke_compactor(context):
    # Truncate oldest messages
    truncated_messages = context['tool_results'][:-config['compaction']['overlap_raw']]
    
    # Summarize truncated messages
    summarized_messages = summarize_messages(truncated_messages, config['compaction']['summarizer_model'])
    
    # Store to disk
    store_to_disk(context['session_id'], summarized_messages)

def summarize_messages(messages, model):
    # Implement message summarization using the specified model
    pass

def store_to_disk(session_id, messages):
    # Create directory if it doesn't exist
    directory = f'memory/tool_results/{session_id}'
    if not os.path.exists(directory):
        os.makedirs(directory)
    
    # Store messages to disk with 7-day TTL cleanup
    timestamp = int(time.time())
    filename = f'{directory}/{timestamp}.json'
    with open(filename, 'w') as f:
        json.dump(messages, f)
  • Integrate the compactor with the sessions_spawn hook:
def sessions_spawn(context):
    check_compaction_threshold(context)
    # Rest of the sessions_spawn logic

Verification

To verify that the fix worked, monitor the context size and tool results accumulation during multi-tool workflows. Check that the compactor is invoked when the threshold is exceeded and that the oldest messages are truncated and summarized correctly.

Extra Tips

  • Ensure that the disk storage for summarized messages does not grow unbounded by implementing a reliable TTL cleanup mechanism.
  • Consider making the threshold dynamic based on session age or message count for more efficient compaction.
  • Test the compactor with different summarizer models to find the most effective one for your use case.

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

openclaw - 💡(How to fix) Fix ToolResultCompactor for Agent Context Management [1 participants]