feat: add Session Cost (with subagents) widget#475
Open
lala74 wants to merge 1 commit into
Open
Conversation
This was referenced Jun 22, 2026
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>
2207da0 to
5e47111
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a Session Cost (with subagents) widget (
session-cost-total) that shows the full session cost in USD: Claude Code'scost.total_cost_usdplus the cost of all subagents (Task/Agent tool) spawned this session.Renders as
Total Cost: $3.55(or$3.55raw). Pairs naturally with the existingsession-costwidget (main-only) on the same line.Relates to #474.
Problem
cost.total_cost_usdfrom the status line stdin covers only the main transcript. Subagent runs are stored in separate<session>/subagents/agent-*.jsonltranscripts 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 (referencedagentIds ∩ on-diskagent-*.jsonl), then prices each transcript from its per-call tokenusage: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.stop_reasondedup asgetTokenMetrics, 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 +costForUsagegetSubagentCostUsd()insrc/utils/jsonl-metrics.ts(+ barrel re-export)RenderContext.subagentCostUsd; precomputed insrc/ccstatusline.tsonly when the widget is usedSessionCostTotalWidget(src/widgets/SessionCostTotal.ts) registered in widgets index + manifestmodeladded to the transcript message type (TokenMetrics.ts)model-pricing.test.ts+ subagent-cost cases injsonl-metrics.test.tsTesting
bun run lint— cleanbun test— 1596 pass (7 new)total_cost_usd: 2.00, a session with one subagent rendersTotal Cost: $2.46; a session with no subagents rendersTotal Cost: $2.00.