Context Pruning Strategies: Maintaining Agent Sanity in Long Sessions

How to prevent token bloat and performance degradation in long-lived agentic sessions using selective pruning and summarization.

Team · · 10 min read

In the world of autonomous agents, context is everything. However, context is also finite. As an agent session progresses through dozens of tool calls, subagent spawns, and file reads, the token count swells. Without a robust pruning strategy, the agent eventually hits the "context wall"—where latency spikes, reasoning quality degrades (the "lost in the middle" phenomenon), and eventually, the system crashes due to hard limit violations.

At OpenClaw, we treat context not as a static bucket, but as a dynamic, flowing stream that must be filtered and distilled in real-time.

The Problem: The Context Bloat Cycle

Most naive agent implementations simply append every interaction to the history. This creates several failure modes:

  1. Attention Dilution: The model struggles to distinguish between a critical system instruction from 50 turns ago and a trivial directory listing from 5 turns ago.
  2. Cost Escalation: For cloud-based LLMs, you pay for every token in the prefix. Bloated context leads to exponential cost growth.
  3. Reasoning Loops: Agents can get "stuck" on old errors if they remain too prominent in the immediate history.

Pruning Strategy 1: The Sliding Window with Distillation

The most common approach is the sliding window, but in OpenClaw, we enhance this with Distillation. Instead of simply dropping old turns, we summarize them into a "State Summary" block that sits at the top of the context.

The Distillation Workflow

  1. Threshold Trigger: When the context reaches 70% of the model's limit (e.g., 90k tokens of a 128k limit).
  2. Identification: Select the oldest 40% of the conversation.
  3. Summarization: A dedicated "janitor" call (often using a smaller, faster model like Gemini Flash) extracts key facts, completed tasks, and pending goals.
  4. Injection: The raw turns are deleted, and the summary is prepended.

Pruning Strategy 2: Tool Output Compaction

Tool outputs are often the biggest context offenders. A ls -R or a large cat of a source file can consume 10k+ tokens instantly.

OpenClaw implements ACP (Automatic Compaction Protocol):

  • Direct Truncation: If a tool output exceeds a specific limit (e.g., 2000 tokens), it is truncated, and the agent is given a hint: [truncated: use read with offset/limit to see more].
  • Instructional Erasure: Once a file has been read and the agent has confirmed it "understands" the content, the full file content is replaced with a reference tag: [File /path/to/script.js previously read; content purged from context to save space].

Pruning Strategy 3: Semantic Importance Filtering

Not all turns are created equal. In OpenClaw's advanced architectures, we score turns based on semantic relevance to the current goal.

Turn TypeRetention PriorityPruning Action
System InstructionsCritical (10/10)Never prune.
Core User GoalHigh (9/10)Move to persistent "Goal Buffer."
Tool Error LogsMedium (5/10)Distill into "lessons learned" then delete.
Successful ls outputLow (2/10)Delete immediately after next turn.

Technical Artifact: The Compaction Loop Logic

async function compactContext(history) {
    const tokenCount = countTokens(history);
    if (tokenCount < MAX_THRESHOLD) return history;

    const summary = await generateSummary(history.slice(0, history.length / 2));
    const recentTurns = history.slice(history.length / 2);
    
    return [
        { role: 'system', content: `Previous session summary: ${summary}` },
        ...recentTurns
    ];
}

Conclusion

Effective context management is the difference between an agent that completes complex, multi-hour tasks and one that falls apart after ten minutes. By implementing automated pruning, distillation, and semantic filtering, we ensure that the "brain" of the agent remains focused on the present task without losing the wisdom of the past.

CTA Footer: "Hangryfeed helps founders and businesses scale with high-impact marketing. From Reddit and influencer campaigns to content strategy, UGC, and points programs, we build the systems that drive growth—including deploying AI employees to handle the heavy lifting."