Asset Detail
Token优化器
📦 ClawHub: [smartpeopleconnected/token-optimizer](https://clawhub.ai/smartpeopleconnected/token-optimizer) 📂 Source: [openclaw/skills](https://github.com/openclaw/skills/tree/main/skills/smartpeopleconnected/token-optimizer) 🏷️ Version: 1.0.18 📅 Published: 2026-02-21
openclawmp install trigger/@u-77dacad50c2960aa/token-optimizer标签
README
Token Optimizer for OpenClaw
Reduce your AI costs by 97% - From $1,500+/month to under $50/month
The Problem
If you've been running OpenClaw and watching your API bills climb, you're not alone. The default configuration prioritizes capability over cost, which means you're probably burning through tokens on routine tasks that don't need expensive models.
Common issues:
- Loading 50KB of history on every message (2-3M wasted tokens/session)
- Using Sonnet/Opus for simple tasks that Haiku handles perfectly
- Paying for API heartbeats that could run on a free local LLM
- No rate limits leading to runaway automation costs
The Solution
Token Optimizer applies four key optimizations that work together to slash your costs:
| Optimization | Before | After | Savings |
|---|---|---|---|
| Session Management | 50KB context | 8KB context | 80% |
| Model Routing | Sonnet for everything | Haiku default | 92% |
| Heartbeat to Ollama | Paid API | Free local LLM | 100% |
| Prompt Caching | No caching | 90% cache hits | 90% |
Combined result: 97% cost reduction
Cost Comparison
| Time Period | Before | After |
|---|---|---|
| Daily | $2-3 | $0.10 |
| Monthly | $70-90 | $3-5 |
| Yearly | $800+ | $40-60 |
What This Tool Modifies
All changes are written under ~/.openclaw/. A backup is created before any modification.
| Path | Purpose |
|---|---|
~/.openclaw/openclaw.json | Main OpenClaw config (model routing, heartbeat, budgets) |
~/.openclaw/backups/ | Timestamped config backups (created automatically) |
~/.openclaw/workspace/ | Template files (SOUL.md, USER.md, IDENTITY.md) |
~/.openclaw/prompts/ | Agent prompt optimization rules |
~/.openclaw/token-optimizer-stats.json | Usage stats for savings reports |
Safe by default - All commands run in dry-run (preview) mode. Pass --apply to write changes.
Quick Start
Installation
# Preview changes (dry-run by default)
python cli.py optimize
# Apply changes
python cli.py optimize --apply
# Quick health check
python cli.py health
Verify Setup
python cli.py verify
Features
1. Intelligent Model Routing
Sets Haiku as the default model with easy aliases for switching:
haiku- Fast, cheap, perfect for 80% of taskssonnet- Complex reasoning, architecture decisionsopus- Mission-critical only
2. Free Heartbeats via Ollama
Routes heartbeat checks to a local LLM (llama3.2:3b) instead of paid API:
- Zero API calls for status checks
- No impact on rate limits
- Saves $5-15/month automatically
3. Lean Session Management
Optimized context loading rules that reduce startup context from 50KB to 8KB:
- Load only essential files (SOUL.md, USER.md)
- On-demand history retrieval
- Daily memory notes instead of history bloat
4. Prompt Caching
Automatic 90% discount on repeated content:
- Agent prompts cached and reused
- 5-minute TTL for optimal cache hits
- Per-model cache configuration
5. Budget Controls
Built-in rate limits and budget warnings:
- Daily/monthly budget caps
- Warning at 75% threshold
- Rate limiting between API calls
Usage
Analyze Current Setup
python cli.py analyze
Shows current configuration status, workspace file sizes, optimization opportunities, and estimated monthly savings.
Preview Changes (Dry Run - Default)
python cli.py optimize
Shows a colored unified diff of what would change, without modifying anything.
Apply Full Optimization
python cli.py optimize --apply
Applies all optimizations: model routing, heartbeat, caching, rate limits, workspace templates, and agent prompts.
Apply Specific Optimizations
python cli.py optimize --apply --mode routing # Model routing only
python cli.py optimize --apply --mode heartbeat # Heartbeat only
python cli.py optimize --apply --mode caching # Prompt caching only
python cli.py optimize --apply --mode limits # Rate limits only
Quick Health Check
python cli.py health
Checks config exists, valid JSON, provider reachable, workspace lean, and budget active.
Configure Heartbeat Provider
# Preview (dry-run by default)
python cli.py setup-heartbeat --provider ollama
# Apply changes
python cli.py setup-heartbeat --provider ollama --apply
python cli.py setup-heartbeat --provider lmstudio --apply
python cli.py setup-heartbeat --provider groq --apply
python cli.py setup-heartbeat --provider none --apply
python cli.py setup-heartbeat --provider groq --fallback ollama --apply
Rollback Configuration
python cli.py rollback --list # List available backups
python cli.py rollback --to <filename> # Restore a specific backup
Verify Setup
python cli.py verify
Disable Colors
python cli.py --no-color optimize
# or
NO_COLOR=1 python cli.py optimize
Configuration
After installation, edit these files:
~/.openclaw/workspace/SOUL.md
Agent principles and operating rules. Includes:
- Model selection rules
- Session initialization rules
- Rate limit rules
~/.openclaw/workspace/USER.md
Your context: name, role, mission, success metrics.
~/.openclaw/prompts/OPTIMIZATION-RULES.md
Copy these rules into your agent prompt.
Requirements
- Python 3.8+
- OpenClaw installed and configured
- Ollama (optional, for free heartbeats)
Installing Ollama (Optional)
Ollama is only needed if you want free local heartbeats. Download from https://ollama.ai, then:
ollama pull llama3.2:3b
ollama serve
Or use the CLI to configure a different provider:
python cli.py setup-heartbeat --provider lmstudio
python cli.py setup-heartbeat --provider none # disable heartbeat
File Structure
token-optimizer/
+-- skill.json # Skill manifest
+-- README.md # This file
+-- src/
| +-- __init__.py # Version (single source of truth)
| +-- colors.py # Shared ANSI colors
| +-- analyzer.py # Analyzes current config
| +-- optimizer.py # Applies optimizations
| +-- verify.py # Verifies setup
+-- templates/
| +-- openclaw-config-optimized.json
| +-- SOUL.md
| +-- USER.md
| +-- OPTIMIZATION-RULES.md
+-- test/
+-- simulation_test.py # Simulation tests
Troubleshooting
Context size still large
- Ensure SESSION INITIALIZATION RULE is in your agent prompt
- Check that SOUL.md and USER.md are lean (<15KB total)
Still using Sonnet for everything
- Verify
~/.openclaw/openclaw.jsonhas correct model configuration - Ensure MODEL SELECTION RULE is in agent prompt
Heartbeat errors
- Make sure Ollama is running:
ollama serve - Verify model is installed:
ollama list
Costs haven't dropped
- Run
python src/verify.pyto check all optimizations - Ensure agent prompt includes all optimization rules
Support
- Issues: GitHub Issues
If this tool saved you money, consider supporting development:
License
MIT License. See LICENSE for details.
Built with care by Smart People Connected
Stop burning tokens. Start building things.
相关推荐
使用Yahoo Finance数据分析和股票及加密货币。支持投资组合管理、带提醒的观察列表、股息分析、八维股票评分、病毒式趋势检测(热扫描器)和谣言/早期信号检测。可用于股票分析、投资组合跟踪、财报反应、加密货币监控、热门股票追踪或在主流传播前发现谣言。
监控文件系统事件(创建/修改/删除),自动唤醒 Agent 执行任务。支持通配符、防抖、变量注入。
每日健康检查触发器,定时运行系统健康检查脚本,输出报告到指定路径。
AI Agent self-evolution engine that enables agents to learn from experience, detect problems, extract insights, and optimize strategies autonomously. Invoke when users need to improve agent performance, analyze execution errors, or implement continuous learning capabilities.