Context Cleaner (.md cleanup)
by @sdotwinter
Optimize OpenClaw agent context files by reducing token usage 30-50% with backups, preview, rollback, and preserving core functionality on request.
clawhub install context-cleanerπ About This Skill
name: context-cleaner description: Optimize agent context files for 30-50% token reduction. Creates backup, applies optimizations, offers rollback. Trigger when: "run context-cleaner", "optimize agent context", "clean up agent files", "reduce context tokens".
Context Cleaner
Optimize OpenClaw agent workspace files to reduce context token usage by 30-50% without losing operational capability.
Triggers
Safety First
CRITICAL: This skill modifies agent workspace files. Always: 1. Create timestamped backup before any changes 2. Show user what will change before applying 3. Offer immediate rollback option 4. Never touch main workspace files without explicit confirmation
Workflow
Step 1: Parse Request
Determine scope:
Step 2: Validate Target
For single agent:
ls /home/[USER]/.openclaw/workspace/AGENT_NAME/*.md
If no files found β Error: "Agent 'AGENT_NAME' not found. Available agents: [list]"For all agents: Validate all agents exist in workspace.
For main workspace: β οΈ WARNING: Main workspace optimization affects your core behavior. Require explicit user confirmation:
β οΈ This will modify YOUR core files (SOUL.md, AGENTS.md, etc.). This could affect your behavior.Proceed? (yes/no)
Step 3: Create Backup
Timestamp format: YYYYMMDD-HHMM
cd /home/[USER]/.openclaw/workspaceSingle agent backup
tar -czf agent-backup-AGENT_NAME-TIMESTAMP.tar.gz AGENT_NAME/All agents backup
tar -czf agent-workspaces-backup-TIMESTAMP.tar.gz [AGENT_LIST]/Main workspace backup (if requested)
tar -czf main-workspace-backup-TIMESTAMP.tar.gz SOUL.md AGENTS.md TOOLS.md IDENTITY.md USER.md HEARTBEAT.md OPERATING_RULES.md
Report backup location to user:
β
Backup created: /home/[USER]/.openclaw/workspace/agent-backup-AGENT_NAME-TIMESTAMP.tar.gzTo restore: tar -xzf agent-backup-AGENT_NAME-TIMESTAMP.tar.gz
Step 4: Analyze Current State
Count lines and estimate tokens for each file:
for file in IDENTITY.md AGENTS.md USER.md TASK.md TOOLS.md SOUL.md; do
lines=$(wc -l < "AGENT_NAME/$file" 2>/dev/null || echo "0")
echo "$file: $lines lines"
done
Step 5: Apply Optimizations
Optimization Rules:
1. Single responsibility per file: - IDENTITY.md: Role + directives only - AGENTS.md: Workflow + boundaries - USER.md: User info only - TASK.md: Directive steps, no explanations - TOOLS.md: Integration IDs + tool params + checklists - SOUL.md: One-liner persona statement
2. Directive-style language: - β "I should verify information from multiple sources" - β "VERIFY: cross-reference β₯2 sources"
3. Remove: - Emoji clutter (π―πβοΈπ etc.) - Redundant examples - Obvious follow-up instructions - Duplicated checklists - Verbose explanations
4. Preserve: - Discord channel IDs (replace with [DISCORD_CHANNEL_ID] placeholder) - Notion DB IDs (replace with [NOTION_DB_ID] placeholder) - API endpoints - Workflow steps - Quality gates - Boundaries (NEVER rules)
Step 6: Rewrite Files
Apply optimized templates. See Agent Templates section below.
Step 7: Show Results
Display summary table:
=== OPTIMIZATION RESULTS ===| File | Before | After | Change | % Saved |
|-------------|--------|-------|--------|---------|
| IDENTITY.md | [X] | [Y] | -[Z] | [P]% |
| AGENTS.md | [X] | [Y] | -[Z] | [P]% |
| USER.md | [X] | [Y] | -[Z] | [P]% |
| TASK.md | [X] | [Y] | -[Z] | [P]% |
| TOOLS.md | [X] | [Y] | -[Z] | [P]% |
| SOUL.md | [X] | [Y] | -[Z] | [P]% |
|-------------|--------|-------|--------|---------|
| TOTAL | [X] | [Y] | -[Z] | [P]% |
Token Savings: ~[BEFORE] β ~[AFTER] tokens (-[SAVED] tokens)
Step 8: Ask for Confirmation
β
Optimization complete for AGENT_NAME.Changes:
Removed emoji clutter
Converted explanations to directives
Consolidated duplicate content
Reduced total lines by [X]% Operational capability: PRESERVED β
Discord channel ID: β
(placeholder preserved)
Workflow steps: β
Tool params: β
Quality gates: β
Are you happy with these changes?
Options:
1. β
Keep changes (done)
2. β©οΈ Restore from backup now
3. π Show me the diff first
4. π Optimize next agent
Reply with number or custom request.
Step 9: Handle Response
If "keep" or "happy" or "β ":
β
Changes kept. Backup retained for future rollback if needed.Backup file: agent-backup-AGENT_NAME-TIMESTAMP.tar.gz
To restore later: tar -xzf agent-backup-AGENT_NAME-TIMESTAMP.tar.gz
If "restore" or "no" or "β©οΈ":
cd /home/[USER]/.openclaw/workspace
tar -xzf agent-backup-AGENT_NAME-TIMESTAMP.tar.gz
β
Restored from backup. Agent files reverted to original state.
If "diff" or "show me": Show before/after comparison for each file.
If "next" or continue: Proceed to next agent in queue.
Agent Templates (Generic)
Researcher Agent Template
IDENTITY.md:
# [AGENT_NAME]Role: Research specialist β API docs, web scraping, context gathering
Purpose: Support implementation agents with thorough research before they act
Directives:
ACCURACY: Verify from multiple sources; distinguish facts vs opinions
DEPTH: Read actual documentation, not summaries
CITATIONS: Every factual claim needs source URL + access date
CRITICAL: Question assumptions; identify gaps and bias Output: Structured reports with summary, findings, sources, caveats
AGENTS.md:
# [AGENT_NAME] WorkflowActivation Triggers
Implementation agents need API documentation
Background research required
Web scraping or context gathering needed
Technical research before implementation Research Process
1. CLARIFY β Confirm question, format, scope, source priorities
2. SEARCH β web_search for official docs, API refs, tutorials
3. FETCH β web_fetch to read actual content; note dates
4. SYNTHESIZE β Combine findings; identify patterns, contradictions
5. REPORT β Summary + key findings + sources + caveatsAgent Handoffs
Implementation agents: Technical details, code examples, API signatures
Main: Findings in requested format
TOOLS.md:
# ToolsDiscord: [DISCORD_CHANNEL_ID] (deliver findings here)
web_search
Find relevant sources. Params: query, count (1-10), freshness (pd/pw/pm/py), country, search_langweb_fetch
Lightweight extraction. Params: url, extractMode (markdown|text), maxCharsscrapling
Advanced scraping (Cloudflare bypass). Location: [WORKSPACE_PATH]/skills/scrapling/CLI: scrapling extract get 'URL' output.md
browser
Interactive control (login, scroll, click). Actions: navigate, snapshot, actTool Selection
| Scenario | Tool |
|----------|------|
| Simple docs | web_fetch |
| Bot-protected | scrapling |
| Interactive | browser |
| Quick URLs | web_search |
Content Writer Agent Template
IDENTITY.md:
# [AGENT_NAME] βοΈRole: Content writer β blogs, newsletters, web copy, landing pages
Directives:
HUMANIZE: Always apply humanizer before delivery
CLEAR: No jargon, no fluff, no AI patterns
PURPOSEFUL: Every sentence serves reader or goal
ADAPTIVE: Match tone to audience and medium Style: Conversational, professional, value-first, structured
TASK.md:
# Task Procedure1. Clarify
Confirm: type, goal, audience, tone, key points, length, references. Ask if unclear.2. Research (if needed)
web_search for stats, quotes, context
web_fetch for competitor/reference content
Skip for brand content, opinion, familiar topics 3. Write
Hook/opening that captures attention
Clear heading structure
Reader value focus
Agreed tone/format
CTA if applicable 4. Humanize (MANDATORY)
Apply humanizer skill. Verify:
Sounds natural when read aloud
Has personality (aside, analogy, humor)
Varied sentence rhythm
No fluff phrases 5. Deliver
Send to Discord channel (see TOOLS.md). Include content + assumption notes.Quality Gate
Requirements met β Research done β Format appropriate β Tone correct β Humanized β CTA clear β Scannable
Usage Examples
Single Agent
run context-cleaner for outreach
optimize context for [AGENT_NAME]
clean up content-writer files
All Agents
run context-cleaner for all agents
optimize all agent contexts
clean up all agent files
Main Workspace (β οΈ Requires Confirmation)
context-cleaner your main core files
optimize your SOUL and AGENTS files
Batch Processing
optimize Albert, then George, then Winston
Error Handling
Agent not found:
β Agent 'AGENT_NAME' not found.Available agents:
[LIST_AGENTS] Check spelling and try again.
Backup failed:
β Backup creation failed. Aborting optimization.Manual backup required before proceeding:
tar -czf manual-backup-TIMESTAMP.tar.gz AGENT_NAME/
Try again after backup is created.
File write error:
β Failed to write optimized file: FILENAME.mdError: [error details]
Rolling back to backup...
[restore command]
Please try again or report this issue.
Batch Mode (All Agents)
When optimizing all agents:
1. Create single backup of all agents 2. Process sequentially (not parallel) to avoid conflicts 3. Show progress after each agent:
β
Albert: 63% reduction (-274 lines)
β³ Next: George (1,225 lines)
4. Final summary:
=== BATCH OPTIMIZATION COMPLETE ===
| Agent | Before | After | Saved |
|------------------|--------|-------|-------|
| researcher | 2,687 | 1,200 | 55% |
| outreach | 1,644 | 800 | 51% |
| content-writer | 1,225 | 600 | 51% |
| ... (more) | ... | ... | ... |
|------------------|--------|-------|-------|
| TOTAL | 8,810 | 4,200 | 52% |
Total token savings: ~35,240 β ~16,800 (-18,440 tokens)
Backup: agent-workspaces-backup-TIMESTAMP.tar.gz
Keep all changes? (yes/no/restore-specific)
Safety Checklist
Before applying optimizations: