Skill: ccjingeth/openclaw-token-save (version 1.0.0)

--- name: openclaw-token-optimizer description: Optimize OpenClaw token usage and cost by auditing context injection, trimming workspace files (AGENTS.md/SOUL.md/MEMORY.md and daily memory), enabling prompt caching, heartbeat, context pruning, compaction, memory search or qmd, subagents, model tiering, and cron frequency. Use when a user asks to reduce OpenClaw token spend, speed up sessions, shrink context, or configure openclaw.json and memory search settings. --- # OpenClaw Token Optimizer ## Overview Deliver a practical audit and configuration plan that cuts input tokens and unnecessary calls while keeping answer quality. Provide concrete config edits, workspace file trimming guidance, and a prioritized rollout plan. ## Workflow ### 1) Scope and locate configuration - Identify the OpenClaw config file location (common paths include `~/.openclaw/openclaw.json`, `.openclaw/openclaw.json`, or project root config). - List injected workspace files in scope (e.g., `AGENTS.md`, `SOUL.md`, `TOOLS.md`, `IDENTITY.md`, `USER.md`, `HEARTBEAT.md`, `MEMORY.md`, and `memory/YYYY-MM-DD.md`). - Confirm provider and model support for prompt caching and memory search to avoid proposing unsupported keys. ### 2) Baseline token sources - Break input cost into buckets: system prompt, tool schema, workspace files, memory files, and conversation history. - Use a rough sizing method if exact token counts are unavailable (e.g., characters/4 as a quick estimate) and call out that the estimate is approximate. ### 3) Input reduction (highest ROI) - Trim workspace files first. Target budgets: - `AGENTS.md`: keep only essential agent rules and policies. - `SOUL.md`: reduce to short persona bullets. - `MEMORY.md`: keep durable facts only; archive the rest. - `memory/YYYY-MM-DD.md`: prune or rotate daily logs. - Remove unused workspace injections in config (e.g., if `TOOLS.md` or `IDENTITY.md` is unused). - Prefer memory search over full-file injection for large memories. If using qmd, index only needed paths. ### 4) Cache and context control - Enable prompt caching for the primary model when supported. Set `cacheRetention` to a long window and keep a consistent system prompt to maximize cache hits. - Configure heartbeat to keep the cache warm (e.g., ~55 minutes), using a low-cost model and a minimal heartbeat prompt. - Enable context pruning with a TTL that matches the cache window to prevent unbounded history growth. - Add compaction with memory flush so long sessions preserve durable decisions while clearing history. ### 5) Call reduction - Audit cron and scheduled tasks. Consolidate overlapping checks, reduce frequency, and move non-creative tasks to cheaper models. - Configure delivery to be on-demand or only on change to avoid no-op calls. ### 6) Model strategy - Default to a cost-effective model for routine work and provide aliases for manual upgrades to premium models. - Use subagents for parallel, isolated tasks with cheaper models to avoid bloating the main context. ### 7) Deliverables Provide: - A short audit summary and estimated savings. - A concrete config patch or JSON snippet for `openclaw.json`. - A list of files to trim, with before/after size targets. - A phased rollout plan (quick wins first, then advanced options). ## References - Use `references/openclaw-token-optimization.md` for configuration snippets, checklists, and qmd guidance.

path: references/openclaw-token-optimization.md
content: # OpenClaw token optimization reference

## Quick checklist
- Trim injected workspace files (`AGENTS.md`, `SOUL.md`, `MEMORY.md`, `memory/YYYY-MM-DD.md`).
- Remove unused workspace injections from config.
- Enable prompt caching where supported.
- Add heartbeat to keep cache warm; use a cheap model.
- Set context pruning TTL to align with cache.
- Enable compaction with memory flush.
- Use subagents for parallel tasks.
- Enable memory search or qmd for large corpora.
- Audit cron tasks: consolidate, slow down, and downgrade models.

## Example snippets (adjust to your OpenClaw version)

### Prompt caching (model params)
```json
{
  "models": {
    "anthropic/claude-opus-4-6": {
      "params": {
        "cacheRetention": "long",
        "maxTokens": 65536
      }
    }
  }
}
```

### Heartbeat
```json
{
  "heartbeat": {
    "every": "55m",
    "target": "last",
    "model": "minimax/MiniMax-M2.5",
    "quiet": {
      "start": "23:00",
      "end": "08:00"
    }
  }
}
```

### Context pruning
```json
{
  "contextTokens": 200000,
  "contextPruning": {
    "mode": "cache-ttl",
    "ttl": "55m"
  }
}
```

### Compaction with memory flush
```json
{
  "compaction": {
    "mode": "safeguard",
    "reserveTokensFloor": 24000,
    "memoryFlush": {
      "enabled": true,
      "softThresholdTokens": 6000,
      "systemPrompt": "Session nearing compaction. Store durable memories now.",
      "prompt": "Write any lasting notes to memory/YYYY-MM-DD.md; reply with NO_REPLY if nothing to store."
    }
  }
}
```

### Subagents
```json
{
  "subagents": {
    "model": "minimax/MiniMax-M2.5",
    "maxConcurrent": 12,
    "archiveAfterMinutes": 60
  }
}
```

### Memory search (local)
```json
{
  "memorySearch": {
    "provider": "local",
    "cache": {
      "enabled": true,
      "maxEntries": 50000
    }
  }
}
```

### qmd backend
```json
{
  "memory": {
    "backend": "qmd",
    "citations": "auto",
    "qmd": {
      "includeDefaultMemory": true,
      "update": {
        "interval": "5m",
        "debounceMs": 15000
      },
      "limits": {
        "maxResults": 8,
        "timeoutMs": 5000
      },
      "paths": [
        {
          "name": "main-workspace",
          "path": "/home/user/.openclaw/workspace",
          "pattern": "**/*.md"
        }
      ]
    }
  }
}
```

### Model aliasing (manual upgrades)
```json
{
  "agents": {
    "defaults": {
      "models": {
        "anthropic/claude-opus-4-6": {
          "alias": "opus"
        },
        "anthropic/claude-sonnet-4-5": {
          "alias": "sonnet"
        }
      }
    }
  }
}
```

## Workspace file trimming targets
- `AGENTS.md`: keep only core instructions, required policies, and any non-negotiable rules.
- `SOUL.md`: short persona bullets (tone, response style, do/don't).
- `MEMORY.md`: durable facts only. Archive or move transient items.
- `memory/YYYY-MM-DD.md`: keep recent decisions; remove verbose logs.

## Cron optimization heuristics
- Consolidate checks that run within the same 15-30 minute window.
- Downgrade non-creative tasks to lower-cost models.
- Use on-change notifications instead of periodic status spam.

## Notes
- Parameter names and supported features vary by OpenClaw version and provider. Verify keys before applying changes.
- Prompt caching requires stable system prompts and tool schemas to maximize cache hits.


Security scan verdicts — ClawScan: clean | static analysis: clean | VirusTotal: suspicious | SkillSpector: clean (LOW)