How full the context window and the quota are, and what today cost, above the prompt.

Claude Code mods, shipped as plugins. Requires Claude Code 2.1.287 or later.
One line above the prompt that shows how full your context window and your plan's usage limits are, and what today's usage would cost at API prices.
https://github.com/user-attachments/assets/8f059df5-95c5-4a7b-ae20-fa0baffb73ff
/compact or /clear; the quota, countdowns and today's total refresh every 30 seconds.• behind.Early in the day, a busy session, and close to the limits, in the desktop app's dark and light themes:


And in the terminal, as text with a gauge for the context:


Session and weekly only appear on a Claude subscription, once the first reply of the session has arrived. With an API key there are no usage limits to show, so only the context is drawn.
Today's figure is read from Claude Code's local transcripts (~/.claude/projects, ~/.config/claude/projects, every entry of CLAUDE_CONFIG_DIR, and the desktop app's Cowork sessions), so it includes sessions that ran before the plugin loaded. It follows ccusage's rules and matches its claude daily output to the cent:
It is an API-equivalent figure: on a subscription you are not billed this amount. Models it has no price for count toward tokens but not cost. The scan reads only new bytes each time and keeps its offsets in ~/.token-bar/. It runs through osascript, so today's figure is macOS only; elsewhere the rest of the band works as before.
In Claude Code:
/plugin marketplace add santosli/claude-mods
/plugin install token-bar@santos-mods
Or from a shell:
claude plugin marketplace add santosli/claude-mods
claude plugin install token-bar@santos-mods --scope user
Then start a new session (or restart the desktop app).
claude --plugin-dir ./token-bar # loads it and reloads on every save
claude plugin validate ./token-bar
claude plugin test ./token-bar
token-bar 在 Claude Code 输入框上方显示一行用量(40 秒宣传片):
颜色跟手机电量一样:60% 以下绿色,60–79% 黄色,80% 及以上红色。每轮回复后、/compact 和 /clear 后更新;额度、倒计时和今日用量每 30 秒刷新一次。session 和 weekly 只在订阅账号下显示(会话收到第一条回复后出现)。
上方截图是桌面端和终端在深色、浅色主题下的三种情况:刚开工、用得正猛、快到额度上限。
今日用量读取本地对话记录,所以插件加载之前的会话也算在内。计算规则和 ccusage 一致(按回复去重、按本地日期统计、按官方价格计费),结果与 ccusage claude daily 逐日一致。金额是按 API 价格的折算值,订阅用户并不会被这样扣费。今日用量依赖 osascript,仅支持 macOS。
安装:在 Claude Code 里运行
/plugin marketplace add santosli/claude-mods
/plugin install token-bar@santos-mods
然后新开一个会话(桌面端重启一下 app)。
MIT
hooks/register.tsx 473 lines1// Token Bar: how full the context window and the quota are, above the prompt.
2import { atom, read, update } from 'claude-code'
3import type { EngineInterface, Register } from 'claude-code'
4
5import type { TokenBarLimit, TokenBarReading, TokenBarToday } from '../types'
6import { bars, barsWidth, ring, week } from './draw'
7import { SCAN_SCRIPT, SCAN_STATE, scanRoots } from './scan'
8
9const HISTORY = 12
10const TICK_MS = 30_000
11const BARS = '▁▂▃▄▅▆▇█'
12// Colored by how much is used, as a phone's battery is: green, then yellow, then red.
13// `color` is the terminal's name; `hex` the desktop's, chosen to read on a light card and a dark one.
14const LEVELS: readonly { upTo: number; color: string; hex: string }[] = [
15 { upTo: 60, color: 'green', hex: '#30a14e' },
16 { upTo: 80, color: 'yellow', hex: '#d99a00' },
17 { upTo: Infinity, color: 'red', hex: '#e5484d' },
18]
19// The rule between two groups on the desktop.
20const RULE = 'rgba(128,128,128,0.45)'
21// The turns chart and last-turn delta: off for now; the history is still kept.
22const SHOW_TURNS = false
23
24// Held by the host, so they survive a hot reload of this file.
25const readings = atom({ plugin: 'token-bar', key: 'readings' } as const, [] as TokenBarReading[])
26const limits = atom({ plugin: 'token-bar', key: 'limits' } as const, [] as TokenBarLimit[])
27// The clock the countdown is drawn against; ticked so the band redraws.
28const now = atom({ plugin: 'token-bar', key: 'now' } as const, 0)
29const today = atom({ plugin: 'token-bar', key: 'today' } as const, null as TokenBarToday | null)
30
31export const register: Register = on => {
32 on('session.start', async ($, e, next) => {
33 const result = await next(e)
34 // The quota is the account's: show the last one known until a response reports it again.
35 const stored = await $.store.get('limits').catch(() => undefined)
36 if (Array.isArray(stored)) await saveLimits($, stored as TokenBarLimit[])
37 await takeReading($)
38 await askApp($)
39 await scanToday($)
40 // Quota moves between turns too, and the countdown runs down: refresh both.
41 $.clock.every(TICK_MS, () => refresh($))
42 return result
43 })
44
45 on('turn.complete', async ($, e, next) => {
46 const result = await next(e)
47 if (!e.agentId) {
48 await takeReading($) // main-loop turns only, not subagents
49 }
50 if (!e.agentId) await scanToday($)
51 return result
52 })
53
54 // The engine's own measurements: the context's fill and the quota, whenever either moves.
55 on('session.measure', async ($, e, next) => {
56 if (e.changed.includes('rateLimits')) await saveLimits($, e.rateLimits, true)
57 if (e.changed.includes('context') && e.context.tokens !== undefined) await addReading($, e.context.tokens, e.context.window)
58 return next(e)
59 })
60
61 // A compaction is not a turn, and no response has measured the window since: estimate it,
62 // system prompt and tools included, as /context does. The size the compaction reports
63 // counts the conversation alone.
64 on('session.compact', async ($, e, next) => {
65 const result = await next(e)
66 if (e.agentId || e.trigger === 'precompute' || 'skip' in result) return result
67 const after = 'tokensAfter' in result ? result.tokensAfter : undefined
68 await setContext($, (await estimate($)) ?? after)
69 return result
70 })
71
72 // A /clear starts the conversation over, with no session.start.
73 on('session.end', async ($, e, next) => {
74 if (e.reason !== 'clear') return next(e)
75 await setContext($, 0)
76 const result = await next(e)
77 // Once the conversation is gone, what is left is the system prompt and the tools.
78 $.clock.after(1_000, () => void estimate($).then(t => setContext($, t)))
79 return result
80 })
81
82 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
83 const history = await read($, readings)
84 const last = history.at(-1)
85 if (e.props.hasSurvey || !last) {
86 return next(e)
87 }
88
89 const quota = await read($, limits)
90 const at = await read($, now)
91 const lv = level(last.percent)
92 const fiveHour = quota.find(l => l.kind === 'five_hour')
93 const sevenDay = quota.find(l => l.kind === 'seven_day')
94 const columns = e.props.bodyColumns
95 const spent = await read($, today)
96
97 if (e.surface !== 'terminal') {
98 const { Box, Text, Svg } = $.ui.resolve(e)
99 // Each group's width in columns, so a narrow band drops the right-most first.
100 // A group may offer a shorter form, tried before it is dropped.
101 type Group = { key: string; color: string; cells: number; draw: () => JSX.Element; compact?: Group }
102 const groups: Group[] = [
103 {
104 key: 'context',
105 color: lv.hex,
106 cells: fit(RING, `${last.percent}%`, 'of context'),
107 draw: () => (
108 <Box key="context" flexDirection="row" alignItems="center" gap={1}>
109 <Svg source={ring(last.percent, lv.hex)} alt={`${last.percent}% of context used`} width={18} height={18} />
110 <Box flexDirection="row" alignItems="center" gap={FIGURE_GAP}>
111 <Text color={lv.hex} bold>{`${last.percent}%`}</Text>
112 <Text dimColor>of context</Text>
113 </Box>
114 </Box>
115 ),
116 },
117 ]
118 if (fiveHour) {
119 const q = level(fiveHour.percentUsed)
120 const qUsed = used(fiveHour.percentUsed)
121 groups.push({
122 key: 'five-hour',
123 color: q.hex,
124 cells: fit(RING, `${qUsed}%`, 'of session', fiveHour.resetsAt ? `↻ ${countdown(fiveHour.resetsAt, at)}` : ''),
125 draw: () => (
126 <Box key="five-hour" flexDirection="row" alignItems="center" gap={1}>
127 <Svg source={ring(qUsed, q.hex)} alt={`${qUsed}% of session used`} width={18} height={18} />
128 <Box flexDirection="row" alignItems="center" gap={FIGURE_GAP}>
129 <Text color={q.hex} bold>{`${qUsed}%`}</Text>
130 <Text dimColor>of session</Text>
131 </Box>
132 {fiveHour.resetsAt && <Text dimColor>{`↻ ${countdown(fiveHour.resetsAt, at)}`}</Text>}
133 </Box>
134 ),
135 })
136 }
137 if (sevenDay) {
138 const q = level(sevenDay.percentUsed)
139 const qUsed = used(sevenDay.percentUsed)
140 groups.push({
141 key: 'seven-day',
142 color: q.hex,
143 cells: fit(RING, `${qUsed}%`, 'of weekly', sevenDay.resetsAt ? `↻ ${countdown(sevenDay.resetsAt, at)}` : ''),
144 draw: () => (
145 <Box key="seven-day" flexDirection="row" alignItems="center" gap={1}>
146 <Svg source={week(qUsed, q.hex)} alt={`${qUsed}% of weekly used`} width={18} height={18} />
147 <Box flexDirection="row" alignItems="center" gap={FIGURE_GAP}>
148 <Text color={q.hex} bold>{`${qUsed}%`}</Text>
149 <Text dimColor>of weekly</Text>
150 </Box>
151 {sevenDay.resetsAt && <Text dimColor>{`↻ ${countdown(sevenDay.resetsAt, at)}`}</Text>}
152 </Box>
153 ),
154 })
155 }
156 if (spent) {
157 groups.push({
158 key: 'today',
159 color: '#8a8a8a',
160 cells: fit(0, money(spent.usd), `today · ${short(spent.tokens)} tokens`),
161 draw: () => (
162 <Box key="today" flexDirection="row" alignItems="center" gap={1}>
163 <Box flexDirection="row" alignItems="center" gap={FIGURE_GAP}>
164 <Text bold>{money(spent.usd)}</Text>
165 <Text dimColor>{`today · ${short(spent.tokens)} tokens`}</Text>
166 </Box>
167 </Box>
168 ),
169 compact: {
170 key: 'today',
171 color: '#8a8a8a',
172 cells: fit(0, money(spent.usd), `today · ${short(spent.tokens)}`),
173 draw: () => (
174 <Box key="today" flexDirection="row" alignItems="center" gap={1}>
175 <Box flexDirection="row" alignItems="center" gap={FIGURE_GAP}>
176 <Text bold>{money(spent.usd)}</Text>
177 <Text dimColor>{`today · ${short(spent.tokens)}`}</Text>
178 </Box>
179 </Box>
180 ),
181 compact: {
182 key: 'today',
183 color: '#8a8a8a',
184 cells: fit(0, money(spent.usd), 'today'),
185 draw: () => (
186 <Box key="today" flexDirection="row" alignItems="center" gap={1}>
187 <Box flexDirection="row" alignItems="center" gap={FIGURE_GAP}>
188 <Text bold>{money(spent.usd)}</Text>
189 <Text dimColor>today</Text>
190 </Box>
191 </Box>
192 ),
193 },
194 },
195 })
196 }
197 if (SHOW_TURNS && history.length > 1) {
198 groups.push({
199 key: 'trend',
200 color: lv.hex,
201 cells: 22,
202 draw: () => (
203 <Box key="trend" flexDirection="row" alignItems="center" gap={1}>
204 <Svg source={bars(history.map(r => r.tokens), lv.hex)} alt={`Context after each of the last ${history.length} turns`} width={barsWidth(history.length)} height={14} />
205 <Text dimColor>{trend(history).trim()}</Text>
206 </Box>
207 ),
208 })
209 }
210 // Groups sit a rule apart: a column of space, the rule, and a column again. Those that
211 // don't fit leave a dot each, a column apart, so the band never wraps.
212 const rule = 2 + width('│')
213 const dots = (n: number) => (n ? 1 + n * width('•') + (n - 1) : 0)
214 const need = (row: Group[]) =>
215 row.reduce((n, g) => n + g.cells, 0) + (row.length - 1) * rule + dots(groups.length - row.length)
216 // From the whole band down to its first group, the last one shown in each shorter form first.
217 const rows = groups.flatMap((_, i) => {
218 const k = groups.length - i
219 const row = groups.slice(0, k - 1)
220 const forms: Group[][] = []
221 for (let g: Group | undefined = groups[k - 1]; g; g = g.compact) forms.push([...row, g])
222 return forms
223 })
224 const shown = rows.find(row => need(row) <= columns - INSET - MARGIN) ?? rows.at(-1)!
225 const hidden = groups.slice(shown.length)
226
227 return (
228 <Box flexDirection="row" alignItems="center" paddingX={1} gap={1}>
229 {shown.flatMap((g, i) => [
230 ...(i > 0 ? [<Text key={`rule-${g.key}`} color={RULE}>│</Text>] : []),
231 g.draw(),
232 ])}
233 {hidden.length > 0 && (
234 <Box key="hidden" flexDirection="row" gap={1}>
235 {hidden.map(g => (
236 <Text key={g.key} color={g.color}>•</Text>
237 ))}
238 </Box>
239 )}
240 </Box>
241 )
242 }
243
244 const { Box, Text } = $.ui.resolve(e)
245 const q5 = fiveHour && level(fiveHour.percentUsed)
246 const q7 = sevenDay && level(sevenDay.percentUsed)
247
248 return (
249 <Box flexDirection="row" paddingX={1}>
250 <Text color={lv.color}>{gauge(last.percent)}</Text>
251 <Text color={lv.color} bold>{` ${last.percent}%`}</Text>
252 <Text dimColor>{' of context'}</Text>
253 {columns >= 80 && fiveHour && <Text color={q5!.color} bold>{` ${used(fiveHour.percentUsed)}%`}</Text>}
254 {columns >= 80 && fiveHour && <Text dimColor>{' of session'}</Text>}
255 {columns >= 80 && fiveHour?.resetsAt && <Text dimColor>{` ↻ ${countdown(fiveHour.resetsAt, at)}`}</Text>}
256 {columns >= 80 && sevenDay && <Text color={q7!.color} bold>{` ${used(sevenDay.percentUsed)}%`}</Text>}
257 {columns >= 80 && sevenDay && <Text dimColor>{' of weekly'}</Text>}
258 {columns >= 80 && sevenDay?.resetsAt && <Text dimColor>{` ↻ ${countdown(sevenDay.resetsAt, at)}`}</Text>}
259 {columns >= 110 && spent && <Text bold>{` ${money(spent.usd)}`}</Text>}
260 {columns >= 110 && spent && <Text dimColor>{` today · ${short(spent.tokens)} tokens`}</Text>}
261 {SHOW_TURNS && columns >= 120 && <Text dimColor>{' last turns '}</Text>}
262 {SHOW_TURNS && columns >= 120 && <Text color={lv.color}>{sparkline(history)}</Text>}
263 {SHOW_TURNS && columns >= 120 && history.length > 1 && <Text dimColor>{trend(history)}</Text>}
264 </Box>
265 )
266 })
267}
268
269// The desktop's band is 13px system text, and a column there is 8px. Each character's width in
270// columns, measured in Chromium and rounded up, then checked against the desktop itself, where
271// `↻` is narrower; anything not listed counts as wide.
272const CHARS: Record<string, number> = {
273 '0': 1.02, '1': 0.75, '2': 0.98, '3': 1.01, '4': 1.04, '5': 1, '6': 1.03, '7': 0.92, '8': 1.03, '9': 1.03,
274 a: 0.89, b: 0.99, c: 0.9, d: 0.99, e: 0.92, f: 0.58, g: 0.99, h: 0.95, i: 0.4, j: 0.4, k: 0.88, l: 0.41, m: 1.41,
275 n: 0.94, o: 0.96, p: 0.99, q: 0.99, r: 0.61, s: 0.85, t: 0.59, u: 0.94, v: 0.88, w: 1.25, x: 0.85, y: 0.88, z: 0.87,
276 ' ': 0.45, '.': 0.48, $: 1.02, '%': 1.5, M: 1.42, '↻': 1.17, '·': 0.48, '•': 0.76, '│': 0.42,
277}
278// The rings are 18px. The desktop's bold figures are no wider than the table's.
279const RING = 18 / 8
280// The band draws about three columns short of the width the desktop reports for it, with its
281// padding (measured: 95 reported, 91.8 drawn). Half a column more to spare.
282const INSET = 3
283const MARGIN = 0.5
284
285function width(text: string) {
286 return [...text].reduce((n, c) => n + (CHARS[c] ?? 1.6), 0)
287}
288
289// A figure sits closer to its label than the group's other parts sit to each other.
290const FIGURE_GAP = 0.5
291
292// A group's width in columns: its icon's, its figure's, its texts' and the space between each.
293function fit(icon: number, figure: string, ...texts: string[]) {
294 const parts = texts.filter(Boolean)
295 const gaps = parts.length ? FIGURE_GAP + parts.length - 1 : 0
296 return icon + width(figure) + parts.reduce((n, t) => n + width(t), 0) + gaps + (icon ? 1 : 0)
297}
298
299async function takeReading($: EngineInterface) {
300 const { context, rateLimits } = await $.session.usage()
301 await saveLimits($, rateLimits, true)
302 if (!context?.window) return
303 // Before a response has measured the window (a new session, or one just compacted) there
304 // is no fill: estimate it rather than draw an empty window.
305 const tokens = context.tokens ?? (await estimate($))
306 if (tokens !== undefined) await addReading($, tokens, context.window)
307}
308
309// The window's fill as /context estimates it locally, without a request.
310async function estimate($: EngineInterface) {
311 try {
312 const { context } = await $.session.usage({ breakdown: 'summary' })
313 return context.breakdown?.totalTokens
314 } catch {
315 return undefined
316 }
317}
318
319// A reading of the size given, against the window the last reading had.
320async function setContext($: EngineInterface, tokens: number | undefined) {
321 if (tokens === undefined) return
322 const last = (await read($, readings)).at(-1)
323 const window = last?.window ?? (await $.session.usage()).context?.window
324 if (window) await addReading($, tokens, window)
325}
326
327async function addReading($: EngineInterface, tokens: number, window: number) {
328 const percent = Math.round((tokens / window) * 100)
329 await update($, readings, history => {
330 const last = history.at(-1)
331 if (last && last.tokens === tokens && last.window === window) return history
332 return [...history, { tokens, window, percent }].slice(-HISTORY)
333 })
334}
335
336async function refresh($: EngineInterface) {
337 const { rateLimits } = await $.session.usage()
338 await saveLimits($, rateLimits, true)
339 await askApp($)
340 await scanToday($) // other sessions add to today too
341 const at = await $.clock.now()
342 await update($, now, () => at)
343}
344
345// Whether the engine has reported the quota in this session. The desktop app's sessions
346// may not pass it on; the app's own usage card is asked then.
347let engineReports = false
348let askedAt = -Infinity
349const APP_USAGE = 'mcp__ccd_session_mgmt__get_usage'
350const ASK_EVERY_MS = 120_000
351
352async function askApp($: EngineInterface) {
353 if (engineReports) return
354 const at = await $.clock.now()
355 if (at - askedAt < ASK_EVERY_MS) return
356 askedAt = at
357 try {
358 if (!(await $.tool.list()).some(t => t.name === APP_USAGE)) return
359 const answer = await $.tool.call({ tool: APP_USAGE } as Parameters<EngineInterface['tool']['call']>[0])
360 if (answer.isError || answer.deny !== undefined) return
361 const text = answer.text ?? textOf(answer.result)
362 const plan = JSON.parse(text.slice(text.indexOf('{'))).plan as
363 | { status: string; windows?: { label: string; percentUsed: number; resetsAt?: string }[] }
364 | undefined
365 if (plan?.status !== 'ok' || !plan.windows) return
366 const found: TokenBarLimit[] = []
367 for (const w of plan.windows) {
368 const kind = /^5-hour/i.test(w.label) ? 'five_hour' : /^weekly · all/i.test(w.label) ? 'seven_day' : undefined
369 if (kind && typeof w.percentUsed === 'number') found.push({ kind, percentUsed: w.percentUsed, resetsAt: w.resetsAt })
370 }
371 if (!engineReports) await saveLimits($, found)
372 } catch {
373 // No app to ask, or it could not answer: keep what is shown.
374 }
375}
376
377// An MCP tool's output as text: a string, or its content blocks' text joined.
378function textOf(result: unknown): string {
379 if (typeof result === 'string') return result
380 if (Array.isArray(result)) return result.map(b => (b && typeof b.text === 'string' ? b.text : '')).join('')
381 return ''
382}
383
384async function saveLimits($: EngineInterface, rateLimits: readonly TokenBarLimit[], fromEngine = false) {
385 if (fromEngine && rateLimits.length > 0) engineReports = true
386 const at = await $.clock.now()
387 // A response may report only some windows: keep each window's last reading until it resets.
388 await update($, limits, kept => {
389 const byKind = new Map(kept.map(l => [l.kind, l]))
390 for (const { kind, percentUsed, resetsAt } of rateLimits) byKind.set(kind, { kind, percentUsed, resetsAt })
391 return [...byKind.values()].filter(l => !l.resetsAt || Date.parse(l.resetsAt) > at)
392 })
393 // Kept across sessions too, for the next one to start from.
394 if (rateLimits.length > 0) await $.store.set('limits', await read($, limits)).catch(() => {})
395 await update($, now, () => at)
396}
397
398// The local calendar day, as YYYY-MM-DD.
399function dayOf(ms: number) {
400 const d = new Date(ms)
401 return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
402}
403
404// One scan at a time per session; a request while one runs is folded into it.
405let scanning: Promise<void> | null = null
406
407function scanToday($: EngineInterface) {
408 scanning ??= runScan($).finally(() => (scanning = null))
409 return scanning
410}
411
412// Counts today from the transcripts. Off macOS, or when the scan fails, the band
413// simply goes without today's group.
414async function runScan($: EngineInterface) {
415 try {
416 const day = dayOf(await $.clock.now())
417 const roots = scanRoots(await $.env.get('CLAUDE_CONFIG_DIR'))
418 const { exitCode, stdout } = await $.process.run(
419 ['osascript', '-l', 'JavaScript', '-e', SCAN_SCRIPT, roots, SCAN_STATE, day],
420 { timeoutMs: 60_000 },
421 )
422 if (exitCode !== 0) return
423 const r = JSON.parse(stdout) as { day: string; tokens: number; usd: number }
424 if (r.day !== day || typeof r.tokens !== 'number' || typeof r.usd !== 'number') return
425 await update($, today, () => ({ day: r.day, tokens: r.tokens, usd: r.usd }))
426 } catch {
427 // Leave the last count in place.
428 }
429}
430
431function money(usd: number) {
432 return usd >= 100 ? `$${Math.round(usd)}` : `$${usd.toFixed(2)}`
433}
434
435function level(percentUsed: number) {
436 return LEVELS.find(l => percentUsed < l.upTo) ?? LEVELS[LEVELS.length - 1]!
437}
438
439function used(percentUsed: number) {
440 return Math.round(percentUsed)
441}
442
443// The terminal's gauge: five cells, filled as far as the context is used.
444function gauge(used: number) {
445 const full = Math.round((Math.min(Math.max(used, 0), 100) / 100) * 5)
446 return `[${'█'.repeat(full)}${'░'.repeat(5 - full)}]`
447}
448
449function countdown(resetsAt: string, at: number) {
450 const minutes = Math.max(0, Math.floor((Date.parse(resetsAt) - at) / 60_000))
451 // The weekly window runs for days: days and hours read better than a count of hours.
452 if (minutes >= 24 * 60) return `${Math.floor(minutes / 1440)}d ${Math.floor((minutes % 1440) / 60)}h`
453 if (minutes >= 60) return `${Math.floor(minutes / 60)}h ${minutes % 60}m`
454 return `${minutes}m`
455}
456
457function sparkline(history: TokenBarReading[]) {
458 const top = Math.max(...history.map(r => r.tokens), 1)
459 return history.map(r => BARS[Math.floor((r.tokens / top) * (BARS.length - 1))]).join('')
460}
461
462function trend(history: TokenBarReading[]) {
463 const delta = (history.at(-1)?.tokens ?? 0) - (history.at(-2)?.tokens ?? 0)
464 if (delta === 0) return ' steady'
465 return delta > 0 ? ` ▲ +${short(delta)} last turn` : ` ▼ ${short(-delta)} last turn`
466}
467
468function short(n: number) {
469 if (n >= 1_000_000) return `${+(n / 1_000_000).toFixed(1)}M`
470 if (n >= 1_000) return `${+(n / 1_000).toFixed(1)}k`
471 return String(n)
472}
473hooks/draw.ts 73 lines1// The drawings the surfaces with Svg show, as plain SVG markup.
2// Each takes how much is used (0 to 100) and the level's color.
3
4// A mid-tone that reads on the desktop's light card and on its dark one.
5const TRACK = 'rgba(128,128,128,0.28)'
6const QUIET = 'rgba(128,128,128,0.55)'
7
8// A ring that fills clockwise from twelve as the share is used.
9export function ring(used: number, color: string) {
10 const r = 7
11 const c = 2 * Math.PI * r
12 return svg(
13 18,
14 18,
15 `<circle cx="9" cy="9" r="${r}" fill="none" stroke="${TRACK}" stroke-width="3"/>` +
16 (used > 0
17 ? `<circle cx="9" cy="9" r="${r}" fill="none" stroke="${color}" stroke-width="3" stroke-linecap="round" ` +
18 `stroke-dasharray="${Math.max(share(used) * c, 0.1).toFixed(2)} ${c.toFixed(2)}" transform="rotate(-90 9 9)"/>`
19 : ''),
20 )
21}
22
23// The week as a ring of seven arcs, one a day, filled clockwise as the quota is used.
24export function week(used: number, color: string) {
25 const filled = share(used) * 7
26 const span = 360 / 7
27 const gap = 9
28 let body = ''
29 for (let i = 0; i < 7; i++) {
30 const from = i * span + gap / 2
31 const to = (i + 1) * span - gap / 2
32 body += `<path d="${arc(from, to)}" fill="none" stroke="${TRACK}" stroke-width="3"/>`
33 const part = Math.min(Math.max(filled - i, 0), 1)
34 // A started day shows at least a quarter of its arc, so a sliver does not read as a glitch.
35 if (part > 0) body += `<path d="${arc(from, from + (to - from) * Math.max(part, 0.25))}" fill="none" stroke="${color}" stroke-width="3"/>`
36 }
37 return svg(18, 18, body)
38}
39
40// An arc of the ring, clockwise from twelve, between two angles in degrees.
41function arc(from: number, to: number) {
42 const at = (deg: number) => {
43 const rad = ((deg - 90) * Math.PI) / 180
44 return `${(9 + 7 * Math.cos(rad)).toFixed(2)} ${(9 + 7 * Math.sin(rad)).toFixed(2)}`
45 }
46 return `M ${at(from)} A 7 7 0 0 1 ${at(to)}`
47}
48
49// The context after each of the last turns, as bars; the newest in the level's color.
50export function bars(values: number[], color: string) {
51 const top = Math.max(...values, 1)
52 const body = values
53 .map((v, i) => {
54 const h = Math.max((v / top) * 14, 1.5)
55 const fill = i === values.length - 1 ? color : QUIET
56 return `<rect x="${i * 5}" y="${(14 - h).toFixed(1)}" width="3.5" height="${h.toFixed(1)}" rx="1" fill="${fill}"/>`
57 })
58 .join('')
59 return svg(barsWidth(values.length), 14, body)
60}
61
62export function barsWidth(count: number) {
63 return Math.max(count * 5 - 1.5, 1)
64}
65
66function share(percent: number) {
67 return Math.min(Math.max(percent, 0), 100) / 100
68}
69
70function svg(w: number, h: number, body: string) {
71 return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${w} ${h}" width="${w}" height="${h}">${body}</svg>`
72}
73hooks/scan.ts 189 lines1// Today's tokens and their API-price cost, counted from Claude Code's own transcripts, so
2// every session of the day counts, those from before the plugin loaded included.
3// Run by osascript as JavaScript for Automation (macOS); a run reads only what the
4// transcripts gained since the last, from the offsets it keeps in its state directory.
5// Counting rules follow ccusage and OpenUsage (see the script's header).
6export const SCAN_STATE = '~/.token-bar'
7
8// The transcript roots, as ccusage and OpenUsage find them: CLAUDE_CONFIG_DIR's entries
9// (comma-separated, each a config directory or its projects directory), or else both
10// default homes. The script adds the desktop app's Cowork sessions itself.
11export function scanRoots(configDir: string | undefined) {
12 const dirs = configDir?.split(',').map(d => d.trim()).filter(Boolean) ?? []
13 if (dirs.length === 0) return '~/.config/claude/projects,~/.claude/projects'
14 return dirs.map(d => (/\/projects\/?$/.test(d) ? d : `${d.replace(/\/+$/, '')}/projects`)).join(',')
15}
16
17export const SCAN_SCRIPT = String.raw`ObjC.import('Foundation')
18
19// Today's tokens and API-price cost, from Claude Code's transcripts.
20// argv: transcript roots ('projects' directories, comma-separated, '~' allowed),
21// state directory ('~' allowed), day (YYYY-MM-DD, local).
22// Also reads the Claude desktop app's Cowork sessions. Incremental: the state file keeps
23// each transcript's read offset and what each reply was counted at, so a run reads only
24// what was appended since, and a fuller copy of a reply corrects its count.
25// The rules follow ccusage and OpenUsage: one count per reply (message id + request id),
26// the fullest copy wins, a sidechain replay of a parent reply is the same reply.
27
28// $ per million tokens: input, output, cache read, fast-mode multiplier. Cache writes
29// cost 1.25x input (5 minutes) or 2x (1 hour).
30const PRICES = [
31 ['claude-fable-5-1', 10, 50, 0.25, 2], ['claude-mythos-5-1', 10, 50, 0.25, 2],
32 ['claude-fable-5', 10, 50, 1, 2], ['claude-mythos-5', 10, 50, 1, 2],
33 ['claude-opus-5-5', 4, 20, 0.2, 2], ['claude-opus-5', 5, 25, 0.5, 2],
34 ['claude-opus-4-8', 5, 25, 0.5, 2], ['claude-opus-4-7', 5, 25, 0.5, 6], ['claude-opus-4-6', 5, 25, 0.5, 6],
35 ['claude-sonnet-5-5', 2, 10, 0.2, 2], ['claude-sonnet-5', 2, 10, 0.2, 2], ['claude-sonnet-4-6', 3, 15, 0.3, 2],
36 ['claude-haiku-4-5', 1, 5, 0.1, 2],
37]
38
39function priceOf(model) {
40 // The longest matching id wins ('claude-opus-5-5' before 'claude-opus-5'); a dated or
41 // provider-spelled id ('claude-opus-4-8@20260101', 'anthropic.claude-...') still matches.
42 const id = model.replace(/^(anthropic\.|us\.anthropic\.|eu\.anthropic\.)/, '').replace(/[@.]/g, '-')
43 let best = null
44 for (const p of PRICES) if (id === p[0] || id.indexOf(p[0] + '-') === 0) if (!best || p[0].length > best[0].length) best = p
45 return best
46}
47
48function tokensOf(u) {
49 return (u.input_tokens || 0) + (u.output_tokens || 0) + (u.cache_read_input_tokens || 0) + (u.cache_creation_input_tokens || 0)
50}
51
52function costOf(model, u) {
53 const p = priceOf(model)
54 if (!p) return null
55 const [, input, output, read, fast] = p
56 const cc = u.cache_creation || {}
57 const w1h = cc.ephemeral_1h_input_tokens || 0
58 const w5m = cc.ephemeral_5m_input_tokens !== undefined ? cc.ephemeral_5m_input_tokens : Math.max((u.cache_creation_input_tokens || 0) - w1h, 0)
59 const usd = ((u.input_tokens || 0) * input + (u.output_tokens || 0) * output + (u.cache_read_input_tokens || 0) * read
60 + w5m * input * 1.25 + w1h * input * 2) / 1e6
61 return u.speed === 'fast' ? usd * fast : usd
62}
63
64function dayOf(ms) {
65 const d = new Date(ms)
66 return d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0')
67}
68
69function run(argv) {
70 const home = $.NSHomeDirectory().js
71 const fm = $.NSFileManager.defaultManager
72 const isDir = path => { const flag = Ref(); return fm.fileExistsAtPathIsDirectory(path, flag) && flag[0] }
73 const roots = []
74 for (const r of argv[0].split(',')) {
75 const path = r.trim().replace(/^~/, home).replace(/\/+$/, '')
76 if (path && isDir(path) && roots.indexOf(path) === -1) roots.push(path)
77 }
78 // Cowork sessions keep a Claude Code home of their own, deep under the desktop app's data.
79 const cowork = home + '/Library/Application Support/Claude/local-agent-mode-sessions'
80 if (isDir(cowork)) {
81 const walker = fm.enumeratorAtPath(cowork)
82 let rel
83 while (!(rel = walker.nextObject).isNil()) {
84 if (/(^|\/)\.claude\/projects$/.test(rel.js)) { roots.push(cowork + '/' + rel.js); walker.skipDescendants }
85 }
86 }
87 const stateDir = argv[1].replace(/^~/, home)
88 const day = argv[2]
89 fm.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(stateDir, true, $(), $())
90 const statePath = stateDir + '/scan-' + day + '.json'
91
92 // counted: key -> [tokens, usd, sidechain, message id]; byMessage: message id -> key
93 let state = { version: 2, day: day, files: {}, counted: {}, byMessage: {}, tokens: 0, usd: 0, unpriced: {} }
94 const old = $.NSString.stringWithContentsOfFileEncodingError(statePath, $.NSUTF8StringEncoding, $())
95 if (!old.isNil()) { try { const s = JSON.parse(old.js); if (s.day === day && s.version === 2) state = s } catch (e) {} }
96
97 // A reply's count, replacing what it was counted at before.
98 function count(key, entry) {
99 const was = state.counted[key]
100 if (was) { state.tokens -= was[0]; state.usd -= was[1] }
101 state.counted[key] = entry
102 state.tokens += entry[0]
103 state.usd += entry[1]
104 }
105
106 // Midnight today, local: a transcript last written before then holds nothing of today's.
107 const parts = day.split('-').map(Number)
108 const startOfDay = new Date(parts[0], parts[1] - 1, parts[2]).getTime()
109
110 for (const root of roots) {
111 const walker = fm.enumeratorAtPath(root)
112 let rel
113 while (!(rel = walker.nextObject).isNil()) {
114 if (!rel.js.endsWith('.jsonl')) continue
115 const path = root + '/' + rel.js
116 const attrs = fm.attributesOfItemAtPathError(path, $())
117 if (attrs.isNil()) continue
118 if (attrs.objectForKey($.NSFileModificationDate).timeIntervalSince1970 * 1000 < startOfDay) continue
119 const size = Number(attrs.objectForKey($.NSFileSize).longLongValue)
120 let offset = state.files[path] || 0
121 if (size < offset) offset = 0 // rewritten: read again; replies already counted stay counted once
122 if (size === offset) continue
123
124 const fh = $.NSFileHandle.fileHandleForReadingAtPath(path)
125 if (fh.isNil()) continue
126 fh.seekToFileOffset(offset)
127 const data = fh.readDataToEndOfFile
128 fh.closeFile
129 const text = $.NSString.alloc.initWithDataEncoding(data, $.NSUTF8StringEncoding).js || ''
130 // Whole lines only; a line still being written waits for the next run.
131 const end = text.lastIndexOf('\n') + 1
132 const rest = text.slice(end)
133 state.files[path] = offset + Number(data.length) - (rest ? $(rest).lengthOfBytesUsingEncoding($.NSUTF8StringEncoding) : 0)
134
135 for (const line of text.slice(0, end).split('\n')) {
136 if (line.indexOf('"usage"') === -1 || line.indexOf('"assistant"') === -1) continue
137 let d
138 try { d = JSON.parse(line) } catch (e) { continue }
139 const m = d.message
140 if (d.type !== 'assistant' || !m || !m.usage || !d.timestamp) continue
141 if (dayOf(Date.parse(d.timestamp)) !== day) continue
142 const u = m.usage
143 const model = m.model || ''
144 const tokens = tokensOf(u)
145 const usd = model === '<synthetic>' ? 0 : costOf(model, u)
146 if (usd === null) state.unpriced[model] = (state.unpriced[model] || 0) + 1
147 const sidechain = !!d.isSidechain
148 const entry = [tokens, usd || 0, sidechain, m.id || '']
149
150 // A reply is its message id and request id; without a request id, its session and time.
151 const key = !m.id ? 'line:' + (d.uuid || d.sessionId + ':' + d.timestamp)
152 : d.requestId ? m.id + ':' + d.requestId : m.id + ':' + d.sessionId + ':' + d.timestamp
153 // A sidechain replaying a parent reply under a new request id is that reply.
154 let same = state.counted[key] ? key : null
155 if (!same && m.id && state.byMessage[m.id]) {
156 const other = state.counted[state.byMessage[m.id]]
157 if (other && (other[2] || sidechain)) same = state.byMessage[m.id]
158 }
159 if (!same) {
160 count(key, entry)
161 if (m.id && !state.byMessage[m.id]) state.byMessage[m.id] = key
162 continue
163 }
164 // Keep the better copy: the parent over a replay, then the fullest.
165 const was = state.counted[same]
166 const better = was[2] && !sidechain ? true : !was[2] && sidechain ? false : tokens > was[0]
167 if (better) {
168 if (same !== key) { state.tokens -= was[0]; state.usd -= was[1]; delete state.counted[same] }
169 count(key, entry)
170 if (m.id) state.byMessage[m.id] = key
171 }
172 }
173 }
174 }
175
176 // An atomic write renames its own temporary file over the state, so sessions scanning at once
177 // leave the last one's and nothing behind.
178 $(JSON.stringify(state)).writeToFileAtomicallyEncodingError(statePath, true, $.NSUTF8StringEncoding, $())
179 const names = fm.contentsOfDirectoryAtPathError(stateDir, $())
180 if (!names.isNil()) for (let i = 0; i < names.count; i++) {
181 const n = names.objectAtIndex(i).js
182 // Earlier days' state, and the temporary files 2.3.0 and before could leave when two raced.
183 const stale = /^scan-\d{4}-\d{2}-\d{2}\.json$/.test(n) ? n !== 'scan-' + day + '.json' : /^scan-[\d-]+\.json\.\d+\.tmp$/.test(n)
184 if (stale) fm.removeItemAtPathError(stateDir + '/' + n, $())
185 }
186 return JSON.stringify({ day: day, tokens: state.tokens, usd: Math.round(state.usd * 1e4) / 1e4, unpriced: state.unpriced, roots: roots.length })
187}
188`
189types/index.d.ts 16 lines1export type TokenBarReading = { tokens: number; window: number; percent: number }
2export type TokenBarLimit = { kind: string; percentUsed: number; resetsAt?: string }
3// What today has used across every session, as the transcript scan last counted it.
4export type TokenBarToday = { day: string; tokens: number; usd: number }
5
6declare module 'claude-code' {
7 interface PluginState {
8 'token-bar': {
9 readings: TokenBarReading[]
10 limits: TokenBarLimit[]
11 now: number
12 today: TokenBarToday | null
13 }
14 }
15}
16