关于 cost-export
Cost Export 是一项 Agent 技能,可将成本跟踪中的会话和预算记录导出到外部可观测性系统。支持 Prometheus 文本文件、标准输出 JSON 和 Webhook 投递。
主要功能
- 01导出会话与预算记录
- 02输出 Prometheus 文本文件
- 03向标准输出输出 JSON
- 04通过 Webhook POST JSON
- 05配置命名空间与 Webhook 请求头
适用场景
- 01跟踪成本后刷新外部仪表板
- 02通过 Webhook 发送成本数据以供报告
- 03向 Prometheus 提供预算指标用于告警
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ ruvnet/ ruflo --skill cost-export安装后在 Claude Code 中输入/cost-export确认已加载。
详细介绍
成本导出
从 cost-tracking 拉取所有 session-* 和 budget-config-* 记录,并以外部可观测性系统可使用的格式输出。没有此工具时,成本数据只存在于 AgentDB 命名空间中;有了它,同一份数据就能呈现在仪表板上。
适用场景
- 每次成本跟踪运行后,刷新 Grafana / Datadog / Prometheus 的指标。
- 可通过
/loop 5m配合 Cron 使用,让外部仪表板接近实时更新。 - 将数据单次发送到 Webhook,用于临时报表(Slack、自定义端点)。
步骤
-
选择格式——Prometheus / Grafana 使用 textfile collector,其他情况使用 Webhook POST:
# Prometheus node_exporter textfile collector node plugins/ruflo-cost-tracker/scripts/export.mjs --prometheus /var/lib/node_exporter/textfile_collector/cost_tracker.prom # Webhook (POSTs JSON; add auth via env) EXPORT_WEBHOOK_HEADER='Authorization: Bearer $TOKEN' \ node plugins/ruflo-cost-tracker/scripts/export.mjs --webhook https://hooks.example.com/cost-tracker # Stdout JSON (default if no flag) node plugins/ruflo-cost-tracker/scripts/export.mjs -
检查输出内容——Prometheus 输出包括:
cost_tracker_total_usd <gauge> cost_tracker_tier_total_usd{tier="opus|sonnet|haiku"} <gauge> cost_tracker_session_total_usd{session="<8-char>"} <gauge> cost_tracker_session_messages{session="<8-char>"} <counter> cost_tracker_budget_usd <gauge> (if budget configured) cost_tracker_budget_utilization <gauge> (spent / budget) -
Webhook 负载结构——Webhook 接收的 JSON 与 stdout JSON 一致:
{ exportedAt, sessions: [...], budget, totalUsd, byTier }。可通过EXPORT_WEBHOOK_HEADER='K1: V1, K2: V2'添加请求头(以逗号分隔)。
环境变量覆盖项
| 环境变量 | 默认值 | 用途 |
|---|---|---|
EXPORT_NAMESPACE | cost-tracking | 覆盖目标命名空间 |
EXPORT_WEBHOOK_HEADER | 未设置 | 用于 Webhook 身份验证的逗号分隔 K: V 键值对 |
EXPORT_QUIET=1 | 未设置 | 不显示非错误确认信息 |
交叉引用
cost-track——生成此工具导出的数据cost-budget-check——相同的cost_tracker_budget_*指标可在 Prometheus 中用于告警- node_exporter textfile collector 文档(Prometheus 约定)——将
.prom文件放入 collector 目录,Prometheus 就会读取该文件
评论
支持 Markdown