cost-track

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cost Track

成本追踪

Reads the active Claude Code session jsonl (
~/.claude/projects/<encoded-cwd>/<session>.jsonl
), tallies assistant-message
usage
per model, computes USD cost using REFERENCE.md pricing, and writes a structured record to the
cost-tracking
AgentDB namespace. This is the producer that gives
cost-report
and
cost-optimize
real data to consume.
读取活跃的Claude Code会话jsonl文件(
~/.claude/projects/<encoded-cwd>/<session>.jsonl
),按模型统计助手消息的
usage
数据,使用REFERENCE.md中的定价计算USD成本,并将结构化记录写入
cost-tracking
AgentDB命名空间。这是为
cost-report
cost-optimize
提供真实数据的生产者

When to use

使用场景

  • After a meaningful chunk of work, to capture spend for the report.
  • At session-end to persist the final tally.
  • Periodically during long sessions (cron-friendly — see
    /loop 30m
    ).
  • 在完成一段重要工作后,捕获支出数据用于生成报告。
  • 会话结束时,持久化最终统计结果。
  • 在长会话期间定期执行(支持cron定时任务——参见
    /loop 30m
    )。

Steps

操作步骤

  1. Run the tracker from the project root:
    bash
    node plugins/ruflo-cost-tracker/scripts/track.mjs
    The script auto-discovers the session jsonl from the current working directory. To pin a specific session:
    TRACK_SESSION=/path/to/session.jsonl
    . To dry-run (no memory write):
    TRACK_DRY_RUN=1
    .
  2. Inspect the markdown summary — total cost, per-model and per-tier breakdowns, and the persisted memory key.
  3. Verify persistence
    mcp__claude-flow__memory_search --query "session-" --namespace cost-tracking
    should list the new record.
    cost-report
    step 1 reads from this namespace.
  1. 从项目根目录运行追踪器
    bash
    node plugins/ruflo-cost-tracker/scripts/track.mjs
    脚本会自动从当前工作目录发现会话jsonl文件。要指定特定会话:
    TRACK_SESSION=/path/to/session.jsonl
    。要执行试运行(不写入内存):
    TRACK_DRY_RUN=1
  2. 查看markdown摘要——包含总成本、按模型和层级划分的明细,以及持久化的内存键。
  3. 验证持久化结果——执行
    mcp__claude-flow__memory_search --query "session-" --namespace cost-tracking
    应能列出新记录。
    cost-report
    的第一步会从此命名空间读取数据。

Record shape (in
cost-tracking
namespace)

cost-tracking
命名空间中的记录结构

Key:
session-<sessionId>
. Value (JSON):
json
{
  "sessionId": "1dba3b8c-...",
  "cwd": "/Users/cohen/Projects/ruflo",
  "startedAt": "2026-05-04T...",
  "endedAt": "2026-05-05T...",
  "messageCount": 234,
  "byModel": {
    "claude-opus-4-7": {
      "tier": "opus",
      "input_tokens": 12345,
      "output_tokens": 6789,
      "cache_creation_input_tokens": 800000,
      "cache_read_input_tokens": 2000000,
      "messages": 50,
      "cost_usd": 1.23
    }
  },
  "byTier": { "haiku": 0.0, "sonnet": 0.45, "opus": 1.23, "unknown": 0 },
  "total_cost_usd": 1.68,
  "capturedAt": "2026-05-05T..."
}
键:
session-<sessionId>
。值(JSON格式):
json
{
  "sessionId": "1dba3b8c-...",
  "cwd": "/Users/cohen/Projects/ruflo",
  "startedAt": "2026-05-04T...",
  "endedAt": "2026-05-05T...",
  "messageCount": 234,
  "byModel": {
    "claude-opus-4-7": {
      "tier": "opus",
      "input_tokens": 12345,
      "output_tokens": 6789,
      "cache_creation_input_tokens": 800000,
      "cache_read_input_tokens": 2000000,
      "messages": 50,
      "cost_usd": 1.23
    }
  },
  "byTier": { "haiku": 0.0, "sonnet": 0.45, "opus": 1.23, "unknown": 0 },
  "total_cost_usd": 1.68,
  "capturedAt": "2026-05-05T..."
}

Pricing source of truth

定价来源

The script's
PRICING
constant mirrors REFERENCE.md "Model pricing (USD per 1M tokens)". Update both together when prices change. Cache-write tokens are billed at
cache_write
rate; cache-read tokens at
cache_read
(per Anthropic billing docs).
脚本中的
PRICING
常量与REFERENCE.md中的“Model pricing (USD per 1M tokens)”保持一致。当价格变动时,需同时更新两者。缓存写入token按
cache_write
费率计费;缓存读取token按
cache_read
费率计费(依据Anthropic计费文档)。

Env overrides

环境变量覆盖

EnvDefaultPurpose
TRACK_CWD
process.cwd()
Override which project's sessions to scan
TRACK_SESSION
most-recent jsonlPin a specific session file
TRACK_OUT
unsetAlso write the JSON summary to this path
TRACK_DRY_RUN=1
unsetSkip the
memory store
call
TRACK_QUIET=1
unsetSuppress markdown output
TRACK_NAMESPACE
cost-tracking
Override target namespace
环境变量默认值用途
TRACK_CWD
process.cwd()
覆盖要扫描的项目会话目录
TRACK_SESSION
最新的jsonl文件指定特定会话文件
TRACK_OUT
未设置将JSON摘要同时写入指定路径
TRACK_DRY_RUN=1
未设置跳过
memory store
调用
TRACK_QUIET=1
未设置抑制markdown输出
TRACK_NAMESPACE
cost-tracking
覆盖目标命名空间

Cross-references

交叉引用

  • cost-report
    (consumer) — reads records produced by this skill
  • REFERENCE.md "Cost attribution formula" — the math the script implements
  • cost-budget-check
    (consumer; landing in P2) — reads totals to evaluate alerts
  • cost-report
    (消费者)——读取本工具生成的记录
  • REFERENCE.md中的“Cost attribution formula”——脚本实现的计算逻辑
  • cost-budget-check
    (消费者;计划在P2阶段推出)——读取总支出数据以生成告警