๐ฐ Cost Optimizer
March 22, 2026 ยท View on GitHub
Save 60-80% on your AI token costs. Zero config required.
If you find this useful, please give it a star โ it helps others discover it.
The Problem
Running AI-powered dev tools with default settings is shockingly expensive:
| Metric | Default (all Opus) |
|---|---|
| Daily token usage | ~500K tokens |
| Daily cost | ~$13.50/day |
| Monthly cost | ~$405/month |
| Heartbeat alone | $50-100/month |
Most of that spend is wasted โ heartbeat checks, file listings, and simple queries don't need a $27/M-token model.
The Solution
Cost Optimizer intelligently routes each task to the cheapest model that can handle it. With the balanced preset, you cut costs by 84% โ from $405/month down to ~$64/month.
Five subcommands. One skill. Massive savings.
Features
| Command | What it does |
|---|---|
/cost-route | Classifies your prompt (P0-P4) and recommends the optimal model |
/cost-compress | Compresses conversation context when it exceeds 50K tokens |
/cost-heartbeat | Optimizes heartbeat interval, content, and model selection |
/cost-report | Generates detailed cost reports with ASCII charts and trends |
/cost-config | Generates optimized configuration with three preset tiers |
Quick Start
Install:
openclaw install cost-optimizer
Use:
# See your current spending
/cost-optimizer report
# Get a routing recommendation for your next prompt
/cost-optimizer route
# Generate optimized config (balanced preset)
/cost-optimizer config
That's it. No API keys, no setup, no config files needed.
Commands
/cost-route โ Smart Model Routing
Analyzes your prompt across multiple dimensions and recommends the cheapest model that meets quality requirements.
How it works:
- Loads pricing data from
references/model-pricing.md(falls back to hardcodedMODEL_PRICINGconstant) - Extracts classification features: keywords, complexity score (0-10), context length, code ratio
- Matches against P0-P4 routing rules (see Model Routing Rules)
- Applies degradation if needed (rate limits, timeouts, budget thresholds)
- Outputs a structured recommendation
Parameters:
| Parameter | Description | Default |
|---|---|---|
| (none) | Analyzes the current prompt context | โ |
Output example:
## ๐ Model Routing Recommendation
| Dimension | Value |
|------------------|-----------------------------|
| Task Category | code_generation |
| Complexity Score | 5/10 |
| Context Length | 12,000 tokens |
| Recommended | claude-sonnet-4-6 |
| Estimated Cost | \$0.027/call |
| vs Default | Saves 80% |
> Routing basis: Keywords [implement, test], Complexity: 5/10
Custom overrides โ add your own routing rules in openclaw.json:
{
"cost-optimizer": {
"routing": {
"overrides": [
{ "pattern": "deploy|ๅๅธ", "model": "claude-sonnet-4-6", "reason": "Deploy needs reliability, not max intelligence" }
]
}
}
}
/cost-compress โ Context Compression
Reduces token usage by compressing conversation history while preserving recent context.
Triggers:
- Auto: When context exceeds 50K tokens
- Manual:
/cost-optimizer compress
Compression strategies by category:
| Category | Compression Rate | Strategy |
|---|---|---|
| Recent 5 turns | 0% (preserved) | Kept in full |
| Older turns | 80-90% | One-line summaries |
| Tool results | 70-85% | Key output only (path + summary) |
| File contents | 90-95% | Path + line count + function list |
| Code blocks | 50-70% | Signatures + key logic |
| System context | 0% | Never compressed |
Output example:
## ๐ฆ Context Compression Report
| Metric | Value |
|----------------|----------------------------------|
| Before | 82,000 tokens (~\$0.44) |
| After | 24,600 tokens (~\$0.13) |
| Saved | 57,400 tokens (~\$0.31, 70%) |
| Turns Preserved| Last 5 turns fully retained |
| Snapshot | .context-snapshot.md |
/cost-heartbeat โ Heartbeat Optimization
Default heartbeat burns $50-100/month. This command brings it under $5/month.
What's wrong with defaults:
| Problem | Default | Optimized |
|---|---|---|
| Interval | 15-30 min | 45 min (smart) |
| Content | Full status report | Minimal (3 checks) |
| Model | Default (expensive) | deepseek/v3 ($0.07/M) |
| Monthly cost | $50-100 | < $5 |
Smart interval adjustment:
| Condition | Interval | Icon |
|---|---|---|
| Active development (frequent file changes) | 30 min | ๐ข |
| Normal working hours | 45 min | ๐ก |
| Idle (no changes > 30 min) | 60 min | ๐ด |
| Night hours (23:00-07:00) | 120 min or disabled | ๐ |
Minimal checks (replaces full status report):
process_aliveโ is the process running?disk_space_criticalโ disk space OK?active_tasks_countโ how many tasks in flight?
/cost-report โ Usage Report
Generates a comprehensive cost breakdown with charts and savings recommendations.
Data sources:
- Usage log at
~/.openclaw/usage-log.jsonl - Current session estimation (if no log exists)
- Pricing data from
references/model-pricing.md
Output includes:
- Session/daily/weekly/monthly totals
- Cost breakdown by model and task type
- 7-day trend with ASCII bar chart
- Actionable savings recommendations
ASCII chart example:
03-14 | \$12.30 | โโโโโโโโโโโโโโโโโโโโ
03-15 | $ 8.50 | โโโโโโโโโโโโโโโโโโโโ
03-16 | \$15.20 | โโโโโโโโโโโโโโโโโโโโ
03-17 | $ 6.30 | โโโโโโโโโโโโโโโโโโโโ
03-18 | \$11.00 | โโโโโโโโโโโโโโโโโโโโ
03-19 | $ 3.20 | โโโโโโโโโโโโโโโโโโโโ
03-20 | $ 1.50 | โโโโโโโโโโโโโโโโโโโโ
+---------+โโโโโโโโโโโโโโโโโโโโ
Total: \$58.00 Avg: \$8.29
/cost-config โ Configuration Generator
Generates a complete optimized openclaw.json with three preset tiers.
Presets:
| Preset | Daily Cost | Monthly Cost | Savings | Best For |
|---|---|---|---|---|
conservative | $8-12 | $240-360 | ~40% | First-time users, risk-averse |
balanced โญ | $4-8 | $120-240 | ~84% | Daily development (recommended) |
aggressive | $1-4 | $30-120 | ~97% | Budget-constrained, simple tasks |
Workflow:
- Analyzes your current config
- Generates optimized config with diff preview
- Shows estimated savings
- Backs up existing config before applying (
openclaw.json.bak.{timestamp}) - Validates JSON after writing
Cost Comparison
Three Scenarios (500K tokens/day)
| Metric | No Optimization | Balanced | Aggressive |
|---|---|---|---|
| Model allocation | 100% Opus | Mixed routing | Mostly cheap models |
| Daily cost | $13.50 | $2.12 | $0.45 |
| Monthly cost | $405.00 | $63.60 | $13.50 |
| Monthly savings | โ | $341.40 (84%) | $391.50 (97%) |
| Code quality risk | None | None | โ ๏ธ Possible degradation |
Visual Comparison
Monthly Cost (500K tokens/day)
No Optimization \$405 |โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ|
Balanced \$64 |โโโโโโโโ |
Aggressive \$14 |โโ |
+----------------------------------------------------
\$0 \$100 \$200 \$400
Model Pricing Reference (per 1M tokens, blended 4:1 input/output)
| Model | Blended Price | Relative to Opus | Used For |
|---|---|---|---|
| deepseek/v3 | $0.084 | 0.3% | Heartbeat, status checks |
| gemini-2.0-flash | $0.16 | 0.6% | Simple queries, file lists |
| claude-haiku-4-5 | $1.44 | 5.3% | File reads, code completion |
| claude-sonnet-4-6 | $5.40 | 20.0% | Code generation, debugging |
| claude-opus-4-6 | $27.00 | 100.0% | Complex architecture (use sparingly) |
Model Routing Rules
P0-P4 Classification Table
| Priority | Condition | Target Model | Cost/1M tokens | Example Prompts |
|---|---|---|---|---|
| P0 | Heartbeat, status, cron, ping | deepseek/v3 | $0.084 | "heartbeat check", "ping" |
| P1 | Simple queries (complexity 0-2) | gemini-2.0-flash | $0.16 | "find all .tsx files", "list components" |
| P2 | File reads, completion, lint | claude-haiku-4-5 | $1.44 | "read page.tsx", "fix eslint error" |
| P3 | Code generation, debug, test | claude-sonnet-4-6 | $5.40 | "implement login component", "write tests" |
| P4 | Architecture, multi-file refactor | claude-opus-4-6 | $27.00 | "design microservice architecture" |
Routing Decision Flow
flowchart TD
A[User Prompt] --> B{Keyword Match?}
B -->|P0 keywords| C[P0: deepseek/v3]
B -->|P1 keywords| D[P1: gemini-flash]
B -->|P2 keywords| E[P2: haiku]
B -->|P3 keywords| F[P3: sonnet]
B -->|P4 keywords| G[P4: opus]
B -->|No match| H{Complexity Score?}
H -->|0-2| D
H -->|3-5| E
H -->|6-8| F
H -->|9-10| G
C --> I{Complexity Override?}
D --> I
E --> I
F --> I
G --> I
I -->|score >= 9| G
I -->|score >= 6 & P0/P1| F
I -->|No override| J{User Override Rules?}
J -->|Match| K[Apply user-defined model]
J -->|No match| L{Budget Check}
L -->|">80% budget"| M[Degrade one level]
L -->|">95% budget"| N[Force cheapest model]
L -->|OK| O[Final Model Selection]
K --> O
M --> O
N --> O
style C fill:#22c55e,color:#fff
style D fill:#86efac,color:#000
style E fill:#fde68a,color:#000
style F fill:#fb923c,color:#fff
style G fill:#ef4444,color:#fff
style O fill:#3b82f6,color:#fff
Degradation Chain
When errors occur, the system automatically falls back:
claude-opus-4-6 โ claude-sonnet-4-6 โ claude-haiku-4-5 โ gemini-2.0-flash โ deepseek/v3
| Trigger | Action |
|---|---|
| HTTP 429 (rate limit) | Degrade one level, retry after cooldown |
| HTTP 503 (unavailable) | Degrade one level, probe every 5 min |
| Response > 30s | Degrade one level for session |
| Cost > 80% budget | Force degrade one level |
| Cost > 95% budget | Force cheapest model |
Configuration Presets
Conservative โ Safe first optimization
{
"model_routing": { "default_model": "claude-sonnet-4-6" },
"heartbeat": { "base_interval_minutes": 30, "model": "gemini-2.0-flash" },
"cost_limits": { "daily_budget_usd": 12.00 }
}
Best for: teams new to cost optimization, production-critical workflows.
Balanced (Recommended) โ Best cost/quality ratio
{
"model_routing": { "default_model": "claude-sonnet-4-6", "enabled": true },
"heartbeat": { "base_interval_minutes": 45, "model": "deepseek/v3" },
"context": { "auto_compress_threshold": 50000 },
"cost_limits": { "daily_budget_usd": 10.00 }
}
Best for: daily development, individual developers, most use cases.
Aggressive โ Maximum savings
{
"model_routing": { "default_model": "claude-haiku-4-5" },
"heartbeat": { "base_interval_minutes": 60, "model": "deepseek/v3" },
"cost_limits": { "daily_budget_usd": 3.00 }
}
Best for: budget-constrained projects, simple task workloads. โ ๏ธ May reduce code quality for complex tasks.
Architecture
The skill is built around a shared utility library (index.ts) with the following core functions:
| Function | Signature | Description |
|---|---|---|
classifyTask | (prompt: string) โ TaskCategory | Multi-dimensional task classification (keywords + complexity scoring), returns P0-P4 priority |
estimateTokens | (text: string) โ number | Fast token estimation with Chinese/English/code-aware heuristics |
routeModel | (category, contextSize) โ ModelChoice | Selects cheapest model meeting quality requirements, with context-size override |
compressContext | (messages[], budget) โ CompressedContext | Compresses conversation history: summarizes old turns, extracts key decisions, preserves recent context |
parseUsageLog | (rawContent: string) โ ParseResult | Safely parses JSONL usage logs, skipping corrupted lines |
generateUsageReport | (entries[]) โ UsageReport | Aggregates usage data by model/task/day with recommendations |
calculateSavings | (entries[]) โ SavingsEstimate | Simulates routing on historical data to estimate potential savings |
renderAsciiBarChart | (daily[], width?) โ string | Renders daily cost data as an ASCII bar chart |
Key types: TaskPriority (P0-P4), ModelId (5 supported models), ModelChoice, CompressedContext, UsageReport, SavingsEstimate.
Error handling philosophy:
- Read failure โ degrade to defaults (never abort)
- Write failure โ fall back to stdout
- Parse failure โ skip bad lines, report count
- Config write โ backup first; if backup fails, refuse to write
Compatibility
| Feature | OpenClaw | Claude Code |
|---|---|---|
| Model routing | โ Full support | โ
Via /model switch suggestion |
| Context compression | โ Full support | โ Generates snapshot file |
| Heartbeat optimization | โ Full support | โ ๏ธ Requires manual cron config |
| Usage reporting | โ Reads JSONL log | โ Session-based estimation |
| Config generation | โ
openclaw.json | โ
settings.json |
Requirements:
- Runtime: Node.js
- Platforms: macOS, Linux, Windows (WSL)
Contributing
- Fork this repository
- Create a feature branch:
git checkout -b feature/my-improvement - Make your changes and test
- Submit a pull request
Please follow the existing code style and include tests for new functionality.
License
MIT โ OpenClaw Community
ไธญๆ่ฏดๆ
ๆฆ่ฟฐ
Cost Optimizer ๆฏ OpenClaw/Claude Code ็ๆๆฌไผๅๅทฅๅ ทๅ ๏ผ้่ฟไธๅคงๆ ธๅฟ็ญ็ฅๅฐ AI token ๅผ้้ไฝ 60-80%๏ผ
-
ๆบ่ฝๆจกๅ่ทฏ็ฑ โ ๅฐไปปๅกๅไธบ P0-P4 ไบไธช็ญ็บง๏ผ่ชๅจๅน้ ๆ็ปๆต็ๆจกๅใๅฟ่ทณๆฃๆฅ็จ deepseek/v3๏ผ$0.084/M๏ผ๏ผๆฅๅธธ็ผ็ ็จ sonnet๏ผ$5.40/M๏ผ๏ผไป ๅคๆๆถๆไปปๅกไฝฟ็จ opus๏ผ$27.00/M๏ผใ
-
ไธไธๆๅ็ผฉ โ ๅฝๅฏน่ฏไธไธๆ่ถ ่ฟ 50K tokens ๆถ่ชๅจๅ็ผฉ๏ผไฟ็ๆ่ฟ 5 ่ฝฎๅฎๆดๅฏน่ฏ๏ผๅๅฒ้จๅ็ๆไธๅฅ่ฏๆ่ฆ๏ผๅทฅๅ ท่ฐ็จ็ปๆไป ไฟ็ๅ ณ้ฎ่พๅบใๅ็ผฉ็ 70-90%ใ
-
Heartbeat ไผๅ โ ้ป่ฎคๅฟ่ทณๆฏๆๆถ่ $50-100ใไผๅๅ๏ผๅปถ้ฟ้ด้๏ผ45ๅ้ๅบ็บฟ + ๆบ่ฝ่ฐๆด๏ผใ็ฒพ็ฎๆฃๆฅๅ ๅฎน๏ผไป 3 ้กนๅฟ ่ฆๆฃๆฅ๏ผใไฝฟ็จๆไพฟๅฎๆจกๅ๏ผๆๆๆฌ้่ณ $5 ไปฅไธใ
ๆ ธๅฟๆฐๆฎ
| ๆนๆก | ๆฅๅๆๆฌ | ๆๅๆๆฌ | ่็ |
|---|---|---|---|
| ๆ ไผๅ๏ผๅ จ Opus๏ผ | $13.50 | $405 | โ |
| Balanced๏ผๆจ่๏ผ | $2.12 | $64 | 84% |
| Aggressive | $0.45 | $14 | 97% |
ๅฟซ้ไฝฟ็จ
# ๅฎ่ฃ
openclaw install cost-optimizer
# ๆฅ็ๆถ่ๆฅๅ
/cost-optimizer report
# ็ๆไผๅ้
็ฝฎ
/cost-optimizer config
# ่ทๅ่ทฏ็ฑๅปบ่ฎฎ
/cost-optimizer route
# ๅ็ผฉไธไธๆ
/cost-optimizer compress
# ไผๅๅฟ่ทณ
/cost-optimizer heartbeat
ไบไธชๅญๅฝไปค
| ๅฝไปค | ๅ่ฝ |
|---|---|
/cost-route | ๅๆ prompt๏ผๆจ่ๆไผๆจกๅ๏ผP0-P4 ไบ็บงๅ็ฑป๏ผ |
/cost-compress | ๅ็ผฉๅฏน่ฏไธไธๆ๏ผ็ๆ .context-snapshot.md |
/cost-heartbeat | ไผๅๅฟ่ทณ้ ็ฝฎ๏ผ้ด้/ๅ ๅฎน/ๆจกๅ๏ผ๏ผๆ็ $45-95 |
/cost-report | ็ๆๆถ่ๆฅๅ๏ผๆๆจกๅ/ไปปๅก/ๆฅๆ่ๅ + ASCII ๅพ่กจ๏ผ |
/cost-config | ไธ้ฎ็ๆไผๅ้ ็ฝฎ๏ผconservative / balanced / aggressive ไธๆกฃ๏ผ |