Skip to content

feat: Token cost visibility — full attribution, budgets, anomaly detection, and efficiency scoring #78

@vivekchand

Description

@vivekchand

Context

A Reddit comment called out cron job token cost visibility as a game-changer for stopping AI spending bleed. After checking the actual code and UI, this feature does not currently exist. The Crons panel shows timing (runtime_ms as "Took: Xs"), last run status, and errors -- but zero token cost data. This issue proposes building it from scratch.

What the cron_fired event currently stores: job_id, name, status, runtime_ms. No token counts, no cost.
What _build_cron_jobs() sends in the snapshot: id, name, enabled, schedule, task, lastRun=None, lastStatus=None. No cost fields.

This is the full roadmap for token cost visibility in ClawMetry.


Proposed improvements

1. Per-cron token cost tracking (foundation -- must ship first)

The cron_fired event needs to capture token usage from the session it spawned. The sync daemon should correlate cron job_id with the session that ran it and pull token counts + cost_usd. Without this, nothing else below is possible.

Fields to add to cron_fired events: tokens_in, tokens_out, cost_usd, model, session_id

2. Per-cron cost display in the Crons panel

Once cost data flows, show it inline in the cron card: "Took: 74s -- 12,400 tokens -- $0.04". Simple text, no redesign needed.

3. Per-cron cost history graph

Show cost per run over time for each cron job. A sparkline in the cron list + an expanded chart in the detail view. Pattern changes are immediately visible: "this heartbeat doubled in cost 3 days ago."

4. Budget thresholds and alerts

Let users set a per-cron or total daily/monthly budget. Alert (Telegram, push, dashboard banner) when a single run exceeds a threshold, daily total crosses a limit, or 7-day rolling average is rising fast.

5. Spending anomaly detection

Automatically flag runs that cost 2x or more than the rolling average for that cron. Show a warning badge. No configuration needed -- just pattern-based detection.

6. Top cost driver leaderboard

A simple ranked list: "Your 5 most expensive crons this week." One glance = instant prioritization.

7. Sub-agent cost attribution tree

When a cron spawns sub-agents, show a cost tree: parent + each spawned child with its share of total spend. Currently sub-agent costs are completely invisible from the parent cron view.

8. Idle cost metric

Show how much a heartbeat or cron spent and produced zero external output (replied HEARTBEAT_OK, no tools called, no messages sent). This is pure waste cost -- surfacing it is the fastest way to help users cut spend.

9. Cost breakdown by tool type

Within a cron run, show what percentage of tokens went to exec, web_search, browser, memory_search, sessions_spawn, etc. Answers "why is this cron expensive" without log diving.

10. Model cost comparison (what-if)

Show estimated monthly savings if the user switches a cron from claude-sonnet to claude-haiku. Use real token counts from history. "Switching to Haiku saves ~$8/month on this cron."

11. Rolling 30-day cost projection

Based on the last 7 days of spend, project the current month total. "On track for $X this month."

12. Efficiency score

Useful output actions (tool calls with side effects, messages sent, files written) divided by total tokens. Show as A/B/C badge on each cron card.


Priority order

  1. Cron-session cost correlation (foundation -- unblocks everything)
  2. Cost display in cron card (immediate user value)
  3. Anomaly detection (zero-config, high value)
  4. Top cost leaderboard (fast to build)
  5. Budget alerts (uses existing notification plumbing)
  6. Idle cost metric (directly addresses heartbeat-waste use case)
  7. Cost history graph
  8. Sub-agent cost tree
  9. Tool type breakdown
  10. Model comparison, rolling projection, efficiency score

Notes

  • cron_fired event schema change is backward compatible (new fields are additive)
  • Anomaly detection can be client-side using existing Turso run history
  • Budget alerts can reuse existing Resend + Telegram notification plumbing
  • This is the most direct path to ClawMetry owning the "how much is my AI actually costing me" question

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions