๐Ÿ“‰Cost Optimization

How to Fix OpenClaw Compaction Cost Issues

Intermediate30-60 minutesUpdated 2025-01-15

Long conversations in OpenClaw trigger automatic compaction โ€” a process that summarizes older messages to free up context window space. By default, OpenClaw uses your primary model (often Opus) for compaction, resulting in expensive summarization calls that can cost more than the actual conversation. This guide shows you how to switch compaction to Claude Haiku and tune settings to reduce compaction costs by 85%.

Why This Is Hard to Do Yourself

These are the common pitfalls that trip people up.

๐Ÿ”„

Invisible compaction calls

Compaction happens automatically in the background. You see the API charges but don't realize they're from summarization, not your actual requests

๐Ÿ’ธ

Opus for summarization

Default config uses your primary model for compaction. Summarizing with Opus costs 50x more than using Haiku

๐Ÿ“

Aggressive trigger threshold

Default triggers at 60% context window usage, causing frequent compaction even when there's plenty of room left

๐Ÿ—‘๏ธ

Over-aggressive summarization

Compaction discards too much context, causing the model to lose track of earlier conversation details

Step-by-Step Guide

Step 1

Understand how compaction works

Check current compaction behavior.

# Compaction flow:
# 1. Conversation reaches X% of context window (default: 60%)
# 2. OpenClaw selects older messages to summarize
# 3. Sends those messages to your configured model
# 4. Model returns a summary
# 5. Original messages replaced with summary

# Check if compaction is happening:
grep "compaction triggered" ~/.openclaw/logs/system.log | tail -20

# View recent compaction costs:
grep "compaction_cost" ~/.openclaw/logs/tokens.log | \
  awk '{sum+=$3} END {print "Total compaction cost: $"sum}'
Step 2

Check current compaction settings

See what model is being used.

# View current compaction config:
cat ~/.openclaw/config/compaction.yaml

# If this file doesn't exist, defaults are:
# - trigger_threshold: 0.6 (60%)
# - model: (inherits your primary model)
# - max_summary_tokens: 4000
# - preserve_recent_messages: 5
Step 3

Switch compaction model to Haiku

Use the cheapest model for summarization.

# Create or edit config/compaction.yaml:
compaction:
  model: anthropic/claude-3-haiku
  trigger_threshold: 0.8  # Wait until 80% full
  max_summary_tokens: 2000
  preserve_recent_messages: 10

  # Optional: disable compaction for short conversations
  min_messages_before_compact: 20

Warning: Haiku is 98% as good as Opus at summarization but costs 50x less. For compaction specifically, the quality difference is imperceptible.

Step 4

Adjust trigger threshold

Reduce compaction frequency.

# In config/compaction.yaml:
compaction:
  trigger_threshold: 0.85  # Only compact at 85% full

  # This reduces compaction frequency significantly:
  # At 60%: Compacts every ~15 messages (default)
  # At 85%: Compacts every ~25 messages

  # For very long conversations, consider:
  trigger_threshold: 0.9  # Compact at 90% full

Warning: Setting threshold above 0.9 risks hitting hard context limits, which causes errors. 0.8-0.85 is the sweet spot for most use cases.

Step 5

Reduce context window usage

Prevent compaction from being needed.

# In config/context.yaml:
context:
  max_messages_in_context: 30  # Default is often 50+
  max_tokens_per_message: 1500

  # Exclude system messages from context:
  include_system_messages: false

  # Summarize code blocks before adding to context:
  summarize_code_blocks: true
  max_code_block_tokens: 500
Step 6

Verify compaction savings

Measure before and after costs.

# Track compaction costs over one week:
# Before changes:
grep "compaction_cost" ~/.openclaw/logs/tokens.log.old | \
  awk '{sum+=$3} END {print "Before: $"sum}'

# After changes (wait 1 week):
grep "compaction_cost" ~/.openclaw/logs/tokens.log | \
  awk '{sum+=$3} END {print "After: $"sum}'

# Expected savings: 70-85% reduction in compaction costs

Optimize Your Entire OpenClaw Cost Structure

Compaction is just one source of hidden costs. Our experts audit your entire OpenClaw setup โ€” routing, budgets, compaction, automations โ€” and implement optimizations that typically save $500-2000/month.

Get matched with a specialist who can help.

Sign Up for Expert Help โ†’

Frequently Asked Questions