SLOPSHOPPER

on-demand-savior

Pauses Claude before the 5-hour or weekly limit spills into on-demand usage, writes a handoff, and resumes from it once the limit resets

panebandguardcommandstatusprompt
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · on-demand-savior
│ ┃ handoff ✕ › fix the failing auth test and add an audit log call │ ┃ No handoff yet. │ ⏺ 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 │ │ › /savior │ ⎿ on-demand-savior: on-demand-savior is on: pauses at 5-hour 96% o │ │ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Pane · handoff
No handoff yet.
README

on-demand-savior

Stops Claude before your 5-hour or weekly limit spills into on-demand (extra) usage, then picks the work back up once the limit resets.

  1. Watch: every usage reading is checked against the 5-hour and weekly thresholds (96% by default), or the on-demand budget when one is set.
  2. Pause: the running turn is stopped, and new prompts and tool calls are held back.
  3. Hand off: the model writes a handoff (goal, done so far, in progress, next steps, key files, open questions) to handoff-<project>-<time>.md, in the project's .claude/handoffs or the folder you choose. Handoffs are kept: each pause writes a new file, and nothing deletes one.
  4. Resume: a minute after the window resets, the handoff is sent back as a prompt and the work carries on.

While it waits, the band right above the prompt shows 🛟 Paused at 5-hour 97% · resumes 3:31 PM with a Resume now button (r once the band has focus) to go on now.

The handoff opens in a side pane on the right, with the same button. A pane opened on its own needs a terminal at least 144 columns wide; /savior handoff opens it at any width.

Commands

CommandWhat it does
/savior or /savior statusWhether it is on, and any pause in force
/savior offStop guarding (and clear a pause): Claude goes on into on-demand usage
/savior onGuard again, ending an early resume's quiet; pauses at once if the last reading is already past the threshold
/savior resumeResume from the handoff now, before the reset. Checks on that limit stay off until it resets, so this may use on-demand usage; other limits still pause
/savior handoffShow the handoff in the side pane

On or off is remembered across sessions.

Settings

Set in /config:

SettingDefaultWhat it does
fiveHourThreshold96Pause once the 5-hour limit reaches this percent used
weeklyThreshold96Pause once the weekly limit reaches this percent used
onDemandBudgetUsd0On-demand usage to let through once a limit is used up, then pause
handoffDiremptyFolder to keep handoffs in: absolute (D:/notes/handoffs), under your home (~/handoffs), or relative to the project (docs/handoffs). Empty uses the project's .claude/handoffs

With a budget above 0, the thresholds step aside: the limit runs full, on-demand usage is counted from that moment (at API prices, as /cost counts it, within this session), and the pause comes once the budget is spent.

Good to know

  • The pause comes from a usage reading, and one arrives after each model response, so it can land a point or two past the threshold. Turning off extra usage in your claude.ai settings is the hard stop; this mod is the graceful one.
  • Writing the handoff is one more model call, which is what the room below 100% is for.
  • The resume needs Claude Code open. Closed, the next session in the same project finds the pause and resumes from its handoff once the window has reset.
  • A pause belongs to the project it happened in.

Develop

claude plugin validate mods/on-demand-savior
claude plugin test mods/on-demand-savior
Source 2 files
hooks/register.tsx 479 lines
1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register, SessionMessage, SessionRateLimit, Timer } from 'claude-code'
3
4import type { Pause, Settings, Spend, Trip } from '../types'
5
6const COMMAND = 'savior'
7// The pause in force, mirrored from the store for what the band draws.
8const pauseState = atom({ plugin: 'on-demand-savior', key: 'pause' } as const, null)
9// The handoff's text, for the side pane.
10const handoffState = atom({ plugin: 'on-demand-savior', key: 'handoff' } as const, null)
11const PANE = 'handoff'
12const WATCHED = ['five_hour', 'seven_day']
13const LABELS: Record<string, string> = { five_hour: '5-hour', seven_day: 'weekly' }
14const DEFAULT_THRESHOLD = 96
15// Resume a minute after the reset, so the first request lands in the new window.
16const GRACE_MS = 60_000
17const POLL_MS = 30_000
18const DAY = 24 * 60 * 60 * 1000
19
20export const label = (kind: string) => LABELS[kind] ?? kind.replace(/_/g, ' ')
21
22export const usd = (amount: number) => `$${amount.toFixed(2)}`
23
24export const settings = (options: Record<string, unknown>): Settings => {
25  const percent = (setting: unknown) => {
26    const n = Number(setting)
27
28    return Number.isFinite(n) && n > 0 && n <= 100 ? n : DEFAULT_THRESHOLD
29  }
30  const budget = Number(options.onDemandBudgetUsd)
31
32  return {
33    fiveHour: percent(options.fiveHourThreshold),
34    weekly: percent(options.weeklyThreshold),
35    budgetUsd: Number.isFinite(budget) && budget > 0 ? budget : 0,
36    handoffDir: typeof options.handoffDir === 'string' ? options.handoffDir.trim() : '',
37  }
38}
39
40// Where a handoff is saved: the setting's folder (`~` the home folder, a relative
41// one under the project), else the project's .claude/handoffs. Named by project
42// and time, so a shared folder keeps every one apart and nothing is overwritten.
43export const handoffFile = (dir: string, cwd: string, home: string | undefined, now: number) => {
44  const trim = (path: string) => path.replace(/[\\/]+$/, '')
45  const root = trim(cwd)
46  const folder =
47    dir === ''
48      ? `${root}/.claude/handoffs`
49      : /^~([\\/]|$)/.test(dir) && home
50        ? trim(home) + dir.slice(1)
51        : /^([a-zA-Z]:)?[\\/]/.test(dir)
52          ? dir
53          : `${root}/${dir}`
54  const project = root.split(/[\\/]/).pop() || 'project'
55
56  return `${trim(folder)}/handoff-${project}-${stamp(now)}.md`
57}
58
59// The watched windows of a reading, a window whose reset has passed being stale.
60const live = (limits: readonly SessionRateLimit[], now: number) =>
61  limits.filter(l => WATCHED.includes(l.kind) && l.resetsAt && Date.parse(l.resetsAt) > now)
62
63// Of several, the window that resets last: the one to wait out.
64const latest = (limits: readonly SessionRateLimit[]) =>
65  [...limits].sort((a, b) => Date.parse(b.resetsAt!) - Date.parse(a.resetsAt!))[0]
66
67// A window at or past its own threshold.
68export const tripped = (limits: readonly SessionRateLimit[], s: Settings, now: number): Trip | undefined => {
69  const hit = latest(live(limits, now).filter(l => l.percentUsed >= (l.kind === 'five_hour' ? s.fiveHour : s.weekly)))
70
71  return hit && { kind: hit.kind, resetsAt: hit.resetsAt!, reason: `${label(hit.kind)} ${hit.percentUsed}%` }
72}
73
74// A window used up: past it, requests are billed as on-demand usage.
75export const spilled = (limits: readonly SessionRateLimit[], now: number) =>
76  latest(live(limits, now).filter(l => l.percentUsed >= 100))
77
78// The budget spent since the window read full; undefined while there is room.
79export const overBudget = (full: SessionRateLimit, spend: Spend, costUsd: number, budgetUsd: number): Trip | undefined => {
80  const spent = costUsd - spend.baseUsd
81
82  return spent >= budgetUsd ? { kind: full.kind, resetsAt: full.resetsAt!, reason: `${usd(spent)} of on-demand` } : undefined
83}
84
85export const resumesAt = (pause: Pause) => Date.parse(pause.resetsAt) + GRACE_MS
86
87// "3:31 PM", or "Sat 3:31 PM" when more than a day out.
88export const clockTime = (at: number, now: number) =>
89  new Intl.DateTimeFormat('en-US', {
90    weekday: at - now > DAY ? 'short' : undefined,
91    hour: 'numeric',
92    minute: '2-digit',
93  }).format(at)
94
95// 2026-10-09T14:30:05.000Z -> "2026-10-09T14-30-05"
96export const stamp = (now: number) => new Date(now).toISOString().slice(0, 19).replace(/:/g, '-')
97
98export const handoffPrompt = (pause: Pause) =>
99  [
100    `You are being paused to keep usage in bounds (${pause.reason}; the ${label(pause.kind)} limit).`,
101    'Write a handoff document that lets you pick this work up after the limit resets, with no other memory of this conversation.',
102    'Use Markdown with these sections: Goal; Done so far; In progress (exactly where you stopped, including any half-finished edit);',
103    'Next steps (in order); Key files and facts (paths, commands, decisions, gotchas); Open questions for the user.',
104    'Be specific and concise. Reply with the document only.',
105  ].join(' ')
106
107// Used when the model could not write the handoff: the conversation's tail, as it stands.
108export const transcriptHandoff = (messages: readonly SessionMessage[]) =>
109  [
110    '# Handoff (transcript excerpt)',
111    'The model could not write a summary, so this is the end of the conversation as it stood.',
112    ...messages.slice(-20).map(m => {
113      const tools = m.toolUses.length ? `\n_tools: ${m.toolUses.map(u => u.tool).join(', ')}_` : ''
114
115      return `**${m.role}**: ${m.text.slice(0, 2000)}${tools}`
116    }),
117  ].join('\n\n')
118
119export const resumePrompt = (path: string | undefined, doc: string | undefined) =>
120  doc
121    ? `The usage limit has reset. on-demand-savior paused you before it ran into on-demand usage and saved this handoff to ${path}. Pick the work up from it:\n\n${doc}`
122    : 'The usage limit has reset. on-demand-savior paused you before it ran into on-demand usage. Pick the work up where you left off.'
123
124const pausedLine = (pause: Pause, now: number) =>
125  `🛟 Paused at ${pause.reason} · resumes ${clockTime(resumesAt(pause), now)}`
126
127// Module variables: a reload starts them over, the store keeps the pause itself.
128let config = settings({})
129let turnId: string | undefined
130let poll: Timer | undefined
131let isPausing = false
132
133// One pause per project, so a session elsewhere neither resumes nor clears it.
134async function key($: EngineInterface) {
135  return `pause:${await $.session.cwd()}`
136}
137
138async function quietKey($: EngineInterface) {
139  return `quiet:${await $.session.cwd()}`
140}
141
142// Until when an early resume keeps the checks quiet; 0 when it does not.
143async function quietUntil($: EngineInterface) {
144  return Number((await $.store.get(await quietKey($))) ?? 0)
145}
146
147async function isEnabled($: EngineInterface) {
148  return (await $.store.get('enabled')) !== false
149}
150
151async function getPause($: EngineInterface) {
152  return (await $.store.get(await key($))) as Pause | undefined
153}
154
155// Keeps the pause in the store (across sessions) and in state (for the band).
156async function savePause($: EngineInterface, pause: Pause | undefined) {
157  if (pause) {
158    await $.store.set(await key($), pause)
159  } else {
160    await $.store.delete(await key($))
161  }
162  await update($, pauseState, () => pause ?? null)
163}
164
165// Opens the side pane on the handoff; unasked it waits for a wide enough terminal.
166async function showHandoff($: EngineInterface, doc: string) {
167  await update($, handoffState, () => doc)
168  await $.ui.open({ id: PANE, title: 'Handoff' }).catch(() => undefined)
169}
170
171async function closeHandoff($: EngineInterface) {
172  await update($, handoffState, () => null)
173  await $.ui.close({ id: PANE }).catch(() => undefined)
174}
175
176// The pause in force: enabled, and its window not yet reset.
177async function active($: EngineInterface) {
178  const pause = await getPause($)
179  if (!pause || !(await isEnabled($))) {
180    return undefined
181  }
182
183  return (await $.clock.now()) < resumesAt(pause) ? pause : undefined
184}
185
186function stopPoll() {
187  poll?.cancel()
188  poll = undefined
189}
190
191// A resume before the reset ('early', the person's choice) also quiets the
192// checks on that window until it resets: its readings stay past the threshold.
193async function resume($: EngineInterface, isEarly = false) {
194  const pause = await getPause($)
195  stopPoll()
196  if (!pause) {
197    return
198  }
199  if (isEarly) {
200    await $.store.set(await quietKey($), resumesAt(pause))
201  }
202  await savePause($, undefined)
203  await closeHandoff($)
204  const doc = pause.handoffPath ? await $.fs.read(pause.handoffPath).catch(() => undefined) : undefined
205  // Off this dispatch: a command.run hook may not wait on a turn of its own.
206  $.clock.after(0, () => void $.prompt.submit({ text: resumePrompt(pause.handoffPath, doc) }))
207}
208
209function arm($: EngineInterface) {
210  if (poll) {
211    return
212  }
213  poll = $.clock.every(POLL_MS, async () => {
214    const pause = await getPause($)
215    if (!pause || !(await isEnabled($))) {
216      stopPoll()
217    } else if ((await $.clock.now()) >= resumesAt(pause)) {
218      await resume($)
219    }
220  })
221}
222
223async function pauseSession($: EngineInterface, trip: Trip) {
224  if (isPausing || (await getPause($))) {
225    return
226  }
227  isPausing = true
228  try {
229    const now = await $.clock.now()
230    const paused: Pause = { ...trip, pausedAt: now }
231    await savePause($, paused)
232    if (turnId) {
233      await $.turn.abort({ turnId }).catch(() => undefined)
234    }
235
236    const reply = await $.model.fork({ prompt: handoffPrompt(paused) })
237    const doc = reply.isAnswered ? reply.text : transcriptHandoff(await $.session.messages().catch(() => []))
238    const home = config.handoffDir.startsWith('~')
239      ? ((await $.env.get('USERPROFILE').catch(() => undefined)) ?? (await $.env.get('HOME').catch(() => undefined)))
240      : undefined
241    const handoffPath = handoffFile(config.handoffDir, await $.session.cwd(), home, now)
242    // A failed write still keeps the pause, so it still ends at the reset.
243    const isSaved = await $.fs.write(handoffPath, doc).then(() => true, () => false)
244
245    // `/savior off` or `resume` while the handoff was being written wins.
246    if (!(await getPause($))) {
247      return
248    }
249    await savePause($, isSaved ? { ...paused, handoffPath } : paused)
250    arm($)
251    await showHandoff($, doc)
252  } finally {
253    isPausing = false
254  }
255}
256
257// With a budget, the windows run full and on into on-demand usage, counted per
258// session from its cost when one first read full until every full window reset.
259async function spentTrip($: EngineInterface, limits: readonly SessionRateLimit[], costUsd: number | undefined, now: number) {
260  const full = spilled(limits, now)
261  if (!full || costUsd === undefined) {
262    return undefined
263  }
264  const { startedAt } = await $.session.usage()
265  const spendKey = `spend:${await $.session.cwd()}`
266  const stored = await $.store.get(spendKey)
267  const spends = (Array.isArray(stored) ? stored : []) as Spend[]
268  const others = spends.filter(s => s.startedAt !== startedAt && Date.parse(s.resetsAt) > now)
269  const mine = spends.find(s => s.startedAt === startedAt && Date.parse(s.resetsAt) > now)
270  const resetsAt = mine && Date.parse(mine.resetsAt) > Date.parse(full.resetsAt!) ? mine.resetsAt : full.resetsAt!
271  if (!mine || mine.resetsAt !== resetsAt) {
272    await $.store.set(spendKey, [...others, { resetsAt, startedAt, baseUsd: mine?.baseUsd ?? costUsd }])
273  }
274
275  return mine && overBudget(full, mine, costUsd, config.budgetUsd)
276}
277
278async function check($: EngineInterface, limits: readonly SessionRateLimit[], costUsd: number | undefined) {
279  if (isPausing || !(await isEnabled($)) || (await getPause($))) {
280    return
281  }
282  const now = await $.clock.now()
283  // An early resume quiets only the windows it pushed past: those resetting before the quiet ends.
284  const quiet = await quietUntil($)
285  const watched = limits.filter(l => !l.resetsAt || Date.parse(l.resetsAt) >= quiet)
286  const hit = config.budgetUsd > 0 ? await spentTrip($, watched, costUsd, now) : tripped(watched, config, now)
287  if (hit) {
288    // Off this dispatch: the pause aborts the turn and waits on the model.
289    $.clock.after(0, () => void pauseSession($, hit))
290  }
291}
292
293async function status($: EngineInterface) {
294  if (!(await isEnabled($))) {
295    return 'on-demand-savior is off: at the limit, Claude goes on into on-demand usage. /savior on to guard again.'
296  }
297  const pause = await active($)
298  if (pause) {
299    const handoff = pause.handoffPath ? ` Handoff: ${pause.handoffPath}.` : ''
300
301    return `${pausedLine(pause, await $.clock.now())}.${handoff} /savior resume to go on now (may use on-demand usage).`
302  }
303
304  const now = await $.clock.now()
305  const quiet = await quietUntil($)
306  if (now < quiet) {
307    return `on-demand-savior is on, but resumed early: the paused limit's checks are off until ${clockTime(quiet, now)}, when it resets. /savior on to check again now.`
308  }
309  const { fiveHour, weekly, budgetUsd } = config
310
311  return budgetUsd > 0
312    ? `on-demand-savior is on: lets ${usd(budgetUsd)} of on-demand usage through once a limit is used up, then pauses.`
313    : `on-demand-savior is on: pauses at 5-hour ${fiveHour}% or weekly ${weekly}%.`
314}
315
316export const register: Register = (on, options) => {
317  config = settings(options)
318
319  on('session.start', async ($, e, next) => {
320    const result = await next(e)
321    // The band above the prompt says it all; clear a status line an older version left.
322    $.ui.status(undefined)
323    await $.command.register({
324      name: COMMAND,
325      description: 'Guard against on-demand usage: on, off, status, resume now, or show the handoff',
326      argumentHint: '[on|off|status|resume|handoff]',
327      immediate: true,
328    })
329    // A pause from an earlier session in this project: wait it out, or resume
330    // from its handoff right away if the window already reset.
331    const pause = await getPause($)
332    if (pause && (await isEnabled($))) {
333      await update($, pauseState, () => pause)
334      arm($)
335      const doc = pause.handoffPath ? await $.fs.read(pause.handoffPath).catch(() => undefined) : undefined
336      if (doc) {
337        await showHandoff($, doc)
338      }
339    }
340
341    return result
342  })
343
344  on('command.run', { command: COMMAND }, async ($, e) => {
345    const action = e.args.trim().toLowerCase()
346    if (action === 'off') {
347      await $.store.set('enabled', false)
348      await savePause($, undefined)
349      await closeHandoff($)
350      stopPoll()
351    } else if (action === 'on') {
352      await $.store.set('enabled', true)
353      await $.store.delete(await quietKey($))
354      // The last reading may already be past the threshold; no new point may come.
355      const usage = await $.session.usage()
356      await check($, usage.rateLimits, usage.cost?.usd)
357    } else if (action === 'resume') {
358      if (!(await getPause($))) {
359        return { text: 'on-demand-savior: nothing is paused.' }
360      }
361      await resume($, true)
362
363      return { text: 'on-demand-savior: resumed from the handoff before the reset; checks on that limit are off until it, so this may use on-demand usage.' }
364    } else if (action === 'handoff') {
365      const pause = await getPause($)
366      const doc = pause?.handoffPath ? await $.fs.read(pause.handoffPath).catch(() => undefined) : undefined
367      if (!doc) {
368        return { text: 'on-demand-savior: no handoff to show.' }
369      }
370      await showHandoff($, doc)
371
372      return { text: `on-demand-savior: showing the handoff from ${pause!.handoffPath}.` }
373    } else if (action !== '' && action !== 'status') {
374      return { text: `on-demand-savior: unknown "${action}". Use /savior on, off, status, resume or handoff.` }
375    }
376
377    return { text: await status($) }
378  })
379
380  on('session.measure', async ($, e, next) => {
381    if (e.changed.includes('rateLimits') || e.changed.includes('cost')) {
382      await check($, e.rateLimits, e.cost?.usd)
383    }
384
385    return next(e)
386  })
387
388  on('turn.start', async ($, e, next) => {
389    turnId = e.turnId
390    const result = await next(e)
391    // A turn that starts while paused (a continuation, a skill) ends at once.
392    if (await active($)) {
393      void $.turn.abort({ turnId: e.turnId }).catch(() => undefined)
394    }
395
396    return result
397  })
398
399  on('turn.complete', async ($, e, next) => {
400    if (!e.agentId && e.turnId === turnId) {
401      turnId = undefined
402    }
403
404    return next(e)
405  })
406
407  // Slash commands pass, so /savior itself still answers while paused.
408  on('prompt.submit', async ($, e, next) => {
409    const pause = e.text.trimStart().startsWith('/') ? undefined : await active($)
410    if (pause) {
411      return { drop: `${pausedLine(pause, await $.clock.now())}. /savior resume to go on now (may use on-demand usage), /savior off to stop guarding.` }
412    }
413
414    return next(e)
415  })
416
417  on('tool.call', async ($, e, next) => {
418    if (await active($)) {
419      return { deny: 'on-demand-savior paused this session at the usage limit; stop here, the work resumes when the limit resets.' }
420    }
421
422    return next(e)
423  })
424
425  // The pause, with the button that ends it, right above the prompt.
426  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
427    const pause = await read($, pauseState)
428    if (!pause || e.props.hasSurvey) {
429      return next(e)
430    }
431    const below = await next(e)
432    const now = await $.clock.now()
433    const { Box, Button, Text } = $.ui.resolve(e)
434
435    return (
436      <Box flexDirection="column">
437        <Box key="savior" flexDirection="row" flexWrap="wrap">
438          <Text color="yellow">{pausedLine(pause, now)} </Text>
439          <Button
440            key="resume"
441            label="Resume now"
442            hotkey="r"
443            variant="primary"
444            onPress={() => resume($, true)}
445          />
446        </Box>
447        {below}
448      </Box>
449    )
450  })
451
452  // The side pane: the handoff, and the same button.
453  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
454    const doc = await read($, handoffState)
455    const pause = await read($, pauseState)
456    const now = await $.clock.now()
457    const { Box, Button, Markdown, Text } = $.ui.resolve(e)
458
459    return (
460      // Two cells either side (about 16px) keep the text off the frame.
461      <Box key="pane" flexDirection="column" paddingX={2}>
462        {pause && (
463          <Box key="paused" flexDirection="row" flexWrap="wrap">
464            <Text color="yellow">{pausedLine(pause, now)} </Text>
465            <Button
466              key="resume"
467              label="Resume now"
468              hotkey="r"
469              variant="primary"
470              onPress={() => resume($, true)}
471            />
472          </Box>
473        )}
474        <Markdown key="handoff" text={doc ?? '_No handoff yet._'} />
475      </Box>
476    )
477  })
478}
479
types/index.d.ts 18 lines
1/** What trips a pause: the window to wait out, and why, as the status line says it. */
2export type Trip = { kind: string; resetsAt: string; reason: string }
3
4/** A pause in one project: what tripped it and the handoff written for it. */
5export type Pause = Trip & { pausedAt: number; handoffPath?: string }
6
7/** The settings: each window's threshold, the on-demand budget (0: none) and the handoff folder ('' : the project's). */
8export type Settings = { fiveHour: number; weekly: number; budgetUsd: number; handoffDir: string }
9
10/** One session's on-demand spend: its cost when a window first read full, kept until the last full window resets. */
11export type Spend = { resetsAt: string; startedAt: number; baseUsd: number }
12
13declare module 'claude-code' {
14  interface PluginState {
15    'on-demand-savior': { pause: Pause | null; handoff: string | null }
16  }
17}
18