asif2bd/openclaw-token-optimizer
Overview
This skill reduces OpenClaw token usage and API costs using smart model routing, heartbeat tuning, budget tracking, and native 2026.2.15 features (session pruning, bootstrap limits, cache TTL alignment). It provides four local, non-network scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) to analyze prompts, recommend context bundles, enforce cheaper models for routine tasks, and track daily spending. The toolkit focuses on practical, high-impact changes that typically cut token costs by 50–80% for common deployments.
How this skill works
The scripts inspect local workspace files and runtime patterns without making any network requests or system changes. context_optimizer analyzes a user prompt to recommend a minimal set of context files to load; model_router classifies prompts and suggests the least-cost model tier appropriate for the task; heartbeat_optimizer computes optimal polling intervals to keep Anthropic cache warm; token_tracker aggregates local usage and issues budget status. Configuration patches and guidance map these recommendations into OpenClaw settings (heartbeat intervals, bootstrap limits, session pruning).
When to use it
- When token costs are noticeably high or trending upward
- If API rate limits or expensive model usage are causing outages
- Before scaling to many hosted agents or tenants
- When you want deterministic, local tooling to audit and reduce context injection
- If you use Anthropic API keys and need cache-ttl alignment to avoid write costs
Best practices
- Run context_optimizer.recommend before starting sessions and incorporate its bundle into session bootstrap logic
- Enforce Haiku for communication, heartbeat, cronjobs, and background parsing; reserve Sonnet/Opus for truly complex reasoning
- Set heartbeat to just under your provider cache TTL (e.g., 55m for 1h Anthropic TTL) to favor cache reads
- Use token_tracker checks before expensive batches and switch to cheaper models or defer work when status = warning/exceeded
- Apply bootstrapMaxChars and bootstrapTotalMaxChars to cap injected file size and prune long-lived session caches
Example use cases
- Generate an optimized AGENTS.md to stop injecting hundreds of docs at session start and save ~50–80% context tokens
- Automatically route greetings, thanks, and short responses to Haiku to eliminate wasted Opus/Sonnet calls
- Tune heartbeat intervals to keep Anthropic cache warm and avoid expensive cache writes after idle periods
- Track daily cost and token usage locally and block or downgrade non-urgent jobs when budget thresholds are reached
- Configure cronjobs to use Haiku for routine parsing and monitoring, reserving Opus only when necessary
FAQ
No. The four primary scripts are local-only and do not perform network requests. Optional multi-provider strategies documented in PROVIDERS.md require external keys and network access if you choose to use them.
Will applying recommendations break agent behavior?
Recommendations are conservative: context_optimizer suggests only the files needed by prompt complexity and model_router enforces cheaper models for patterns like greetings. Review generated AGENTS.md.optimized and test changes in staging before replacing production configs.