OpenClaw Daily Ops
Two tasks in one nightly cron: know what you spent, kill what's dead.
- - Parses all OpenClaw session JSONL files → computes today's API cost per session
- Posts a clean cost report to Discord with 7-day trend
- Wipes sessions older than 24h with >1MB context (zombie killer)
- Logs everything to
state/cost-log.json and INLINECODE1
Zero AI credits spent — pure Python + one Discord message.
Setup
1. Configure
Copy config.example.json to config.json and fill in your values:
CODEBLOCK0
Edit config.json:
CODEBLOCK1
How to get a Discord webhook:
- 1. Go to your Discord channel → Edit Channel → Integrations → Webhooks → New Webhook
- Copy the webhook URL
How to get your Discord user ID:
- 1. Enable Developer Mode in Discord (Settings → Advanced → Developer Mode)
- Right-click your username → Copy User ID
Channel names (optional): Map your OpenClaw channel session keys to human-readable names for the report. Find session keys in ~/.openclaw/agents/main/sessions/sessions.json.
2. Test it
CODEBLOCK2
This prints the report without posting to Discord or writing logs.
3. Set up the cron
Add a nightly OpenClaw cron job. In your OpenClaw config or via CLI:
CODEBLOCK3
Or run it as a system cron:
CODEBLOCK4
What the Report Looks Like
CODEBLOCK5
Color coding:
- - 🔴 session cost > $5
- 🟡 session cost $1–$5
- 🟢 session cost < $1
Skill Steps (for OpenClaw cron payload)
When running as an OpenClaw cron agentTurn, the agent should:
Step 1 — Run cost parser
Execute
scripts/cost_report.py --config /path/to/config.json and capture output.
Step 2 — Run zombie killer
Execute
scripts/zombie_killer.py --config /path/to/config.json and capture output.
Step 3 — Format and post
Combine both outputs into the report format above and post to your Discord webhook.
Configuration Reference
| Key | Type | Description |
|---|
| INLINECODE8 | string | Path to OpenClaw sessions directory |
| INLINECODE9 |
string | Path to OpenClaw workspace (for state logs) |
|
discord_webhook | string | Discord webhook URL to post the report |
|
discord_user_id | string | Your Discord ID — tagged on urgent alerts |
|
channel_names | object | Map session keys → display names (optional) |
|
zombie_min_age_hours | number | Sessions older than this get reset (default: 24) |
|
zombie_min_size_mb | number | Minimum file size to reset (default: 1MB) |
|
alert_high_cost | number | Daily cost threshold for 🚨 URGENT flag (default: $50) |
|
alert_low_cost | number | Daily cost threshold for ✅ UNDER BUDGET flag (default: $10) |
|
timezone_offset_hours | number | Your UTC offset for date filtering (default: -6 for CST) |
Files
CODEBLOCK6
FAQ
Q: Will this break my active sessions?
No. The zombie killer only resets sessions older than 24h AND larger than 1MB. Active sessions are never touched.
Q: What does "reset" mean?
It truncates the session JSONL file to empty. OpenClaw recreates it fresh on next use. All logs are saved to session-reset-log.json before wiping.
Q: Can I adjust the thresholds?
Yes — zombie_min_age_hours and zombie_min_size_mb in config.json.
Q: Does this work on Andre/multi-machine setups?
Yes. Run the setup on each machine with its own config.json. Both can post to the same Discord webhook.
OpenClaw 每日运维
一个夜间定时任务,完成两项工作:了解你的花费,清除无用会话。
- - 解析所有 OpenClaw 会话 JSONL 文件 → 计算每个会话当天的 API 成本
- 向 Discord 发布包含 7 天趋势的清晰成本报告
- 清除超过 24 小时且上下文大于 1MB 的会话(僵尸会话清理器)
- 将所有日志记录到 state/cost-log.json 和 state/session-reset-log.json
零 AI 信用点消耗——纯 Python + 一条 Discord 消息。
设置
1. 配置
将 config.example.json 复制为 config.json 并填写你的配置值:
bash
cp config.example.json config.json
编辑 config.json:
json
{
sessions_dir: ~/.openclaw/agents/main/sessions,
workspace_dir: ~/.openclaw/workspace,
discord_webhook: https://discord.com/api/webhooks/YOUR/WEBHOOK,
discorduserid: YOURDISCORDUSER_ID,
channel_names: {
channel:YOURCHANNELID: #your-channel-name
},
zombieminage_hours: 24,
zombieminsize_mb: 1,
alerthighcost: 50,
alertlowcost: 10,
timezoneoffsethours: -6
}
如何获取 Discord Webhook:
- 1. 进入你的 Discord 频道 → 编辑频道 → 集成 → Webhooks → 新建 Webhook
- 复制 Webhook URL
如何获取你的 Discord 用户 ID:
- 1. 在 Discord 中启用开发者模式(设置 → 高级 → 开发者模式)
- 右键点击你的用户名 → 复制用户 ID
频道名称(可选):将你的 OpenClaw 频道会话键映射为报告中可读的名称。在 ~/.openclaw/agents/main/sessions/sessions.json 中查找会话键。
2. 测试
bash
python3 scripts/cost_report.py --config config.json --dry-run
此命令会打印报告,但不会发布到 Discord 或写入日志。
3. 设置定时任务
添加一个夜间 OpenClaw 定时任务。在 OpenClaw 配置中或通过 CLI 设置:
Schedule: 0 21 *(每天晚 9 点——根据你的时区调整)
Model: haiku(成本报告很简单,不需要重型模型)
Payload: 读取 /path/to/openclaw-daily-ops/SKILL.md 并严格遵循。
或者作为系统定时任务运行:
bash
添加到 crontab -e
0 21
* python3 /path/to/openclaw-daily-ops/scripts/cost
report.py --config /path/to/config.json >> /path/to/costreport.log 2>&1
报告样式
📊 每日成本报告 — 2026年3月9日
💰 总计:$4.21 · 830万 tokens
按会话分类:
🔴 #general — $2.45 · 410万 tok
🟡 #posting — $1.32 · 280万 tok
🟢 heartbeat — $0.44 · 140万 tok
7天趋势:$12 → $8 → $6 → $5 → $4 → $4 → $4
✅ 预算内
🔄 已重置 2 个过期会话(释放 3.2MB)
颜色编码:
- - 🔴 会话成本 > $5
- 🟡 会话成本 $1–$5
- 🟢 会话成本 < $1
技能步骤(用于 OpenClaw 定时任务负载)
当作为 OpenClaw 定时任务代理运行时,代理应:
步骤 1 — 运行成本解析器
执行 scripts/cost_report.py --config /path/to/config.json 并捕获输出。
步骤 2 — 运行僵尸会话清理器
执行 scripts/zombie_killer.py --config /path/to/config.json 并捕获输出。
步骤 3 — 格式化并发布
将两个输出合并为上述报告格式,并发布到你的 Discord Webhook。
配置参考
| 键 | 类型 | 描述 |
|---|
| sessionsdir | 字符串 | OpenClaw 会话目录路径 |
| workspacedir |
字符串 | OpenClaw 工作区路径(用于状态日志) |
| discord_webhook | 字符串 | 用于发布报告的 Discord Webhook URL |
| discord
userid | 字符串 | 你的 Discord ID——在紧急警报中标记 |
| channel_names | 对象 | 将会话键映射为显示名称(可选) |
| zombie
minage_hours | 数字 | 超过此时间的会话将被重置(默认:24) |
| zombie
minsize_mb | 数字 | 重置的最小文件大小(默认:1MB) |
| alert
highcost | 数字 | 🚨 紧急标志的每日成本阈值(默认:$50) |
| alert
lowcost | 数字 | ✅ 预算内标志的每日成本阈值(默认:$10) |
| timezone
offsethours | 数字 | 你的 UTC 偏移量,用于日期过滤(默认:-6 对应 CST) |
文件结构
openclaw-daily-ops/
├── SKILL.md ← 本文件
├── config.example.json ← 配置模板(复制为 config.json)
├── scripts/
│ ├── cost_report.py ← 解析会话,计算成本,发布到 Discord
│ └── zombie_killer.py ← 清除过期会话,记录清理内容
└── state/ ← 自动创建
├── cost-log.json ← 滚动 90 天成本历史
└── session-reset-log.json ← 所有僵尸会话清理日志
常见问题
问:这会破坏我的活跃会话吗?
不会。僵尸会话清理器只重置超过 24 小时且大于 1MB 的会话。活跃会话永远不会被触及。
问:重置是什么意思?
它会将会话 JSONL 文件截断为空。OpenClaw 会在下次使用时重新创建。所有日志在清除前都会保存到 session-reset-log.json。
问:我可以调整阈值吗?
可以——在 config.json 中调整 zombieminagehours 和 zombieminsizemb。
问:这适用于 Andre/多机器设置吗?
适用。在每台机器上使用各自的 config.json 运行设置。两者都可以发布到同一个 Discord Webhook。