Skip to content

feat: add Session Cost (with subagents) widget#475

Open
lala74 wants to merge 1 commit into
sirmalloc:mainfrom
lala74:dla/agent-count-widget
Open

feat: add Session Cost (with subagents) widget#475
lala74 wants to merge 1 commit into
sirmalloc:mainfrom
lala74:dla/agent-count-widget

Conversation

@lala74

@lala74 lala74 commented Jun 22, 2026

Copy link
Copy Markdown

Summary

Adds a Session Cost (with subagents) widget (session-cost-total) that shows the full session cost in USD: Claude Code's cost.total_cost_usd plus the cost of all subagents (Task/Agent tool) spawned this session.

Renders as Total Cost: $3.55 (or $3.55 raw). Pairs naturally with the existing session-cost widget (main-only) on the same line.

Relates to #474.

Note: this PR previously contained an agent-count widget; it's been replaced — the cost total is the actually-useful piece.

Problem

cost.total_cost_usd from the status line stdin covers only the main transcript. Subagent runs are stored in separate <session>/subagents/agent-*.jsonl transcripts and are excluded from that number, so the displayed cost stays flat while subagents burn tokens. Heavy parallel-agent sessions show a misleadingly low cost.

How it works

getSubagentCostUsd() reuses the subagent discovery already used by the speed-metrics widgets (referenced agentIds ∩ on-disk agent-*.jsonl), then prices each transcript from its per-call token usage:

  • model-pricing.ts — per-MTok price table by model family (Opus 5/25, Sonnet 3/15, Haiku 1/5, Fable 10/50), cache write at 1.25× input, cache read at 0.1× input; Opus-tier default for unknown models.
  • Same streaming stop_reason dedup as getTokenMetrics, so per-call usage isn't double-counted.

The widget total = stdin total_cost_usd (main, authoritative) + computed subagent cost. Computed only when the widget is present on a line (same gating as speed/clock widgets).

Changes

  • src/utils/model-pricing.ts (new) — price table + costForUsage
  • getSubagentCostUsd() in src/utils/jsonl-metrics.ts (+ barrel re-export)
  • RenderContext.subagentCostUsd; precomputed in src/ccstatusline.ts only when the widget is used
  • SessionCostTotalWidget (src/widgets/SessionCostTotal.ts) registered in widgets index + manifest
  • model added to the transcript message type (TokenMetrics.ts)
  • tests: model-pricing.test.ts + subagent-cost cases in jsonl-metrics.test.ts

Testing

  • bun run lint — clean
  • bun test — 1596 pass (7 new)
  • Manual e2e against real sessions: with stdin total_cost_usd: 2.00, a session with one subagent renders Total Cost: $2.46; a session with no subagents renders Total Cost: $2.00.

Adds a `session-cost-total` widget showing the full session cost in USD:
Claude Code's `cost.total_cost_usd` (main transcript only) plus the cost
of all subagents (Task/Agent tool) spawned this session.

Claude Code prices only the main transcript; subagent runs live in
separate `<session>/subagents/agent-*.jsonl` transcripts and are omitted
from `total_cost_usd`, so the figure stays flat while subagents burn
tokens. This widget prices those transcripts from their per-call token
usage and a model-family price table, reusing the same subagent
discovery the speed-metrics widgets already use.

- model-pricing.ts: per-MTok price table (Opus/Sonnet/Haiku/Fable),
  cache write 1.25x / read 0.1x of input; Opus-tier default for unknowns
- getSubagentCostUsd() in jsonl-metrics (+ barrel re-export), with the
  same streaming stop_reason dedup as getTokenMetrics
- subagentCostUsd precomputed in ccstatusline only when the widget is used
- SessionCostTotalWidget registered in widgets index + manifest
- tests for pricing and subagent cost summation

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@lala74 lala74 force-pushed the dla/agent-count-widget branch from 2207da0 to 5e47111 Compare June 22, 2026 04:56
@lala74 lala74 changed the title feat: add Agent Count widget (subagents spawned this session) feat: add Session Cost (with subagents) widget Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant