token-optimizer

OpenClaw Token Optimizer by M Asif Rahman

Installation
CLI
npx skills add https://github.com/asif2bd/openclaw-token-optimizer --skill token-optimizer

Installieren Sie diesen Skill ΓΌber die CLI und beginnen Sie mit der Verwendung des SKILL.md-Workflows in Ihrem Arbeitsbereich.

Zuletzt aktualisiert am 4/22/2026

OpenClaw Token Optimizer

Reduce OpenClaw token usage and API costs by 50-80%

An OpenClaw skill for smart model routing, lazy context loading, optimized heartbeats, budget tracking, and native OpenClaw 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment).

ClawHub
Version
License: Apache 2.0
OpenClaw


πŸš€ Installation

clawhub install Asif2BD/openclaw-token-optimizer

Or browse to: clawhub.ai/Asif2BD/openclaw-token-optimizer

Option 2: Manual (GitHub)

git clone https://github.com/Asif2BD/OpenClaw-Token-Optimizer.git \
  ~/.openclaw/skills/openclaw-token-optimizer

Then add to openclaw.json:

{
  "skills": {
    "load": {
      "extraDirs": ["~/.openclaw/skills/openclaw-token-optimizer"]
    }
  }
}

One-line install prompt for your agent

"Install the OpenClaw Token Optimizer skill from https://clawhub.ai/Asif2BD/openclaw-token-optimizer β€” or if ClawHub isn't available, clone https://github.com/Asif2BD/OpenClaw-Token-Optimizer and add the path to skills.load.extraDirs in openclaw.json"


✨ What's New in v1.4.x (OpenClaw 2026.2.15)

Three native config patches that work today with zero external dependencies:

Session Pruning

Auto-trim old tool results when the Anthropic cache TTL expires β€” reduces cache re-write costs.

{ "agents": { "defaults": { "contextPruning": { "mode": "cache-ttl", "ttl": "5m" } } } }

Bootstrap Size Limits

Cap workspace file injection into the system prompt (20-40% reduction for large workspaces).

{ "agents": { "defaults": { "bootstrapMaxChars": 10000, "bootstrapTotalMaxChars": 15000 } } }

Cache Retention for Opus

Amortize cache write costs on long Opus sessions.

{ "agents": { "defaults": { "models": { "anthropic/claude-opus-4-5": { "params": { "cacheRetention": "long" } } } } } }

Cache TTL Heartbeat Alignment

Keep the Anthropic 1h prompt cache warm β€” avoid the re-write penalty.

python3 scripts/heartbeat_optimizer.py cache-ttl
# β†’ recommended_interval: 55min (3300s)

πŸ› οΈ Quick Start

1. Context optimization (biggest win):

python3 scripts/context_optimizer.py recommend "hi, how are you?"
# β†’ Load only 2 files, skip everything else β†’ ~80% savings

2. Model routing:

python3 scripts/model_router.py "design a microservices architecture"
# β†’ Complex task β†’ Opus
python3 scripts/model_router.py "thanks!"
# β†’ Simple ack β†’ Sonnet (cheapest available)

3. Optimized heartbeat:

cp assets/HEARTBEAT.template.md ~/.openclaw/workspace/HEARTBEAT.md
python3 scripts/heartbeat_optimizer.py plan

4. Token budget check:

python3 scripts/token_tracker.py check

5. Cache TTL alignment:

python3 scripts/heartbeat_optimizer.py cache-ttl
# Set heartbeat to 55min to keep Anthropic 1h cache warm

πŸ” Native OpenClaw Diagnostics (2026.2.15+)

/context list    β†’ per-file token breakdown (use before applying bootstrap limits)
/context detail  β†’ full system prompt breakdown
/usage tokens    β†’ append token count to every reply
/usage cost      β†’ cumulative cost summary

πŸ“ Skill Structure

openclaw-token-optimizer/
β”œβ”€β”€ SKILL.md                    ← Skill definition (loaded by OpenClaw)
β”œβ”€β”€ SECURITY.md                 ← Full security audit + provenance
β”œβ”€β”€ CHANGELOG.md                ← Version history
β”œβ”€β”€ .clawhubsafe                ← SHA256 integrity manifest (13 files)
β”œβ”€β”€ .clawhubignore              ← Files excluded from publish bundle
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ context_optimizer.py    ← Context lazy-loading
β”‚   β”œβ”€β”€ model_router.py         ← Task classification + model routing
β”‚   β”œβ”€β”€ heartbeat_optimizer.py  ← Interval management + cache-ttl alignment
β”‚   β”œβ”€β”€ token_tracker.py        ← Budget monitoring
β”‚   └── optimize.sh             ← Convenience CLI wrapper (calls Python scripts)
β”œβ”€β”€ assets/
β”‚   β”œβ”€β”€ config-patches.json     ← Ready-to-apply config patches
β”‚   β”œβ”€β”€ HEARTBEAT.template.md   ← Drop-in optimized heartbeat template
β”‚   └── cronjob-model-guide.md  ← Model selection for cron tasks
└── references/
    └── PROVIDERS.md            ← Multi-provider strategy guide

πŸ“Š Expected Savings

Strategy Context Model Monthly (100K tok/day) Savings
Baseline (no optimization) 50K Sonnet $9.00 0%
Context optimization only 10K Sonnet $5.40 40%
Model routing only 50K Mixed $5.40 40%
Both (this skill) 10K Mixed $2.70 70%

πŸ”’ Security

All scripts are local-only β€” no network calls, no subprocess spawning, no system modifications. See SECURITY.md for full per-script audit.

Verify integrity:

cd ~/.openclaw/skills/openclaw-token-optimizer
sha256sum -c .clawhubsafe

Quick audit (should return nothing):

grep -r "urllib\|requests\|socket\|subprocess\|curl\|wget" scripts/

πŸ“œ Changelog

See CHANGELOG.md for full version history.

v1.4.2 β€” Security scanner fixes (provenance, optimize.sh manifest, SECURITY.md)
v1.4.1 β€” .clawhubignore added (fixes public visibility)
v1.4.0 β€” Native OpenClaw 2026.2.15 features (session pruning, bootstrap limits, cache TTL)
v1.3.3 β€” Correct display name on ClawHub
v1.3.2 β€” Security audit, SECURITY.md, .clawhubsafe manifest