OpenClaw Implements Agent History Compression to Reduce Context Usage

✍️ OpenClawRadar📅 Published: March 10, 2026🔗 Source
OpenClaw Implements Agent History Compression to Reduce Context Usage
Ad

Context Management Problem

When running OpenClaw inside Docker, direct code writing by the agent fills context with noise: reading files (5K tokens), writing edits (500 tokens), running tests (200 tokens), and receiving stack traces (3K tokens). A single debug cycle consumes 10K-15K tokens, mostly from console output and stack traces that become useless after bug fixes. With 20-30 debug cycles per session, the entire context window gets consumed by noise.

Brain/Worker Architecture

The solution involves separating responsibilities: OpenClawd (in Docker) acts as the brain for planning, breaking work into subtasks, delegating, and coordinating. A local worker on the macOS host, powered by Qwen3.5-27B running on Apple Silicon via MLX with zero cost, serves as the hands for reading files, writing code, running tests, and debugging. This keeps noisy back-and-forth in the worker's context, with the brain only seeing final results like "task done, here are the files that changed."

Compression Strategy

Even with the brain/worker split, the orchestrator's context still fills up with operating docs: AGENTS (~6.6K tokens), SOUL (~1.5K tokens), LESSONS (~10K tokens), and plans/walkthroughs (~13K tokens on disk), totaling 20K-30K tokens before any work begins. Sessions can reach 100K-200K tokens.

The key insight: finished work doesn't need raw detail. Once a subtask is completed, its raw history becomes dead weight. The agent only needs to know: what was the task, did it succeed, what files changed, and any errors.

Ad

Implementation Details

Step 1: Detect lifecycle boundaries - The orchestrator decomposes work into subtasks with lifecycles: Spawn (agent calls sessions_spawn or delegate_task), Execute (tool calls, reasoning), and Complete (System Message "subagent 'task_name' completed"). A 4-pass scanner walks the session JSONL:

  • Pass 1: Find spawn events
  • Pass 2: Find spawn errors
  • Pass 3: Find completion markers
  • Pass 4: Compute tokens count and duration per lifecycle

This identifies message ranges belonging to completed subtasks.

Step 2: Summarize in "agent-language" (masking) - Summaries are generated to look like normal agent output to maintain compatibility with the orchestrator's expected message format (roles, content blocks, tool call structures, parent-child ID chains). These masked summaries replace raw task history.

Example compacted task summary:

── COMPACTED TASK ──
origin: agent
task: Implement idle timeout for MLX server
outcome: success
result: Added 5-min idle timer to MlxServerManager.
Server auto-unloads when no requests received.
files+: src/services/mlx_idle_monitor.py
files~: src/services/mlx_server.py, config.json
errors: none
tried_and_failed: threading.Timer — race condition
must_remember: MLX server must only reload on explicit worker request, not any tool call
─────────────────

This ~100 token summary replaces 5K tokens of raw tool calls and reasoning (99.2% reduction). Summaries are generated by a cheap LLM (Gemini Flash Lite or local MLX), with fallback mechanisms if generation fails.

📖 Read the full source: r/openclaw

Ad

👀 See Also

Claude Usage Bar Colorizer Browser Extension Built with Claude Code
Tools

Claude Usage Bar Colorizer Browser Extension Built with Claude Code

A developer built a browser extension that recolors Claude's usage bars from green to yellow to red based on percentage thresholds, adds a popup with live usage data, and allows customization of thresholds and colors. The extension only runs on Claude's usage page, stores settings locally, and makes no external network requests.

OpenClawRadar
Hawkeye Update Adds Swarm Orchestration, Remote Tasks, and Local Model Support
Tools

Hawkeye Update Adds Swarm Orchestration, Remote Tasks, and Local Model Support

Hawkeye v1.0+ now supports multi-agent swarm orchestration, remote task queuing, and improved Ollama/LM Studio integration. The local-first AI agent flight recorder helps developers track what happens when agents work in repositories.

OpenClawRadar
MCP-Loci: Local Persistent Memory Server for Claude and MCP-Compatible AI
Tools

MCP-Loci: Local Persistent Memory Server for Claude and MCP-Compatible AI

MCP-Loci is a persistent memory server that solves Claude's session-based memory limitation with five tools: remember, recall, forget, synthesize, and health. It uses hybrid BM25 keyword matching and semantic embeddings for accurate recall without requiring API keys.

OpenClawRadar
HolyCode: Docker Container for Persistent AI Coding Agent Environments
Tools

HolyCode: Docker Container for Persistent AI Coding Agent Environments

HolyCode is a Docker container that provides a persistent development environment for AI coding agents, keeping sessions, settings, and plugins across rebuilds. It includes preconfigured browser tooling for agent workflows and supports Claude, OpenAI, Gemini, and other providers through OpenCode.

OpenClawRadar