๐Ÿ’ฐ Cost Optimizer

March 22, 2026 ยท View on GitHub

Save 60-80% on your AI token costs. Zero config required.

GitHub stars OpenClaw Skill Version License: MIT Platform

If you find this useful, please give it a star โ€” it helps others discover it. Node.js


The Problem

Running AI-powered dev tools with default settings is shockingly expensive:

MetricDefault (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

CommandWhat it does
/cost-routeClassifies your prompt (P0-P4) and recommends the optimal model
/cost-compressCompresses conversation context when it exceeds 50K tokens
/cost-heartbeatOptimizes heartbeat interval, content, and model selection
/cost-reportGenerates detailed cost reports with ASCII charts and trends
/cost-configGenerates 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:

  1. Loads pricing data from references/model-pricing.md (falls back to hardcoded MODEL_PRICING constant)
  2. Extracts classification features: keywords, complexity score (0-10), context length, code ratio
  3. Matches against P0-P4 routing rules (see Model Routing Rules)
  4. Applies degradation if needed (rate limits, timeouts, budget thresholds)
  5. Outputs a structured recommendation

Parameters:

ParameterDescriptionDefault
(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:

CategoryCompression RateStrategy
Recent 5 turns0% (preserved)Kept in full
Older turns80-90%One-line summaries
Tool results70-85%Key output only (path + summary)
File contents90-95%Path + line count + function list
Code blocks50-70%Signatures + key logic
System context0%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:

ProblemDefaultOptimized
Interval15-30 min45 min (smart)
ContentFull status reportMinimal (3 checks)
ModelDefault (expensive)deepseek/v3 ($0.07/M)
Monthly cost$50-100< $5

Smart interval adjustment:

ConditionIntervalIcon
Active development (frequent file changes)30 min๐ŸŸข
Normal working hours45 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:

PresetDaily CostMonthly CostSavingsBest 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:

  1. Analyzes your current config
  2. Generates optimized config with diff preview
  3. Shows estimated savings
  4. Backs up existing config before applying (openclaw.json.bak.{timestamp})
  5. Validates JSON after writing

Cost Comparison

Three Scenarios (500K tokens/day)

MetricNo OptimizationBalancedAggressive
Model allocation100% OpusMixed routingMostly 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 riskNoneNoneโš ๏ธ 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)

ModelBlended PriceRelative to OpusUsed For
deepseek/v3$0.0840.3%Heartbeat, status checks
gemini-2.0-flash$0.160.6%Simple queries, file lists
claude-haiku-4-5$1.445.3%File reads, code completion
claude-sonnet-4-6$5.4020.0%Code generation, debugging
claude-opus-4-6$27.00100.0%Complex architecture (use sparingly)

Model Routing Rules

P0-P4 Classification Table

PriorityConditionTarget ModelCost/1M tokensExample Prompts
P0Heartbeat, status, cron, pingdeepseek/v3$0.084"heartbeat check", "ping"
P1Simple queries (complexity 0-2)gemini-2.0-flash$0.16"find all .tsx files", "list components"
P2File reads, completion, lintclaude-haiku-4-5$1.44"read page.tsx", "fix eslint error"
P3Code generation, debug, testclaude-sonnet-4-6$5.40"implement login component", "write tests"
P4Architecture, multi-file refactorclaude-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
TriggerAction
HTTP 429 (rate limit)Degrade one level, retry after cooldown
HTTP 503 (unavailable)Degrade one level, probe every 5 min
Response > 30sDegrade one level for session
Cost > 80% budgetForce degrade one level
Cost > 95% budgetForce 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.

{
  "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:

FunctionSignatureDescription
classifyTask(prompt: string) โ†’ TaskCategoryMulti-dimensional task classification (keywords + complexity scoring), returns P0-P4 priority
estimateTokens(text: string) โ†’ numberFast token estimation with Chinese/English/code-aware heuristics
routeModel(category, contextSize) โ†’ ModelChoiceSelects cheapest model meeting quality requirements, with context-size override
compressContext(messages[], budget) โ†’ CompressedContextCompresses conversation history: summarizes old turns, extracts key decisions, preserves recent context
parseUsageLog(rawContent: string) โ†’ ParseResultSafely parses JSONL usage logs, skipping corrupted lines
generateUsageReport(entries[]) โ†’ UsageReportAggregates usage data by model/task/day with recommendations
calculateSavings(entries[]) โ†’ SavingsEstimateSimulates routing on historical data to estimate potential savings
renderAsciiBarChart(daily[], width?) โ†’ stringRenders 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

FeatureOpenClawClaude 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

  1. Fork this repository
  2. Create a feature branch: git checkout -b feature/my-improvement
  3. Make your changes and test
  4. 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%๏ผš

  1. ๆ™บ่ƒฝๆจกๅž‹่ทฏ็”ฑ โ€” ๅฐ†ไปปๅŠกๅˆ†ไธบ P0-P4 ไบ”ไธช็ญ‰็บง๏ผŒ่‡ชๅŠจๅŒน้…ๆœ€็ปๆตŽ็š„ๆจกๅž‹ใ€‚ๅฟƒ่ทณๆฃ€ๆŸฅ็”จ deepseek/v3๏ผˆ$0.084/M๏ผ‰๏ผŒๆ—ฅๅธธ็ผ–็ ็”จ sonnet๏ผˆ$5.40/M๏ผ‰๏ผŒไป…ๅคๆ‚ๆžถๆž„ไปปๅŠกไฝฟ็”จ opus๏ผˆ$27.00/M๏ผ‰ใ€‚

  2. ไธŠไธ‹ๆ–‡ๅŽ‹็ผฉ โ€” ๅฝ“ๅฏน่ฏไธŠไธ‹ๆ–‡่ถ…่ฟ‡ 50K tokens ๆ—ถ่‡ชๅŠจๅŽ‹็ผฉ๏ผšไฟ็•™ๆœ€่ฟ‘ 5 ่ฝฎๅฎŒๆ•ดๅฏน่ฏ๏ผŒๅކๅฒ้ƒจๅˆ†็”Ÿๆˆไธ€ๅฅ่ฏๆ‘˜่ฆ๏ผŒๅทฅๅ…ท่ฐƒ็”จ็ป“ๆžœไป…ไฟ็•™ๅ…ณ้”ฎ่พ“ๅ‡บใ€‚ๅŽ‹็ผฉ็އ 70-90%ใ€‚

  3. Heartbeat ไผ˜ๅŒ– โ€” ้ป˜่ฎคๅฟƒ่ทณๆฏๆœˆๆถˆ่€— $50-100ใ€‚ไผ˜ๅŒ–ๅŽ๏ผšๅปถ้•ฟ้—ด้š”๏ผˆ45ๅˆ†้’ŸๅŸบ็บฟ + ๆ™บ่ƒฝ่ฐƒๆ•ด๏ผ‰ใ€็ฒพ็ฎ€ๆฃ€ๆŸฅๅ†…ๅฎน๏ผˆไป… 3 ้กนๅฟ…่ฆๆฃ€ๆŸฅ๏ผ‰ใ€ไฝฟ็”จๆœ€ไพฟๅฎœๆจกๅž‹๏ผŒๆœˆๆˆๆœฌ้™่‡ณ $5 ไปฅไธ‹ใ€‚

ๆ ธๅฟƒๆ•ฐๆฎ

ๆ–นๆกˆๆ—ฅๅ‡ๆˆๆœฌๆœˆๅ‡ๆˆๆœฌ่Š‚็œ
ๆ— ไผ˜ๅŒ–๏ผˆๅ…จ Opus๏ผ‰$13.50$405โ€”
Balanced๏ผˆๆŽจ่๏ผ‰$2.12$6484%
Aggressive$0.45$1497%

ๅฟซ้€Ÿไฝฟ็”จ

# ๅฎ‰่ฃ…
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 ไธ‰ๆกฃ๏ผ‰