Track, aggregate, and report OpenClaw token usage and costs across sessions.
文档
OpenClaw Token Optimizer
试用为 OpenClaw 代理减少重复性的 token 消耗:按需加载上下文、Sonnet/Opus 分流、缓存感知的 heartbeat 调度。
它能做什么
面向 OpenClaw 2026.6.x 代理的本地脚本集:根据当前提示词推荐最小可用上下文,把常规任务路由到 Sonnet、复杂推理才走 Opus;按 due-check 与静默时段规划 heartbeat,并给出与 1 小时缓存 TTL 对齐的 55 分钟间隔建议;本地跟踪每日 token 预算。仅在显式传入参数时才写入工作区文件,安装 HEARTBEAT
技能文档
OpenClaw Token Optimizer v3.2.0
Built for current OpenClaw 2026.6.x agents by Asif2BD · GitHub · Security Notes
Security notice: local-only optimization toolkit. The Python scripts make no network requests and do not execute dynamic code. Commands that write files are explicit, documented, and backup-safe.
Install
openclaw skills install @asif2bd/openclaw-token-optimizer
What's New in v3.2
Modern ClawHub Card
The skill card now leads with a clearer product promise, current OpenClaw compatibility, security posture, and practical commands instead of the older long-form v3.0 copy.
Current OpenClaw Model Routing
Routine Anthropic work maps to anthropic/claude-sonnet-4-5; only explicitly complex reasoning routes to anthropic/claude-opus-4-5.
Audit-Safe Writes
context_optimizer.py generate-agentsprints to stdout by default.- Writing an optimized
AGENTS.mdrequires--outputor--workspace-output. ./scripts/optimize.sh heartbeatpreviews only by default../scripts/optimize.sh heartbeat installwritesHEARTBEAT.mdonly after backing up an existing file.
What It Optimizes
Context Loading
Stop loading every workspace file into every session. Recommend only the context needed for the current prompt.
python3 scripts/context_optimizer.py recommend "hi"
Typical result: SOUL.md + IDENTITY.md only for a greeting.
Model Routing
Keep standard work on Sonnet and reserve Opus for deep reasoning.
python3 scripts/model_router.py "thanks!"
python3 scripts/model_router.py "design a multi-agent architecture"
Heartbeat Scheduling
Run only checks that are due, respect quiet hours, and align heartbeat timing with prompt-cache TTL.
python3 scripts/heartbeat_optimizer.py plan
python3 scripts/heartbeat_optimizer.py cache-ttl
Token Budgets
Track local daily budget state and surface warnings before runaway spend.
python3 scripts/token_tracker.py check
Quick Start
1. Find the minimum context for a request
python3 scripts/context_optimizer.py recommend "write a deployment checklist"
2. Preview an optimized AGENTS.md
python3 scripts/context_optimizer.py generate-agents
Save a review copy only when you explicitly ask for it:
python3 scripts/context_optimizer.py generate-agents --output ~/.openclaw/workspace/AGENTS.md.optimized
3. Preview heartbeat installation
./scripts/optimize.sh heartbeat
Install with backup protection:
./scripts/optimize.sh heartbeat install
4. Route a task
python3 scripts/model_router.py "debug this error"
Savings Profile
| Area | Typical waste | Optimizer approach |
|---|---|---|
| Startup context | Loading docs, memory, tools, and old logs every turn | Lazy-load only what the prompt needs |
| Routine model use | Opus used for quick replies or simple checks | Sonnet for routine work, Opus for complex reasoning |
| Heartbeats | Every check runs on every heartbeat | Due-check planner + quiet hours |
| Cache TTL | Idle sessions trigger expensive cache rewrites | 55-minute heartbeat guidance for 1-hour cache TTL |
| Budget control | Spend discovered after the fact | Local budget checks before expensive work |
Expected savings: 50-80% in context-heavy OpenClaw workspaces, with context optimization usually delivering the biggest win.
Files
Scripts
scripts/context_optimizer.py— prompt classification and context bundle recommendationsscripts/model_router.py— Sonnet/Opus-aware task routingscripts/heartbeat_optimizer.py— due-check planning, quiet hours, cache-TTL interval guidancescripts/token_tracker.py— local budget trackingscripts/optimize.sh— convenience wrapper around bundled scripts
Assets
assets/HEARTBEAT.template.md— backup-safe heartbeat templateassets/cronjob-model-guide.md— scheduled-task model guidanceassets/config-patches.json— native and optional config examples
References
references/PROVIDERS.md— optional external-provider strategy notesSECURITY.md— complete security and data-handling breakdown
Security Posture
- No network calls in executable scripts
- No
eval, dynamic imports, shell command execution, or downloader behavior - Local JSON state only under the OpenClaw workspace memory directory
- Workspace writes require explicit commands
- Existing
HEARTBEAT.mdis backed up before replacement .clawhubsafecovers every published file with SHA256 hashes
Verify locally:
sha256sum -c .clawhubsafe
Native OpenClaw Diagnostics
Use OpenClaw's built-in context and usage commands alongside this skill:
/context list
/context detail
/usage tokens
/usage cost
Use the built-ins to see where tokens are going, then use Token Optimizer to reduce the recurring waste.
Best Fit
- Multi-agent OpenClaw workspaces with large instruction files
- Agents that run heartbeats or scheduled checks
- Teams trying to keep Opus for hard work instead of routine work
- xCloud, MissionDeck, or other hosted OpenClaw deployments where every saved prompt token compounds across users
More by Asif2BD
openclaw skills install @asif2bd/jarvis-mission-control
openclaw skills search asif2bd
相关技能
Track every token and dollar your OpenClaw agent spends, set budget walls that actually refuse calls, and replay runs on free local models. Local-first: all data stays on this machine. Use when the user asks about cost, spend, tokens, budgets, usage reports, or what the agent has been doing.
通过多轮引导式问答,生成完整的 OpenClaw 个人 AI 助手配置文件。
Deploy, diagnose, and operate an unattended GitHub-repo optimization loop for OpenClaw with multi-CLI failover, layered timeout controls, report-only audit g...
OpenClaw usage and performance reporting. Parse session JSONL to answer how long each agent task took, which tools/skills/models were used, and how many tokens were consumed — zero dependency, local only, no cost dimension (token is the primary metric). Read-only: never modifies any file, aggregates statistics only, never leaks conversation content. Use when the user asks about token usage, task duration, slowest tools, skill usage, or per-agent consumption (今天花了多少 token/哪个工具最慢/ 任务耗时/用量报告), or scheduled via cron. 中文:OpenClaw 用量与性能查询。 解析 session JSONL,回答每次 agent 任务耗时、所用工具/技能/模型、token 消耗。零依赖纯本地,不提供成本维度(token 为主指标)。只读:不修改任何 文件,只输出聚合统计,不泄露会话内容。用户问 token 用量、任务耗时、 最慢工具、技能使用、按 agent 消耗时使用。cron 每日日报为可选项,用户 自行设置。
按命令族和 profile 选择合适的 OpenClaw CLI 命令,并校验执行结果。