SLOPSHOPPER

spend-meter

A live spend meter docked at the right: session cost with subagents and workflows, burn rate, subscription limits and where the money goes. Toggle with /spend.

paneguardcommandtoaststatusprocess
v0.1.5MITupdated 2026-10-23Hekzory/spend-meter/plugin
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · spend-meter
│ ┃ Spend ✕ › fix the failing auth╭────────────────────────────────────────────╮ │ ┃ ◆ spend idle 30m │ spend-meter │ │ ┃ ⏺ Read(src/auth.ts) │ Spend meter: press a row for its details · │ │ ┃ $0.42 ⎿ Read 6 lines │ /spend help │ │ ┃ this session · USD at API list price ⏺ Update(src/auth.ts) ╰────────────────────────────────────────────╯ │ ┃ ⎿ Added 2 lines, removed 1 line │ ┃ last turn +$0.00 0s ⏺ Bash(bun test) │ ┃ burn idle ⎿ 3 pass, 1 fail │ ┃ │ ┃ OVER TIME last 27 min ● Done. refresh now rejects expired claims and logs an audit event. │ ┃ No spend in the last 27 minutes. │ ┃ ✻ Worked for 42s · done 4:20 PM │ ┃ LIMITS whole account │ ┃ 5h ━━━━━━━━━━─────────────────────── 31%↻ › /spend │ ┃ ctx ━━━━━━━━━━━━━━━━───────────────── 49%9 ⎿ spend-meter: Spend meter open. │ ┃ │ ┃ WHERE IT GOES │ ┃ · before the meter │ ┃ $0.42 │ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Pane · Spend
◆ spend idle 30m $0.42 this session · USD at API list price last turn +$0.00 0s burn idle OVER TIME last 27 min No spend in the last 27 minutes. LIMITS whole account 5h ━━━━━━━━━━─────────────────────── 31% ↻ NaNd NaNh ctx ━━━━━━━━━━━━━━━━───────────────── 49% 97k/200k WHERE IT GOES · before the meter $0.42
README

spend-meter

CI License: MIT

A live spend meter for Claude Code. It docks at the right of the terminal and shows what the session has cost so far, as it happens: the main thread, every subagent and every workflow agent, the burn rate, your subscription limits and where the money goes.

The spend meter docked beside a Claude Code session: the total, the turn, burn rate and budget, a chart of spend stacked by main thread, subagents and workflows, the 5-hour, 7-day and context gauges, the sources with an open workflow, and the token split

Install

Type this at a Claude Code prompt in a terminal:

/plugin install spend-meter --marketplace Hekzory/spend-meter

Answer y to add the marketplace, then pick a scope; the user scope loads the meter in every session. From a shell, the same is claude plugin marketplace add Hekzory/spend-meter && claude plugin install spend-meter@spend-meter.

It needs Claude Code 2.1.296 or later; showing a subagent's transcript from its row needs 2.1.300. The pane opens by itself in a wide terminal; /spend opens it at any width.

Claude Code updates plugins from marketplaces other than its own only when told to: turn it on in /plugin → Marketplaces → spend-meter → Enable auto-update, and each new version arrives when Claude Code starts. Otherwise, claude plugin update spend-meter@spend-meter fetches it.

Using it

/spendtoggle the pane (it also works mid-turn)
/spend budget 25alerts at 50 %, 80 % and 100 % of $25; budget off clears it
/spend chipthe one-line meter under the prompt while the pane is closed
/spend unitwhat the big number counts
`/spend milestones on\off`the toasts at $1, π, $10, $42…
`/spend gfx auto\cells\pixels`how the chart is drawn
/spend rebuildread the session back from its transcripts again

Press the line under the total to change what it counts. Press a row under where it goes to see its model, requests, tokens, cache hit and, for a workflow, its agents; a subagent's row can also put that agent's transcript in the main view, and bring the main conversation back. Press the ? by cache served for what the cache saved.

What it draws with

  • The chart stacks what the main thread, subagents and workflows spent per 30 seconds over the last ~20 minutes, in 24-bit block elements exact to an eighth of a cell; a │ marks where the session compacted. Windows Terminal and kitty both draw block elements as geometry, so it comes out seamless.
  • kitty and Ghostty also get the chart as an anti-aliased picture through the kitty graphics protocol, the one picture protocol Claude Code's Image speaks (no Sixel, no iTerm2). /spend gfx cells|pixels|auto overrides the choice.

What the numbers are

  • History is read back from the session's transcripts when the meter starts (the main thread's, subagents/ and subagents/workflows/), every response priced exactly with its 5-minute and 1-hour cache writes split, so a meter that starts late or after --resume shows the whole session. /spend rebuild reads them again.
  • The big total is the engine's own ledger, the figure /cost and the status line show: every priced response of the session, subagents, workflow agents and the engine's side calls (auto-mode classifier, titles, compaction) included. On a subscription it is what the session would cost at API list prices.
  • Where it goes prices each response with Claude Code 2.1.300's own catalog (hooks/pricing.ts). The engine does not tell a mod which response cost what, so the rows are estimates; a row marked ≥ has a model the catalog lacks. Main-thread cache writes are priced at the 1-hour rate on a subscription and subagents' at the 5-minute rate, as the CLI requests them. side calls (the classifier, titles) is the rest of the total; before the meter is what the session had spent when the meter started.
  • Limits are the account's 5-hour and 7-day windows from the last response. They are account-wide: other sessions move them too. +N pts since start is how far a window moved while this session ran; the pace line projects the last 15 minutes forward and warns when the window would fill before it resets.
  • ctx is the context window; "≈ $X to re-read per main request" is that context priced at the main model's cache-read rate, the floor of every request. A compaction is noted under it (and marked on the chart); nothing resets. /clear starts the meter over.
  • Tokens are split by what is paid for: what the models wrote (output), new input stored in the prompt cache (1.25× the input price for 5 minutes, 2× for an hour), input re-read from the cache (0.1× of input, 0.05× on Opus 5.5, 0.025× on Fable 5.1), and input never cached.
  • Colors are ones a 256-color terminal rounds the same way in the chart and in text. Windows Terminal draws 24-bit color but does not say so; with COLORTERM=truecolor set, Claude Code uses it.

When a Claude Code update changes prices or models, update hooks/pricing.ts.

Development

The mod is plugin/, and only that folder is what an install copies. The repository around it (this README, the tools in package.json, CI, the screenshot) stays behind. Claude Code installs a plugin folder's own packages when it finds a package.json and a lockfile in it, so the dev tools must not live there.

The tools come from Bun, as Claude Code itself is built with it:

bun install          # Claude Code and TypeScript, pinned in package.json
bun run validate     # claude plugin validate --strict, the marketplace and the mod
bun run test         # claude plugin test plugin

bun run test, not bun test: the tests run in Claude Code's own test kit, not Bun's runner.

claude --plugin-dir plugin runs a session with the working copy loaded, and reloads it as files change. That first load also writes the engine's types into plugin/.claude-plugin/types/; after it, bun run typecheck type-checks the mod.

A release is a new version in plugin/.claude-plugin/plugin.json: Claude Code compares versions, so commits under the same version never reach an install.

CI runs the validator and the tests on every push and pull request, and daily, against the Claude Code in package.json and the newest one on npm. Dependabot checks the pins daily.

docs/screenshot.png shows a made-up session: bun run screenshot (needs uv) drives docs/screenshot/scenario.tsx through the real mod under the engine's test kit and paints the pane it draws.

License

MIT

Source 7 files
hooks/register.tsx 715 lines
1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, ModelUsage, Register } from 'claude-code'
3
4import type { SpendAgentMeta, SpendCore, SpendLive, SpendPrefs } from '../types'
5import {
6  absorbUsage,
7  addStep,
8  addToSeries,
9  attribute,
10  EMPTY_SERIES,
11  EMPTY_SOURCES,
12  ensureRow,
13  finishAgent,
14  fmtEta,
15  fmtUsd,
16  pace,
17} from './ledger'
18import { rebuild } from './history'
19import type { TranscriptFile } from './history'
20import { outputRate, partsTotal, priceUsage } from './pricing'
21import type { Parts } from './pricing'
22import { capsOf, chipText, clockText, drawPane, OLD_ENGINE, UNITS } from './view'
23
24const PANE = 'spend'
25const TITLE = 'Spend'
26const TICK_MS = 250
27const POLL_EVERY = 4 // ticks: the engine's figures once a second
28const CONTEXT_CONFIRM_MS = 10_000
29const CONTEXT_RETRY_MS = 1000
30// The ledger's format: a session whose ledger is older is read back from its transcripts again.
31const LEDGER = 3
32
33const DEFAULT_PREFS: SpendPrefs = { unit: 0, chip: true, eggs: true, budget: null, open: true, selected: null }
34const IDLE: SpendLive = { tick: 0, inflight: 0, estUsd: 0, keys: [], flash: null }
35
36const core = atom({ plugin: 'spend-meter', key: 'core' } as const, null)
37const sources = atom({ plugin: 'spend-meter', key: 'sources' } as const, EMPTY_SOURCES)
38const series = atom({ plugin: 'spend-meter', key: 'series' } as const, EMPTY_SERIES)
39const prefs = atom({ plugin: 'spend-meter', key: 'prefs' } as const, DEFAULT_PREFS)
40const live = atom({ plugin: 'spend-meter', key: 'live' } as const, IDLE)
41
42const MILESTONES: readonly [number, string][] = [
43  [1, 'First dollar. Frame it.'],
44  [Math.PI, 'π dollars. Irrational, yet here we are.'],
45  [10, 'Double digits: the meter is warming up.'],
46  [42, '$42: the answer to life, the universe, and this session.'],
47  [100, 'Triple digits. Worth a look at who is spending it.'],
48  [256, '$256: a round number, in binary.'],
49  [1000, 'Four digits. Somewhere a GPU fan spins a little faster.'],
50]
51
52const HELP = [
53  '/spend              toggle the meter pane',
54  '/spend budget 25    alert at 50 %, 80 % and 100 % of $25 (budget off clears it)',
55  '/spend chip         the one-line meter under the prompt while the pane is closed',
56  '/spend unit         cycle what the big number counts',
57  '/spend milestones on|off   a toast at $1, $10, $100 and a few rounder numbers',
58  '/spend gfx auto|cells|pixels   the chart as block cells, or as a kitty-protocol picture',
59  '/spend rebuild      read the session back from its transcripts again',
60  'In the pane: press the line under the total to change units, a row of "where it goes"',
61  'for its model, tokens, cache hit and agents, and the ? by "cache served" for what it means.',
62].join('\n')
63
64type Flight = { key: string; model: string; chars: number }
65type Held = { agentId?: string; model: string; usage: ModelUsage; parts: Parts | undefined; at: number }
66
67// The module's own memory: gone on a reload, rebuilt from `$.state` where it matters.
68const mem = {
69  agents: {} as Record<string, SpendAgentMeta>,
70  isSubscriber: false,
71  isPaneOpen: false,
72  chip: undefined as string | undefined,
73  ticks: 0,
74  isPolling: false,
75  /** Set when a response finished: the total is read again at the next tick. */
76  isStale: false,
77  wasBusy: false,
78  flashUntil: 0,
79  flights: new Map<string, Flight>(),
80  split: { main: 0, agents: 0, workflows: 0 },
81  /** What the terminal draws a picture with: kitty-protocol pixels, or cells. */
82  gfx: 'cells' as 'cells' | 'pixels',
83  /** Responses that finished while the transcripts were being read back. */
84  backfilling: false,
85  /** What the pane last showed that moves with the clock alone; see `clockText`. */
86  clock: undefined as string | undefined,
87  /** The subagent whose transcript the person has on screen, as the pane was last drawn. */
88  viewedAgentId: undefined as string | undefined,
89  /** What this engine draws and does, by its version. */
90  caps: OLD_ENGINE,
91  held: [] as Held[],
92  /** The main conversation's context as last read; see `sawContext`. */
93  ctxTokens: undefined as number | undefined,
94  contextPending: undefined as { until: number; nextAt: number; isReading: boolean } | undefined,
95}
96
97// ─── the session's past ──────────────────────────────────────────────────────
98
99async function backfill($: EngineInterface, cwd: string, force: boolean): Promise<number> {
100  const before = await read($, core)
101  if (mem.backfilling || ((before?.ledger ?? 0) >= LEDGER && !force)) return -1
102  mem.backfilling = true
103  let found = 0
104  try {
105    const cutoff = await $.clock.now()
106    const r = rebuild(await sessionFiles($, cwd), cutoff)
107    found = r.responses
108    if (r.responses > 0) {
109      const s = await update($, sources, live => ({
110        ...r.sources,
111        agents: { ...r.sources.agents, ...live.agents },
112        workflows: { ...live.workflows },
113      }))
114      mem.agents = s.agents
115      await update($, series, () => r.series)
116    }
117    await update($, core, c => {
118      if (c === null) return c
119      const turn =
120        c.turn ?? (r.turn === undefined ? null : { startedAt: r.turn.startedAt, usd0: c.usd - r.turn.usd, endedAt: r.turn.endedAt, usd1: c.usd })
121      const compactions = [...r.compactions, ...(c.compactions ?? []).filter(one => one.at > cutoff)].slice(-20)
122      return { ...c, ledger: LEDGER, usd0: r.responses > 0 ? 0 : c.usd0, turn, compactions }
123    })
124  } finally {
125    mem.backfilling = false
126    const held = mem.held.splice(0)
127    if (held.length > 0) {
128      await update($, sources, s => held.reduce((acc, one) => addStep(acc, attribute(one.agentId, acc), one.model, one.usage, one.parts, one.at), s))
129    }
130  }
131  return found
132}
133
134const CHUNK = 3 * 1024 * 1024
135
136/** A file's text, whole: one read under the read cap, else in `dd` chunks. */
137async function readText($: EngineInterface, path: string, size: number): Promise<string> {
138  if (size <= 3_900_000) return String(await $.fs.read(path))
139  let text = ''
140  for (let k = 0; k * CHUNK < size; k++) {
141    const r = await $.process.run(['dd', `if=${path}`, `bs=${CHUNK}`, `skip=${k}`, 'count=1', 'status=none'], {
142      timeoutMs: 20_000,
143    })
144    if (r.exitCode !== 0) break
145    text += r.stdout
146  }
147  return text
148}
149
150async function readMeta($: EngineInterface, path: string): Promise<Record<string, unknown>> {
151  try {
152    return JSON.parse(String(await $.fs.read(path))) as Record<string, unknown>
153  } catch {
154    return {}
155  }
156}
157
158/** Every transcript of this session: the main thread's, its subagents', its workflows'. */
159export async function sessionFiles($: EngineInterface, cwd: string, mainOnly = false): Promise<TranscriptFile[]> {
160  const id = await $.session.id()
161  const config = (await $.env.get('CLAUDE_CONFIG_DIR')) ?? `${(await $.env.get('HOME')) ?? ''}/.claude`
162  const projects = `${config}/projects`
163  let main = `${projects}/${cwd.replace(/[^A-Za-z0-9]/g, '-')}/${id}.jsonl`
164  if (!(await $.fs.exists(main))) {
165    main = ''
166    for (const dir of await $.fs.list(projects).catch(() => [])) {
167      const candidate = `${projects}/${dir.name}/${id}.jsonl`
168      if (dir.kind === 'dir' && (await $.fs.exists(candidate))) {
169        main = candidate
170        break
171      }
172    }
173    if (main === '') return []
174  }
175  const root = main.slice(0, -'.jsonl'.length)
176  const files: TranscriptFile[] = [{ text: await readText($, main, (await $.fs.stat(main)).size) }]
177  if (mainOnly) return files
178
179  for (const e of await $.fs.list(`${root}/subagents`).catch(() => [])) {
180    const agentId = /^agent-(.+)\.jsonl$/.exec(e.name)?.[1]
181    if (e.kind !== 'file' || agentId === undefined) continue
182    const meta = await readMeta($, `${root}/subagents/agent-${agentId}.meta.json`)
183    const type = typeof meta['agentType'] === 'string' ? meta['agentType'] : 'agent'
184    const description = typeof meta['description'] === 'string' ? meta['description'] : agentId
185    files.push({
186      agentId,
187      meta: { label: `${type} · ${description}`, type },
188      text: await readText($, `${root}/subagents/${e.name}`, e.size),
189    })
190  }
191
192  // A run's name is in its saved script's file name: `<name>-<runId>.js`.
193  const names: Record<string, string> = {}
194  for (const e of await $.fs.list(`${root}/workflows/scripts`).catch(() => [])) {
195    const m = /^(.+)-(wf_[^.]+)\.js$/.exec(e.name)
196    if (m !== null) names[m[2]!] = m[1]!
197  }
198  for (const run of await $.fs.list(`${root}/subagents/workflows`).catch(() => [])) {
199    if (run.kind !== 'dir') continue
200    const runId = run.name
201    const label = names[runId] ?? `workflow ${runId.slice(3, 9)}`
202    for (const e of await $.fs.list(`${root}/subagents/workflows/${runId}`).catch(() => [])) {
203      const agentId = /^agent-(.+)\.jsonl$/.exec(e.name)?.[1]
204      if (e.kind !== 'file' || agentId === undefined) continue
205      const meta = await readMeta($, `${root}/subagents/workflows/${runId}/agent-${agentId}.meta.json`)
206      const type = typeof meta['description'] === 'string' ? meta['description'] : 'agent'
207      files.push({
208        agentId,
209        meta: { label, type, runId },
210        text: await readText($, `${root}/subagents/workflows/${runId}/${e.name}`, e.size),
211      })
212    }
213  }
214  return files
215}
216
217async function detectGfx($: EngineInterface): Promise<'cells' | 'pixels'> {
218  // As Claude Code decides where its Image draws pixels: kitty and Ghostty,
219  // never through a multiplexer, or wherever it is told to.
220  if ((await $.env.get('CLAUDE_CODE_FORCE_TERMINAL_IMAGES')) !== undefined) return 'pixels'
221  if ((await $.env.get('TMUX')) !== undefined || (await $.env.get('STY')) !== undefined) return 'cells'
222  const term = (await $.env.get('TERM')) ?? ''
223  const program = ((await $.env.get('TERM_PROGRAM')) ?? '').toLowerCase()
224  if ((await $.env.get('KITTY_WINDOW_ID')) !== undefined || term === 'xterm-kitty' || program === 'kitty') return 'pixels'
225  if (program === 'ghostty' || term === 'xterm-ghostty') return 'pixels'
226  return 'cells'
227}
228
229// ─── the engine's figures ────────────────────────────────────────────────────
230
231async function tick($: EngineInterface) {
232  mem.ticks += 1
233  const isBusy = mem.flights.size > 0 || mem.flashUntil > 0
234  if (isBusy || mem.wasBusy) await animate($)
235  mem.wasBusy = isBusy
236  if ((mem.ticks % POLL_EVERY === 0 || mem.isStale) && !mem.isPolling) {
237    mem.isPolling = true
238    mem.isStale = false
239    try {
240      await measure($)
241    } finally {
242      mem.isPolling = false
243    }
244  }
245}
246
247async function measure($: EngineInterface) {
248  const pane = (await $.ui.panes()).find(one => one.id === PANE)
249  mem.isPaneOpen = pane !== undefined && pane.isPlaced && pane.isShown
250  const u = await $.session.usage()
251  await sawContext($, u.context.tokens)
252  const now = await $.clock.now()
253  const p = await read($, prefs)
254  let before = await read($, core)
255  if (before !== null && (u.cost?.usd ?? before.usd) < before.usd - 1e-9) {
256    await resetSession($)
257    before = null
258  }
259
260  // A session standing still writes nothing, so nothing is drawn again: the clock alone
261  // redraws a quiet pane, when the text it moves changes (`redrawOnClock`).
262  const isStill = before !== null && isQuiet(before, absorbUsage(before, u, now), p, now)
263  let raised: string[] = []
264  const after = isStill ? before : await update($, core, c => {
265    const n = absorbUsage(c, u, now)
266    raised = c === null ? [] : alertsFor(c, n, p, now)
267    // Milestones already behind a session the meter joins late stay quiet.
268    const quiet = c === null ? MILESTONES.filter(([m]) => n.usd >= m).map(([m]) => `m:${m}`) : []
269    return { ...n, fired: [...n.fired, ...quiet, ...raised].slice(-512) }
270  })
271  mem.isSubscriber = after?.isSubscriber ?? false
272
273  const parts = { ...mem.split }
274  mem.split.main = mem.split.agents = mem.split.workflows = 0
275  // An empty bucket is not written: the chart and the burn count the buckets since the last.
276  if (parts.main + parts.agents + parts.workflows > 0) await update($, series, s => addToSeries(s, now, parts))
277  // The context a compaction left, once a response has measured it.
278  const last = after?.compactions?.at(-1)
279  if (last !== undefined && last.to === undefined && u.context.tokens !== undefined && now - last.at > 1000) {
280    const to = u.context.tokens
281    await update($, core, c =>
282      c === null ? c : { ...c, compactions: (c.compactions ?? []).map((one, i, all) => (i === all.length - 1 ? { ...one, to } : one)) },
283    )
284  }
285
286  // A budget set below what is already spent says its highest step alone.
287  const steps = raised.filter(id => id.startsWith('b:')).map(id => Number(id.split(':')[2]))
288  const top = Math.max(0, ...steps)
289  const said = raised.filter(id => !id.startsWith('b:') || Number(id.split(':')[2]) === top)
290  if (after !== null) for (const id of said) announce($, id, after, p, now)
291  await showChip($)
292  if (mem.isPaneOpen) await redrawOnClock($, after, p, now)
293}
294
295/** Whether a reading differs from the last only in when it was taken, and raises nothing. */
296function isQuiet(prev: SpendCore, next: SpendCore, p: SpendPrefs, now: number): boolean {
297  return JSON.stringify({ ...prev, at: 0 }) === JSON.stringify({ ...next, at: 0 }) && alertsFor(prev, next, p, now).length === 0
298}
299
300/** Missing usage can also occur during a routed response. Confirm it against the
301 * main transcript, retrying for a bounded window even after usage recovers:
302 * the boundary can be flushed later, and a transcript read can fail temporarily.
303 */
304async function sawContext($: EngineInterface, tokens: number | undefined) {
305  const from = mem.ctxTokens
306  mem.ctxTokens = tokens
307  const disappeared = from !== undefined && tokens === undefined
308  if (!disappeared && mem.contextPending === undefined) return
309  const at = await $.clock.now()
310  if (disappeared) mem.contextPending = { until: at + CONTEXT_CONFIRM_MS, nextAt: at, isReading: false }
311  const pending = mem.contextPending
312  if (pending === undefined) return
313  if (at > pending.until) {
314    mem.contextPending = undefined
315    return
316  }
317  if (pending.isReading || at < pending.nextAt) return
318  pending.nextAt = at + CONTEXT_RETRY_MS
319  pending.isReading = true
320  try {
321    const files = await sessionFiles($, await $.session.cwd(), true)
322    if (files.length === 0 || mem.contextPending !== pending) return
323    const confirmed = rebuild(files, at).compactions
324    const before = await read($, core)
325    if (before === null || !confirmed.some(one => !(before.compactions ?? []).some(prev => prev.at === one.at))) return
326    await update($, core, c => c === null ? c : {
327      ...c,
328      compactions: confirmed.map(one => ({ ...one, to: one.to ?? c.compactions?.find(prev => prev.at === one.at)?.to })),
329    })
330    if (mem.contextPending === pending) mem.contextPending = undefined
331  } finally {
332    pending.isReading = false
333  }
334}
335
336/** Draws the pane again when what it shows moved with the clock alone. */
337async function redrawOnClock($: EngineInterface, c: SpendCore | null, p: SpendPrefs, now: number) {
338  const [se, so] = await Promise.all([read($, series), read($, sources)])
339  const text = clockText({ now, core: c, series: se, sources: so, prefs: p, viewedAgentId: mem.viewedAgentId })
340  if (text === mem.clock) return
341  const isFirst = mem.clock === undefined
342  mem.clock = text
343  if (!isFirst) $.ui.invalidate('ui.render')
344}
345
346function alertsFor(prev: SpendCore, next: SpendCore, p: SpendPrefs, now: number): string[] {
347  const ids: string[] = []
348  const fresh = (id: string) => !next.fired.includes(id) && !ids.includes(id)
349  if (p.eggs) {
350    for (const [m] of MILESTONES) if (prev.usd < m && next.usd >= m && fresh(`m:${m}`)) ids.push(`m:${m}`)
351  }
352  if (p.budget !== null && p.budget > 0) {
353    for (const step of [50, 80, 100]) {
354      const id = `b:${p.budget}:${step}`
355      if (next.usd >= (p.budget * step) / 100 && fresh(id)) ids.push(id)
356    }
357  }
358  const w = next.windows['five_hour']
359  const pc = w === undefined ? undefined : pace(w, now)
360  if (pc?.isShort === true && pc.fullAt !== undefined && pc.fullAt - now < 3 * 3600_000) {
361    const id = `p:${Math.floor(now / (20 * 60_000))}`
362    if (fresh(id)) ids.push(id)
363  }
364  return ids
365}
366
367function announce($: EngineInterface, id: string, c: SpendCore, p: SpendPrefs, now: number) {
368  if (id.startsWith('m:')) {
369    const line = MILESTONES.find(([m]) => `m:${m}` === id)?.[1]
370    if (line !== undefined) $.ui.toast(`${fmtUsd(c.usd)} · ${line}`)
371    return
372  }
373  if (id.startsWith('b:') && p.budget !== null) {
374    const step = Number(id.split(':')[2])
375    if (step >= 100) {
376      const text = `Budget of ${fmtUsd(p.budget)} reached: ${fmtUsd(c.usd)} spent this session.`
377      $.ui.toast(text, { timeoutMs: 8000 })
378      void $.ui.notify(text, { title: 'Spend meter' }).catch(() => undefined)
379    } else {
380      $.ui.toast(`${step} % of the ${fmtUsd(p.budget)} budget spent (${fmtUsd(c.usd)}).`)
381    }
382    return
383  }
384  if (id.startsWith('p:')) {
385    const w = c.windows['five_hour']
386    const pc = w === undefined ? undefined : pace(w, now)
387    if (pc?.fullAt === undefined) return
388    const reset = pc.resetAt === undefined ? '' : ` (it resets in ${fmtEta(pc.resetAt - now)})`
389    $.ui.toast(`⚠ 5-hour limit: full in ~${fmtEta(pc.fullAt - now)} at this pace${reset}.`, { timeoutMs: 8000 })
390  }
391}
392
393async function showChip($: EngineInterface) {
394  const p = await read($, prefs)
395  const text = !mem.isPaneOpen && p.chip ? chipText(await read($, core), await read($, live)) : undefined
396  if (text === mem.chip) return
397  mem.chip = text
398  $.ui.status(text)
399}
400
401async function resetSession($: EngineInterface) {
402  await update($, core, () => null)
403  await update($, sources, () => EMPTY_SOURCES)
404  await update($, series, () => EMPTY_SERIES)
405  await update($, prefs, p => ({ ...p, selected: null }))
406  mem.agents = {}
407  mem.flights.clear()
408  mem.ctxTokens = undefined
409  mem.contextPending = undefined
410}
411
412// ─── what is streaming now ───────────────────────────────────────────────────
413
414async function animate($: EngineInterface) {
415  const now = await $.clock.now()
416  if (mem.flashUntil > 0 && mem.flashUntil <= now) mem.flashUntil = 0
417  let estUsd = 0
418  for (const f of mem.flights.values()) estUsd += ((f.chars / 3.6) * outputRate(f.model)) / 1e6
419  const keys = [...new Set([...mem.flights.values()].map(f => f.key))]
420  const isBusy = mem.flights.size > 0 || mem.flashUntil > 0
421  // With the pane away, the chip only needs the count now and then.
422  if (isBusy && !mem.isPaneOpen && mem.ticks % POLL_EVERY !== 0) return
423  await update($, live, l => ({
424    tick: isBusy ? l.tick + 1 : l.tick,
425    inflight: mem.flights.size,
426    estUsd,
427    keys,
428    flash: mem.flashUntil > 0 ? { kind: 'brrr' as const, until: mem.flashUntil } : null,
429  }))
430}
431
432// ─── preferences and the pane ────────────────────────────────────────────────
433
434async function setPrefs($: EngineInterface, change: (p: SpendPrefs) => SpendPrefs): Promise<SpendPrefs> {
435  const p = await update($, prefs, change)
436  await $.store.set('prefs', { unit: p.unit, chip: p.chip, eggs: p.eggs, budget: p.budget, open: p.open, gfx: p.gfx ?? 'auto' })
437  return p
438}
439
440async function setOpen($: EngineInterface, want: boolean | undefined): Promise<string> {
441  const pane = (await $.ui.panes()).find(one => one.id === PANE)
442  // Open but waiting for room, or a tab behind another pane, counts as closed:
443  // the person asking for it then brings it up rather than putting it away.
444  const isUp = pane !== undefined && pane.isPlaced && pane.isShown
445  const open = want ?? !isUp
446  if (!open) {
447    if (pane !== undefined) await $.ui.close({ id: PANE })
448    mem.isPaneOpen = false
449    const p = await setPrefs($, x => ({ ...x, open: false }))
450    await showChip($)
451    return p.chip ? 'Spend meter closed; its chip stays under the prompt (/spend chip hides it).' : 'Spend meter closed.'
452  }
453  const placed = await $.ui.open({ id: PANE, title: TITLE, columns: 44 })
454  mem.isPaneOpen = placed.isPlaced
455  await setPrefs($, x => ({ ...x, open: true }))
456  await showChip($)
457  if ((await $.store.get('welcomed')) !== true) {
458    await $.store.set('welcomed', true)
459    $.ui.toast('Spend meter: press a row for its details · /spend help', { timeoutMs: 7000 })
460  }
461  return placed.isPlaced ? 'Spend meter open.' : `Spend meter open, waiting for room: ${placed.reason}`
462}
463
464async function runCommand($: EngineInterface, args: string): Promise<string> {
465  const [verb = '', arg] = args.trim().toLowerCase().split(/\s+/)
466  switch (verb) {
467    case '':
468      return setOpen($, undefined)
469    case 'on':
470    case 'open':
471    case 'show':
472      return setOpen($, true)
473    case 'off':
474    case 'close':
475    case 'hide':
476      return setOpen($, false)
477    case 'budget': {
478      if (arg === undefined) {
479        const p = await read($, prefs)
480        return p.budget === null ? 'No budget set; /spend budget 25 sets one.' : `Budget: ${fmtUsd(p.budget)}.`
481      }
482      if (arg === 'off' || arg === 'none' || arg === '0') {
483        await setPrefs($, p => ({ ...p, budget: null }))
484        return 'Budget cleared.'
485      }
486      const usd = Number(arg.replace(/^\$/, ''))
487      if (!Number.isFinite(usd) || usd <= 0) return `Not an amount: ${arg}. Try /spend budget 25.`
488      await setPrefs($, p => ({ ...p, budget: usd }))
489      const c = await read($, core)
490      return `Budget set: ${fmtUsd(usd)} (${fmtUsd(c?.usd ?? 0)} spent so far).`
491    }
492    case 'chip': {
493      const p = await setPrefs($, x => ({ ...x, chip: arg === 'on' ? true : arg === 'off' ? false : !x.chip }))
494      await showChip($)
495      return p.chip ? 'Chip on: shown under the prompt while the pane is closed.' : 'Chip off.'
496    }
497    case 'unit': {
498      const p = await setPrefs($, x => ({ ...x, unit: (x.unit + 1) % UNITS.length }))
499      return `The big number now counts ${UNITS[p.unit]!.label}.`
500    }
501    case 'milestones': {
502      const p = await setPrefs($, x => ({ ...x, eggs: arg === 'on' ? true : arg === 'off' ? false : !x.eggs }))
503      return p.eggs ? 'Milestone toasts on.' : 'Milestone toasts off.'
504    }
505    case 'brrr':
506      mem.flashUntil = (await $.clock.now()) + 3600
507      return 'money printer go brrr'
508    case 'gfx': {
509      const want = arg === 'cells' || arg === 'pixels' || arg === 'auto' ? arg : undefined
510      if (want === undefined) {
511        const p = await read($, prefs)
512        return `Chart drawn as ${p.gfx === 'cells' || p.gfx === 'pixels' ? p.gfx : `${mem.gfx} (auto)`}; /spend gfx auto|cells|pixels changes it.`
513      }
514      await setPrefs($, x => ({ ...x, gfx: want }))
515      return want === 'auto' ? `Chart drawn as ${mem.gfx}, as this terminal takes.` : `Chart drawn as ${want}.`
516    }
517    case 'rebuild': {
518      const found = await backfill($, await $.session.cwd(), true)
519      return found < 0 ? 'Already reading the transcripts.' : `Read ${found} responses back from the transcripts.`
520    }
521    default:
522      return HELP
523  }
524}
525
526// ─── the hooks ───────────────────────────────────────────────────────────────
527
528export const register: Register = on => {
529  on('session.start', async ($, e, next) => {
530    mem.agents = (await read($, sources)).agents
531    await $.command.register({
532      name: 'spend',
533      description: 'Toggle the live spend meter: cost with subagents and workflows, limits, burn rate',
534      argumentHint: '[budget <usd>|chip|unit|milestones|help]',
535      immediate: true,
536    })
537    mem.caps = capsOf(await $.session.version().catch(() => undefined))
538    const held = await $.state.get({ plugin: 'spend-meter', key: 'prefs' })
539    if (held.version === 0) {
540      const stored = await $.store.get('prefs')
541      const saved = typeof stored === 'object' && stored !== null ? (stored as Partial<SpendPrefs>) : {}
542      await update($, prefs, () => ({ ...DEFAULT_PREFS, ...saved, selected: null }))
543    }
544    if (e.isInteractive) {
545      mem.gfx = await detectGfx($)
546      await measure($).catch(() => undefined)
547      $.clock.every(TICK_MS, () => void tick($).catch(() => undefined))
548      // Read the session's past back after start, not before the first prompt.
549      const cwd = e.cwd
550      $.clock.after(50, () => void backfill($, cwd, false).catch(() => undefined))
551      const p = await read($, prefs)
552      if (p.open && !(await $.ui.panes()).some(pane => pane.id === PANE)) {
553        mem.isPaneOpen = (await $.ui.open({ id: PANE, title: TITLE, columns: 44 })).isPlaced
554      }
555      await showChip($)
556    }
557    return next(e)
558  })
559
560  on('session.end', async ($, e, next) => {
561    if (e.reason === 'clear') await resetSession($).catch(() => undefined)
562    return next(e)
563  })
564
565  on('ui.close', { id: PANE }, async ($, e, next) => {
566    const r = await next(e)
567    if (e.origin.kind !== 'plugin') mem.isPaneOpen = false
568    if (e.origin.kind === 'person') await setPrefs($, p => ({ ...p, open: false })).catch(() => undefined)
569    return r
570  }).catch(($, e, next) => next(e))
571
572  // Subagents' and teammates' turns start here too: only the main loop's is "this turn".
573  on('turn.start', async ($, e, next) => {
574    const r = await next(e)
575    if (e.agentId === undefined) {
576      const now = await $.clock.now()
577      await update($, core, c => (c === null ? c : { ...c, turn: { startedAt: now, usd0: c.usd } }))
578    }
579    return r
580  })
581
582  on('turn.complete', async ($, e, next) => {
583    const r = await next(e)
584    if (e.agentId === undefined) {
585      const now = await $.clock.now()
586      const u = await $.session.usage()
587      await update($, core, c =>
588        c === null || c.turn === null ? c : { ...c, turn: { ...c.turn, endedAt: now, usd1: u.cost?.usd ?? c.usd } },
589      )
590    } else {
591      const status = e.reason === 'answer' ? 'done' : 'failed'
592      const agentId = e.agentId
593      await update($, sources, s => finishAgent(s, agentId, status))
594    }
595    return r
596  })
597
598  on('tool.call', { tool: 'Workflow' }, async ($, e, next) => {
599    let name = e.name
600    if (name === undefined && e.script !== undefined) name = /\bname\s*:\s*['"`]([^'"`]+)['"`]/.exec(e.script)?.[1]
601    if (name === undefined && e.scriptPath !== undefined) {
602      const text = await $.fs.read(e.scriptPath).catch(() => '')
603      name = /\bname\s*:\s*['"`]([^'"`]+)['"`]/.exec(text)?.[1]
604    }
605    if (name !== undefined) {
606      const label = name
607      await update($, sources, s => ({ ...s, workflows: { ...s.workflows, [e.tool_use_id]: label } }))
608    }
609    return next(e)
610  }).catch(($, e, next) => next(e))
611
612  on('agent.spawn', async ($, e, next) => {
613    const r = await next(e)
614    if (r.deny !== undefined || r.agentId === undefined) return r
615    const agentId = r.agentId
616    const runId = e.workflow?.runId
617    const now = await $.clock.now()
618    await update($, sources, s => {
619      const wfName = s.workflows[e.tool_use_id] ?? (runId === undefined ? undefined : `workflow ${runId.slice(3, 9)}`)
620      // A resumed agent keeps the name it was spawned with.
621      const meta: SpendAgentMeta =
622        s.agents[agentId] ??
623        (runId === undefined
624          ? { label: `${e.subagentType} · ${e.description}`, type: e.subagentType, model: r.model }
625          : { label: wfName ?? 'workflow', type: e.description || e.subagentType, runId, model: r.model })
626      const withMeta = { ...s, agents: { ...s.agents, [agentId]: meta } }
627      mem.agents = withMeta.agents
628      return ensureRow(withMeta, attribute(agentId, withMeta), now)
629    })
630    return r
631  }).catch(($, e, next) => next(e))
632
633  on('turn.step', async function* ($, e, next) {
634    // A compaction mid-turn is followed by a step at once, before a tick may have seen it.
635    if (e.agentId === undefined) await $.session.usage().then(u => sawContext($, u.context.tokens)).catch(() => undefined)
636    const flightId = `${e.agentId ?? 'main'}:${e.turnId}:${e.index}`
637    const flight: Flight = { key: attribute(e.agentId, { ...EMPTY_SOURCES, agents: mem.agents }).key, model: e.model, chars: 0 }
638    mem.flights.set(flightId, flight)
639    const seated = $.clock
640      .now()
641      .then(now => update($, sources, s => ensureRow(s, attribute(e.agentId, s), now)))
642      .catch(() => undefined)
643    const stream = next(e)
644    try {
645      for await (const chunk of stream) {
646        if (chunk.kind === 'text' || chunk.kind === 'thinking') flight.chars += chunk.text.length
647        else if (chunk.kind === 'input') flight.chars += chunk.json.length
648        yield chunk
649      }
650      const result = await stream.result
651      await seated
652      const usage = result.usage
653      if (usage !== null) {
654        const model = usage.model || e.model
655        const parts = priceUsage(model, usage, e.agentId === undefined && mem.isSubscriber ? '1h' : '5m')
656        const now = await $.clock.now()
657        const kind = e.agentId === undefined ? 'main' : mem.agents[e.agentId]?.runId !== undefined ? 'workflows' : 'agents'
658        mem.split[kind] += parts === undefined ? 0 : partsTotal(parts)
659        mem.isStale = true
660        if (mem.backfilling) {
661          mem.held.push({ agentId: e.agentId, model, usage, parts, at: now })
662        } else {
663          await update($, sources, s => addStep(s, attribute(e.agentId, s), model, usage, parts, now))
664        }
665      }
666      return result
667    } finally {
668      mem.flights.delete(flightId)
669    }
670  })
671
672  on('command.run', { command: 'spend' }, async ($, e) => ({ text: await runCommand($, e.args) }))
673
674  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
675    const ui = $.ui.resolve(e)
676    mem.viewedAgentId = e.props.view.agentId
677    const [c, s, se, p, l, now] = await Promise.all([
678      read($, core),
679      read($, sources),
680      read($, series),
681      read($, prefs),
682      read($, live),
683      $.clock.now(),
684    ])
685    return drawPane(
686      ui,
687      e.surface === 'terminal' && 'Raster' in ui ? { Raster: ui.Raster, Image: ui.Image } : undefined,
688      {
689        now,
690        core: c,
691        sources: s,
692        series: se,
693        prefs: p,
694        live: l,
695        width: e.props.bodyColumns,
696        rows: e.props.scroll.bodyRows,
697        placement: e.props.placement,
698        surface: e.surface,
699        viewedAgentId: e.props.view.agentId,
700        gfx: p.gfx === 'cells' || p.gfx === 'pixels' ? p.gfx : mem.gfx,
701        caps: mem.caps,
702      },
703      {
704        cycleUnit: () => void setPrefs($, x => ({ ...x, unit: (x.unit + 1) % UNITS.length })),
705        select: key => void update($, prefs, x => ({ ...x, selected: key })),
706        view: agentId =>
707          void $.ui
708            .view(agentId === undefined ? {} : { agentId })
709            .then(v => v.deny !== undefined && $.ui.toast(`Not shown: ${v.deny}`))
710            .catch(() => undefined),
711      },
712    )
713  })
714}
715
hooks/ledger.ts 420 lines
1// The meter's arithmetic: who spent what, how fast, and how it is written.
2// Pure functions over the state contract, so the tests drive them directly.
3
4import type { ModelUsage, SessionUsage } from 'claude-code'
5
6import { addParts, NO_PARTS, partsTotal } from './pricing'
7import type { Parts } from './pricing'
8
9import type {
10  SpendCore,
11  SpendSeries,
12  SpendSource,
13  SpendSourceKind,
14  SpendSources,
15  SpendWindow,
16} from '../types'
17
18export const BUCKET_MS = 30_000
19const KEEP_BUCKETS = 240 // two hours of history
20const KEEP_SAMPLES_MS = 30 * 60_000
21const PACE_SPAN_MS = 15 * 60_000
22
23export const EMPTY_SOURCES: SpendSources = { rows: {}, agents: {}, workflows: {} }
24export const EMPTY_SERIES: SpendSeries = { bucketMs: BUCKET_MS, t0: 0, buckets: [] }
25
26// ─── the engine's figures ────────────────────────────────────────────────────
27
28/** Folds a `$.session.usage()` reading into the core, starting it when absent. */
29export function absorbUsage(prev: SpendCore | null, u: SessionUsage, now: number): SpendCore {
30  const usd = u.cost?.usd ?? prev?.usd ?? 0
31  const limits = u.rateLimits.map(l => ({ kind: l.kind, pct: l.percentUsed, resetsAt: l.resetsAt }))
32  const base: SpendCore = prev ?? {
33    usd,
34    usd0: usd,
35    startedAt: u.startedAt,
36    at: now,
37    ctxWindow: u.context.window,
38    limits: [],
39    windows: {},
40    isSubscriber: false,
41    turn: null,
42    compactions: [],
43    fired: [],
44  }
45  return {
46    ...base,
47    usd,
48    startedAt: u.startedAt,
49    at: now,
50    ctxTokens: u.context.tokens,
51    ctxWindow: u.context.window,
52    ctxPct: u.context.percent,
53    limits,
54    windows: absorbWindows(base.windows, limits, now),
55    isSubscriber: base.isSubscriber || limits.length > 0,
56  }
57}
58
59function absorbWindows(
60  prev: Record<string, SpendWindow>,
61  limits: readonly { kind: string; pct: number; resetsAt?: string }[],
62  now: number,
63): Record<string, SpendWindow> {
64  const next: Record<string, SpendWindow> = { ...prev }
65  for (const l of limits) {
66    const w = prev[l.kind]
67    if (w === undefined) {
68      next[l.kind] = { first: l.pct, last: l.pct, resetsAt: l.resetsAt, carried: 0, samples: [[now, l.pct]] }
69      continue
70    }
71    const isReset = l.resetsAt !== undefined && w.resetsAt !== undefined && l.resetsAt !== w.resetsAt && l.pct < w.last
72    const samples = isReset ? [] : keepSamples(w.samples, now)
73    // Only a move is a sample: a window standing still writes nothing, so the pane is not drawn again.
74    if (samples.at(-1)?.[1] !== l.pct) samples.push([now, l.pct])
75    next[l.kind] = isReset
76      ? { first: l.pct, last: l.pct, resetsAt: l.resetsAt, carried: w.carried + Math.max(0, w.last - w.first), samples }
77      : { ...w, last: l.pct, resetsAt: l.resetsAt ?? w.resetsAt, samples }
78  }
79  return next
80}
81
82/** The recent samples, and the newest older one: where the window stood since. */
83function keepSamples(samples: readonly [number, number][], now: number): [number, number][] {
84  const i = samples.findIndex(([at]) => now - at <= KEEP_SAMPLES_MS)
85  return samples.slice(i === -1 ? -1 : Math.max(0, i - 1))
86}
87
88/** How far a window moved while this session watched it, in percent points. */
89export function windowMoved(w: SpendWindow): number {
90  return w.carried + Math.max(0, w.last - w.first)
91}
92
93export type Pace = {
94  /** Percent points a minute over the recent span. */
95  perMin: number
96  /** When it reaches 100 % at that pace, if it climbs at all. */
97  fullAt?: number
98  resetAt?: number
99  /** Whether the pace fills it before it resets. */
100  isShort: boolean
101}
102
103/** The window holds still between its samples: each is taken when it moves. */
104export function pace(w: SpendWindow, now: number): Pace | undefined {
105  const first = w.samples[0]
106  const last = w.samples.at(-1)
107  if (first === undefined || last === undefined) return undefined
108  const from = Math.max(first[0], now - PACE_SPAN_MS)
109  const span = now - from
110  if (span < 3 * 60_000) return undefined
111  // Where the window stood when the span began.
112  const start = w.samples.findLast(([at]) => at <= from) ?? first
113  const perMin = ((last[1] - start[1]) / span) * 60_000
114  const resetAt = w.resetsAt === undefined ? undefined : Date.parse(w.resetsAt)
115  const reset = resetAt !== undefined && Number.isFinite(resetAt) ? resetAt : undefined
116  if (perMin <= 0.001) return { perMin: 0, resetAt: reset, isShort: false }
117  const fullAt = now + ((100 - last[1]) / perMin) * 60_000
118  return { perMin, fullAt, resetAt: reset, isShort: reset === undefined || fullAt < reset }
119}
120
121// ─── who spent it ────────────────────────────────────────────────────────────
122
123export type Attribution = {
124  key: string
125  kind: SpendSourceKind
126  label: string
127  type?: string
128  /** The agent inside a workflow run's row. */
129  member?: { id: string; label: string }
130}
131
132export function attribute(agentId: string | undefined, src: SpendSources): Attribution {
133  if (agentId === undefined) return { key: 'main', kind: 'main', label: 'main thread' }
134  const meta = src.agents[agentId]
135  if (meta === undefined) return { key: 'fork', kind: 'fork', label: 'engine loops' }
136  if (meta.runId !== undefined) {
137    return {
138      key: `wf:${meta.runId}`,
139      kind: 'workflow',
140      label: meta.label,
141      type: 'workflow',
142      member: { id: agentId, label: meta.type },
143    }
144  }
145  return { key: `agent:${agentId}`, kind: 'agent', label: meta.label, type: meta.type }
146}
147
148/** Seats a row for a source that has started but not yet been billed. */
149export function ensureRow(src: SpendSources, who: Attribution, now: number): SpendSources {
150  const row = src.rows[who.key]
151  if (row !== undefined && (who.member === undefined || row.members?.[who.member.id] !== undefined)) {
152    return row.status === 'running' || who.kind === 'main' || who.kind === 'fork'
153      ? src
154      : { ...src, rows: { ...src.rows, [who.key]: { ...row, status: 'running' } } }
155  }
156  return addStep(src, who, undefined, { input_tokens: 0, output_tokens: 0, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 }, NO_PARTS, now, false)
157}
158
159/** Adds one response to its source's row. */
160export function addStep(
161  src: SpendSources,
162  who: Attribution,
163  model: string | undefined,
164  usage: ModelUsage,
165  parts: Parts | undefined,
166  now: number,
167  isStep = true,
168): SpendSources {
169  const step = isStep ? 1 : 0
170  const usd = parts === undefined ? undefined : partsTotal(parts)
171  const row: SpendSource = src.rows[who.key] ?? {
172    key: who.key,
173    kind: who.kind,
174    label: who.label,
175    type: who.type,
176    usd: 0,
177    cost: NO_PARTS,
178    isPriced: true,
179    tok: { in: 0, out: 0, cr: 0, cw: 0 },
180    steps: 0,
181    status: 'running',
182    firstAt: now,
183    lastAt: now,
184  }
185  const members = who.member === undefined ? row.members : { ...row.members }
186  if (who.member !== undefined && members !== undefined) {
187    const m = members[who.member.id] ?? { label: who.member.label, usd: 0, steps: 0, status: 'running' as const }
188    members[who.member.id] = { ...m, usd: m.usd + (usd ?? 0), steps: m.steps + step, status: 'running' }
189  }
190  const next: SpendSource = {
191    ...row,
192    label: who.label,
193    model: model ?? row.model,
194    usd: row.usd + (usd ?? 0),
195    cost: addParts(row.cost ?? NO_PARTS, parts),
196    isPriced: row.isPriced && usd !== undefined,
197    tok: {
198      in: row.tok.in + usage.input_tokens,
199      out: row.tok.out + usage.output_tokens,
200      cr: row.tok.cr + usage.cache_read_input_tokens,
201      cw: row.tok.cw + usage.cache_creation_input_tokens,
202    },
203    steps: row.steps + step,
204    status: who.kind === 'main' || who.kind === 'fork' ? row.status : 'running',
205    lastAt: isStep ? now : row.lastAt,
206    members,
207  }
208  return { ...src, rows: { ...src.rows, [who.key]: next } }
209}
210
211/** Marks an agent's row (or its member of a workflow row) done or failed. */
212export function finishAgent(src: SpendSources, agentId: string, status: 'done' | 'failed'): SpendSources {
213  const who = attribute(agentId, src)
214  const row = src.rows[who.key]
215  if (row === undefined || who.kind === 'main' || who.kind === 'fork') return src
216  if (who.member === undefined) return { ...src, rows: { ...src.rows, [who.key]: { ...row, status } } }
217  const members = { ...row.members }
218  const m = members[who.member.id]
219  if (m !== undefined) members[who.member.id] = { ...m, status }
220  const isRunning = Object.values(members).some(one => one.status === 'running')
221  const isFailed = Object.values(members).some(one => one.status === 'failed')
222  return {
223    ...src,
224    rows: { ...src.rows, [who.key]: { ...row, members, status: isRunning ? 'running' : isFailed ? 'failed' : 'done' } },
225  }
226}
227
228export function sumTokens(rows: readonly SpendSource[]) {
229  const t = { in: 0, out: 0, cr: 0, cw: 0 }
230  for (const r of rows) {
231    t.in += r.tok.in
232    t.out += r.tok.out
233    t.cr += r.tok.cr
234    t.cw += r.tok.cw
235  }
236  return t
237}
238
239export function sumCost(rows: readonly SpendSource[]): Parts {
240  return rows.reduce((acc, r) => addParts(acc, r.cost), NO_PARTS)
241}
242
243export function cacheHit(t: { in: number; cr: number; cw: number }): number | undefined {
244  const prompt = t.in + t.cr + t.cw
245  return prompt === 0 ? undefined : t.cr / prompt
246}
247
248// ─── over time ───────────────────────────────────────────────────────────────
249
250/**
251 * Moves the series up to `now`, adding what each source spent to the newest
252 * bucket: the bars are the priced responses, timed as they finished.
253 */
254export function addToSeries(s: SpendSeries, now: number, split: { main: number; agents: number; workflows: number }): SpendSeries {
255  const slot = Math.floor(now / s.bucketMs) * s.bucketMs
256  let buckets = s.buckets.slice()
257  let t0 = s.t0
258  if (t0 === 0 || buckets.length === 0) {
259    t0 = slot
260    buckets = [[0, 0, 0, 0]]
261  } else if (slot > t0) {
262    const gap = Math.min(KEEP_BUCKETS, Math.round((slot - t0) / s.bucketMs))
263    for (let i = 0; i < gap; i++) buckets.push([0, 0, 0, 0])
264    t0 = slot
265  }
266  const spent = split.main + split.agents + split.workflows
267  if (spent > 0) {
268    const last = buckets[buckets.length - 1]!
269    buckets[buckets.length - 1] = [
270      last[0] + spent,
271      last[1] + split.main,
272      last[2] + split.agents,
273      last[3] + split.workflows,
274    ]
275  }
276  if (buckets.length > KEEP_BUCKETS) buckets = buckets.slice(-KEEP_BUCKETS)
277  return { ...s, t0, buckets }
278}
279
280/** USD a minute over the last `spanMs`, from the series. */
281export function burnPerMin(s: SpendSeries, now: number, spanMs = 5 * 60_000): number {
282  if (s.buckets.length === 0) return 0
283  const n = Math.max(1, Math.round(spanMs / s.bucketMs))
284  const stale = Math.max(0, Math.round((Math.floor(now / s.bucketMs) * s.bucketMs - s.t0) / s.bucketMs))
285  const take = Math.min(s.buckets.length, n - stale)
286  if (take <= 0) return 0
287  const spent = s.buckets.slice(-take).reduce((a, b) => a + b[0], 0)
288  // A young session has not had the whole span: average over what it had.
289  const watched = Math.min(spanMs, (s.buckets.length + stale) * s.bucketMs)
290  return spent / (Math.max(60_000, watched) / 60_000)
291}
292
293// ─── writing it down ─────────────────────────────────────────────────────────
294
295export function fmtUsd(n: number): string {
296  if (!Number.isFinite(n) || n <= 0) return '$0.00'
297  if (n < 0.1) return `$${n.toFixed(3)}`
298  if (n < 10_000) return `$${n.toFixed(2)}`
299  return `$${(n / 1000).toFixed(1)}k`
300}
301
302export function fmtTok(n: number): string {
303  if (n < 1000) return `${Math.round(n)}`
304  if (n < 1e6) return `${(n / 1e3).toFixed(n < 1e4 ? 1 : 0)}k`
305  return `${(n / 1e6).toFixed(n < 1e7 ? 2 : 1)}M`
306}
307
308export function fmtDur(ms: number): string {
309  const s = Math.max(0, Math.floor(ms / 1000))
310  if (s < 60) return `${s}s`
311  const m = Math.floor(s / 60)
312  if (m < 60) return `${m}m ${String(s % 60).padStart(2, '0')}s`
313  const h = Math.floor(m / 60)
314  if (h < 48) return `${h}h ${String(m % 60).padStart(2, '0')}m`
315  return `${Math.floor(h / 24)}d ${h % 24}h`
316}
317
318/** A duration to the minute, for what a quiet pane shows: `<1m`, `37m`, `1h 05m`, `2d 3h`. */
319export function fmtMin(ms: number): string {
320  const m = Math.max(0, Math.floor(ms / 60_000))
321  if (m < 1) return '<1m'
322  if (m < 60) return `${m}m`
323  const h = Math.floor(m / 60)
324  if (h < 48) return `${h}h ${String(m % 60).padStart(2, '0')}m`
325  return `${Math.floor(h / 24)}d ${h % 24}h`
326}
327
328/** A coarse duration for a forecast: `37m`, `1h 40m`. */
329export function fmtEta(ms: number): string {
330  const m = Math.max(1, Math.round(ms / 60_000))
331  return m < 60 ? `${m}m` : `${Math.floor(m / 60)}h ${String(m % 60).padStart(2, '0')}m`
332}
333
334export function fmtPct(n: number): string {
335  return n >= 10 || Number.isInteger(n) ? `${Math.round(n)}%` : `${n.toFixed(1)}%`
336}
337
338const EIGHTHS = ['', '▏', '▎', '▍', '▌', '▋', '▊', '▉']
339
340/** A bar `width` cells long filled to `frac`, in eighths of a cell. */
341export function blocks(width: number, frac: number): string {
342  const eighths = Math.round(Math.min(1, Math.max(0, frac)) * width * 8)
343  const full = Math.floor(eighths / 8)
344  return '█'.repeat(full) + (EIGHTHS[eighths % 8] ?? '')
345}
346
347/** A thin gauge: `━` filled, `─` the track, `[filled, track]`. */
348export function gauge(width: number, frac: number): [string, string] {
349  const f = Math.round(Math.min(1, Math.max(0, frac)) * width)
350  return ['━'.repeat(f), '─'.repeat(Math.max(0, width - f))]
351}
352
353export function clip(text: string, width: number): string {
354  if (width <= 0) return ''
355  const chars = [...text]
356  return chars.length <= width ? text : `${chars.slice(0, Math.max(0, width - 1)).join('')}…`
357}
358
359// ─── the raster ──────────────────────────────────────────────────────────────
360
361const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
362
363export function base64(bytes: Uint8Array): string {
364  let out = ''
365  for (let i = 0; i < bytes.length; i += 3) {
366    const a = bytes[i]!
367    const b = bytes[i + 1]
368    const c = bytes[i + 2]
369    out += B64[a >> 2]
370    out += B64[((a & 3) << 4) | ((b ?? 0) >> 4)]
371    out += b === undefined ? '=' : B64[((b & 15) << 2) | ((c ?? 0) >> 6)]
372    out += c === undefined ? '=' : B64[c & 63]
373  }
374  return out
375}
376
377export const DEFAULT_COLOR = 0x01000000
378
379/** Packs `[codePoint, fg, bg]` cells, row-major, as a Raster's `cells`. */
380export function packCells(cells: readonly (readonly [number, number, number])[]): string {
381  const words = new Uint32Array(cells.length * 3)
382  cells.forEach(([cp, fg, bg], i) => {
383    words[i * 3] = cp
384    words[i * 3 + 1] = fg
385    words[i * 3 + 2] = bg
386  })
387  return base64(new Uint8Array(words.buffer))
388}
389
390// Each color is one a 256-color terminal rounds the same way whether it comes
391// as a cell of the chart or as text, so the legend matches the chart there too.
392export const PALETTE = {
393  main: 0xd9774b,
394  agents: 0x4ba8f5,
395  workflows: 0xaf7fff,
396  other: 0x8a8a8a,
397  mark: 0x767676, // a compaction's guide line
398  axis: 0x3a3a3a,
399}
400
401export const hex = (color: number) => `#${color.toString(16).padStart(6, '0')}`
402
403/** One bar of the chart: what each source spent in its bucket, and whether the session compacted then. */
404export type Bar = { main: number; agents: number; workflows: number; isCompacted: boolean }
405
406/** The last `columns` buckets up to `now`, oldest first. */
407export function barsOf(s: SpendSeries, now: number, columns: number, compactedAt: readonly number[] = []): Bar[] {
408  const slot = Math.floor(now / s.bucketMs) * s.bucketMs
409  const stale = s.t0 === 0 ? 0 : Math.max(0, Math.round((slot - s.t0) / s.bucketMs))
410  const series: [number, number, number, number][] = [...s.buckets]
411  for (let i = 0; i < Math.min(stale, columns); i++) series.push([0, 0, 0, 0])
412  const shown = series.slice(-columns)
413  while (shown.length < columns) shown.unshift([0, 0, 0, 0])
414  const first = slot - (columns - 1) * s.bucketMs
415  const marks = new Set(compactedAt.map(at => Math.round((Math.floor(at / s.bucketMs) * s.bucketMs - first) / s.bucketMs)))
416  return shown.map(([, main, agents, workflows], i) => ({ main, agents, workflows, isCompacted: marks.has(i) }))
417}
418
419export const barTotal = (b: Bar) => b.main + b.agents + b.workflows
420
hooks/history.ts 151 lines
1// What the session spent before the meter watched: read back from its
2// transcripts (the main thread's, each subagent's, each workflow agent's),
3// every response priced exactly as Claude Code prices it.
4
5import type { ModelUsage } from 'claude-code'
6
7import type { SpendAgentMeta, SpendCompaction, SpendSeries, SpendSources } from '../types'
8import { addStep, addToSeries, attribute, EMPTY_SERIES, EMPTY_SOURCES, finishAgent } from './ledger'
9import { partsTotal, priceRaw } from './pricing'
10import type { RawUsage } from './pricing'
11
12export type TranscriptFile = { agentId?: string; meta?: SpendAgentMeta; text: string }
13
14export type Rebuilt = {
15  sources: SpendSources
16  series: SpendSeries
17  usd: number
18  responses: number
19  /** The last prompt's turn: when it began and ended, and what it cost. */
20  turn?: { startedAt: number; endedAt: number; usd: number }
21  compactions: SpendCompaction[]
22}
23
24type Billed = { at: number; agentId?: string; model: string; usage: RawUsage }
25
26const num = (v: unknown) => (typeof v === 'number' && Number.isFinite(v) ? v : 0)
27
28// A response is written once per content block, each row carrying its usage
29// as of then: the largest of each count is the response's.
30function merge(a: RawUsage, b: RawUsage): RawUsage {
31  return {
32    input_tokens: Math.max(num(a.input_tokens), num(b.input_tokens)),
33    output_tokens: Math.max(num(a.output_tokens), num(b.output_tokens)),
34    cache_read_input_tokens: Math.max(num(a.cache_read_input_tokens), num(b.cache_read_input_tokens)),
35    cache_creation_input_tokens: Math.max(num(a.cache_creation_input_tokens), num(b.cache_creation_input_tokens)),
36    cache_creation: {
37      ephemeral_5m_input_tokens: Math.max(
38        num(a.cache_creation?.ephemeral_5m_input_tokens),
39        num(b.cache_creation?.ephemeral_5m_input_tokens),
40      ),
41      ephemeral_1h_input_tokens: Math.max(
42        num(a.cache_creation?.ephemeral_1h_input_tokens),
43        num(b.cache_creation?.ephemeral_1h_input_tokens),
44      ),
45    },
46    server_tool_use: {
47      web_search_requests: Math.max(
48        num(a.server_tool_use?.web_search_requests),
49        num(b.server_tool_use?.web_search_requests),
50      ),
51    },
52    inference_geo: b.inference_geo ?? a.inference_geo,
53  }
54}
55
56const asModelUsage = (u: RawUsage): ModelUsage => ({
57  input_tokens: num(u.input_tokens),
58  output_tokens: num(u.output_tokens),
59  cache_read_input_tokens: num(u.cache_read_input_tokens),
60  cache_creation_input_tokens: num(u.cache_creation_input_tokens),
61})
62
63/** Whether a user row is a prompt the person sent, not a tool result or a note. */
64function isPrompt(row: { isMeta?: unknown; isSidechain?: unknown; isCompactSummary?: unknown; message?: { content?: unknown } }) {
65  if (row.isMeta === true || row.isSidechain === true || row.isCompactSummary === true) return false
66  const content = row.message?.content
67  if (typeof content === 'string') return !content.startsWith('<local-command') && !content.startsWith('Caveat:')
68  return (
69    Array.isArray(content) &&
70    content.some(b => (b as { type?: unknown }).type === 'text') &&
71    !content.some(b => (b as { type?: unknown }).type === 'tool_result')
72  )
73}
74
75/** The ledger the transcripts add up to, counting responses up to `cutoff`. */
76export function rebuild(files: readonly TranscriptFile[], cutoff: number): Rebuilt {
77  const agents: Record<string, SpendAgentMeta> = {}
78  for (const f of files) if (f.agentId !== undefined && f.meta !== undefined) agents[f.agentId] = f.meta
79  const billed: Billed[] = []
80  const compactions: SpendCompaction[] = []
81  let promptAt: number | undefined
82  for (const f of files) {
83    const byId = new Map<string, Billed>()
84    for (const line of f.text.split('\n')) {
85      const isMain = f.agentId === undefined
86      const isUser = isMain && line.includes('"type":"user"')
87      const isCompaction = isMain && line.includes('"compact_boundary"')
88      if (!isUser && !isCompaction && !line.includes('"type":"assistant"')) continue
89      let row: Record<string, unknown>
90      try {
91        row = JSON.parse(line) as Record<string, unknown>
92      } catch {
93        continue // a torn last line, or a chunk's edge
94      }
95      const at = Date.parse(String(row['timestamp']))
96      if (!Number.isFinite(at) || at > cutoff) continue
97      if (row['subtype'] === 'compact_boundary') {
98        const meta = row['compactMetadata'] as { trigger?: string; preTokens?: number; postTokens?: number } | undefined
99        compactions.push({ at, trigger: meta?.trigger ?? 'auto', from: meta?.preTokens, to: meta?.postTokens })
100        continue
101      }
102      if (row['type'] === 'user') {
103        if (isPrompt(row as Parameters<typeof isPrompt>[0])) promptAt = at
104        continue
105      }
106      if (row['type'] !== 'assistant') continue
107      const m = row['message'] as { id?: string; model?: string; usage?: RawUsage } | undefined
108      if (m?.usage === undefined || m.model === undefined || m.model === '<synthetic>') continue
109      const id = m.id ?? String(row['requestId'] ?? row['uuid'])
110      const prev = byId.get(id)
111      byId.set(
112        id,
113        prev === undefined
114          ? { at, agentId: f.agentId, model: m.model, usage: m.usage }
115          : { ...prev, at: Math.max(prev.at, at), usage: merge(prev.usage, m.usage) },
116      )
117    }
118    billed.push(...byId.values())
119  }
120  billed.sort((a, b) => a.at - b.at)
121
122  let sources: SpendSources = { ...EMPTY_SOURCES, agents }
123  let series: SpendSeries = EMPTY_SERIES
124  let usd = 0
125  let turnUsd = 0
126  let lastAt = 0
127  for (const b of billed) {
128    const parts = priceRaw(b.model, b.usage)
129    const who = attribute(b.agentId, sources)
130    sources = addStep(sources, who, b.model, asModelUsage(b.usage), parts, b.at)
131    const c = parts === undefined ? 0 : partsTotal(parts)
132    usd += c
133    series = addToSeries(series, b.at, {
134      main: who.kind === 'main' ? c : 0,
135      agents: who.kind === 'agent' || who.kind === 'fork' ? c : 0,
136      workflows: who.kind === 'workflow' ? c : 0,
137    })
138    if (promptAt !== undefined && b.at >= promptAt) turnUsd += c
139    lastAt = b.at
140  }
141  for (const id of Object.keys(agents)) sources = finishAgent(sources, id, 'done')
142  return {
143    sources,
144    series,
145    usd,
146    responses: billed.length,
147    turn: promptAt === undefined ? undefined : { startedAt: promptAt, endedAt: Math.max(lastAt, promptAt), usd: turnUsd },
148    compactions: compactions.slice(-20),
149  }
150}
151
hooks/pricing.ts 180 lines
1// List prices per million tokens, as Claude Code 2.1.300's own model catalog
2// has them (the same table /cost prices with), and the step pricing it does.
3// The engine hands a mod the session's total but not each response's dollars,
4// so this prices each response to say who spent what; the total stays the
5// engine's.
6
7import type { ModelUsage } from 'claude-code'
8
9type Rates = { in: number; out: number; w5m: number; w1h: number; read: number }
10type Tier = Rates & { long?: Rates & { above: number } }
11
12const tier = (input: number, output: number, read = input / 10): Tier => ({
13  in: input,
14  out: output,
15  w5m: input * 1.25,
16  w1h: input * 2,
17  read,
18})
19
20const TIERS = {
21  haiku_35: tier(0.8, 4),
22  haiku_45: tier(1, 5),
23  haiku_55: { ...tier(0.1, 0.5), long: { ...tier(0.5, 2.5), above: 100_000 } },
24  tier_2_10: tier(2, 10),
25  tier_2_10_cr_010: tier(2, 10, 0.1),
26  tier_3_15: tier(3, 15),
27  tier_4_20_cr_020: tier(4, 20, 0.2),
28  tier_5_25: tier(5, 25),
29  tier_10_50: tier(10, 50),
30  tier_10_50_cr_025: tier(10, 50, 0.25),
31  tier_15_75: tier(15, 75),
32} satisfies Record<string, Tier>
33
34const CATALOG: Record<string, { name: string; tier: keyof typeof TIERS }> = {
35  'claude-3-5-haiku': { name: 'Haiku 3.5', tier: 'haiku_35' },
36  'claude-haiku-4-5': { name: 'Haiku 4.5', tier: 'haiku_45' },
37  'claude-haiku-5-5': { name: 'Haiku 5.5', tier: 'haiku_55' },
38  'claude-3-5-sonnet': { name: 'Sonnet 3.5', tier: 'tier_3_15' },
39  'claude-3-7-sonnet': { name: 'Sonnet 3.7', tier: 'tier_3_15' },
40  'claude-sonnet-4-0': { name: 'Sonnet 4', tier: 'tier_3_15' },
41  'claude-sonnet-4-5': { name: 'Sonnet 4.5', tier: 'tier_3_15' },
42  'claude-sonnet-4-6': { name: 'Sonnet 4.6', tier: 'tier_3_15' },
43  'claude-sonnet-5': { name: 'Sonnet 5', tier: 'tier_2_10' },
44  'claude-sonnet-5-5': { name: 'Sonnet 5.5', tier: 'tier_2_10_cr_010' },
45  'claude-opus-4-0': { name: 'Opus 4', tier: 'tier_15_75' },
46  'claude-opus-4-1': { name: 'Opus 4.1', tier: 'tier_15_75' },
47  'claude-opus-4-5': { name: 'Opus 4.5', tier: 'tier_5_25' },
48  'claude-opus-4-6': { name: 'Opus 4.6', tier: 'tier_5_25' },
49  'claude-opus-4-7': { name: 'Opus 4.7', tier: 'tier_5_25' },
50  'claude-opus-4-8': { name: 'Opus 4.8', tier: 'tier_5_25' },
51  'claude-opus-5': { name: 'Opus 5', tier: 'tier_5_25' },
52  'claude-opus-5-5': { name: 'Opus 5.5', tier: 'tier_4_20_cr_020' },
53  'claude-fable-5': { name: 'Fable 5', tier: 'tier_10_50' },
54  'claude-fable-5-1': { name: 'Fable 5.1', tier: 'tier_10_50_cr_025' },
55  'claude-mythos-5': { name: 'Mythos 5', tier: 'tier_10_50' },
56  'claude-mythos-5-1': { name: 'Mythos 5.1', tier: 'tier_10_50_cr_025' },
57}
58
59// Longest first, so `claude-opus-5-5` wins over `claude-opus-5`.
60const IDS = Object.keys(CATALOG).sort((a, b) => b.length - a.length)
61
62/**
63 * The catalog id a reported model id names: a dated id
64 * (`claude-opus-5-5-20261001`), a provider's (`us.anthropic.claude-...-v1:0`)
65 * or one with a context suffix (`claude-fable-5-1[1m]`).
66 */
67export function catalogId(model: string | undefined): string | undefined {
68  if (model === undefined) return undefined
69  const id = model.toLowerCase()
70  for (const known of IDS) {
71    const at = id.indexOf(known)
72    if (at < 0) continue
73    const before = id[at - 1]
74    const rest = id.slice(at + known.length)
75    if (before !== undefined && /[a-z0-9]/.test(before)) continue
76    // `-5` after `claude-opus-5` is another model; `-20261001` is a date.
77    if (/^-\d(\D|$)/.test(rest) || /^\d/.test(rest)) continue
78    return known
79  }
80  return undefined
81}
82
83export function modelName(model: string | undefined): string {
84  const id = catalogId(model)
85  if (id !== undefined) return CATALOG[id]?.name ?? id
86  if (model === undefined || model === '') return '?'
87  return model.replace(/^claude-/, '')
88}
89
90/** USD per million output tokens, for pricing what is still streaming. */
91export function outputRate(model: string | undefined): number {
92  const id = catalogId(model)
93  return id === undefined ? 0 : (TIERS[CATALOG[id]!.tier].out ?? 0)
94}
95
96/** USD per million uncached input tokens: what input would cost without the cache. */
97export function inputRate(model: string | undefined): number {
98  const id = catalogId(model)
99  return id === undefined ? 0 : TIERS[CATALOG[id]!.tier].in
100}
101
102/** USD per million cache-read tokens: what re-reading the context costs. */
103export function readRate(model: string | undefined): number {
104  const id = catalogId(model)
105  return id === undefined ? 0 : TIERS[CATALOG[id]!.tier].read
106}
107
108/** A response's usage as the transcript records it: the API's own fields. */
109export type RawUsage = {
110  input_tokens?: number
111  output_tokens?: number
112  cache_read_input_tokens?: number
113  cache_creation_input_tokens?: number
114  cache_creation?: { ephemeral_5m_input_tokens?: number; ephemeral_1h_input_tokens?: number }
115  server_tool_use?: { web_search_requests?: number }
116  inference_geo?: string
117}
118
119/**
120 * What a response cost, by what was paid for: `output` the tokens the model
121 * wrote, `write` new input stored in the prompt cache, `read` input served
122 * from it, `input` input never cached (web searches count here too).
123 */
124export type Parts = { input: number; output: number; read: number; write: number }
125
126export const NO_PARTS: Parts = { input: 0, output: 0, read: 0, write: 0 }
127
128export const partsTotal = (p: Parts) => p.input + p.output + p.read + p.write
129
130export function addParts(a: Parts, b: Parts | undefined): Parts {
131  if (b === undefined) return a
132  return { input: a.input + b.input, output: a.output + b.output, read: a.read + b.read, write: a.write + b.write }
133}
134
135function rates(model: string | undefined, prompt: number): Rates | undefined {
136  const id = catalogId(model)
137  if (id === undefined) return undefined
138  const base: Tier = TIERS[CATALOG[id]!.tier]
139  return base.long !== undefined && prompt > base.long.above ? base.long : base
140}
141
142/**
143 * What a transcript's response cost, priced as Claude Code prices it: the
144 * cache writes split by their TTL, US-only inference at 1.1×, web searches
145 * at a cent each. Undefined for a model the catalog lacks.
146 */
147export function priceRaw(model: string | undefined, u: RawUsage): Parts | undefined {
148  const input = u.input_tokens ?? 0
149  const read = u.cache_read_input_tokens ?? 0
150  const written = u.cache_creation_input_tokens ?? 0
151  const r = rates(model, input + read + written)
152  if (r === undefined) return undefined
153  const w1h = Math.min(u.cache_creation?.ephemeral_1h_input_tokens ?? 0, written)
154  const k = (u.inference_geo === 'us' ? 1.1 : 1) / 1e6
155  return {
156    input: input * r.in * k + (u.server_tool_use?.web_search_requests ?? 0) * 0.01,
157    output: (u.output_tokens ?? 0) * r.out * k,
158    read: read * r.read * k,
159    write: (w1h * r.w1h + (written - w1h) * r.w5m) * k,
160  }
161}
162
163/**
164 * What one response cost at list price, or undefined for a model the catalog
165 * lacks. `ttl` is how long the prompt cache was written for: Claude Code
166 * writes the main thread's for an hour on a subscription, a subagent's for
167 * five minutes.
168 */
169export function priceUsage(model: string | undefined, usage: ModelUsage, ttl: '5m' | '1h'): Parts | undefined {
170  const prompt = usage.input_tokens + usage.cache_read_input_tokens + usage.cache_creation_input_tokens
171  const r = rates(model, prompt)
172  if (r === undefined) return undefined
173  return {
174    input: (usage.input_tokens * r.in) / 1e6,
175    output: (usage.output_tokens * r.out) / 1e6,
176    read: (usage.cache_read_input_tokens * r.read) / 1e6,
177    write: (usage.cache_creation_input_tokens * (ttl === '1h' ? r.w1h : r.w5m)) / 1e6,
178  }
179}
180
hooks/view.tsx 673 lines
1// The pane's drawing: a tree from the meter's state, sized to the pane body.
2
3import type { Elements, RenderElement, RenderSurface } from 'claude-code'
4
5import type { SpendCompaction, SpendCore, SpendLive, SpendPrefs, SpendSeries, SpendSource, SpendSources } from '../types'
6import { areaCells, areaPixels, areaText } from './art'
7import {
8  barsOf,
9  barTotal,
10  burnPerMin,
11  cacheHit,
12  clip,
13  fmtDur,
14  fmtEta,
15  fmtMin,
16  fmtPct,
17  fmtTok,
18  fmtUsd,
19  gauge,
20  hex,
21  pace,
22  packCells,
23  PALETTE,
24  sumCost,
25  sumTokens,
26  windowMoved,
27} from './ledger'
28import { inputRate, modelName, readRate } from './pricing'
29
30export const UNITS = [
31  { id: 'usd', label: 'this session · USD at API list price' },
32  { id: 'tok', label: 'tokens through the models this session' },
33  { id: 'coffee', label: 'flat whites at $4.50', per: 4.5, glyph: '☕ ' },
34  { id: 'banana', label: 'bananas, for scale', per: 0.3, glyph: '🍌 ' },
35  { id: 'dev', label: 'minutes of a senior engineer', per: 1.5, glyph: '' },
36] as const
37
38const KIND_COLOR = {
39  main: hex(PALETTE.main),
40  agent: hex(PALETTE.agents),
41  workflow: hex(PALETTE.workflows),
42  fork: hex(PALETTE.other),
43}
44
45const KIND_NAME = { main: 'main thread', agent: 'subagents', workflow: 'workflows' } as const
46
47const SPINNER = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
48
49export type ViewData = {
50  now: number
51  core: SpendCore | null
52  sources: SpendSources
53  series: SpendSeries
54  prefs: SpendPrefs
55  live: SpendLive
56  width: number
57  rows: number
58  placement: 'dock' | 'inline'
59  surface: RenderSurface
60  viewedAgentId?: string
61  /** Whether the chart may be a kitty-protocol picture rather than cells. */
62  gfx: 'cells' | 'pixels'
63  caps: Caps
64}
65
66export type ViewActions = {
67  cycleUnit: () => void
68  select: (key: string | null) => void
69  /** Puts an agent's transcript in the main view; undefined goes back to the main one. */
70  view: (agentId: string | undefined) => void
71}
72
73/** What the engine the meter runs in can draw and do, as its version says. */
74export type Caps = {
75  /** Text `weight`, `size`, `tabular` and `mono`, since 2.1.299; an older engine refuses a tree that names them. */
76  textStyles: boolean
77  /** `$.ui.view`, since 2.1.300. */
78  agentView: boolean
79}
80
81export const OLD_ENGINE: Caps = { textStyles: false, agentView: false }
82
83export function capsOf(v: { version: string; base?: string } | undefined): Caps {
84  const m = /^(\d+)\.(\d+)\.(\d+)/.exec(v?.base ?? v?.version ?? '')
85  if (m === null) return OLD_ENGINE
86  const n = Number(m[1]) * 1e6 + Number(m[2]) * 1e3 + Number(m[3])
87  return { textStyles: n >= 2_001_299, agentView: n >= 2_001_300 }
88}
89
90/** Text styles as the engine spells them: bold was `bold` before 2.1.299, `weight` since. */
91function looksOf(caps: Caps) {
92  return caps.textStyles
93    ? ({ strong: { weight: 'semibold' }, hero: { weight: 'semibold', size: 'heading', tabular: true }, num: { tabular: true }, table: { mono: true } } as const)
94    : ({ strong: { bold: true }, hero: { bold: true }, num: {}, table: {} } as const)
95}
96type Looks = ReturnType<typeof looksOf>
97
98type Ui = Elements[RenderSurface]
99/** The terminal's own elements: cells colored one by one, and pictures. */
100export type Art = { Raster: Elements['terminal']['Raster']; Image: Elements['terminal']['Image'] }
101
102type Row = {
103  key: string
104  kind: SpendSource['kind'] | 'overhead' | 'earlier'
105  label: string
106  usd: number
107  status?: SpendSource['status']
108  isStreaming: boolean
109  source?: SpendSource
110}
111
112const pctColor = (pct: number) => (pct >= 80 ? 'error' : pct >= 50 ? 'warning' : 'success')
113
114function windowName(kind: string): string {
115  if (kind === 'five_hour') return '5h'
116  if (kind === 'seven_day') return '7d'
117  if (kind === 'spend_limit') return '$'
118  if (kind.startsWith('seven_day_')) return `7d${kind.slice(10, 11)}`
119  return kind.slice(0, 3)
120}
121
122/** How good a cache hit rate is, said in a word. */
123function hitWord(hit: number): [string, string] {
124  if (hit >= 0.95) return ['excellent', 'success']
125  if (hit >= 0.85) return ['good', 'success']
126  if (hit >= 0.7) return ['fair', 'warning']
127  return ['poor', 'error']
128}
129
130/** The rows of "where it goes", the engine's total split as far as it can be. */
131export function rowsOf(d: ViewData): Row[] {
132  const live = new Set(d.live.keys)
133  const rows: Row[] = Object.values(d.sources.rows).map(s => ({
134    key: s.key,
135    kind: s.kind,
136    label: s.kind === 'main' ? `main thread · ${modelName(s.model)}` : s.kind === 'fork' ? 'compaction & memory' : s.label,
137    usd: s.usd,
138    status: s.status,
139    isStreaming: live.has(s.key),
140    source: s,
141  }))
142  const order = (r: Row) => (r.kind === 'main' ? 0 : r.kind === 'fork' ? 3 : r.status === 'running' ? 1 : 2)
143  rows.sort((a, b) => order(a) - order(b) || b.usd - a.usd)
144  if (d.core !== null) {
145    const priced = rows.reduce((a, r) => a + r.usd, 0)
146    const overhead = d.core.usd - d.core.usd0 - priced
147    if (overhead >= 0.0005) {
148      rows.push({ key: 'overhead', kind: 'overhead', label: 'side calls: classifier, titles', usd: overhead, isStreaming: false })
149    }
150    if (d.core.usd0 >= 0.0005) {
151      rows.push({ key: 'earlier', kind: 'earlier', label: 'before the meter', usd: d.core.usd0, isStreaming: false })
152    }
153  }
154  return rows
155}
156
157function glyphOf(r: Row, tick: number): string {
158  if (r.isStreaming) return SPINNER[tick % SPINNER.length]!
159  if (r.kind === 'overhead') return '◇'
160  if (r.kind === 'earlier') return '·'
161  if (r.kind === 'fork') return '○'
162  if (r.status === 'done') return '✓'
163  if (r.status === 'failed') return '✗'
164  return r.kind === 'workflow' ? '◆' : '●'
165}
166
167function colorOf(r: Row): string {
168  if (r.kind === 'overhead' || r.kind === 'earlier') return 'subtle'
169  return KIND_COLOR[r.kind]
170}
171
172function heroValue(d: ViewData, tokens: number): { text: string; caption: string } {
173  const unit = UNITS[d.prefs.unit % UNITS.length]!
174  const usd = d.core?.usd ?? 0
175  if (unit.id === 'usd') return { text: fmtUsd(usd), caption: unit.label }
176  if (unit.id === 'tok') return { text: fmtTok(tokens), caption: unit.label }
177  const n = usd / unit.per
178  return { text: `${unit.glyph}${n >= 100 ? n.toFixed(0) : n.toFixed(1)}`, caption: `${unit.label} · ${fmtUsd(usd)}` }
179}
180
181function compactionNote(list: readonly SpendCompaction[] | undefined, now: number): string | undefined {
182  const last = list?.at(-1)
183  if (last === undefined) return undefined
184  const sizes = last.from === undefined ? '' : ` · ${fmtTok(last.from)} → ${last.to === undefined ? '…' : fmtTok(last.to)}`
185  const times = list!.length > 1 ? `compacted ${list!.length}×, last ` : 'compacted '
186  return `${times}${fmtMin(now - last.at)} ago${sizes}`
187}
188
189/**
190 * What the pane shows that moves with the clock alone, written as the pane writes it.
191 * The meter draws the pane again only when this changes: a quiet pane, once a minute.
192 */
193export function clockText(d: Pick<ViewData, 'now' | 'core' | 'series' | 'sources' | 'prefs' | 'viewedAgentId'>): string {
194  const core = d.core
195  if (core === null) return ''
196  const burn = burnPerMin(d.series, d.now)
197  const parts = [fmtMin(d.now - core.startedAt), fmtUsd(burn), fmtUsd(burn * 60)]
198  if (core.turn !== null && core.turn.endedAt === undefined) parts.push(fmtDur(d.now - core.turn.startedAt))
199  for (const l of core.limits) parts.push(l.resetsAt === undefined ? '' : fmtMin(Date.parse(l.resetsAt) - d.now))
200  for (const w of Object.values(core.windows)) {
201    const p = pace(w, d.now)
202    const full = p?.fullAt === undefined ? '' : `${fmtEta(p.fullAt - d.now)} ${p.fullAt - d.now < 60 * 60_000}`
203    parts.push(p === undefined ? '' : `${p.perMin > 0} ${p.isShort} ${full}`)
204  }
205  parts.push(compactionNote(core.compactions, d.now) ?? '')
206  // The chart moves a bucket at a time while a bar or a mark is in sight.
207  const bars = barsOf(d.series, d.now, 240, (core.compactions ?? []).map(c => c.at))
208  if (bars.some(b => barTotal(b) > 0 || b.isCompacted)) parts.push(String(Math.floor(d.now / d.series.bucketMs)))
209  const selected = d.prefs.selected ?? (d.viewedAgentId === undefined ? null : `agent:${d.viewedAgentId}`)
210  const s = selected === null ? undefined : d.sources.rows[selected]
211  if (s?.status === 'running' && s.kind !== 'main' && d.now - s.lastAt >= 5000) parts.push(fmtDur(d.now - s.lastAt))
212  return parts.join('|')
213}
214
215export function drawPane(ui: Ui, art: Art | undefined, d: ViewData, act: ViewActions): RenderElement {
216  const { Box, Text, Button } = ui
217  const W = Math.max(24, d.width - 2) // the root Box pads a cell each side
218  const core = d.core
219  const tick = d.live.tick
220  const sourceRows = Object.values(d.sources.rows)
221  const tokens = sumTokens(sourceRows)
222  const allTokens = tokens.in + tokens.out + tokens.cr + tokens.cw
223  const hero = heroValue(d, allTokens)
224  const budget = d.prefs.budget
225  const usd = core?.usd ?? 0
226  const budgetFrac = budget === null || budget <= 0 ? undefined : usd / budget
227  const heroColor = budgetFrac === undefined ? 'claude' : budgetFrac >= 1 ? 'error' : budgetFrac >= 0.8 ? 'warning' : 'claude'
228  const isBusy = d.live.inflight > 0
229  const main = d.sources.rows['main']
230  const isInline = d.placement === 'inline'
231  const look = looksOf(d.caps)
232
233  const heading = (text: string, right?: string) => (
234    <Box justifyContent="space-between" marginTop={1}>
235      <Text {...look.strong} color="subtle">
236        {text}
237      </Text>
238      {right !== undefined && <Text dimColor>{right}</Text>}
239    </Box>
240  )
241
242  // ── header ──
243  const header = (
244    <Box justifyContent="space-between">
245      <Box gap={2}>
246        <Text {...look.strong} color="claude">
247          ◆ spend
248        </Text>
249        {isBusy ? (
250          <Text color="claude">
251            {SPINNER[tick % SPINNER.length]} {d.live.inflight} {d.live.inflight === 1 ? 'request' : 'requests'} live
252          </Text>
253        ) : (
254          <Text dimColor>idle</Text>
255        )}
256      </Box>
257      <Text dimColor>{core === null ? '' : fmtMin(d.now - core.startedAt)}</Text>
258    </Box>
259  )
260
261  if (core === null) {
262    return (
263      <Box flexDirection="column" paddingX={1}>
264        {header}
265        <Text dimColor>Measuring the session…</Text>
266      </Box>
267    )
268  }
269
270  // ── the total ──
271  const brrr = d.live.flash?.kind === 'brrr' && d.live.flash.until > d.now
272  const streaming = d.live.estUsd >= 0.0005 ? `+${fmtUsd(d.live.estUsd)} streaming` : undefined
273  const total = (
274    <Box flexDirection="column" marginTop={1}>
275      {brrr ? (
276        printer(Text, tick)
277      ) : (
278        <Box justifyContent="space-between">
279          <Text {...look.hero} color={heroColor}>
280            {hero.text}
281          </Text>
282          {streaming !== undefined && (
283            <Text {...look.num} color="claude">
284              {streaming}
285            </Text>
286          )}
287        </Box>
288      )}
289      <Button key="caption" plain dimColor onPress={act.cycleUnit}>
290        {hero.caption} ⇄
291      </Button>
292    </Box>
293  )
294
295  // ── the turn, the pace, the budget ──
296  const turn = core.turn
297  const turnUsd = turn === null ? 0 : (turn.usd1 ?? core.usd) - turn.usd0
298  const turnMs = turn === null ? 0 : (turn.endedAt ?? d.now) - turn.startedAt
299  const burn = burnPerMin(d.series, d.now)
300  const statLine = (label: string, value: string, right: string, color?: string) => (
301    <Box justifyContent="space-between">
302      <Box gap={1}>
303        <Text {...look.table} dimColor>
304          {label.padEnd(10)}
305        </Text>
306        <Text {...look.num} color={color}>
307          {value}
308        </Text>
309      </Box>
310      <Text dimColor>{right}</Text>
311    </Box>
312  )
313  const budgetColor = budgetFrac === undefined ? undefined : budgetFrac >= 1 ? 'error' : budgetFrac >= 0.8 ? 'warning' : 'success'
314  const [budgetOn, budgetOff] = gauge(Math.max(6, W - 27), budgetFrac ?? 0)
315  const stats = (
316    <Box flexDirection="column" marginTop={1}>
317      {turn !== null &&
318        statLine(
319          turn.endedAt === undefined ? 'this turn' : 'last turn',
320          `+${fmtUsd(turnUsd)}`,
321          `${fmtDur(turnMs)}${turn.endedAt === undefined ? ' so far' : ''}`,
322        )}
323      {statLine('burn', burn < 0.0005 ? 'idle' : `${fmtUsd(burn)}/min`, burn < 0.0005 ? '' : `≈ ${fmtUsd(burn * 60)}/h`)}
324      {budget !== null && budgetFrac !== undefined && (
325        <Box justifyContent="space-between">
326          <Box gap={1}>
327            <Text dimColor>{'budget'.padEnd(10)}</Text>
328            <Text>
329              <Text color={budgetColor}>{budgetOn}</Text>
330              <Text dimColor>{budgetOff}</Text>
331            </Text>
332          </Box>
333          <Text color={budgetColor}>
334            {fmtPct(budgetFrac * 100)} of {fmtUsd(budget)}
335          </Text>
336        </Box>
337      )}
338    </Box>
339  )
340
341  // ── over time ──
342  const spanMin = Math.round((W * d.series.bucketMs) / 60_000)
343  const bars = barsOf(
344    d.series,
345    d.now,
346    W,
347    (core.compactions ?? []).map(c => c.at),
348  )
349  const peak = Math.max(...bars.map(barTotal))
350  const chartRows = Math.min(7, Math.max(3, Math.floor((d.rows - 30) / 3)))
351  const kinds = (['main', 'agent', 'workflow'] as const).filter(k =>
352    bars.some(b => (k === 'main' ? b.main : k === 'agent' ? b.agents : b.workflows) > 0),
353  )
354  let chart: RenderElement
355  if (peak <= 0) {
356    chart = <Text dimColor>No spend in the last {spanMin} minutes.</Text>
357  } else if (art !== undefined && d.gfx === 'pixels') {
358    chart = art.Image({ key: 'chart', source: areaPixels(bars, chartRows), columns: W, rows: chartRows, alt: areaText(bars) })
359  } else if (art !== undefined) {
360    chart = art.Raster({ key: 'chart', columns: W, rows: chartRows, cells: packCells(areaCells(bars, chartRows)) })
361  } else {
362    chart = <Text color="claude">{areaText(bars)}</Text>
363  }
364  const overTime = (
365    <Box flexDirection="column">
366      {heading('OVER TIME', peak > 0 ? `last ${spanMin} min · peak ${fmtUsd(peak)}/${Math.round(d.series.bucketMs / 1000)}s` : `last ${spanMin} min`)}
367      {chart}
368      {kinds.length > 1 && (
369        <Box flexWrap="wrap" columnGap={2}>
370          {kinds.map(k => (
371            <Text>
372              <Text color={KIND_COLOR[k]}>■ </Text>
373              <Text dimColor>{KIND_NAME[k]}</Text>
374            </Text>
375          ))}
376        </Box>
377      )}
378    </Box>
379  )
380
381  // ── limits ──
382  const gaugeW = Math.max(6, W - 21)
383  const limitLines: RenderElement[] = []
384  const note = (text: RenderElement | string) => (
385    <Box paddingLeft={4}>
386      <Text dimColor wrap="truncate-end">
387        {text}
388      </Text>
389    </Box>
390  )
391  for (const l of core.limits) {
392    const [on, off] = gauge(gaugeW, l.pct / 100)
393    const resets = l.resetsAt === undefined ? '' : `↻ ${fmtMin(Date.parse(l.resetsAt) - d.now)}`
394    limitLines.push(
395      <Box justifyContent="space-between">
396        <Box gap={1}>
397          <Text {...look.strong} {...look.table}>
398            {windowName(l.kind).padEnd(3)}
399          </Text>
400          <Text>
401            <Text color={pctColor(l.pct)}>{on}</Text>
402            <Text dimColor>{off}</Text>
403          </Text>
404          <Text {...look.table} color={pctColor(l.pct)}>
405            {fmtPct(l.pct).padStart(4)}
406          </Text>
407        </Box>
408        <Text dimColor>{resets}</Text>
409      </Box>,
410    )
411    const w = core.windows[l.kind]
412    if (w === undefined || isInline) continue
413    const moved = windowMoved(w)
414    const p = pace(w, d.now)
415    if (moved < 0.05 && (p === undefined || p.perMin <= 0)) continue
416    const parts: RenderElement[] = [<Text dimColor>+{moved.toFixed(1)} pts since start</Text>]
417    if (p !== undefined && p.perMin > 0 && p.fullAt !== undefined) {
418      parts.push(
419        p.isShort ? (
420          <Text color={p.fullAt - d.now < 60 * 60_000 ? 'error' : 'warning'}> · full in ~{fmtEta(p.fullAt - d.now)} ⚠</Text>
421        ) : (
422          <Text color="success"> · pace ok</Text>
423        ),
424      )
425    }
426    limitLines.push(note(<Text>{parts}</Text>))
427  }
428  if (core.ctxPct !== undefined) {
429    const [on, off] = gauge(gaugeW, core.ctxPct / 100)
430    limitLines.push(
431      <Box justifyContent="space-between">
432        <Box gap={1}>
433          <Text {...look.strong}>ctx</Text>
434          <Text>
435            <Text color={pctColor(core.ctxPct)}>{on}</Text>
436            <Text dimColor>{off}</Text>
437          </Text>
438          <Text {...look.table} color={pctColor(core.ctxPct)}>
439            {fmtPct(core.ctxPct).padStart(4)}
440          </Text>
441        </Box>
442        <Text dimColor>
443          {fmtTok(core.ctxTokens ?? 0)}/{fmtTok(core.ctxWindow)}
444        </Text>
445      </Box>,
446    )
447    const tax = ((core.ctxTokens ?? 0) * readRate(main?.model)) / 1e6
448    if (tax > 0 && !isInline) limitLines.push(note(`≈ ${fmtUsd(tax)} to re-read per main request`))
449  }
450  // The same mark the chart draws where the session compacted, with or without the gauge:
451  // from a compaction until the next response the context is unknown.
452  const compacted = compactionNote(core.compactions, d.now)
453  if (compacted !== undefined && !isInline) {
454    limitLines.push(
455      note(
456        <Text>
457          <Text color={hex(PALETTE.mark)}>│ </Text>
458          {compacted}
459        </Text>,
460      ),
461    )
462  }
463
464  // ── where it goes ──
465  const rows = rowsOf(d)
466  const costW = 8
467  const barW = W >= 36 ? 7 : 0
468  const labelW = Math.max(6, W - 1 - 2 - costW - barW)
469  // Shares of what the meter watched: the spend before it has no bar.
470  const watched = Math.max(0.000001, rows.reduce((a, r) => a + (r.kind === 'earlier' ? 0 : r.usd), 0))
471  const maxRows = isInline ? 3 : Math.max(4, d.rows - 34)
472  const selected = d.prefs.selected ?? (d.viewedAgentId === undefined ? null : `agent:${d.viewedAgentId}`)
473  const sourceLines: RenderElement[] = []
474  for (const r of rows.slice(0, maxRows)) {
475    const isSel = selected === r.key
476    // The engine's own calls, named in full where the row has room.
477    const label = r.kind === 'overhead' && r.label.length > labelW ? 'side calls' : r.label
478    // The row's share as a thin gauge, the limits' kind: a proportion, not a badge.
479    const [shareOn, shareOff] = barW === 0 || r.kind === 'earlier' ? ['', ''] : gauge(barW - 1, r.usd / watched)
480    const cost = r.usd === 0 && r.isStreaming ? '…' : `${r.source?.isPriced === false ? '≥' : ''}${fmtUsd(r.usd)}`
481    const children = [
482      <Text color={colorOf(r)}>{glyphOf(r, tick)}</Text>,
483      ` ${clip(label, labelW).padEnd(labelW)} `,
484      <Text {...(isSel ? look.strong : {})}>{cost.padStart(costW)}</Text>,
485      shareOn === '' && shareOff === '' ? '' : ' ',
486      <Text color={colorOf(r)}>{shareOn}</Text>,
487      <Text dimColor>{shareOff}</Text>,
488    ]
489    sourceLines.push(
490      r.source !== undefined ? (
491        <Button key={`src:${r.key}`} plain onPress={() => act.select(isSel ? null : r.key)}>
492          <Text {...look.table}>{children}</Text>
493        </Button>
494      ) : (
495        <Text {...look.table} dimColor={r.kind === 'earlier'}>
496          {children}
497        </Text>
498      ),
499    )
500    if (isSel && r.source !== undefined) sourceLines.push(details(ui, look, r.source, d, W, act.view))
501  }
502  const hidden = rows.slice(maxRows)
503  if (hidden.length > 0) {
504    sourceLines.push(
505      <Text dimColor>
506        {'  '}+{hidden.length} more · {fmtUsd(hidden.reduce((a, r) => a + r.usd, 0))}
507      </Text>,
508    )
509  }
510
511  // ── what the tokens cost ──
512  const cost = sumCost(sourceRows)
513  const hit = cacheHit(tokens)
514  const tokW = 7
515  const tokenLabelW = Math.max(8, W - tokW - costW - 2)
516  const isCacheOpen = d.prefs.selected === 'cache'
517  const tokenRow = (label: string, n: number, usdOf: number, color?: string) => (
518    <Text {...look.table}>
519      <Text color={color}>{clip(label, tokenLabelW).padEnd(tokenLabelW)}</Text> {fmtTok(n).padStart(tokW)} {fmtUsd(usdOf).padStart(costW)}
520    </Text>
521  )
522  const tokenLines = (
523    <Box flexDirection="column">
524      <Box justifyContent="space-between" marginTop={1}>
525        <Text {...look.strong} color="subtle">
526          TOKENS
527        </Text>
528        <Text {...look.table} dimColor>
529          {'tokens'.padStart(tokW)} {'cost'.padStart(costW)}
530        </Text>
531      </Box>
532      {tokenRow('written by models', tokens.out, cost.output)}
533      {tokenRow('new input, cached', tokens.cw, cost.write)}
534      {tokenRow('re-read from cache', tokens.cr, cost.read)}
535      {tokens.in > 0 && tokenRow('uncached input', tokens.in, cost.input)}
536      {hit !== undefined && cacheLine(Button, Text, look, hit, W, isCacheOpen, () => act.select(isCacheOpen ? null : 'cache'))}
537      {hit !== undefined && isCacheOpen && (
538        <Box paddingLeft={2} marginBottom={1}>
539          <Text dimColor>{cacheExplained(sourceRows, tokens.cr, cost.read, main?.model)}</Text>
540        </Box>
541      )}
542    </Box>
543  )
544
545  if (isInline) {
546    return (
547      <Box flexDirection="column" paddingX={1}>
548        <Box justifyContent="space-between">
549          <Box gap={1}>
550            <Text {...look.strong} {...look.num} color={heroColor}>
551              {hero.text}
552            </Text>
553            {turn !== null && <Text dimColor>this turn +{fmtUsd(turnUsd)}</Text>}
554          </Box>
555          <Text dimColor>{burn < 0.0005 ? 'idle' : `${fmtUsd(burn)}/min`}</Text>
556        </Box>
557        {limitLines}
558        {sourceLines}
559      </Box>
560    )
561  }
562
563  return (
564    <Box flexDirection="column" paddingX={1}>
565      {header}
566      {total}
567      {stats}
568      {overTime}
569      {limitLines.length > 0 && heading(core.isSubscriber ? 'LIMITS' : 'CONTEXT', core.isSubscriber ? 'whole account' : undefined)}
570      {limitLines}
571      {heading('WHERE IT GOES')}
572      {sourceLines.length > 0 ? sourceLines : <Text dimColor>Nothing yet.</Text>}
573      {allTokens > 0 && tokenLines}
574    </Box>
575  )
576}
577
578/** "cache served 99% of input · excellent", a toggle for what that means. */
579function cacheLine(Button: Ui['Button'], Text: Ui['Text'], look: Looks, hit: number, width: number, isOpen: boolean, toggle: () => void) {
580  const lead = `cache served ${fmtPct(hit * 100)} of input · `
581  const [word, color] = hitWord(hit)
582  const gap = ' '.repeat(Math.max(1, width - lead.length - word.length - 1))
583  return (
584    <Button key="cache" plain onPress={toggle}>
585      <Text dimColor>{lead}</Text>
586      <Text color={color}>{word}</Text>
587      <Text dimColor={!isOpen} {...(isOpen ? look.strong : {})}>
588        {gap}?
589      </Text>
590    </Button>
591  )
592}
593
594/** What the cache does, in this session's own figures. */
595function cacheExplained(rows: readonly SpendSource[], reread: number, readCost: number, model: string | undefined): string {
596  const atFull = rows.reduce((a, s) => a + (s.tok.cr * inputRate(s.model)) / 1e6, 0)
597  const ratio = inputRate(model) > 0 ? readRate(model) / inputRate(model) : 0
598  const rate = ratio > 0 ? ` at ${Number(ratio.toFixed(3))}× on ${modelName(model)}` : ' for a fraction of it'
599  const saved =
600    atFull > readCost
601      ? ` Without the cache, the ${fmtTok(reread)} re-read tokens would have cost ≈ ${fmtUsd(atFull)}, not ${fmtUsd(readCost)}.`
602      : ''
603  return `Each request resends the whole conversation. New input is cached once, at 1.25–2× the input price; later requests re-read it${rate}.${saved}`
604}
605
606function details(ui: Ui, look: Looks, s: SpendSource, d: ViewData, width: number, view: ViewActions['view']): RenderElement {
607  const { Box, Text, Button } = ui
608  const now = d.now
609  const agentId = s.kind === 'agent' ? s.key.slice('agent:'.length) : undefined
610  const isViewed = agentId !== undefined && d.viewedAgentId === agentId
611  const hit = cacheHit(s.tok)
612  const perStep = s.steps === 0 ? 0 : s.usd / s.steps
613  const members = Object.values(s.members ?? {}).sort((a, b) => b.usd - a.usd)
614  // Inside the details' indent: two for the marker, a space, the name, a space, eight for the cost.
615  const nameW = Math.max(6, width - 2 - 13)
616  return (
617    <Box flexDirection="column" paddingLeft={2} marginBottom={1}>
618      <Text dimColor wrap="truncate-end">
619        {s.type !== undefined && s.kind !== 'workflow' ? `${s.type} · ` : ''}
620        {modelName(s.model)} · {s.steps} requests
621        {s.lastAt - s.firstAt >= 1000 ? ` over ${fmtDur(s.lastAt - s.firstAt)}` : ''}
622        {s.status === 'running' && s.kind !== 'main' && now - s.lastAt >= 5000 ? ` · quiet ${fmtDur(now - s.lastAt)}` : ''}
623      </Text>
624      <Text dimColor wrap="truncate-end">
625        {fmtTok(s.tok.out)} out · {fmtTok(s.tok.cw)} cached · {fmtTok(s.tok.cr)} re-read
626      </Text>
627      <Text dimColor wrap="truncate-end">
628        {fmtUsd(perStep)} per request{hit === undefined ? '' : ` · cache served ${fmtPct(hit * 100)}`}
629      </Text>
630      {members.length > 0 && <Text dimColor>{members.length} {members.length === 1 ? 'agent' : 'agents'}:</Text>}
631      {members.slice(0, 6).map(m => (
632        <Text {...look.table} dimColor wrap="truncate-end">
633          {'  '}
634          {m.status === 'running' ? '●' : m.status === 'failed' ? '✗' : '✓'} {clip(m.label, nameW).padEnd(nameW)}{' '}
635          {fmtUsd(m.usd).padStart(8)}
636        </Text>
637      ))}
638      {members.length > 6 && <Text dimColor>{'  '}+{members.length - 6} more</Text>}
639      {agentId !== undefined && d.caps.agentView && (
640        <Button key={`view:${agentId}`} plain dimColor onPress={() => view(isViewed ? undefined : agentId)}>
641          {isViewed ? '← back to the main conversation' : '↗ show its transcript'}
642        </Button>
643      )}
644    </Box>
645  )
646}
647
648function printer(Text: Ui['Text'], tick: number): RenderElement {
649  const belt = '  $  ▓▓  $  ▓▓  $  ▓▓  $  ▓▓'
650  const at = tick % 8
651  const bills = belt.slice(at, at + 13).padEnd(13)
652  return (
653    <Text color="success">
654      {'╭───────────────╮\n'}
655      {`│ ${bills} │  b${'r'.repeat(2 + (tick % 5))}\n`}
656      {'╰─┬───────────┬─╯\n'}
657      {'  ┷           ┷  '}
658    </Text>
659  )
660}
661
662/** The one-line status chip shown under the prompt while the pane is closed. */
663export function chipText(core: SpendCore | null, live: SpendLive): string | undefined {
664  if (core === null) return undefined
665  const parts = [`◆ ${fmtUsd(core.usd)}`]
666  if (core.turn !== null) parts.push(`turn +${fmtUsd((core.turn.usd1 ?? core.usd) - core.turn.usd0)}`)
667  for (const l of core.limits) {
668    if (l.kind === 'five_hour' || l.kind === 'seven_day') parts.push(`${windowName(l.kind)} ${fmtPct(l.pct)}`)
669  }
670  if (live.inflight > 1) parts.push(`${live.inflight} live`)
671  return `${parts.join(' · ')}  (/spend)`
672}
673
hooks/art.ts 146 lines
1// The spend chart, stacked by who spent: in 24-bit block elements for every
2// terminal, and as an anti-aliased bitmap where the kitty graphics protocol is.
3
4import { barTotal, base64, DEFAULT_COLOR, PALETTE } from './ledger'
5import type { Bar } from './ledger'
6
7export type Cell = readonly [number, number, number]
8
9const ch = (c: number, shift: number) => (c >> shift) & 0xff
10
11export function mix(a: number, b: number, t: number): number {
12  const k = Math.min(1, Math.max(0, t))
13  const m = (s: number) => Math.round(ch(a, s) + (ch(b, s) - ch(a, s)) * k)
14  return (m(16) << 16) | (m(8) << 8) | m(0)
15}
16
17const MARK = PALETTE.mark
18const EIGHTHS = [0x20, 0x2581, 0x2582, 0x2583, 0x2584, 0x2585, 0x2586, 0x2587, 0x2588]
19const COLORS = [PALETTE.main, PALETTE.agents, PALETTE.workflows]
20
21/** The bar's stack in eighths of a cell: where each source's part ends, bottom up. */
22function stack(bar: Bar, peak: number, span: number): number[] {
23  const total = barTotal(bar)
24  if (total <= 0) return [0, 0, 0]
25  const h = Math.max(1, Math.round((total / peak) * span))
26  const at = (v: number) => Math.round((v / total) * h)
27  return [at(bar.main), at(bar.main + bar.agents), h]
28}
29
30/**
31 * The chart in cells. Each bar stacks main thread, subagents and workflows
32 * bottom up; where two parts meet inside a cell, its lower-eighths glyph
33 * takes the lower part's color and its ground the upper's, so the stack stays
34 * exact to an eighth of a cell.
35 */
36export function areaCells(bars: readonly Bar[], rows: number): Cell[] {
37  const peak = Math.max(0.0001, ...bars.map(barTotal))
38  const span = rows * 8
39  const stacks = bars.map(b => stack(b, peak, span))
40  const cells: Cell[] = []
41  for (let r = 0; r < rows; r++) {
42    const floor = (rows - 1 - r) * 8
43    const top = floor + 8
44    bars.forEach((bar, i) => {
45      const bounds = stacks[i]!
46      const h = bounds[2]!
47      if (h <= floor) {
48        if (bar.isCompacted) cells.push([0x2502, MARK, DEFAULT_COLOR])
49        else if (r === rows - 1) cells.push([0x2581, PALETTE.axis, DEFAULT_COLOR])
50        else cells.push([0x20, DEFAULT_COLOR, DEFAULT_COLOR])
51        return
52      }
53      const below = COLORS[bounds.findIndex(b => b > floor)]!
54      const edge = bounds.find(b => b > floor && b < top)
55      if (edge === undefined) {
56        cells.push([0x2588, below, DEFAULT_COLOR])
57        return
58      }
59      const above = edge >= h ? DEFAULT_COLOR : COLORS[bounds.findIndex(b => b > edge)]!
60      cells.push([EIGHTHS[edge - floor]!, below, above])
61    })
62  }
63  return cells
64}
65
66/** The chart as a line of text, the words a picture stands in for. */
67export function areaText(bars: readonly Bar[]): string {
68  const peak = Math.max(0.0001, ...bars.map(barTotal))
69  return bars.map(b => (barTotal(b) <= 0 ? '▁' : '▁▂▃▄▅▆▇█'[Math.min(7, Math.round((barTotal(b) / peak) * 7))])).join('')
70}
71
72const PX_W = 8
73const PX_H = 16
74
75// The last picture drawn: a redraw with the same bars sends the same source,
76// which the terminal is not sent again.
77const memo: { key: string; picture?: { rgba: string; width: number; height: number } } = { key: '' }
78
79/**
80 * The chart as RGBA pixels on a transparent ground: smooth stacked areas
81 * through the bars, an anti-aliased line along the top, and a dashed guide
82 * where the session compacted.
83 */
84export function areaPixels(bars: readonly Bar[], rows: number): { rgba: string; width: number; height: number } {
85  const key = `${rows}|${bars.map(b => `${b.main.toFixed(5)},${b.agents.toFixed(5)},${b.workflows.toFixed(5)},${b.isCompacted ? 1 : 0}`).join(';')}`
86  if (key === memo.key && memo.picture !== undefined) return memo.picture
87  const width = bars.length * PX_W
88  const height = rows * PX_H
89  const room = height - 4 // headroom for the line
90  const px = new Uint8Array(width * height * 4)
91  const put = (x: number, y: number, color: number, a: number) => {
92    if (a <= 0 || y < 0 || y >= height) return
93    const o = (y * width + x) * 4
94    const prior = px[o + 3]! / 255
95    const out = a + prior * (1 - a)
96    const blend = (s: number, k: number) => Math.round((ch(color, s) * a + px[o + k]! * prior * (1 - a)) / out)
97    px[o] = blend(16, 0)
98    px[o + 1] = blend(8, 1)
99    px[o + 2] = blend(0, 2)
100    px[o + 3] = Math.round(out * 255)
101  }
102  // A picture reads as a rate: each bar is softened with its neighbours.
103  const raw = (i: number) => bars[Math.min(bars.length - 1, Math.max(0, i))]!
104  const soft = bars.map((bar, i): Bar => {
105    const [l, r] = [raw(i - 1), raw(i + 1)]
106    const k = (f: (b: Bar) => number) => (f(l) + 2 * f(bar) + f(r)) / 4
107    return { main: k(b => b.main), agents: k(b => b.agents), workflows: k(b => b.workflows), isCompacted: bar.isCompacted }
108  })
109  const softPeak = Math.max(0.0001, ...soft.map(barTotal))
110  const at = (i: number) => soft[Math.min(soft.length - 1, Math.max(0, i))]!
111  const levels = (b: Bar) => [b.main, b.main + b.agents, barTotal(b)].map(v => (v / softPeak) * room)
112  for (let x = 0; x < width; x++) {
113    // Cosine interpolation between the bars' centers.
114    const f = (x + 0.5) / PX_W - 0.5
115    const i = Math.floor(f)
116    const t = (1 - Math.cos((f - i) * Math.PI)) / 2
117    const a = levels(at(i))
118    const b = levels(at(i + 1))
119    const tops = a.map((v, k) => v + (b[k]! - v) * t)
120    if (at(Math.round(f)).isCompacted && x % PX_W === PX_W / 2) {
121      for (let y = 0; y < height; y++) if (y % 4 < 2) put(x, y, MARK, 0.9)
122    }
123    if (tops[2]! < 0.01) {
124      put(x, height - 1, PALETTE.axis, 0.9)
125      continue
126    }
127    let below = 0
128    tops.forEach((top, k) => {
129      for (let y = Math.floor(height - top); y < Math.ceil(height - below); y++) {
130        const cover = Math.min(1, height - below - y) * Math.min(1, y + 1 - (height - top))
131        put(x, y, COLORS[k]!, 0.82 * Math.max(0, cover))
132      }
133      below = Math.max(below, top)
134    })
135    // The line along the top, in the color of the part it runs over.
136    const crest = tops[2]! - tops[1]! > 0.5 ? 2 : tops[1]! - tops[0]! > 0.5 ? 1 : 0
137    const y = height - tops[2]!
138    for (let py = Math.floor(y) - 2; py <= Math.ceil(y) + 1; py++) {
139      put(x, py, mix(COLORS[crest]!, 0xffffff, 0.25), Math.min(1, Math.max(0, 1.2 - Math.abs(py + 0.5 - y))))
140    }
141  }
142  memo.key = key
143  memo.picture = { rgba: base64(px), width, height }
144  return memo.picture
145}
146
types/index.d.ts 124 lines
1// The spend meter's state contract: every value it keeps in `$.state`.
2
3export type SpendTokens = { in: number; out: number; cr: number; cw: number }
4
5/** USD by what was paid for: written output, new input cached, input re-read from cache, input never cached. */
6export type SpendParts = { input: number; output: number; read: number; write: number }
7
8/** A compaction: when, why (read back from the transcript; the meter does not see it live), and the context before and after, in tokens. */
9export type SpendCompaction = { at: number; trigger?: string; from?: number; to?: number }
10
11export type SpendLimit = { kind: string; pct: number; resetsAt?: string }
12
13/** One rate-limit window as this session has watched it move. */
14export type SpendWindow = {
15  /** Percent when the session first saw this window (or since its last reset). */
16  first: number
17  last: number
18  resetsAt?: string
19  /** What earlier windows moved before they reset, in percent points. */
20  carried: number
21  /** Readings `[at, pct]`, one each time the window moved, for the pace projection. */
22  samples: [number, number][]
23}
24
25export type SpendCore = {
26  /** The engine's own total, as /cost has it (subagents and workflows included). */
27  usd: number
28  /** What the total was when the meter first looked: spend it did not watch. */
29  usd0: number
30  startedAt: number
31  at: number
32  ctxTokens?: number
33  ctxWindow: number
34  ctxPct?: number
35  limits: SpendLimit[]
36  windows: Record<string, SpendWindow>
37  isSubscriber: boolean
38  turn: { startedAt: number; usd0: number; endedAt?: number; usd1?: number } | null
39  /** Milestones and alerts already raised, so each fires once. */
40  fired: string[]
41  /** The ledger's format once the session's transcripts were read back into it. */
42  ledger?: number
43  compactions?: SpendCompaction[]
44}
45
46export type SpendSourceKind = 'main' | 'agent' | 'workflow' | 'fork'
47
48export type SpendSource = {
49  key: string
50  kind: SpendSourceKind
51  label: string
52  type?: string
53  model?: string
54  usd: number
55  cost: SpendParts
56  /** False while some of its responses came from a model the price list lacks. */
57  isPriced: boolean
58  tok: SpendTokens
59  steps: number
60  status: 'running' | 'done' | 'failed'
61  firstAt: number
62  lastAt: number
63  /** For a workflow run: its agents, by id, with what each spent. */
64  members?: Record<string, { label: string; usd: number; steps: number; status: 'running' | 'done' | 'failed' }>
65}
66
67export type SpendAgentMeta = {
68  label: string
69  type: string
70  runId?: string
71  model?: string
72}
73
74export type SpendSources = {
75  rows: Record<string, SpendSource>
76  /** agentId to what it is, from `agent.spawn`. */
77  agents: Record<string, SpendAgentMeta>
78  /** Workflow tool_use_id to the workflow's name. */
79  workflows: Record<string, string>
80}
81
82/** Spend per time bucket: the engine's total moved, split by who spent it. */
83export type SpendSeries = {
84  bucketMs: number
85  /** Start of the newest bucket. */
86  t0: number
87  /** Oldest first: `[total, main, agents, workflows]` in USD. */
88  buckets: [number, number, number, number][]
89}
90
91export type SpendPrefs = {
92  unit: number
93  chip: boolean
94  /** The milestone toasts ($1, π, $42…). */
95  eggs: boolean
96  budget: number | null
97  open: boolean
98  selected: string | null
99  /** How the chart is drawn: block-element cells, kitty-protocol pixels, or whichever the terminal takes. */
100  gfx?: 'auto' | 'cells' | 'pixels'
101}
102
103export type SpendLive = {
104  tick: number
105  inflight: number
106  /** What the responses still streaming have written so far, priced. */
107  estUsd: number
108  /** Source keys with a response streaming now. */
109  keys: string[]
110  flash: { kind: 'brrr'; until: number } | null
111}
112
113declare module 'claude-code' {
114  interface PluginState {
115    'spend-meter': {
116      core: SpendCore | null
117      sources: SpendSources
118      series: SpendSeries
119      prefs: SpendPrefs
120      live: SpendLive
121    }
122  }
123}
124