An always-visible usage meter above the prompt with live 5-hour, weekly and usage-credit bars that climb smoothly as usage rises, and a Request more usage…

A Claude Code mod (Desktop and terminal) by Shaheer Shoaib: your live 5-hour, weekly and usage-credit usage above the prompt, and a button that sends a usage-credit request to your organization's admins.

Paste this in a terminal:
curl -fsSL https://raw.githubusercontent.com/shaheershoaib/usage-meter/main/install.sh | bash
Then open a new Claude Code session. That's it. Run the same line again any time to update.
It downloads the mod to ~/.claude/mods/usage-meter and adds it to CLAUDE_CODE_PLUGIN_DIRS in ~/.claude/settings.json (your other settings are kept, and a backup is made first). Needs python3 or node, which most machines have.
To remove it:
curl -fsSL https://raw.githubusercontent.com/shaheershoaib/usage-meter/main/uninstall.sh | bash
In Claude Code:
/plugin marketplace add shaheershoaib/usage-meter
/plugin install usage-meter@usage-meter
Claude Code is still rolling out mods for marketplace plugins. If nothing appears after a new session, use the install line above instead.
/more-usage does the same from the prompt.install.sh works on macOS and Linux.Apache 2.0: free to use, modify, and share, including commercially. Keep the NOTICE file with any redistribution (§4(d)), and don't market a fork under the usage-meter name (§6). The patent grant in §3 means adopting this doesn't expose you to a patent claim over it.
Required Notice: Copyright Shaheer Shoaib (https://github.com/shaheershoaib)
hooks/register.tsx 499 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register } from 'claude-code'
3
4import type { Credits, Limit } from '../types'
5import { barColors, levelOf, runsOf } from './gauge'
6import { SWEEP_TICK_MS, isMoving, isSweeping, shownOf } from './motion'
7import { FILE_BODY, fileUrl, mineUrl, orgOf, pendingOf, profileUrl, reasonOf } from './admin'
8import { asBlock, asCached, backoff, decide, minutes } from './pace'
9import { money, parseUsage } from './usage'
10
11const SETTINGS_URL = 'https://claude.ai/settings/usage'
12const USAGE_API = 'https://api.anthropic.com/api/oauth/usage'
13// One read per 15 seconds at most, one at a time, one after every turn (credits move
14// with each turn once the weekly limit is spent) and one on a timer so an idle session stays current. A failed read is not retried at once: the next turn
15// of the timer or a usage change is the retry.
16// This session asks at most this often; every session together asks about twice a minute (pace.ts).
17const REFRESH_MS = 10_000
18// While you are working, usage is read this often, so the bars climb as you spend.
19const ACTIVE_POLL_MS = 60_000
20const ACTIVE_FOR_MS = 45_000
21// How often a session looks at the reading the other sessions share (no request is made).
22const ADOPT_MS = 15_000
23const POLL_MS = 5 * 60_000
24const FETCH_TIMEOUT_MS = 10_000
25const LABELS: Record<string, string> = { five_hour: '5-hour', seven_day: 'Weekly' }
26const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
27
28type Tile = { key: string; label: string; percent: number | null; detail: string }
29
30const windows = atom({ plugin: 'usage-meter', key: 'windows' } as const, [])
31const credits = atom({ plugin: 'usage-meter', key: 'credits' } as const, null)
32const failure = atom({ plugin: 'usage-meter', key: 'error' } as const, null)
33
34const resetOf = (limit: Limit) => {
35 if (!limit.resetsAt) {
36 return ''
37 }
38 const date = new Date(limit.resetsAt)
39 const hours = date.getHours()
40 const time = `${hours % 12 || 12}:${String(date.getMinutes()).padStart(2, '0')} ${hours < 12 ? 'AM' : 'PM'}`
41
42 return limit.kind === 'seven_day' ? `${DAYS[date.getDay()]} ${time}` : time
43}
44
45const adminNote = (tiles: Tile[]) => {
46 const figures = tiles
47 .filter(tile => tile.percent !== null)
48 .map(tile => `${tile.label} ${Math.round(tile.percent ?? 0)}%`)
49 .join(', ')
50
51 return (
52 `Hi, I am running low on Claude usage${figures ? ` (${figures})` : ''} and need more to keep working. ` +
53 'Could you add usage credits for my account? You can approve it in ' +
54 'Organization settings > Usage > Review requests. Thanks!'
55 )
56}
57
58
59const withTimeout = <T,>($: EngineInterface, work: Promise<T>, ms: number) =>
60 new Promise<T>((resolve, reject) => {
61 const timer = $.clock.after(ms, () => reject(new Error('timeout')))
62 work.then(
63 value => {
64 timer.cancel()
65 resolve(value)
66 },
67 cause => {
68 timer.cancel()
69 reject(cause)
70 },
71 )
72 })
73
74let lastRead = 0
75let isReading = false
76let activeAt = 0
77// The request to the admins: idle (ready to ask, again too), asking you to confirm, or sending.
78let asking: 'idle' | 'confirm' | 'sending' = 'idle'
79// The last request, a small status beside the button. Kept in the store, so one sent from any
80// session shows in every session.
81const LAST_KEY = 'last-request'
82type LastRequest = { at: number; outcome: 'sent' | 'pending' }
83let lastRequest: LastRequest | null = null
84let hasLoadedLast = false
85const asLast = (value: unknown): LastRequest | null => {
86 const v = value as Partial<LastRequest> | null
87 return v && typeof v.at === 'number' && (v.outcome === 'sent' || v.outcome === 'pending') ? { at: v.at, outcome: v.outcome } : null
88}
89const remember = async ($: EngineInterface, outcome: LastRequest['outcome']) => {
90 lastRequest = { at: Date.now(), outcome }
91 await $.store.set(LAST_KEY, lastRequest).catch(() => undefined)
92}
93// When the numbers on screen were read, and the newest shared reading this session has taken.
94let goodAt = 0
95let adoptedAt = 0
96let redrawQueued = false
97
98const clip = (text: string, size: number) => (text.length > size ? `${text.slice(0, size - 1)}…` : text)
99const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
100// "2:14 PM" today, "Oct 9" before that.
101const whenOf = (at: number, now: number) => {
102 const d = new Date(at)
103 if (d.toDateString() !== new Date(now).toDateString()) {
104 return `${MONTHS[d.getMonth()]} ${d.getDate()}`
105 }
106 const h = d.getHours()
107
108 return `${h % 12 || 12}:${String(d.getMinutes()).padStart(2, '0')} ${h < 12 ? 'AM' : 'PM'}`
109}
110
111// A write that changes nothing still redraws everything that reads it, so none is made.
112const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b)
113
114const CACHE_KEY = 'usage-cache'
115const BLOCK_KEY = 'usage-blocked'
116
117const setFailure = async ($: EngineInterface, label: string | null) => {
118 if ((await read($, failure)) !== label) {
119 await update($, failure, () => label)
120 }
121}
122
123const apply = async ($: EngineInterface, usage: { windows: Limit[]; credits: Credits | null }) => {
124 if (!same(await read($, windows), usage.windows)) {
125 await update($, windows, () => usage.windows)
126 }
127 if (!same(await read($, credits), usage.credits)) {
128 await update($, credits, () => usage.credits)
129 }
130}
131
132// Reads the account's usage card, credits included. Every open session shares one cached
133// reading and one "slow down" notice, so the request is made about twice a minute however
134// many sessions there are; a failure leaves the numbers already drawn in place.
135const refresh = async ($: EngineInterface) => {
136 const now = Date.now()
137 if (isReading || (lastRead && now - lastRead < REFRESH_MS)) {
138 return
139 }
140 lastRead = now
141 isReading = true
142 try {
143 const cached = asCached(await $.store.get(CACHE_KEY))
144 const block = asBlock(await $.store.get(BLOCK_KEY))
145 const action = decide(now, cached, block)
146 if (cached && action !== 'ask') {
147 await apply($, cached)
148 goodAt = Math.max(goodAt, cached.at)
149 adoptedAt = Math.max(adoptedAt, cached.at)
150 }
151 if (action === 'adopt') {
152 await setFailure($, null)
153
154 return
155 }
156 if (action === 'wait' && block) {
157 await setFailure($, 'rate limited')
158
159 return
160 }
161 const auth = await $.session.authorize()
162 if (!auth) {
163
164 return
165 }
166 const reply = await withTimeout(
167 $,
168 $.http.fetch(USAGE_API, { auth: auth.handle, headers: { 'anthropic-beta': 'oauth-2025-04-20' } }),
169 FETCH_TIMEOUT_MS,
170 )
171 if (reply.status === 429) {
172 const next = backoff(now, block, reply.headers['retry-after'])
173 await $.store.set(BLOCK_KEY, next)
174 if (cached) {
175 await apply($, cached)
176 }
177 await setFailure($, 'rate limited')
178
179 return
180 }
181 if (!reply.ok) {
182 await setFailure($, `HTTP ${reply.status}`)
183
184 return
185 }
186 let usage: ReturnType<typeof parseUsage>
187 try {
188 usage = parseUsage(reply.text)
189 } catch {
190 await setFailure($, 'unreadable reply')
191
192 return
193 }
194 await $.store.set(CACHE_KEY, { at: now, windows: usage.windows, credits: usage.credits })
195 await $.store.delete(BLOCK_KEY)
196 await apply($, usage)
197 goodAt = now
198 adoptedAt = now
199 await setFailure($, null)
200 } catch (cause) {
201 const label = cause instanceof Error && cause.message === 'timeout' ? 'timed out' : 'offline'
202 await setFailure($, label)
203 } finally {
204 isReading = false
205 }
206}
207
208// Takes up a newer reading another session made, without asking Anthropic: a store read only.
209const adopt = async ($: EngineInterface) => {
210 const cached = asCached(await $.store.get(CACHE_KEY))
211 if (cached && cached.at > adoptedAt) {
212 adoptedAt = cached.at
213 goodAt = Math.max(goodAt, cached.at)
214 await apply($, cached)
215 await setFailure($, null)
216 }
217 const last = asLast(await $.store.get(LAST_KEY))
218 if (last && (last.at !== lastRequest?.at || last.outcome !== lastRequest.outcome)) {
219 lastRequest = last
220 $.ui.invalidate('ui.render')
221 }
222}
223
224const say = ($: EngineInterface, text: string) => {
225 $.ui.toast(text, { timeoutMs: 6_000 })
226
227 return text
228}
229
230// Files one usage-credit request with your organization's admins. A request already pending is
231// not sent twice; a post that times out is reported as unknown, not as failed, and is not retried.
232const fileRequest = async ($: EngineInterface): Promise<string> => {
233 asking = 'sending'
234 $.ui.invalidate('ui.render')
235 try {
236 const auth = await $.session.authorize()
237 if (!auth) {
238 asking = 'idle'
239
240 return say($, 'This session has no Claude sign-in to send the request with. Use claude.ai/settings/usage instead.')
241 }
242 const headers = { 'anthropic-beta': 'oauth-2025-04-20', 'content-type': 'application/json' }
243 const profile = await withTimeout($, $.http.fetch(profileUrl(), { auth: auth.handle, headers }), FETCH_TIMEOUT_MS)
244 const org = profile.ok ? orgOf(profile.text) : null
245 if (!org) {
246 asking = 'idle'
247
248 return say($, `Couldn't find your organization to send the request to (HTTP ${profile.status}). Nothing was sent.`)
249 }
250 const withOrg = { ...headers, 'x-organization-uuid': org }
251 const mine = await withTimeout($, $.http.fetch(mineUrl(org), { auth: auth.handle, headers: withOrg }), FETCH_TIMEOUT_MS)
252 if (mine.ok && pendingOf(mine.text) > 0) {
253 asking = 'idle'
254 await remember($, 'pending')
255
256 return say($, "You've already sent a usage credit request to your admin. It's still pending.")
257 }
258 let sent
259 try {
260 sent = await withTimeout(
261 $,
262 $.http.fetch(fileUrl(org), { method: 'POST', auth: auth.handle, headers: withOrg, body: FILE_BODY }),
263 FETCH_TIMEOUT_MS,
264 )
265 } catch {
266 asking = 'idle'
267
268 return say($, "The request didn't answer in time, so it may or may not have reached your admin. Press again: if it did, you'll be told it's pending.")
269 }
270 if (!sent.ok) {
271 asking = 'idle'
272
273 return say($, reasonOf(sent.text) ?? `Your admin request was not accepted (HTTP ${sent.status}). Nothing was sent; try again later.`)
274 }
275 asking = 'idle'
276 await remember($, 'sent')
277
278 return say($, 'Request sent to your admin for usage credits.')
279 } catch (cause) {
280 asking = 'idle'
281
282 return say($, "Couldn't reach Anthropic to send the request. Nothing was sent; try again in a moment.")
283 } finally {
284 $.ui.invalidate('ui.render')
285 }
286}
287
288export const register: Register = on => {
289 on('session.start', async ($, e, next) => {
290 await $.command.register({
291 name: 'more-usage',
292 description: 'Send a usage-credit request to your organization admins',
293 })
294 void refresh($)
295 $.clock.every(POLL_MS, () => void refresh($))
296 $.clock.every(ADOPT_MS, () => void adopt($))
297 $.clock.every(ACTIVE_POLL_MS, () => {
298 if (Date.now() - activeAt < ACTIVE_FOR_MS) {
299 void refresh($)
300 }
301 })
302
303 return next(e)
304 })
305
306 on('command.run', { command: 'more-usage' }, async $ => ({ text: await fileRequest($) }))
307
308 // Any tool call means work is going on, and usage is moving with it.
309 on('tool.call', ($, e, next) => {
310 activeAt = Date.now()
311
312 return next(e)
313 })
314
315 // An agent's turn ending is another moment usage has moved.
316 on('turn.complete', ($, e, next) => {
317 void refresh($)
318
319 return next(e)
320 })
321
322 on('session.measure', async ($, e, next) => {
323 if (e.changed.includes('rateLimits')) {
324 const known = e.rateLimits.filter(limit => limit.kind in LABELS)
325 if (known.length > 0 && !same(await read($, windows), known)) {
326 await update($, windows, () => known)
327 }
328 }
329 // A turn that cost something is when credits and limits have moved.
330 if (e.changed.includes('rateLimits') || e.changed.includes('cost')) {
331 void refresh($)
332 }
333
334 return next(e)
335 })
336
337 // Always drawn, so the request is one click away at any usage level.
338 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
339 // Another mod may draw in the band too: keep what is beneath this card.
340 const below = await next(e)
341 if (e.props.hasSurvey) {
342 return below
343 }
344
345 if (!hasLoadedLast) {
346 hasLoadedLast = true
347 lastRequest = asLast(await $.store.get(LAST_KEY)) ?? lastRequest
348 }
349 const ui = $.ui.resolve(e)
350 const { Box, Button, Link, Text } = ui
351 const tiles: Tile[] = []
352 for (const limit of await read($, windows)) {
353 const reset = resetOf(limit)
354 tiles.push({
355 key: limit.kind,
356 label: LABELS[limit.kind] ?? limit.kind,
357 percent: Math.round(limit.percentUsed),
358 detail: reset ? `resets ${reset}` : '',
359 })
360 }
361 const balance = await read($, credits)
362 const problem = await read($, failure)
363 // Numbers more than two minutes old say how old they are; a wait for Anthropic is not an error.
364 const age = goodAt ? Date.now() - goodAt : 0
365 const stale = age > 120_000 ? ` · as of ${minutes(age)} min ago` : problem && problem !== 'rate limited' ? ' · not up to date' : ''
366 if (balance?.isEnabled) {
367 tiles.push({
368 key: 'credits',
369 label: 'Credits',
370 // One decimal: near the cap, whole percents hide that it is still moving.
371 percent: Math.round(balance.percentUsed * 10) / 10,
372 detail: `${money(balance.spent, balance.currency)} of ${money(balance.limit, balance.currency)}${stale}`,
373 })
374 } else if (balance) {
375 tiles.push({ key: 'credits', label: 'Credits', percent: null, detail: 'not enabled' })
376 } else if (problem) {
377 tiles.push({
378 key: 'credits',
379 label: 'Credits',
380 percent: null,
381 detail: problem === 'rate limited' ? 'loading…' : `unavailable (${problem})`,
382 })
383 }
384 const now = Date.now()
385 const top = Math.max(0, ...tiles.map(tile => tile.percent ?? 0))
386 const { accent, isLow } = levelOf(top)
387
388 const card = (
389 <Box
390 key="card"
391 borderStyle="round"
392 borderColor={accent}
393 borderDimColor={!isLow}
394 hover={{ borderColor: accent, borderDimColor: false }}
395 paddingX={1}
396 gap={3}
397 alignItems="center"
398 flexWrap="wrap"
399 >
400 <Box gap={3} flexGrow={1} flexWrap="wrap">
401 {tiles.length === 0 && <Text dimColor>Need more? Request it in one click.</Text>}
402 {tiles.map(tile => {
403 const shown = shownOf(tile.key, tile.percent, now)
404 const level = levelOf(shown ?? 0)
405 const figure = shown === null ? '' : tile.key === 'credits' ? (Math.round(shown * 10) / 10).toFixed(1) : String(Math.round(shown))
406
407 return (
408 <Box key={`tile-${tile.key}`} flexDirection="column">
409 <Box gap={1}>
410 <Text bold>{tile.label}</Text>
411 {shown !== null && (
412 <Text bold color={level.accent}>
413 {figure}%
414 </Text>
415 )}
416 </Box>
417 <Box>
418 {runsOf(barColors(shown, level, isMoving(tile.key, now))).map(run => (
419 <Box width={run.n} height={1} backgroundColor={run.color} />
420 ))}
421 </Box>
422 <Text dimColor>{tile.detail}</Text>
423 </Box>
424 )
425 })}
426 </Box>
427 <Box flexDirection="column" gap={1} alignItems="flex-start">
428 {asking === 'idle' && (
429 <Box gap={1} alignItems="center">
430 <Button
431 key="request"
432 variant={isLow ? 'primary' : 'secondary'}
433 label="Request more usage"
434 onPress={() => {
435 asking = 'confirm'
436 $.ui.invalidate('ui.render')
437 }}
438 />
439 {lastRequest && (
440 <Box gap={1}>
441 <Text color={lastRequest.outcome === 'sent' ? '#34d399' : '#fbbf24'}>{lastRequest.outcome === 'sent' ? '✓' : '●'}</Text>
442 <Text dimColor>
443 {lastRequest.outcome === 'sent'
444 ? `Request sent ${whenOf(lastRequest.at, now)}`
445 : `Pending with your admin (checked ${whenOf(lastRequest.at, now)})`}
446 </Text>
447 </Box>
448 )}
449 </Box>
450 )}
451 {asking === 'confirm' && (
452 <Box gap={1} alignItems="center">
453 <Text>Send to your admins?</Text>
454 <Button key="send" variant="primary" label="Send request" onPress={() => void fileRequest($)} />
455 <Button
456 key="cancel"
457 label="Cancel"
458 onPress={() => {
459 asking = 'idle'
460 $.ui.invalidate('ui.render')
461 }}
462 />
463 </Box>
464 )}
465 {asking === 'sending' && <Text dimColor>Sending your request…</Text>}
466 <Box gap={1}>
467 <Button
468 key="copy"
469 label="Copy admin note"
470 onPress={async press => {
471 const { isCopied } = await $.ui.copy({ text: adminNote(tiles), surface: press.surface })
472 $.ui.toast(isCopied ? 'Note for your admin copied.' : 'Could not copy the note.')
473 }}
474 />
475 <Link href={SETTINGS_URL} label="usage settings" />
476 </Box>
477 </Box>
478 </Box>
479 )
480
481 if (isSweeping(now) && !redrawQueued) {
482 redrawQueued = true
483 $.clock.after(SWEEP_TICK_MS, () => {
484 redrawQueued = false
485 $.ui.invalidate('ui.render')
486 })
487 }
488
489 return below.type === 'engine' ? (
490 card
491 ) : (
492 <Box flexDirection="column">
493 {card}
494 {below}
495 </Box>
496 )
497 })
498}
499hooks/gauge.ts 66 lines1// A gauge made of colored cells. Boxes and text update in place when the UI
2// redraws, where a picture would be reloaded: nothing here can flicker.
3export type Level = { accent: string; from: string; to: string; isLow: boolean }
4
5export const BAR_CELLS = 22
6const TRACK = '#2b303b'
7
8export const levelOf = (percent: number): Level => {
9 if (percent >= 100) {
10 return { accent: '#f87171', from: '#fca5a5', to: '#dc2626', isLow: true }
11 }
12 if (percent >= 80) {
13 return { accent: '#fb923c', from: '#fdba74', to: '#ef4444', isLow: true }
14 }
15 if (percent >= 60) {
16 return { accent: '#fbbf24', from: '#fde68a', to: '#f59e0b', isLow: false }
17 }
18
19 return { accent: '#34d399', from: '#6ee7b7', to: '#22d3ee', isLow: false }
20}
21
22const part = (c: string, at: number) => parseInt(c.slice(at, at + 2), 16)
23const two = (n: number) => Math.round(Math.min(255, Math.max(0, n))).toString(16).padStart(2, '0')
24const mix = (a: string, b: string, k: number) =>
25 `#${two(part(a, 1) * (1 - k) + part(b, 1) * k)}${two(part(a, 3) * (1 - k) + part(b, 3) * k)}${two(part(a, 5) * (1 - k) + part(b, 5) * k)}`
26
27// The color of each cell. Filled cells shade along the bar; the cell where the fill ends is
28// blended by how much of it is covered, so the edge slides smoothly instead of jumping a cell
29// at a time; and while the bar is moving (`glow`) its leading edge shines.
30export const barColors = (percent: number | null, level: Level, glow: boolean): string[] => {
31 const filled = (Math.min(100, Math.max(0, percent ?? 0)) / 100) * BAR_CELLS
32
33 return Array.from({ length: BAR_CELLS }, (_, i) => {
34 const shaded = mix(level.from, level.to, i / (BAR_CELLS - 1))
35 let color = TRACK
36 if (percent !== null) {
37 if (i + 1 <= filled) {
38 color = shaded
39 } else if (i < filled) {
40 color = mix(TRACK, shaded, filled - i)
41 }
42 if (glow && i < filled) {
43 const behind = filled - i
44 color = behind <= 1.5 ? mix(color, '#ffffff', 0.5) : behind <= 3 ? mix(color, '#ffffff', 0.18) : color
45 }
46 }
47
48 return color
49 })
50}
51
52// Neighbours of one color share a box, so a bar is a handful of boxes, not twenty-two.
53export const runsOf = (colors: string[]): { color: string; n: number }[] => {
54 const runs: { color: string; n: number }[] = []
55 for (const color of colors) {
56 const last = runs[runs.length - 1]
57 if (last && last.color === color) {
58 last.n += 1
59 } else {
60 runs.push({ color, n: 1 })
61 }
62 }
63
64 return runs
65}
66hooks/motion.ts 39 lines1// A gauge moves from where it was to where it is now, in a short ease-out. The motion is
2// drawn by redrawing the cells in quick bursts, and only while something is moving: when
3// every gauge is still, nothing redraws at all.
4export const SWEEP_MS = 700
5export const SWEEP_TICK_MS = 55
6
7type Move = { from: number; to: number; at: number }
8const anim = new Map<string, Move>()
9const ease = (k: number) => 1 - (1 - k) ** 3
10const valueOf = (a: Move, now: number) => a.from + (a.to - a.from) * ease(Math.min(1, (now - a.at) / SWEEP_MS))
11
12// What a gauge shows at `now`: its first appearance fills from empty, a new value eases from
13// wherever the gauge is at that moment, and an unchanged value stands still.
14export const shownOf = (key: string, percent: number | null, now: number): number | null => {
15 if (percent === null) {
16 anim.delete(key)
17
18 return null
19 }
20 let a = anim.get(key)
21 if (!a) {
22 a = { from: 0, to: percent, at: now }
23 anim.set(key, a)
24 } else if (a.to !== percent) {
25 a = { from: valueOf(a, now), to: percent, at: now }
26 anim.set(key, a)
27 }
28
29 return valueOf(a, now)
30}
31
32export const isSweeping = (now: number) => [...anim.values()].some(a => now - a.at < SWEEP_MS)
33
34export const isMoving = (key: string, now: number) => {
35 const a = anim.get(key)
36
37 return a !== undefined && now - a.at < SWEEP_MS
38}
39hooks/admin.ts 62 lines1// Filing a usage-credit request with the organization's admins, the way Claude Code does
2// once its own confirm is answered: find the organization, check nothing is already
3// pending, then post one `limit_increase` request.
4export const API = 'https://api.anthropic.com'
5
6const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
7
8const parse = (text: string): unknown => {
9 try {
10 return JSON.parse(text) as unknown
11 } catch {
12 return null
13 }
14}
15
16// The signed-in account's organization, from /api/oauth/profile.
17export const orgOf = (text: string): string | null => {
18 const data = parse(text) as { organization?: { uuid?: unknown }; organization_uuid?: unknown } | null
19 const uuid = data?.organization?.uuid ?? data?.organization_uuid
20
21 return typeof uuid === 'string' && UUID.test(uuid) ? uuid : null
22}
23
24// How many of my requests are still pending, from admin_requests/me (a list, or a list under a key).
25export const pendingOf = (text: string): number => {
26 const data = parse(text)
27 if (Array.isArray(data)) {
28 return data.length
29 }
30 const holder = data as Record<string, unknown> | null
31 for (const key of ['data', 'requests', 'admin_requests', 'items', 'results']) {
32 const list = holder?.[key]
33 if (Array.isArray(list)) {
34 return list.length
35 }
36 }
37
38 return 0
39}
40
41// The server's own words for a refusal, when it gives any.
42export const reasonOf = (text: string): string | null => {
43 const data = parse(text) as Record<string, unknown> | null
44 const error = (data?.error ?? null) as Record<string, unknown> | null
45 for (const holder of [error, data]) {
46 for (const key of ['message', 'detail']) {
47 const value = holder?.[key]
48 if (typeof value === 'string' && value.length > 0) {
49 return value
50 }
51 }
52 }
53
54 return null
55}
56
57export const profileUrl = () => `${API}/api/oauth/profile`
58export const mineUrl = (org: string) =>
59 `${API}/api/oauth/organizations/${org}/admin_requests/me?request_type=limit_increase&statuses=pending`
60export const fileUrl = (org: string) => `${API}/api/oauth/organizations/${org}/admin_requests`
61export const FILE_BODY = JSON.stringify({ request_type: 'limit_increase', details: null })
62hooks/pace.ts 46 lines1import type { Credits, Limit } from '../types'
2
3// How often the usage card may ask Anthropic, shared by every open session so five
4// sessions make one request, not five, and how long to wait once it says "slow down".
5export const FRESH_MS = 60_000
6// The server answers 429 with retry-after 0 ("try again now"), so the first wait is short.
7const BACKOFF_FIRST_MS = 30_000
8const BACKOFF_MAX_MS = 5 * 60_000
9
10export type Cached = { at: number; windows: Limit[]; credits: Credits | null }
11export type Block = { until: number; ms: number }
12
13export const asCached = (value: unknown): Cached | undefined => {
14 const v = value as Partial<Cached> | null | undefined
15
16 return v && typeof v.at === 'number' && Array.isArray(v.windows) ? (v as Cached) : undefined
17}
18
19export const asBlock = (value: unknown): Block | undefined => {
20 const v = value as Partial<Block> | null | undefined
21
22 return v && typeof v.until === 'number' && typeof v.ms === 'number' ? (v as Block) : undefined
23}
24
25// What to do now: use what another session already read, wait out a rate limit, or ask.
26export const decide = (now: number, cached: Cached | undefined, block: Block | undefined): 'adopt' | 'wait' | 'ask' => {
27 if (block && now < block.until) {
28 return 'wait'
29 }
30
31 return cached && now - cached.at < FRESH_MS ? 'adopt' : 'ask'
32}
33
34// A 429's new wait: what the server asked for (`retry-after`, in seconds), else 30 s and then twice the last, up to 5 min.
35export const backoff = (now: number, before: Block | undefined, retryAfter: string | undefined): Block => {
36 const asked = Number(retryAfter)
37 const ms =
38 Number.isFinite(asked) && asked > 0
39 ? Math.min(BACKOFF_MAX_MS, asked * 1000)
40 : Math.min(BACKOFF_MAX_MS, before ? before.ms * 2 : BACKOFF_FIRST_MS)
41
42 return { until: now + ms, ms }
43}
44
45export const minutes = (ms: number) => Math.max(1, Math.round(ms / 60_000))
46hooks/usage.ts 48 lines1import type { Credits, Limit } from '../types'
2
3// The reply of GET /api/oauth/usage, the endpoint behind the app's usage card.
4type Window = { utilization?: number | null; resets_at?: string | null } | null | undefined
5type Reply = {
6 five_hour?: Window
7 seven_day?: Window
8 extra_usage?: {
9 is_enabled: boolean
10 monthly_limit?: number | null
11 used_credits?: number | null
12 utilization?: number | null
13 currency?: string | null
14 } | null
15}
16
17const tenth = (value: number) => Math.round(value * 10) / 10
18
19const windowOf = (kind: string, raw: Window): Limit[] =>
20 raw && typeof raw.utilization === 'number'
21 ? [{ kind, percentUsed: tenth(raw.utilization), ...(raw.resets_at ? { resetsAt: raw.resets_at } : {}) }]
22 : []
23
24export const parseUsage = (text: string): { windows: Limit[]; credits: Credits | null } => {
25 const reply = JSON.parse(text) as Reply
26 const extra = reply.extra_usage
27 let credits: Credits | null = null
28 if (extra) {
29 // Amounts are minor units of the currency, cents for USD.
30 const limit = (extra.monthly_limit ?? 0) / 100
31 const spent = (extra.used_credits ?? 0) / 100
32 credits = {
33 isEnabled: extra.is_enabled,
34 spent,
35 limit,
36 percentUsed: tenth(limit > 0 ? (spent / limit) * 100 : (extra.utilization ?? 0)),
37 currency: extra.currency ?? 'USD',
38 }
39 }
40
41 return { windows: [...windowOf('five_hour', reply.five_hour), ...windowOf('seven_day', reply.seven_day)], credits }
42}
43
44const SYMBOLS: Record<string, string> = { USD: '$', EUR: '€', GBP: '£' }
45
46export const money = (amount: number, currency: string) =>
47 `${SYMBOLS[currency] ?? `${currency} `}${String(Math.round(amount)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')}`
48types/index.d.ts 10 lines1export type Limit = { kind: string; percentUsed: number; resetsAt?: string }
2// Dollars, as the usage card shows them (the API sends minor units).
3export type Credits = { isEnabled: boolean; spent: number; limit: number; percentUsed: number; currency: string }
4
5declare module 'claude-code' {
6 interface PluginState {
7 'usage-meter': { windows: Limit[]; credits: Credits | null; error: string | null }
8 }
9}
10