A post on r/openclaw last week got a lot of attention: someone claimed they'd cut their Claude API token usage by 95% using a "workspace compiler" pattern. The comments ranged from skeptical to excited. The technique is real โ though 95% is the ceiling, not the average. Here's what's actually going on.
The Problem: Context Bloat
Every time OpenClaw sends a message to Claude or GPT, it bundles in a system prompt. That system prompt typically includes:
- Your SOUL.md (identity + tone)
- Your AGENTS.md (org structure)
- Your USER.md (your profile)
- Your TOOLS.md (environment notes)
- Recent memory files
- Project context files
- Workspace file listings
On a well-configured setup, this system prompt can run 8,000โ15,000 tokens. At Claude Sonnet pricing ($3/MTok input), that's $0.024โ$0.045 per message โ before the actual content of your message.
If your heartbeat fires every 30 minutes and you're actively chatting, you might send 100โ200 messages per day. That's $2.40โ$9.00/day just in system prompt overhead. About $70โ$270/month in pure context cost โ for the same information injected over and over.
What Is a Workspace Compiler?
A workspace compiler is a script that runs before (or as part of) your OpenClaw session and produces a single compressed context file โ instead of letting OpenClaw inject every configured file in full.
The compiler does three things:
- Strips whitespace, comments, and verbose formatting from markdown files. A SOUL.md that's 800 words with headers and bullet points can often compress to 200 words of dense prose without losing any actual information.
- Deduplicates repeated information across files. If your AGENTS.md and SOUL.md both say "DoIt is Josh's chief of staff," the compiler keeps one instance.
- Summarizes large memory files rather than injecting them in full. Instead of 3,000 tokens of raw opportunity notes, you get a 200-token summary of the most relevant items.
How to Implement It
Option 1: MEMORY.md Consolidation (Easiest โ 30 min)
The single highest-impact change: replace individual injected files with one consolidated, dense CONTEXT.md. Instead of OpenClaw injecting 6 separate files, it injects one compact file.
Create a script that regenerates CONTEXT.md nightly:
#!/bin/bash
# compile-context.sh โ run nightly via cron
WORKSPACE="/home/user/.openclaw/workspace"
OUTPUT="$WORKSPACE/CONTEXT.md"
# Strip all blank lines and markdown headers from key files
# then concatenate into a single dense block
{
echo "# COMPILED CONTEXT โ $(date +%Y-%m-%d)"
echo ""
# Core identity โ stripped of headers and whitespace
grep -v "^#" "$WORKSPACE/SOUL.md" | grep -v "^$" | head -30
echo ""
grep -v "^#" "$WORKSPACE/AGENTS.md" | grep -v "^$" | head -20
echo ""
grep -v "^#" "$WORKSPACE/USER.md" | grep -v "^$" | head -15
echo ""
# Memory summary โ last 50 lines of memory.md (most recent entries)
tail -50 "$WORKSPACE/memory.md"
} > "$OUTPUT"
echo "Context compiled: $(wc -w < $OUTPUT) words"
Then in your openclaw.json, configure only CONTEXT.md as an injected file instead of the individual files. You'll typically go from 8,000+ tokens to under 2,000.
Option 2: Tiered Context Loading (Intermediate โ 2 hrs)
Not all context is needed every turn. A more sophisticated approach injects context in tiers:
- Always-inject (tiny): Identity, name, timezone, model. ~200 tokens.
- Session-inject (small): Current project context, recent memory. ~500โ800 tokens.
- On-demand (large): Full opportunity notes, org structure, historical logs. Only loaded when the agent actually needs them via
readtool calls.
This requires restructuring your workspace files and SOUL.md to be intentionally brief, with pointers to detail files rather than embedding detail inline.
Important tradeoff: Smaller context = faster, cheaper responses. But it also means the agent has to explicitly retrieve information it used to have automatically. For simple conversational use, tiered loading is great. For complex orchestration tasks, having more context up front reduces tool calls and often produces better results.
Option 3: Dynamic Compression Script (Advanced โ half day)
The approach that gets you closest to the 95% claim: a Python script that runs before each session, analyzes what's in your workspace, and generates a maximally compressed context file tailored to recent activity.
#!/usr/bin/env python3
# compress-workspace.py
import os, re, json
from pathlib import Path
WORKSPACE = Path.home() / ".openclaw/workspace"
MAX_TOKENS = 1500 # target compressed context size
def strip_markdown(text):
"""Remove headers, excess whitespace, and formatting."""
text = re.sub(r'^#{1,6}\s+', '', text, flags=re.MULTILINE)
text = re.sub(r'\*\*([^*]+)\*\*', r'\1', text)
text = re.sub(r'\n{3,}', '\n\n', text)
return text.strip()
def summarize_file(filepath, max_lines=20):
"""Return first max_lines non-empty lines of a file."""
try:
lines = [l.strip() for l in open(filepath) if l.strip()]
return '\n'.join(lines[:max_lines])
except:
return ""
# Build compressed context
parts = []
for fname in ['SOUL.md', 'AGENTS.md', 'USER.md']:
fpath = WORKSPACE / fname
if fpath.exists():
parts.append(strip_markdown(summarize_file(fpath, 15)))
# Add recent memory (last 30 lines)
memory = WORKSPACE / 'memory.md'
if memory.exists():
lines = [l.strip() for l in open(memory) if l.strip()]
parts.append('\n'.join(lines[-30:]))
output = '\n\n---\n\n'.join(parts)
(WORKSPACE / 'CONTEXT.md').write_text(output)
print(f"Compiled: {len(output.split())} words")
Run this as a cron job every hour or before each heartbeat cycle.
Realistic Savings Estimates
The 95% claim is achievable but requires a very bloated starting workspace (20,000+ token context) and aggressive compression. Here's what most people actually see:
- Simple workspace (3-5 injected files): 40โ60% reduction
- Medium workspace (6-10 files, some large memory files): 60โ80% reduction
- Large workspace (10+ files, extensive memory, project logs): 75โ95% reduction
For the average ClawReady-configured setup, we see 65โ75% context reduction from basic CONTEXT.md consolidation. On 100 messages/day at Sonnet pricing, that's roughly $3โ$5/day saved โ $90โ$150/month.
What to Watch Out For
- Over-compression degrades quality. If you strip too much, the agent loses important context and starts making mistakes or asking questions it should know the answer to. Test quality after each round of compression.
- Keep the original files. CONTEXT.md should be generated from your source files, not replace them. Source files are your ground truth.
- Re-generate after major changes. If you update SOUL.md significantly, regenerate CONTEXT.md immediately โ don't wait for the nightly cron.
- Monitor response quality, not just token counts. The goal is lower cost for the same quality, not lower cost at the expense of usefulness.
Quick win: Even without any scripting, manually trimming your SOUL.md and AGENTS.md to remove verbose prose and redundant information can cut context 30โ40% in 30 minutes. Start there before building automation.
Want This Set Up For You?
Context optimization is one of the things we cover in ClawReady's Pro setup ($199) and Business setup ($299). We analyze your specific workspace, implement a compiled context approach, and benchmark token usage before and after. Most clients see 60โ75% reduction on the first optimization pass.