← 返回
开发者工具 中文

Openclaw Cost Tracker

Track OpenClaw token usage and API costs from local session data. Prefer openclaw-cost-diff for current cost analysis and window-over-window comparison acros...
从本地会话数据中追踪 OpenClaw token 使用量和 API 成本。推荐使用 openclaw-cost-diff 进行当前成本分析以及跨窗口对比分析。
pfrederiksen
开发者工具 clawhub v1.1.3 3 版本 99831.1 Key: 无需
★ 1
Stars
📥 1,162
下载
💾 12
安装
3
版本
#latest

概述

OpenClaw Cost Tracker

Analyze OpenClaw token usage and API costs from local session data.

Prefer openclaw-cost-diff as the default tool for current analysis because it can compare time windows and break down changes by model, agent, and channel.

Preferred usage

# Compare the last 7 days vs the prior 7 days
/root/.openclaw/venvs/openclaw-cost-diff/bin/openclaw-cost-diff --last 7d --prev 7d

# JSON output for tooling or dashboards
/root/.openclaw/venvs/openclaw-cost-diff/bin/openclaw-cost-diff --data /root/.openclaw/agents --last 7d --prev 7d --json

# Focus on a specific model
/root/.openclaw/venvs/openclaw-cost-diff/bin/openclaw-cost-diff --model openai-codex/gpt-5.4 --last 14d --prev 14d

# Compare agent behavior
/root/.openclaw/venvs/openclaw-cost-diff/bin/openclaw-cost-diff --agent main --prev-agent codex --last 7d --prev 7d

Legacy/local fallback

Use the bundled cost_tracker.py only as a secondary local fallback when openclaw-cost-diff is unavailable or when you want the older single-window daily spend report format.

# All-time cost report
python3 scripts/cost_tracker.py

# Last 7 days
python3 scripts/cost_tracker.py --days 7

# Today only
python3 scripts/cost_tracker.py --days 1

# Since a specific date
python3 scripts/cost_tracker.py --since 2026-02-01

# JSON output for dashboards/integrations
python3 scripts/cost_tracker.py --days 30 --format json

# Custom agents directory
python3 scripts/cost_tracker.py --agents-dir /path/to/agents

What It Reports

Per-model breakdown:

  • Total cost, tokens, and request count
  • Input/output/cache token split
  • Visual percentage bar

Daily spend: Bar chart of cost per day (text) or structured array (JSON).

Grand totals: Combined cost, tokens, and requests across all models.

How It Works

  1. Auto-discovers the OpenClaw agents directory (~/.openclaw/agents)
  2. Scans all agent session JSONL files (filtered by mtime for speed)
  3. Extracts message.usage and message.model from each entry
  4. Aggregates by model and by day
  5. Outputs formatted report or JSON

JSON Output Schema

{
  "models": [
    {
      "model": "claude-opus-4-6",
      "totalTokens": 220800000,
      "inputTokens": 3200,
      "outputTokens": 390800,
      "cacheReadTokens": 149400000,
      "cacheWriteTokens": 1200000,
      "totalCost": 528.55,
      "requestCount": 2088
    }
  ],
  "daily": [
    { "date": "2026-02-20", "cost": 37.14, "byModel": { "opus-4-6": 35.0, "sonnet-4": 2.14 } }
  ],
  "grandTotal": { "totalCost": 580.11, "totalTokens": 269800000, "totalRequests": 3122 },
  "meta": { "agentsDir": "...", "filesScanned": 65, "entriesParsed": 3122, "range": "7d" }
}

Integration

Feed JSON output into dashboards, alerting, or budgeting tools. The daily array is ready for charting. Set up a cron to track spend over time:

# Daily cost snapshot to file
0 0 * * * python3 /path/to/cost_tracker.py --days 1 --format json >> ~/cost-log.jsonl

Notes

  • Prefer openclaw-cost-diff first for comparison and regression work.
  • If totals look surprising, sanity-check against direct raw sums from message.usage.cost.total in local JSONL records.
  • Keep cost_tracker.py as a fallback, not the default source of truth.

Requirements

  • Python 3.8+
  • OpenClaw installed with session data in ~/.openclaw/agents/
  • No external dependencies (stdlib only)

版本历史

共 3 个版本

  • v1.1.3 当前
    2026-05-03 03:02 安全 安全
  • v1.1.2
    2026-03-29 03:46
  • v1.0.0
    2026-03-26 21:28

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

productivity

Shipment Tracker

pfrederiksen
跨多家承运商(USPS、UPS、FedEx、DHL、Amazon、OnTrac、LaserShip)追踪包裹。适用场景:用户询问包裹状态、添加运单号或想了解……
★ 1 📥 1,145
developer-tools

Gog

steipete
Google Workspace 命令行工具,支持 Gmail、日历、云端硬盘、通讯录、表格和文档。
★ 921 📥 185,754
developer-tools

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 66 📥 179,929