SLOPSHOPPER

limits

Never get surprised by a rate limit again. An always-on band above the prompt shows your 5-hour and weekly Claude usage with live bars, reset countdowns and…

bandtoaststatusnetworktimer
★ 4v0.2.5MITupdated 2026-10-22Ryan-LT/claude-mods/limits
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · limits
› fix the failing auth test and add an audit log call ⏺ Read(src/auth.ts) ⎿ Read 6 lines ⏺ Update(src/auth.ts) ⎿ Added 2 lines, removed 1 line ⏺ Bash(bun test) ⎿ 3 pass, 1 fail ● Done. refresh now rejects expired claims and logs an audit event. ✻ Worked for 42s · done 4:20 PM 5hr ━━━━━━━━───────────────── 31% ◷ NaNd ctx ━━━━━━━━━━━━───────────── 49% 97.4k/200k [ ⟳ now ] ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Band
5hr ━━━━━━━━───────────────── 31% ◷ NaNd ctx ━━━━━━━━━━━━───────────── 49% 97.4k/200k [ ⟳ now ]
README

limits

See your Claude usage limits before they bite. An always-on band above the Claude Code prompt:

limits band

  • 5hr (5-hour) and wk (weekly): a bar, the percent used, and a countdown to the reset. The tick on each bar marks where an even pace would be by now.
  • Pace warning: the percent turns red when your current rate would use up the limit before it resets. You find out on Tuesday, not when you're blocked on Thursday.
  • ctx: how full this chat's context window is (89.1k/1M).
  • Alerts: a toast at 75%, 90% and 100%.
  • The real numbers: every 2 minutes (or when you press refresh) it asks Anthropic for the same figures claude.ai's usage page shows, using your Claude login. No model request, so no tokens, and all open sessions share one ask, so 10 open chats still make only one request.
  • Works in the terminal and the desktop app.

Install

Run this in a terminal claude session:

/plugin install limits --marketplace Ryan-LT/claude-mods

Answer y to add the marketplace, then choose the user scope.

Develop

claude --plugin-dir ./limits
claude plugin test ./limits
Source 2 files
hooks/register.tsx 586 lines
1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register, RenderElement, SessionRateLimit } from 'claude-code'
3
4import type { Context, Limit, Snapshot } from '../types'
5
6const snapshot = atom({ plugin: 'limits', key: 'snapshot' } as const, null)
7const now = atom({ plugin: 'limits', key: 'now' } as const, 0)
8const alerted = atom({ plugin: 'limits', key: 'alerted' } as const, {})
9const refreshing = atom({ plugin: 'limits', key: 'refreshing' } as const, false)
10const context = atom({ plugin: 'limits', key: 'context' } as const, null)
11
12const HOUR = 3_600_000
13const WINDOW_MS: Record<string, number> = { five_hour: 5 * HOUR, seven_day: 168 * HOUR }
14const LABEL: Record<string, string> = { five_hour: '5hr', seven_day: 'wk', spend_limit: '$' }
15const LONG_LABEL: Record<string, string> = {
16  five_hour: '5-hour',
17  seven_day: 'Weekly',
18  spend_limit: 'Spend',
19}
20const ALERT_AT = [75, 90, 100]
21const MINUTE = 60_000
22/**
23 * Auto refresh runs once per slot of this length, on the clock (:00, :02, :04 …), so its timing is
24 * steady; every open session shares one slot, so only one of them asks.
25 */
26const SLOT = 2 * MINUTE
27/** A main-thread turn this old is taken as over, should its end never have been heard. */
28const BUSY_MAX = 10 * MINUTE
29/** The refresh button's own floor between presses. */
30const PRESS_GAP = 30_000
31const STORE_LATEST = 'latest'
32const STORE_PINGED = 'lastPingAt'
33const STORE_SLOT = 'lastSlot'
34
35// ---------- formatting ----------
36
37const fmtTokens = (n: number): string => {
38  const short = (v: number, unit: string) => `${Number.isInteger(v) ? v : v.toFixed(1)}${unit}`
39  if (n >= 1_000_000) return short(Math.round(n / 100_000) / 10, 'M')
40  if (n >= 1_000) return short(Math.round(n / 100) / 10, 'k')
41  return String(n)
42}
43
44const fmtSpan = (ms: number): string => {
45  if (ms <= 0) return 'now'
46  const mins = Math.round(ms / 60_000)
47  if (mins < 60) return `${mins}m`
48  const hours = Math.floor(mins / 60)
49  if (hours < 24) return mins % 60 ? `${hours}h${mins % 60}m` : `${hours}h`
50  const days = Math.floor(hours / 24)
51  return hours % 24 ? `${days}d${hours % 24}h` : `${days}d`
52}
53
54const fmtPct = (p: number | undefined): string =>
55  p === undefined ? '—' : `${p < 10 && p % 1 !== 0 ? p.toFixed(1) : Math.round(p)}%`
56
57const tone = (p: number | undefined): string =>
58  p === undefined ? 'inactive' : p >= 85 ? 'error' : p >= 60 ? 'warning' : 'success'
59
60const labelOf = (kind: string): string => LABEL[kind] ?? kind.replace(/_/g, ' ').toUpperCase()
61const longLabelOf = (kind: string): string => LONG_LABEL[kind] ?? kind.replace(/_/g, ' ')
62
63const ORDER = ['five_hour', 'seven_day', 'spend_limit']
64const sortLimits = (limits: Limit[]): Limit[] =>
65  [...limits].sort((a, b) => {
66    const ia = ORDER.indexOf(a.kind)
67    const ib = ORDER.indexOf(b.kind)
68    return (ia < 0 ? 99 : ia) - (ib < 0 ? 99 : ib)
69  })
70
71/** How far into the window we are versus how much is used, and where that trends. */
72const paceOf = (limit: Limit, at: number) => {
73  const length = WINDOW_MS[limit.kind]
74  if (!length || !limit.resetsAt) return null
75  const left = Date.parse(limit.resetsAt) - at
76  if (!Number.isFinite(left)) return null
77  const elapsed = Math.min(length, Math.max(0, length - left))
78  const elapsedPct = (elapsed / length) * 100
79  if (elapsed < length * 0.02) return { elapsedPct, delta: 0, runsOutIn: undefined }
80  const rate = limit.percentUsed / elapsed // % per ms
81  const runsOutIn = rate > 0 && limit.percentUsed < 100 ? (100 - limit.percentUsed) / rate : undefined
82  return {
83    elapsedPct,
84    delta: limit.percentUsed - elapsedPct,
85    runsOutIn: runsOutIn !== undefined && runsOutIn < left ? runsOutIn : undefined,
86  }
87}
88
89// ---------- state ----------
90
91/** `isLive`: the figures came from a response just now, so they stamp `checkedAt`. */
92const toSnapshot = (rateLimits: SessionRateLimit[], at: number, isLive = false): Snapshot => ({
93  limits: rateLimits.map(r => ({
94    kind: r.kind,
95    percentUsed: r.percentUsed,
96    resetsAt: r.resetsAt,
97    ...(isLive ? { checkedAt: at } : {}),
98  })),
99  at,
100})
101
102/** A window whose reset time has passed starts over: show it empty, not stale. */
103const current = (l: Limit, at: number): Limit =>
104  l.resetsAt && Date.parse(l.resetsAt) <= at ? { kind: l.kind, percentUsed: 0 } : l
105
106const resetMs = (l: Limit) => (l.resetsAt ? Date.parse(l.resetsAt) : 0)
107
108/**
109 * Which of two readings of one window is current. The later window wins (its reset is later).
110 * Within one window, the latest live reading wins whatever its figure: a plan upgrade drops the
111 * percent mid-window. A figure that may be replayed (one a chat saw hours ago) never beats a live
112 * one; between two such figures the higher wins, since usage inside a window only climbs.
113 */
114const newer = (a: Limit, b: Limit): Limit => {
115  const gap = resetMs(b) - resetMs(a)
116  if (Math.abs(gap) > 30 * MINUTE) return gap > 0 ? b : a
117  const keep = (l: Limit) => ({ ...l, resetsAt: l.resetsAt ?? (l === a ? b : a).resetsAt })
118  if (a.checkedAt !== undefined || b.checkedAt !== undefined) {
119    return (b.checkedAt ?? -1) >= (a.checkedAt ?? -1) ? keep(b) : keep(a)
120  }
121  return b.percentUsed >= a.percentUsed ? keep(b) : a
122}
123
124/**
125 * One response may report only some windows (the weekly one, say, and not the 5-hour), so a
126 * reading updates the windows it names and keeps the others, unless their reset has passed.
127 */
128const merge = (prev: Snapshot | null | undefined, next: Snapshot): Snapshot => {
129  const byKind = new Map((prev?.limits ?? []).map(l => [l.kind, l]))
130  for (const l of next.limits) {
131    const had = byKind.get(l.kind)
132    byKind.set(l.kind, had ? newer(had, l) : l)
133  }
134  const limits = [...byKind.values()].filter(
135    l => next.limits.some(n => n.kind === l.kind) || !l.resetsAt || Date.parse(l.resetsAt) > next.at,
136  )
137  return { limits, at: Math.max(prev?.at ?? 0, next.at) }
138}
139
140const maybeAlert = async ($: EngineInterface, s: Snapshot) => {
141  const seen = await read($, alerted)
142  const next: Record<string, number> = { ...seen }
143  for (const l of s.limits) {
144    const crossed = ALERT_AT.filter(level => l.percentUsed >= level).pop() ?? 0
145    if (crossed > (seen[l.kind] ?? 0)) {
146      $.ui.toast(`${longLabelOf(l.kind)} limit at ${fmtPct(l.percentUsed)}`, { timeoutMs: 6000 })
147    }
148    next[l.kind] = crossed
149  }
150  await update($, alerted, () => next)
151}
152
153/** This session's context window: its own, never shared. */
154const applyContext = async ($: EngineInterface, c: { tokens?: number; window: number; percent?: number }) => {
155  const value: Context = { tokens: c.tokens, window: c.window, percent: c.percent }
156  await update($, context, () => value)
157}
158
159/** Show a reading here, and hand it to every other open session through the store. */
160const apply = async ($: EngineInterface, reading: Snapshot, isShared = true) => {
161  if (reading.limits.length === 0) return
162  const s = merge(await read($, snapshot), reading)
163  await update($, snapshot, () => s)
164  await update($, now, () => Date.now())
165  await maybeAlert($, s)
166  if (isShared) {
167    const latest = (await $.store.get(STORE_LATEST)) as Snapshot | undefined
168    await $.store.set(STORE_LATEST, merge(latest, reading))
169  }
170}
171
172/** Takes up a newer reading another session made. */
173const pullShared = async ($: EngineInterface) => {
174  const latest = (await $.store.get(STORE_LATEST)) as Snapshot | undefined
175  const mine = await read($, snapshot)
176  if (latest?.limits?.length && (!mine || JSON.stringify(merge(mine, latest).limits) !== JSON.stringify(mine.limits) || mine.at < latest.at)) {
177    await apply($, latest, false)
178  }
179}
180
181const USAGE_URL = 'https://api.anthropic.com/api/oauth/usage'
182const WINDOWS = ['five_hour', 'seven_day']
183
184type UsageWindow = { utilization?: number | null; resets_at?: string | null } | null
185
186/**
187 * The figures claude.ai's usage page shows, asked of Anthropic with this session's login (the engine
188 * sets the credential; the mod never sees it). Null with no claude.ai login: an API key or a gateway.
189 */
190const fetchUsage = async ($: EngineInterface): Promise<SessionRateLimit[] | null> => {
191  const auth = await $.session.authorize()
192  if (auth?.kind !== 'bearer') return null
193  const res = await $.http.fetch(USAGE_URL, { headers: { 'anthropic-beta': 'oauth-2025-04-20' }, auth: auth.handle })
194  if (!res.ok) return null
195  const body = JSON.parse(res.text) as Record<string, UsageWindow>
196  return WINDOWS.flatMap(kind => {
197    const w = body[kind]
198    if (typeof w?.utilization !== 'number') return []
199    const resetsAt = w.resets_at ? new Date(w.resets_at).toISOString() : undefined
200    return [{ kind, percentUsed: Math.round(w.utilization * 10) / 10, ...(resetsAt ? { resetsAt } : {}) }]
201  })
202}
203
204/**
205 * Asks Anthropic for the real figures: no model request, so no tokens, and the slot and press gaps
206 * keep it to one ask per two minutes across every open session.
207 */
208const refresh = async ($: EngineInterface, why: 'press' | 'auto') => {
209  if (await read($, refreshing)) return
210  const at = await $.clock.now()
211  if (why === 'press') {
212    const lastPing = Number((await $.store.get(STORE_PINGED)) ?? 0)
213    if (at - lastPing < PRESS_GAP) return void (await pullShared($))
214  } else {
215    // One ask per clock slot across every session: the first to reach the slot takes it.
216    const slot = Math.floor(at / SLOT)
217    if (Number((await $.store.get(STORE_SLOT)) ?? -1) >= slot) return void (await pullShared($))
218    await $.store.set(STORE_SLOT, slot)
219  }
220  await $.store.set(STORE_PINGED, at)
221  await update($, refreshing, () => true)
222  try {
223    await applyContext($, (await $.session.usage()).context)
224    const limits = await fetchUsage($)
225    if (limits) await apply($, toSnapshot(limits, await $.clock.now(), true))
226  } catch {
227    // offline or refused: the last reading stays, with its age on the button
228  } finally {
229    await update($, refreshing, () => false)
230  }
231}
232
233/** When the running main-thread turn began, or 0: figures that arrive meanwhile ride on its replies. */
234let busySince = 0
235
236const tick = async ($: EngineInterface) => {
237  const at = await $.clock.now()
238  await update($, now, () => at)
239  await pullShared($)
240  // Replies bring figures too, but an idle chat can replay old ones: Anthropic's own answer settles it.
241  await refresh($, 'auto')
242}
243
244/**
245 * The 15-second ticker, started by whichever hook runs first in this module's life: a reload
246 * in an open chat raises no session.start, and without this the bar would never refresh there.
247 */
248let isTicking = false
249
250const ensureTicker = ($: EngineInterface) => {
251  if (isTicking) return
252  isTicking = true
253  $.clock.after(50, () => void tick($).catch(() => {}))
254  $.clock.every(15_000, () => void tick($).catch(() => {}))
255}
256
257// ---------- drawing ----------
258
259/** One limit, reduced to what the band shows. */
260type Meter = {
261  label: string
262  pct: number
263  color: string
264  reset?: string
265  /** Where "now" sits in the window, 0–1: fill past it means ahead of pace. */
266  elapsed?: number
267  runsOut?: string
268  /** Plain grey text after the percent, no icon (the context's token count). */
269  detail?: string
270  /** Shown in place of the percent while there is none yet. */
271  pctText?: string
272  alt: string
273}
274
275const HEX: Record<string, string> = {
276  success: '#3fb950',
277  warning: '#d29922',
278  error: '#f85149',
279}
280// Mid-tone grey reads on both light and dark themes (the surface's theme is not exposed).
281const GREY = '#8c959f'
282const FONT = 12.5
283const CH = FONT * 0.62 // monospace advance; every run is pinned to it with textLength
284const BAR_H = 8
285const TEXT_H = 18
286/** The bar's drawing: as tall as the text beside it, so the row lines up however the surface aligns it. */
287const BAR_BOX_H = TEXT_H
288/** The text's visual middle (between its cap height and baseline), where the bar is centred. */
289const BAR_CY = 8.5
290/** How far the "now" tick reaches above and below the bar's middle. */
291const TICK_R = 7
292/** Each rounded end, drawn apart from the stretching middle so its curve keeps its shape. */
293const CAP_W = BAR_H / 2
294/** Bar units across; the drawing stretches to its box, so this is only its resolution. */
295const BAR_UNITS = 1000
296const GAP = 22
297const ICON = 11
298
299const toMeter = (raw: Limit, at: number): Meter => {
300  const l = current(raw, at)
301  const pace = paceOf(l, at)
302  const runsOut = pace?.runsOutIn !== undefined ? fmtSpan(pace.runsOutIn) : undefined
303  // On track to run out before the reset: the bar and percent go red, no extra text.
304  const level = runsOut ? 'error' : tone(l.percentUsed)
305  const reset = l.resetsAt ? fmtSpan(Date.parse(l.resetsAt) - at) : undefined
306  return {
307    label: labelOf(l.kind),
308    pct: l.percentUsed,
309    color: level,
310    reset,
311    elapsed: pace ? pace.elapsedPct / 100 : undefined,
312    runsOut,
313    alt:
314      `${longLabelOf(l.kind)} limit ${fmtPct(l.percentUsed)} used` +
315      (reset ? `, resets in ${reset}` : '') +
316      (pace ? `, ${Math.round(pace.elapsedPct)}% of window elapsed` : '') +
317      (runsOut ? `, runs out in about ${runsOut}` : ''),
318  }
319}
320
321const esc = (t: string) => t.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
322
323/** One run of text at x, exactly `text.length * CH` wide whatever font the surface picks. */
324const run = (x: number, y: number, text: string, fill: string, bold = false) =>
325  `<text x="${x.toFixed(1)}" y="${y}" fill="${fill}"${bold ? ' font-weight="600"' : ''} ` +
326  `textLength="${(text.length * CH).toFixed(1)}" lengthAdjust="spacingAndGlyphs">${esc(text)}</text>`
327
328/** A small clock: "resets in". */
329const clockIcon = (x: number, cy: number, color: string) => {
330  const r = ICON / 2 - 0.75
331  const cx = x + ICON / 2
332  return (
333    `<g fill="none" stroke="${color}" stroke-width="1.3" stroke-linecap="round">` +
334    `<circle cx="${cx.toFixed(1)}" cy="${cy}" r="${r}"/>` +
335    `<path d="M${cx.toFixed(1)} ${cy - r + 2}V${cy}l${(r * 0.55).toFixed(1)} ${(r * 0.45).toFixed(1)}"/></g>`
336  )
337}
338
339/** A run of text pieces as one drawing of its own width. */
340const textSvg = (body: string, width: number) => {
341  const w = Math.ceil(width)
342  return {
343    source:
344      `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${TEXT_H}" viewBox="0 0 ${w} ${TEXT_H}">` +
345      `<style>text{font-size:${FONT}px;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;white-space:pre}</style>${body}</svg>`,
346    width: w,
347    height: TEXT_H,
348  }
349}
350
351const labelSvg = (m: Meter) => textSvg(run(0, 13, m.label, GREY), m.label.length * CH)
352
353/** The percent, detail and reset after a bar; `pad` leaves the gap before the next meter. */
354const tailSvg = (m: Meter, pad: number) => {
355  const color = HEX[m.color] ?? GREY
356  const pct = m.pctText ?? fmtPct(m.pct)
357  let body = run(0, 13, pct, color, true)
358  let x = pct.length * CH
359  if (m.detail) {
360    body += run(x + CH, 13, m.detail, GREY)
361    x += (m.detail.length + 1) * CH
362  }
363  if (m.reset) {
364    x += 8
365    body += clockIcon(x, TEXT_H / 2, GREY)
366    x += ICON + 4
367    body += run(x, 13, m.reset, GREY)
368    x += m.reset.length * CH
369  }
370  return textSvg(body, x + pad)
371}
372
373/**
374 * The bar's middle, drawn to stretch over whatever width its box has (no aspect kept), so the
375 * fill's end is square and reads exactly against the "now" tick, which keeps its width as it stretches.
376 */
377const barSvg = (m: Meter) => {
378  const color = HEX[m.color] ?? GREY
379  const by = BAR_CY - BAR_H / 2
380  const fill = Math.min(1, Math.max(0, m.pct / 100)) * BAR_UNITS
381  let body = `<rect x="0" y="${by}" width="${BAR_UNITS}" height="${BAR_H}" fill="${GREY}" fill-opacity=".25"/>`
382  if (fill > 0) body += `<rect x="0" y="${by}" width="${fill.toFixed(1)}" height="${BAR_H}" fill="${color}"/>`
383  if (m.elapsed !== undefined) {
384    const tx = Math.min(BAR_UNITS, Math.max(0, m.elapsed * BAR_UNITS))
385    body +=
386      `<line x1="${tx.toFixed(1)}" y1="${BAR_CY - TICK_R}" x2="${tx.toFixed(1)}" y2="${BAR_CY + TICK_R}" stroke="${GREY}" ` +
387      `stroke-width="1" vector-effect="non-scaling-stroke"/>`
388  }
389  return (
390    `<svg xmlns="http://www.w3.org/2000/svg" width="${BAR_UNITS}" height="${BAR_BOX_H}" ` +
391    `viewBox="0 0 ${BAR_UNITS} ${BAR_BOX_H}" preserveAspectRatio="none">${body}</svg>`
392  )
393}
394
395/** A rounded end of the bar: filled once the fill reaches it, else the track. */
396const capSvg = (m: Meter, side: 'left' | 'right') => {
397  const isFilled = side === 'left' ? m.pct > 0 : m.pct >= 100
398  const paint = isFilled ? `fill="${HEX[m.color] ?? GREY}"` : `fill="${GREY}" fill-opacity=".25"`
399  const r = BAR_H / 2
400  const cy = BAR_CY
401  const d =
402    side === 'left'
403      ? `M${CAP_W} ${cy - r}A${r} ${r} 0 0 0 ${CAP_W} ${cy + r}Z`
404      : `M0 ${cy - r}A${r} ${r} 0 0 1 0 ${cy + r}Z`
405  return (
406    `<svg xmlns="http://www.w3.org/2000/svg" width="${CAP_W}" height="${BAR_BOX_H}" ` +
407    `viewBox="0 0 ${CAP_W} ${BAR_BOX_H}"><path d="${d}" ${paint}/></svg>`
408  )
409}
410
411/** Terminal bar: thin line, with a tick at "now" in the window. */
412const barRuns = (m: Meter, width = 8): { text: string; color: string }[] => {
413  const filled = Math.min(width, Math.max(m.pct > 0 ? 1 : 0, Math.round((m.pct / 100) * width)))
414  const tick = m.elapsed === undefined ? -1 : Math.min(width - 1, Math.floor(m.elapsed * width))
415  const runs: { text: string; color: string }[] = []
416  for (let i = 0; i < width; i++) {
417    const [ch, color] = i === tick ? ['┃', 'inactive'] : i < filled ? ['━', m.color] : ['─', 'inactive']
418    const prev = runs[runs.length - 1]
419    if (prev && prev.color === color && i !== tick && i - 1 !== tick) prev.text += ch
420    else runs.push({ text: ch, color })
421  }
422  return runs
423}
424
425// ---------- hooks ----------
426
427export const register: Register = on => {
428  on('session.start', async ($, e, next) => {
429    const ran = await next(e)
430    $.ui.status(undefined)
431    try {
432      const usage = await $.session.usage()
433      await applyContext($, usage.context)
434      await apply($, toSnapshot(usage.rateLimits, Date.now()))
435    } catch {
436      // usage not available yet; session.measure will fill it in
437    }
438    await update($, refreshing, () => false)
439    ensureTicker($)
440
441    return ran
442  })
443
444  on('session.measure', async ($, e, next) => {
445    ensureTicker($)
446    if (e.changed.includes('context')) await applyContext($, e.context)
447    // Mid-turn, the figures ride on the reply that just came in; idle, they may be an old one replayed.
448    const isLive = busySince > 0 && Date.now() - busySince < BUSY_MAX
449    if (e.changed.includes('rateLimits')) await apply($, toSnapshot(e.rateLimits, Date.now(), isLive))
450
451    return next(e)
452  })
453
454  on('turn.start', ($, e, next) => {
455    ensureTicker($)
456    busySince = Date.now()
457
458    return next(e)
459  })
460
461  on('turn.complete', ($, e, next) => {
462    if (!e.agentId) busySince = 0
463
464    return next(e)
465  })
466
467  on('session.end', async ($, e, next) => {
468    if (e.reason === 'clear') {
469      await update($, alerted, () => ({}))
470      await update($, context, () => null)
471    }
472
473    return next(e)
474  })
475
476  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
477    ensureTicker($)
478    // Another mod's band beneath (quick replies) draws under this one.
479    const below = await next(e)
480    if (e.props.hasSurvey) return below
481    const withBelow = (tree: RenderElement) => {
482      if (below.type === 'engine') return tree
483      const { Box } = $.ui.resolve(e)
484      return (
485        <Box flexDirection="column" rowGap={1}>
486          {tree}
487          {below}
488        </Box>
489      )
490    }
491
492    // A fresh chat has no reading yet: take the shared one, then ask Anthropic, so the bar shows before the first reply.
493    if (!(await read($, snapshot))) {
494      await pullShared($).catch(() => {})
495      if (!(await read($, snapshot))) void refresh($, 'press').catch(() => {})
496    }
497    const s = await read($, snapshot)
498    const at = (await read($, now)) || Date.now()
499    const ctx = await read($, context)
500    const meters = sortLimits(s?.limits ?? []).map(l => toMeter(l, at))
501    if (ctx?.percent !== undefined && ctx.tokens !== undefined) {
502      const detail = `${fmtTokens(ctx.tokens)}/${fmtTokens(ctx.window)}`
503      meters.push({
504        label: 'ctx',
505        pct: ctx.percent,
506        color: tone(ctx.percent),
507        detail,
508        alt: `Context ${detail} tokens, ${fmtPct(ctx.percent)} of the window`,
509      })
510    } else if (ctx) {
511      // Just compacted (or fresh): the count comes back with the next reply, so hold the bar's place.
512      meters.push({
513        label: 'ctx',
514        pct: 0,
515        pctText: '–',
516        color: 'inactive',
517        detail: `–/${fmtTokens(ctx.window)}`,
518        alt: 'Context: the token count shows after the next reply',
519      })
520    }
521    const isRefreshing = await read($, refreshing)
522    const age = s?.at ? at - s.at : undefined
523    const ageText = isRefreshing ? '…' : age === undefined || age < MINUTE ? 'now' : fmtSpan(age)
524    type Resolved = typeof $.ui.resolve extends (e: never) => infer T ? T : never
525    const refreshButton = (Button: Resolved['Button']) => (
526      <Button key="refresh" label={`⟳ ${ageText}`} onPress={() => void refresh($, 'press')} />
527    )
528
529    if (e.surface !== 'terminal') {
530      const { Box, Button, Svg, Text } = $.ui.resolve(e)
531      if (meters.length === 0) return withBelow(<Text dimColor>Limits show after the first reply</Text>)
532      // Each bar's box grows from nothing, so the bars share whatever width the text leaves, equally.
533      return withBelow(
534        <Box flexDirection="row" flexWrap="nowrap" alignItems="center" columnGap={1}>
535          {meters.flatMap((m, i) => {
536            const label = labelSvg(m)
537            const tail = tailSvg(m, i < meters.length - 1 ? GAP - 8 : 0)
538            return [
539              <Svg source={label.source} width={label.width} height={label.height} alt={m.label} />,
540              // The surface refuses an Svg with an empty alt, so each end names itself.
541              <Box flexDirection="row" alignItems="center" flexGrow={1} width={0} minWidth={0}>
542                <Svg source={capSvg(m, 'left')} width={CAP_W} height={BAR_BOX_H} alt="bar start" />
543                <Box flexDirection="row" alignItems="center" flexGrow={1} width={0} minWidth={0}>
544                  <Svg source={barSvg(m)} height={BAR_BOX_H} alt={m.alt} />
545                </Box>
546                <Svg source={capSvg(m, 'right')} width={CAP_W} height={BAR_BOX_H} alt="bar end" />
547              </Box>,
548              <Svg source={tail.source} width={tail.width} height={tail.height} alt={m.pctText ?? fmtPct(m.pct)} />,
549            ]
550          })}
551          {refreshButton(Button)}
552        </Box>,
553      )
554    }
555
556    const { Box, Button, Text } = $.ui.resolve(e)
557    if (meters.length === 0) return withBelow(<Text dimColor>Limits show after the first reply</Text>)
558    // The bars share what the text, the gaps and the button leave of the row.
559    const textCols = meters.reduce(
560      (n, m) =>
561        n + m.label.length + 1 + 1 + (m.pctText ?? fmtPct(m.pct)).length +
562        (m.detail ? m.detail.length + 1 : 0) + (m.reset ? m.reset.length + 3 : 0),
563      0,
564    )
565    const spare = e.props.bodyColumns - textCols - 3 * meters.length - (ageText.length + 6)
566    const barWidth = Math.min(40, Math.max(8, Math.floor(spare / meters.length)))
567
568    return withBelow(
569      <Box flexDirection="row" flexWrap="wrap" columnGap={3}>
570        {meters.map(m => (
571          <Box flexDirection="row">
572            <Text dimColor>{m.label} </Text>
573            {barRuns(m, barWidth).map(r => (
574              <Text color={r.color}>{r.text}</Text>
575            ))}
576            <Text color={m.color} bold> {m.pctText ?? fmtPct(m.pct)}</Text>
577            {m.detail ? <Text dimColor> {m.detail}</Text> : null}
578            {m.reset ? <Text dimColor> ◷ {m.reset}</Text> : null}
579          </Box>
580        ))}
581        {refreshButton(Button)}
582      </Box>,
583    )
584  })
585}
586
types/index.d.ts 21 lines
1/** `checkedAt` (ms): when a live response reported it; absent when it may be an old figure replayed. */
2export type Limit = { kind: string; percentUsed: number; resetsAt?: string; checkedAt?: number }
3
4/** The limits as one API response reported them, and when (ms). */
5export type Snapshot = { limits: Limit[]; at: number }
6
7/** This session's context window, as the last response filled it. */
8export type Context = { tokens?: number; window: number; percent?: number }
9
10declare module 'claude-code' {
11  interface PluginState {
12    'limits': {
13      snapshot: Snapshot | null
14      now: number
15      alerted: Record<string, number>
16      refreshing: boolean
17      context: Context | null
18    }
19  }
20}
21