Usage & context progress bars above the prompt: 5-hour limit, 7-day limit, context window

Usage and context at a glance: three compact, colored progress bars in one line above the prompt.
5h ▰▰▱▱▱▱▱▱ 23% ↻ 14:30 7d ▰▰▰▱▱▱▱▱ 41% ↻ Mo ctx ▰▱▱▱▱▱▱▱ 6%
| Bar | Shows |
|---|---|
5h | Current 5-hour rate-limit window, with the time it resets |
7d | 7-day rate-limit window, with the weekday it resets |
ctx | How full the context window is |
Colors follow your theme: green below 50 %, yellow from 50 %, red from 80 %.
5h and 7d need a Claude subscription login; with an API key only ctx is shown.ctx shows a placeholder (▱▱▱▱▱▱▱▱ –), since nothing has been measured yet.ctx shows the estimate /context would give. After /clear ctx shows the placeholder again./plugin install clausage --marketplace Flo0806/fh-claude-modshooks/register.tsx 157 lines1import { atom, read, update } from 'claude-code'
2import type {
3 EngineInterface,
4 Register,
5 SessionContextUsage,
6 SessionRateLimit,
7 SessionUsage,
8} from 'claude-code'
9
10import type { Bar, Usage } from '../types'
11
12const usage = atom({ plugin: 'clausage', key: 'usage' } as const, null)
13const compacting = atom({ plugin: 'clausage', key: 'compacting' } as const, false)
14
15const toBar = (limit?: SessionRateLimit): Bar | undefined =>
16 limit && { percent: limit.percentUsed, resetsAt: limit.resetsAt }
17
18// Fall back to tokens/window when the engine reports no percentage.
19const contextBar = ({ percent, tokens, window }: SessionContextUsage): Bar | undefined => {
20 if (percent !== undefined) return { percent }
21 if (tokens !== undefined && window > 0) return { percent: (tokens / window) * 100 }
22 return undefined
23}
24
25// Without a measurement (right after a compaction) estimate the fill the way /context does, against
26// the same window the status line measures. Only a read that asked for the breakdown has one.
27const estimatedBar = ({ breakdown, window }: SessionContextUsage): Bar | undefined =>
28 breakdown && window > 0 ? { percent: (breakdown.totalTokens / window) * 100 } : undefined
29
30const toUsage = ({ rateLimits, context }: Pick<SessionUsage, 'rateLimits' | 'context'>): Usage => ({
31 fiveHour: toBar(rateLimits.find((r) => r.kind === 'five_hour')),
32 sevenDay: toBar(rateLimits.find((r) => r.kind === 'seven_day')),
33 context: contextBar(context) ?? estimatedBar(context),
34})
35
36// Reads the usage once the compacted conversation stands, then brings the band back. A measured
37// fill still there predates the compaction (some builds keep it until the next response), so the
38// estimate comes first.
39const settle = async ($: EngineInterface) => {
40 try {
41 const now = await $.session.usage({ breakdown: 'summary' })
42 const settled = {
43 ...toUsage(now),
44 context: estimatedBar(now.context) ?? contextBar(now.context),
45 }
46 await update($, usage, () => settled)
47 } finally {
48 await update($, compacting, () => false)
49 }
50}
51
52const WIDTH = 8
53
54const meter = (percent: number) => {
55 const filled = Math.round((Math.min(percent, 100) / 100) * WIDTH)
56 return '▰'.repeat(filled) + '▱'.repeat(WIDTH - filled)
57}
58
59const DAY_MS = 24 * 60 * 60 * 1000
60
61const pad = (n: number) => String(n).padStart(2, '0')
62
63// Within a day the clock time is precise enough, further out the weekday is.
64const resetLabel = (iso: string, now: number) => {
65 const at = new Date(iso)
66 return at.getTime() - now < DAY_MS
67 ? `${pad(at.getHours())}:${pad(at.getMinutes())}`
68 : at.toLocaleDateString(undefined, { weekday: 'short' })
69}
70
71const tone = (percent: number) => (percent >= 80 ? 'error' : percent >= 50 ? 'warning' : 'success')
72
73export const register: Register = (on) => {
74 // Seed right away, so the band shows after a start or reload without waiting for a turn.
75 on('session.start', async ($, e, next) => {
76 const now = toUsage(await $.session.usage())
77 await update($, usage, () => now)
78 // A reload while the band waits for a compaction's figures drops that wait: show the band again.
79 await update($, compacting, () => false)
80 return next(e)
81 })
82
83 on('session.measure', async ($, e, next) => {
84 const measured = toUsage(e)
85 await update($, usage, () => measured)
86 return next(e)
87 })
88
89 // Step aside while the main conversation compacts, so the engine's progress shows in the band.
90 // The engine puts the compacted conversation in place only once every hook has returned, so a
91 // read in here still sees the old fill: read it right after, with the band aside until then.
92 on('session.compact', async ($, e, next) => {
93 if (e.trigger === 'precompute' || e.agentId !== undefined) return next(e)
94
95 await update($, compacting, () => true)
96 try {
97 const result = await next(e)
98 if (result.skip !== undefined) {
99 await update($, compacting, () => false)
100 return result
101 }
102 $.clock.after(0, () => void settle($))
103 return result
104 } catch (error) {
105 await update($, compacting, () => false)
106 throw error
107 }
108 })
109
110 // A /clear starts the conversation over, so the old context fill no longer applies.
111 on('session.end', async ($, e, next) => {
112 if (e.reason === 'clear') {
113 await update($, usage, (current) => current && { ...current, context: undefined })
114 }
115 return next(e)
116 })
117
118 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
119 const current = await read($, usage)
120 if (e.props.hasSurvey || current === null || (await read($, compacting))) {
121 return next(e)
122 }
123
124 const { Box, Text } = $.ui.resolve(e)
125 const now = await $.clock.now()
126
127 // A missing bar draws as an empty placeholder, so the band keeps its shape.
128 const segment = (label: string, bar?: Bar) => (
129 <Box gap={1}>
130 <Text dimColor>{label}</Text>
131 {bar ? (
132 <Text color={tone(bar.percent)}>
133 {meter(bar.percent)} {Math.round(bar.percent)}%
134 </Text>
135 ) : (
136 <Text dimColor>{meter(0)} –</Text>
137 )}
138 {bar?.resetsAt && <Text dimColor>↻ {resetLabel(bar.resetsAt, now)}</Text>}
139 </Box>
140 )
141
142 // Stack below other plugins' bands instead of replacing them, so ours sits next to the prompt.
143 const beneath = await next(e)
144
145 return (
146 <Box flexDirection="column">
147 {beneath}
148 <Box gap={2}>
149 {current.fiveHour && segment('5h', current.fiveHour)}
150 {current.sevenDay && segment('7d', current.sevenDay)}
151 {segment('ctx', current.context)}
152 </Box>
153 </Box>
154 )
155 })
156}
157types/index.d.ts 15 lines1export type Bar = { percent: number; resetsAt?: string }
2
3// Each bar is optional: rate limits are absent off a subscription or before the first response.
4export type Usage = {
5 fiveHour?: Bar
6 sevenDay?: Bar
7 context?: Bar
8}
9
10declare module 'claude-code' {
11 interface PluginState {
12 clausage: { usage: Usage | null; compacting: boolean }
13 }
14}
15