Reduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session prunin...
数据来源:ClawHub。 在 ClawSkills 查看
选择你使用的 Agent
方法一:命令行安装(推荐)
推荐(无需提前安装 clawhub)
npx clawhub@latest --dir ~/.claude/skills install openclaw-token-optimizer或使用 clawhub CLI(需提前安装)
clawhub --dir ~/.claude/skills install openclaw-token-optimizer⚠️ 需要 Node.js 18+,没有 Node?请使用下方方法二直接下载 ZIP。 安装 Node.js →
方法二:手动下载安装(无需 Node)
下载 ZIP,解压后将文件夹放到以下路径,重启 Agent 即可:
安装路径
~/.claude/skills/openclaw-token-optimizer/💡解压后将文件夹放到上方路径,重启 Agent 即可生效
--- name: token-optimizer description: Reduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown. version: 3.0.0 homepage: https://github.com/Asif2BD/OpenClaw-Token-Optimizer source: https://github.com/Asif2BD/OpenClaw-Token-Optimizer author: Asif2BD security: verified: true auditor: Oracle (Matrix Zion) audit_date: 2026-02-18 scripts_no_network: true scripts_no_code_execution: true scripts_no_subprocess: true scripts_data_local_only: true reference_files_describe_external_services: true optimize_sh_is_convenience_wrapper: true optimize_sh_only_calls_bundled_python_scripts: true ---
Comprehensive toolkit for reducing token usage and API costs in OpenClaw deployments. Combines smart model routing, optimized heartbeat intervals, usage tracking, and multi-provider strategies.
Immediate actions (no config changes needed):
```bash python3 scripts/context_optimizer.py generate-agents # Creates AGENTS.md.optimized — review and replace your current AGENTS.md ```
```bash python3 scripts/context_optimizer.py recommend "hi, how are you?" # Shows: Only 2 files needed (not 50+!) ```
```bash cp assets/HEARTBEAT.template.md ~/.openclaw/workspace/HEARTBEAT.md ```
```bash python3 scripts/model_router.py "thanks!" # Single-provider Anthropic setup: Use Sonnet, not Opus # Multi-provider setup (OpenRouter/Together): Use Haiku for max savings ```
```bash python3 scripts/token_tracker.py check ```
Expected savings: 50-80% reduction in token costs for typical workloads (context optimization is the biggest factor!).
The single highest-impact optimization available. Most agents burn 3,000–15,000 tokens per session loading skill files they never use. Stop that first.
The pattern:
SKILLS.md catalog in your workspace (~300 tokens — list of skills + when to load them)Token savings:
| Library size | Before (eager) | After (lazy) | Savings | |---|---|---|---| | 5 skills | ~3,000 tokens | ~600 tokens | 80% | | 10 skills | ~6,500 tokens | ~750 tokens | 88% | | 20 skills | ~13,000 tokens | ~900 tokens | 93% |
Quick implementation in AGENTS.md:
## Skills
At session start: Read SKILLS.md (the index only — ~300 tokens).
Load individual skill files ONLY when a task requires them.
Never load all skills upfront.
Full implementation (with catalog template + optimizer script):
clawhub install openclaw-skill-lazy-loader
The companion skill openclaw-skill-lazy-loader includes a SKILLS.md.template, an AGENTS.md.template lazy-loading section, and a context_optimizer.py CLI that recommends exactly which skills to load for any given task.
Lazy loading handles context loading costs. The remaining capabilities below handle runtime costs. Together they cover the full token lifecycle.
---
Biggest token saver — Only load files you actually need, not everything upfront.
Problem: Default OpenClaw loads ALL context files every session:
Solution: Lazy loading based on prompt complexity.
Usage:
python3 scripts/context_optimizer.py recommend "<user prompt>"
Examples:
# Simple greeting → minimal context (2 files only!)
context_optimizer.py recommend "hi"
→ Load: SOUL.md, IDENTITY.md
→ Skip: Everything else
→ Savings: ~80% of context
# Standard work → selective loading
context_optimizer.py recommend "write a function"
→ Load: SOUL.md, IDENTITY.md, memory/TODAY.md
→ Skip: docs, old memory, knowledge base
→ Savings: ~50% of context
# Complex task → full context
context_optimizer.py recommend "analyze our entire architecture"
→ Load: SOUL.md, IDENTITY.md, MEMORY.md, memory/TODAY+YESTERDAY.md
→ Conditionally load: Relevant docs only
→ Savings: ~30% of context
Output format:
{
"complexity": "simple",
"context_level": "minimal",
"recommended_files": ["SOUL.md", "IDENTITY.md"],
"file_count": 2,
"savings_percent": 80,
"skip_patterns": ["docs/**/*.md", "memory/20*.md"]
}
Integration pattern: Before loading context for a new session:
from context_optimizer import recommend_context_bundle
user_prompt = "thanks for your help"
recommendation = recommend_context_bundle(user_prompt)
if recommendation["context_level"] == "minimal":
# Load only SOUL.md + IDENTITY.md
# Skip everything else
# Save ~80% tokens!
Generate optimized AGENTS.md:
context_optimizer.py generate-agents
# Creates AGENTS.md.optimized with lazy loading instructions
# Review and replace your current AGENTS.md
Expected savings: 50-80% reduction in context tokens.
Automatically classify tasks and route to appropriate model tiers.
NEW: Communication pattern enforcement — Never waste Opus tokens on "hi" or "thanks"!
Usage:
python3 scripts/model_router.py "<user prompt>" [current_model] [force_tier]
Examples:
# Communication (NEW!) → ALWAYS Haiku
python3 scripts/model_router.py "thanks!"
python3 scripts/model_router.py "hi"
python3 scripts/model_router.py "ok got it"
→ Enforced: Haiku (NEVER Sonnet/Opus for casual chat)
# Simple task → suggests Haiku
python3 scripts/model_router.py "read the log file"
# Medium task → suggests Sonnet
python3 scripts/model_router.py "write a function to parse JSON"
# Complex task → suggests Opus
python3 scripts/model_router.py "design a microservices architecture"
Patterns enforced to Haiku (NEVER Sonnet/Opus):
Communication:
Background tasks:
Integration pattern:
from model_router import route_task
user_prompt = "show me the config"
routing = route_task(user_prompt)
if routing["should_switch"]:
# Use routing["recommended_model"]
# Save routing["cost_savings_percent"]
Customization: Edit ROUTING_RULES or COMMUNICATION_PATTERNS in scripts/model_router.py to adjust patterns and keywords.
Reduce API calls from heartbeat polling with smart interval tracking:
Setup:
# Copy template to workspace
cp assets/HEARTBEAT.template.md ~/.openclaw/workspace/HEARTBEAT.md
...安装 OpenClaw Token Optimizer 后,可以对 AI 说这些话来触发它
Help me get started with OpenClaw Token Optimizer
Explains what OpenClaw Token Optimizer does, walks through the setup, and runs a quick demo based on your current project
Use OpenClaw Token Optimizer to reduce OpenClaw token usage and API costs through smart model routi...
Invokes OpenClaw Token Optimizer with the right parameters and returns the result directly in the conversation
What can I do with OpenClaw Token Optimizer in my ai agent & automation workflow?
Lists the top use cases for OpenClaw Token Optimizer, with example commands for each scenario
将技能文件夹放到 ~/.claude/skills/openclaw-token-optimizer/ 目录(个人级,所有项目可用),或 .claude/skills/openclaw-token-optimizer/(项目级)。重启 AI 客户端后,用 /openclaw-token-optimizer 主动调用,或让 AI 根据上下文自动发现并使用。
OpenClaw Token Optimizer 支持 Claude、Cursor、OpenClaw,可与这些 AI 平台无缝集成,扩展其能力。
OpenClaw Token Optimizer 可免费安装使用。请查阅仓库了解许可证信息。
Reduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session prunin...
OpenClaw Token Optimizer 属于「AI Agent & Automation」分类,该分类的技能帮助 AI 智能体在此领域执行专业任务。
Automate my ai agent & automation tasks using OpenClaw Token Optimizer
Identifies repetitive steps in your workflow and sets up OpenClaw Token Optimizer to handle them automatically