claw-compactor
Claw Compactor — 6-layer token compression skill for OpenClaw agents. Cuts workspace token spend by 50–97% using deterministic rule-engines plus Engram: a real-time, LLM-driven Observational Memory system. Run at session start for automatic savings reporting.
Best use case
claw-compactor is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Claw Compactor — 6-layer token compression skill for OpenClaw agents. Cuts workspace token spend by 50–97% using deterministic rule-engines plus Engram: a real-time, LLM-driven Observational Memory system. Run at session start for automatic savings reporting.
Teams using claw-compactor should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
How claw-compactor Compares
| Feature / Agent | claw-compactor | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Claw Compactor — 6-layer token compression skill for OpenClaw agents. Cuts workspace token spend by 50–97% using deterministic rule-engines plus Engram: a real-time, LLM-driven Observational Memory system. Run at session start for automatic savings reporting.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
Related Guides
SKILL.md Source
# Claw Compactor — OpenClaw Skill Reference
## Overview
Claw Compactor reduces token usage across the full OpenClaw workspace using
6 compression layers:
| Layer | Name | Cost | Notes |
|-------|------|------|-------|
| 1 | Rule Engine | Free | Dedup, strip filler, merge sections |
| 2 | Dictionary Encoding | Free | Auto-codebook, `$XX` substitution |
| 3 | Observation Compression | Free | Session JSONL → structured summaries |
| 4 | RLE Patterns | Free | Path/IP/enum shorthand |
| 5 | Compressed Context Protocol | Free | Format abbreviations |
| **6** | **Engram** | LLM API | Real-time Observational Memory |
**Skill location:** `skills/claw-compactor/`
**Entry point:** `scripts/mem_compress.py`
**Engram CLI:** `scripts/engram_cli.py`
---
## Auto Mode (Recommended — Run at Session Start)
```bash
python3 skills/claw-compactor/scripts/mem_compress.py <workspace> auto
```
Automatically compresses all workspace files, tracks token counts between
runs, and reports savings. Run this at the start of every session.
---
## Core Commands
### Full Pipeline (All Layers)
```bash
python3 scripts/mem_compress.py <workspace> full
```
Runs all 5 deterministic layers in optimal order. Typical: 50%+ combined savings.
### Benchmark (Non-Destructive)
```bash
python3 scripts/mem_compress.py <workspace> benchmark
# JSON output:
python3 scripts/mem_compress.py <workspace> benchmark --json
```
Dry-run report showing potential savings without writing any files.
### Individual Layers
```bash
# Layer 1: Rule-based compression
python3 scripts/mem_compress.py <workspace> compress
# Layer 2: Dictionary encoding
python3 scripts/mem_compress.py <workspace> dict
# Layer 3: Observation compression (session JSONL → summaries)
python3 scripts/mem_compress.py <workspace> observe
# Layer 4: RLE pattern encoding (runs inside `compress`)
# Layer 5: Tokenizer optimization
python3 scripts/mem_compress.py <workspace> optimize
# Tiered summaries (L0/L1/L2)
python3 scripts/mem_compress.py <workspace> tiers
# Cross-file deduplication
python3 scripts/mem_compress.py <workspace> dedup
# Token count report
python3 scripts/mem_compress.py <workspace> estimate
# Workspace health check
python3 scripts/mem_compress.py <workspace> audit
```
### Global Options
```
--json Machine-readable JSON output
--dry-run Preview without writing files
--since DATE Filter sessions by date (YYYY-MM-DD)
--auto-merge Auto-merge duplicates (dedup command)
```
---
## Engram — Layer 6: Real-Time Observational Memory
Engram is the flagship layer. It operates as a live engine alongside conversations,
automatically compressing messages into structured, priority-annotated knowledge.
### Prerequisites
Configure via `engram.yaml` (recommended) or environment variables:
```yaml
# engram.yaml — place in claw-compactor root
llm:
provider: openai-compatible
base_url: http://localhost:8403
model: claude-code/sonnet
max_tokens: 4096
threads:
default:
observer_threshold: 30000 # pending tokens before Observer fires
reflector_threshold: 40000 # observation tokens before Reflector fires
concurrency:
max_workers: 4 # parallel thread workers
```
```bash
# Alternative: environment variables
export ANTHROPIC_API_KEY=sk-ant-... # Preferred
# or
export OPENAI_API_KEY=sk-... # OpenAI-compatible fallback
export OPENAI_BASE_URL=https://... # Optional: custom endpoint (local LLM, etc.)
```
### Engram Auto-Mode (Recommended for Production)
Auto-detects all active threads and processes them concurrently (4 workers):
```bash
# Single run — auto-detects all threads
python3 scripts/engram_auto.py --workspace ~/.openclaw/workspace
# Via shell wrapper
bash scripts/engram-auto.sh
# Via CLI
python3 scripts/engram_cli.py <workspace> auto --config engram.yaml
python3 scripts/engram_cli.py <workspace> status --thread openclaw-main
python3 scripts/engram_cli.py <workspace> observe --thread openclaw-main
python3 scripts/engram_cli.py <workspace> reflect --thread openclaw-main
```
**Retry:** LLM calls retry on 429/5xx with exponential backoff (2s→4s→8s, max 3 attempts).
No retry on 400/401/403 (fail fast on config errors).
### Engram via Unified Entry Point
```bash
# Check all thread statuses
python3 scripts/mem_compress.py <workspace> engram status
# Force Observer for a thread
python3 scripts/mem_compress.py <workspace> engram observe --thread <thread-id>
# Force Reflector for a thread
python3 scripts/mem_compress.py <workspace> engram reflect --thread <thread-id>
# Print injectable context
python3 scripts/mem_compress.py <workspace> engram context --thread <thread-id>
```
### Engram via Dedicated CLI
```bash
# Status: all threads
python3 scripts/engram_cli.py <workspace> status
# Status: single thread
python3 scripts/engram_cli.py <workspace> status --thread <thread-id>
# Force observe
python3 scripts/engram_cli.py <workspace> observe --thread <thread-id>
# Force reflect
python3 scripts/engram_cli.py <workspace> reflect --thread <thread-id>
# Import conversation from file (JSON array or JSONL)
python3 scripts/engram_cli.py <workspace> ingest \
--thread <thread-id> --input /path/to/conversation.jsonl
# Get injectable context string (ready for system prompt)
python3 scripts/engram_cli.py <workspace> context --thread <thread-id>
# JSON output for any command
python3 scripts/engram_cli.py <workspace> status --json
python3 scripts/engram_cli.py <workspace> context --thread <id> --json
```
### Engram Daemon Mode (Real-Time Streaming)
```bash
# Start daemon, pipe JSONL messages via stdin
python3 scripts/engram_cli.py <workspace> daemon --thread <thread-id>
# Pipe a message:
echo '{"role":"user","content":"Hello!","timestamp":"12:00"}' | \
python3 scripts/engram_cli.py <workspace> daemon --thread <thread-id>
# Control commands (send as JSONL):
echo '{"__cmd":"observe"}' # force observe now
echo '{"__cmd":"reflect"}' # force reflect now
echo '{"__cmd":"status"}' # print thread status JSON
echo '{"__cmd":"quit"}' # exit daemon
# Quiet mode (suppress startup messages on stderr)
python3 scripts/engram_cli.py <workspace> daemon --thread <id> --quiet
```
### Engram Python API
```python
from scripts.lib.engram import EngramEngine
engine = EngramEngine(
workspace_path="/path/to/workspace",
observer_threshold=30_000, # tokens before auto-observe
reflector_threshold=40_000, # tokens before auto-reflect
anthropic_api_key="sk-ant-...", # or set ANTHROPIC_API_KEY env
)
# Add a message — auto-triggers observe/reflect when thresholds exceeded
status = engine.add_message("thread-id", role="user", content="Hello!")
# Returns: {"observed": bool, "reflected": bool, "pending_tokens": int, ...}
# Manual trigger regardless of thresholds
obs_text = engine.observe("thread-id") # returns None if no pending msgs
ref_text = engine.reflect("thread-id") # returns None if no observations
# Get full context dict
ctx = engine.get_context("thread-id")
# Returns: {"thread_id", "observations", "reflection", "recent_messages", "stats", "meta"}
# Build injectable system context string
ctx_str = engine.build_system_context("thread-id")
# Ready to prepend to system prompt
```
### Engram Configuration Variables
| Variable | Default | Description |
|----------|---------|-------------|
| `ANTHROPIC_API_KEY` | — | Anthropic API key (preferred) |
| `OPENAI_API_KEY` | — | OpenAI-compatible API key |
| `OPENAI_BASE_URL` | `https://api.openai.com` | Custom endpoint for local LLMs |
| `OM_OBSERVER_THRESHOLD` | `30000` | Pending tokens before auto-observe |
| `OM_REFLECTOR_THRESHOLD` | `40000` | Observation tokens before auto-reflect |
| `OM_MODEL` | `claude-opus-4-5` | LLM model override |
### Threshold Tuning Quick Reference
Each Observer call ≈ 2K output tokens (Sonnet). Daily volume at default 30K threshold:
| Channel | Daily Tokens | @30K threshold | @10K threshold |
|---------|-------------|----------------|----------------|
| #aimm | ~149K | ~5×/day | ~15×/day |
| openclaw-main | ~138K | ~4.5×/day | ~14×/day |
| #open-compress | ~68K | ~2.3×/day | ~7×/day |
| #general | ~62K | ~2×/day | ~6×/day |
| subagent | ~43K | ~1.4×/day | ~4×/day |
| cron | ~9K | ~0.3×/day | ~1×/day |
| **Total** | **~470K/day** | **~16×/day (~32K output tokens)** | **~47×/day (~94K output tokens)** |
Start at `observer_threshold: 30000`. Tune down for fresher context; tune up to reduce cost.
### Engram Benchmark Summary
| Strategy | Token Savings | ROUGE-L | IR-F1 | Latency | LLM Calls |
|----------|--------------|---------|-------|---------|-----------|
| **Engram (L6)** | **87.5%** | 0.038 | 0.414 | ~35s | 2 |
| RuleCompressor (L1–5) | 9.0% | 0.923 | 0.958 | ~6ms | 0 |
| RandomDrop | 21.5% | 0.852 | 0.911 | ~0ms | 0 |
- Engram low ROUGE-L = semantic restructuring, not verbatim copy — intent is preserved
- Use RuleCompressor for instant prompt compression; Engram for long-term memory
- Full results → `benchmark/RESULTS.md`
### Observation Format
Engram produces structured, bilingual (EN/中文) priority-annotated logs:
```
Date: 2026-03-05
- 🔴 12:10 User building OpenCompress; deadline one week / 用户在构建 OpenCompress,deadline 一周内
- 🔴 12:10 Using ModernBERT-large / 使用 ModernBERT-large
- 🟡 12:12 Discussed annotation strategy / 讨论了标注策略
- 🟡 12:30 Deployment pipeline discussion on M3 Ultra
- 🟢 12:45 User prefers concise replies
```
- 🔴 **Critical** — goals, deadlines, blockers, key decisions (never dropped)
- 🟡 **Important** — technical details, ongoing work, preferences
- 🟢 **Useful** — background, mentions, soft context
### Memory Storage Layout
```
memory/engram/{thread_id}/
├── pending.jsonl # Unobserved message buffer (auto-cleared after observe)
├── observations.md # Observer output — append-only structured log
├── reflections.md # Reflector output — compressed long-term memory (overwrites)
└── meta.json # Timestamps and token counts
```
---
## Integration with OpenClaw Memory System
### System Prompt Injection
Inject Engram context at the start of each session:
```python
from scripts.lib.engram import EngramEngine
engine = EngramEngine(workspace_path)
ctx_str = engine.build_system_context("my-session")
if ctx_str:
system_prompt = ctx_str + "\n\n" + base_system_prompt
```
The `build_system_context()` output structure:
```
## Long-Term Memory (Reflections)
<Reflector output — long-term compressed context>
## Recent Observations
<Last 200 lines of Observer output>
<!-- engram_tokens: 1234 -->
```
### Combining Engram with Deterministic Layers
After an Engram session, run the deterministic pipeline on the output files:
```bash
# Engram produces observations.md and reflections.md
# Then apply deterministic compression to further reduce those:
python3 scripts/mem_compress.py <workspace> full
```
### Recommended Workflow for Long-Running Agent Sessions
1. **Session start:** inject `build_system_context()` into system prompt
2. **Each message:** call `engine.add_message()` — auto-triggers observe/reflect
3. **Session end / weekly cron:** run `full` pipeline on workspace
4. **Multi-session continuity:** context persists in `memory/engram/{thread}/`
---
## OpenClaw Skill Installation
To install as an OpenClaw skill, ensure the skill directory is available at:
```
~/.openclaw/workspace/skills/claw-compactor/
```
or configure the path in your OpenClaw skill registry.
SKILL.md is read by the OpenClaw agent dispatcher. The `description` and
`triggers` fields above control when this skill is automatically activated.
---
## Heartbeat / Cron Automation
```markdown
## Memory Maintenance (weekly)
- python3 skills/claw-compactor/scripts/mem_compress.py <workspace> benchmark
- If savings > 5%: run full pipeline
- If pending Engram messages: run engram observe --thread <id>
```
Cron (Sunday 3am):
```bash
0 3 * * 0 cd /path/to/skills/claw-compactor && \
python3 scripts/mem_compress.py /path/to/workspace full
```
---
## Output Artifacts Reference
| Artifact | Location | Description |
|----------|----------|-------------|
| Dictionary codebook | `memory/.codebook.json` | Must travel with memory files |
| Observed session log | `memory/.observed-sessions.json` | Tracks processed transcripts |
| Layer 3 summaries | `memory/observations/` | Observation compression output |
| Engram observations | `memory/engram/{thread}/observations.md` | Live Observer log |
| Engram reflections | `memory/engram/{thread}/reflections.md` | Distilled long-term memory |
| Level 0 summary | `memory/MEMORY-L0.md` | ~200 token ultra-compressed summary |
| Level 1 summary | `memory/MEMORY-L1.md` | ~500 token compressed summary |
---
## Troubleshooting
| Problem | Solution |
|---------|----------|
| `FileNotFoundError` on workspace | Point path to workspace root containing `memory/` |
| Dictionary decompression fails | Check `memory/.codebook.json` is valid JSON |
| Zero savings on `benchmark` | Workspace already optimized |
| `observe` finds no transcripts | Check `sessions/` for `.jsonl` files |
| Engram: "no API key configured" | Set `ANTHROPIC_API_KEY` or `OPENAI_API_KEY` |
| Engram Observer returns `None` | No pending messages for that thread |
| Token counts seem wrong | Install tiktoken: `pip3 install tiktoken` |Related Skills
openclaw-youtube
YouTube SERP Scout for agents. Search top-ranking videos, channels, and trends for content research and competitor tracking.
openclaw-search
Intelligent search for agents. Multi-source retrieval with confidence scoring - web, academic, and Tavily in one unified API.
openclaw-media-gen
Generate images & videos with AIsa. Gemini 3 Pro Image (image) + Qwen Wan 2.6 (video) via one API key.
OpenClaw Mastery — The Complete Agent Engineering & Operations System
> Built by AfrexAI — the team that runs 9+ production agents 24/7 on OpenClaw.
clawrouter
Smart LLM router — save 67% on inference costs. Routes every request to the cheapest capable model across 41 models from OpenAI, Anthropic, Google, DeepSeek, and xAI.
openclaw-safe-change-flow
Safe OpenClaw config change workflow with backup, minimal edits, validation, health checks, and rollback. Single-instance first; secondary instance optional.
jqopenclaw-node-invoker
统一通过 Gateway 的 node.invoke 调用 JQOpenClawNode 能力(file.read、file.write、process.exec、process.manage、system.run、process.which、system.info、system.screenshot、system.notify、system.clipboard、system.input、node.selfUpdate)。当用户需要远程文件读写、文件移动/删除、目录创建/删除、进程管理(列表/搜索/终止)、远程进程执行、命令可执行性探测、系统信息采集、截图采集、系统弹窗、系统剪贴板读写、输入控制(鼠标/键盘)、节点自更新、节点命令可用性排查或修复 node.invoke 参数错误时使用。
alphaclaw
AlphaClaw 是 SkillHub 技能商店的 CLI 工具,用于搜索、安装、发布和管理 Claude Code 技能。支持 AK/SK 登录、关键词搜索技能、一键安装/发布技能包、收藏和评论等完整功能。
openclaw-stock-skill
使用 data.diemeng.chat 提供的接口查询股票日线、分钟线、财务指标等数据,支持 A 股等市场。
clawdnet
Register and manage AI agents on ClawdNet, the decentralized agent registry. Use when you need to register an agent, send heartbeats, update agent status, invoke other agents, or discover agents on the network.
claw2ui
Generate interactive web pages (dashboards, charts, tables, reports) and serve them via public URL. Use this skill when the user explicitly asks for data visualization, dashboards, analytics reports, comparison tables, status pages, or web-based content. Also triggers for: "draw me a chart", "make a dashboard", "show me a table", "generate a report", "visualize this data", "render this as a page", "publish a page", "claw2ui". If the response would benefit from charts, sortable tables, or rich layout, **suggest** using Claw2UI and wait for user confirmation before publishing. Chinese triggers: "做个仪表盘", "画个图表", "做个报表", "生成一个页面", "做个dashboard", "数据可视化", "做个网页", "展示数据", "做个表格", "做个图", "发布一个页面", "做个看板". Additional English triggers: "create a webpage", "show analytics", "build a status page", "make a chart", "data overview", "show me stats", "create a board", "render a page", "comparison chart", "trend analysis", "pie chart", "bar chart", "line chart", "KPI dashboard", "metrics overview", "weekly report", "monthly report".
openclaw-whatsapp
WhatsApp bridge for OpenClaw — send/receive messages, auto-reply agents, QR pairing, message search, contact sync