spend-circuit-breaker

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

spend-circuit-breaker

spend-circuit-breaker

OpenClaw has no built-in hard spending cap. This skill monitors session logs to estimate cumulative API cost, alerts at configurable thresholds, and automatically pauses non-essential cron automations when the monthly budget ceiling is hit.
OpenClaw 没有内置的硬性支出上限。本Skill会监控会话日志以估算API累计成本,在达到可配置阈值时发出告警,触及月度预算上限时会自动暂停非必要的cron自动化任务。

Setup (first run)

设置(首次运行)

Before the skill is useful, record your monthly budget in state:
python3 check.py --set-budget 50        # $50/month hard cap
python3 check.py --set-alert 0.5 0.75   # Alert at 50% and 75%
在本Skill正常使用前,需要先在状态中记录你的月度预算:
python3 check.py --set-budget 50        # $50/month hard cap
python3 check.py --set-alert 0.5 0.75   # Alert at 50% and 75%

Cron Wakeup Behaviour

Cron 唤醒逻辑

Runs every 4 hours (
cron: "0 */4 * * *"
). On each wakeup:
  1. Read
    ~/.openclaw/skill-state/spend-circuit-breaker/state.yaml
    — load
    monthly_budget_usd
    and
    spend_this_month_usd
  2. Parse OpenClaw session logs from
    ~/.openclaw/sessions/
    to estimate new spend since
    last_checked_at
    (model × token counts × price table)
  3. Update
    spend_this_month_usd
    and
    last_checked_at
    in state
  4. Apply threshold logic (see below)
  5. Reset spend to 0 on the 1st of each month
每4小时运行一次(
cron: "0 */4 * * *"
)。每次唤醒时执行以下操作:
  1. 读取
    ~/.openclaw/skill-state/spend-circuit-breaker/state.yaml
    —— 加载
    monthly_budget_usd
    spend_this_month_usd
    配置
  2. 解析
    ~/.openclaw/sessions/
    路径下的OpenClaw会话日志,估算自
    last_checked_at
    以来的新增支出(模型 × Token数量 × 价目表)
  3. 更新状态中的
    spend_this_month_usd
    last_checked_at
    字段
  4. 执行阈值判断逻辑(见下文)
  5. 每月1日自动将支出清零

Threshold logic

阈值逻辑

Spend %Action
< 50%Log silently — nothing to surface
≥ 50%Notify user: "You've used ~50% of your $X budget this month"
≥ 75%Notify user + suggest which cron skills to pause
≥ 100%Notify user + automatically pause all
cron
-scheduled skills except
daily-review
,
morning-briefing
, and
spend-circuit-breaker
itself
At 100%, write
circuit_open: true
to state.
install.sh
checks this flag before registering new cron jobs — new installs are allowed, new cron triggers are not.
支出占比执行操作
< 50%静默记录日志 —— 无需对外通知
≥ 50%通知用户:「你本月已使用约50%的$X预算」
≥ 75%通知用户 + 给出可暂停的cron Skill建议
≥ 100%通知用户 + 自动暂停所有
cron
调度的Skill,除了
daily-review
morning-briefing
以及
spend-circuit-breaker
本身
当支出占比达到100%时,会向状态文件写入
circuit_open: true
install.sh
在注册新的cron任务前会检查该标志位 —— 允许安装新Skill,但不允许新增cron触发规则。

Circuit reset

熔断重置

To restore automations after a budget reset or manual override:
python3 check.py --reset-circuit
This sets
circuit_open: false
and re-registers all paused cron jobs.
在预算重置或手动覆盖规则后,可通过以下命令恢复自动化任务:
python3 check.py --reset-circuit
该命令会将
circuit_open
设为
false
,并重新注册所有已暂停的cron任务。

Model price table

模型价目表

The script ships with a price table for common models (claude-3-5-sonnet, gpt-4o, etc.). To add a custom model:
python3 check.py --add-model my-model-name --input-cost 3.00 --output-cost 15.00
Prices are per million tokens.
脚本内置了常用模型(claude-3-5-sonnet、gpt-4o等)的价目表。如需添加自定义模型,可执行以下命令:
python3 check.py --add-model my-model-name --input-cost 3.00 --output-cost 15.00
价格单位为每百万Token。