How to Fix OpenClaw Context Window Errors
When conversations grow too long or skills return massive outputs, OpenClaw can exceed the LLM provider's maximum context window. This results in errors like "context_length_exceeded" or "maximum context length is X tokens, but input is Y tokens." This guide shows you how to configure context compaction, summarization, and limits to keep conversations within bounds.
Why This Is Hard to Do Yourself
These are the common pitfalls that trip people up.
Context exceeds model token limit
Conversation + skills + prompts exceed 100K, 200K, or provider-specific limits
Compaction not working properly
Auto-compaction disabled, threshold too high, or compaction failing silently
Unbounded conversation history
Keeping every message from hours-long conversations in context
Skills returning massive responses
File reading skills dumping 50KB+ into context without truncation
Step-by-Step Guide
Check current context size
Measure how many tokens are currently in the conversation context.
# Enable context size logging:
export OPENCLAW_LOG_CONTEXT_SIZE=true
systemctl restart openclaw
# Check recent context sizes in logs:
grep "context_tokens" ~/.openclaw/logs/openclaw.log | tail -20
# Example output:
# context_tokens=45231 max_tokens=100000 utilization=45%
# Or query via API if available:
curl http://localhost:3000/api/conversation/stats | jq .context_sizeConfigure context compaction settings
Enable automatic summarization when context approaches the limit.
# Edit OpenClaw config:
nano ~/.openclaw/.env
# Add or update:
ENABLE_CONTEXT_COMPACTION=true
COMPACTION_THRESHOLD=0.7 # Trigger at 70% of max
MAX_CONTEXT_TOKENS=90000 # Leave headroom under model max
# Choose compaction strategy:
COMPACTION_STRATEGY=summarize # Options: summarize, truncate, sliding_window
# Configure summarization model (use cheaper/faster):
COMPACTION_MODEL=claude-3-haiku-20240307
# Restart to apply:
systemctl restart openclawSet maximum context length limits
Hard cap the context size to prevent exceeding provider limits.
# Set per-model context limits in config:
cat >> ~/.openclaw/config.json << 'EOF'
{
"models": {
"claude-3-5-sonnet-20241022": {
"max_context_tokens": 180000
},
"gpt-4-turbo": {
"max_context_tokens": 120000
},
"claude-3-haiku-20240307": {
"max_context_tokens": 180000
}
}
}
EOF
# Or in .env for global limit:
MAX_CONTEXT_LENGTH=100000 # Apply to all modelsImplement conversation summarization
Periodically summarize old messages to reduce context size.
# Configure auto-summarization:
nano ~/.openclaw/.env
# Add:
ENABLE_CONVERSATION_SUMMARIZATION=true
SUMMARIZATION_TRIGGER_TOKENS=50000
SUMMARIZATION_PRESERVE_RECENT=20 # Keep last 20 messages
# Summarization prompt (customize if needed):
SUMMARIZATION_PROMPT="Summarize the above conversation, preserving key facts, decisions, and context needed for future messages."
# Test summarization manually:
curl -X POST http://localhost:3000/api/conversation/summarize \
-H "Content-Type: application/json" \
-d '{"conversation_id": "abc123"}'Reduce skill response sizes
Configure skills to truncate large outputs instead of dumping everything into context.
# For file-reading skills, set max output:
SKILL_FILE_READ_MAX_CHARS=5000
SKILL_FILE_READ_TRUNCATE=true
# For web scraping skills:
SKILL_WEB_SCRAPE_MAX_TOKENS=2000
# For code execution skills:
SKILL_CODE_OUTPUT_MAX_LINES=100
# General skill output limit:
SKILL_MAX_OUTPUT_TOKENS=3000
# Skills exceeding limit will truncate with:
# "... (output truncated, 15000 more chars)"Choose models with larger context windows
Switch to models that support larger context sizes for your use case.
# Models with large context windows (as of 2025):
# - Claude 3.5 Sonnet: 200K tokens
# - Claude 3 Opus: 200K tokens
# - GPT-4 Turbo: 128K tokens
# - Gemini 1.5 Pro: 1M tokens
# Update default model in .env:
nano ~/.openclaw/.env
DEFAULT_MODEL=claude-3-5-sonnet-20241022
# Check available models:
curl http://localhost:3000/api/modelsContext Management Overwhelming?
Our experts configure intelligent context compaction strategies, custom summarization prompts, and multi-tier memory systems. Get context management that scales from quick chats to day-long work sessions.
Get matched with a specialist who can help.
Sign Up for Expert Help โ