SLOPSHOPPER

usage-wrapup

When your plan usage gets low, tells every running agent to wrap up before the limit cuts it off, and compacts the conversation when usage is nearly gone.

guardtoaststatus
A shopper browsing a rack in a slop shop
README

usage-wrapup

A Claude Code mod. When your plan usage gets low, it tells every running agent to wrap up — so you don't get cut off mid-task with half-edited files and no handoff.

What it does

  • Watches your rate-limit windows (5-hour, weekly, spend limit).
  • When any window has 1% or less left (configurable), every tool result — in the main agent and in subagents — carries a note telling the model to:
  • start no new work,
  • finish or safely pause the current step,
  • write a short handoff (done / left / next step),
  • arm one wake for just after the reset time, such as send_later to its own session or a create_trigger routine, so the paused work resumes without a person,
  • stop and answer.
  • Shows ⚠ N% left — wrapping up in the status line and a one-time toast.
  • Compacts the conversation when usage is nearly gone. This happens when the 5-hour window is 97% used or more, or the weekly window is 99% used or more.
  • It compacts when a turn ends, so the next turn starts with a smaller context.
  • It compacts once per window. When the window resets, it can compact again.
  • The summary keeps the current task, its state, the files it touched, and the next steps.
  • It skips a turn you interrupted, and it skips subagent turns.

Only works on a Claude subscription (Pro/Max/Team), where Claude Code gets rate-limit data.

Install

git clone https://github.com/DaKev/usage-wrapup ~/claude-mods/usage-wrapup
claude --plugin-dir ~/claude-mods/usage-wrapup

Configure

/config has three options:

  • Wrap-up threshold (% left). Default 1.
  • Compact at 5-hour usage (% used). Default 97.
  • Compact at weekly usage (% used). Default 99.

Test

claude plugin validate .
claude plugin test .

MIT license. See LICENSE.

Source 5 files
hooks/index.ts 9 lines
1import type { Register } from 'claude-code'
2import { register as registerWrapUp } from './register'
3import { register as registerCompact } from './compact'
4
5export const register: Register = (on, options) => {
6  registerWrapUp(on, options)
7  registerCompact(on, options)
8}
9
hooks/register.ts 72 lines
1import type { Register, SessionRateLimit } from 'claude-code'
2import { isAtOrBelow, readablePercentUsed } from './usage'
3
4const LABEL: Record<string, string> = {
5  five_hour: '5-hour',
6  seven_day: 'weekly',
7  spend_limit: 'spend',
8}
9
10type Low = { kind: string; left: number; resetsAt?: string }
11
12/** The readable window with the least left, when it is at or below the threshold. Windows with no finite reading are skipped. */
13export function lowest(limits: readonly SessionRateLimit[], threshold: number): Low | undefined {
14  let worst: Low | undefined
15  for (const l of limits) {
16    const used = readablePercentUsed(l)
17    if (used === undefined) continue
18    const left = Math.max(0, 100 - used)
19    if (!worst || left < worst.left) worst = { kind: l.kind, left, resetsAt: l.resetsAt }
20  }
21  if (!worst || !isAtOrBelow(worst.left, threshold)) return undefined
22  return { ...worst, left: Math.round(worst.left * 10) / 10 }
23}
24
25function describe(low: Low): string {
26  const name = LABEL[low.kind] ?? low.kind
27  const reset = low.resetsAt ? ` It resets at ${low.resetsAt}.` : ''
28  return `${name} usage limit: only ${low.left}% left.${reset}`
29}
30
31export function wrapUpNote(low: Low): string {
32  return [
33    `USAGE LOW — ${describe(low)}`,
34    'You may be cut off soon. Wrap up now:',
35    '1. Do not start new work, new subagents or big searches.',
36    '2. Finish or safely pause the current step. Leave files in a working state.',
37    '3. Write a short handoff: what is done, what is left, the exact next step.',
38    `4. Before you stop, arm one wake for just after ${low.resetsAt ?? 'the reset time'}, such as send_later to your own session or a create_trigger routine, so the paused work resumes without a person.`,
39    '5. Then stop and give the user your final answer.',
40  ].join('\n')
41}
42
43export const register: Register = (on, options) => {
44  const threshold = Number(options.threshold ?? 1)
45  let limits: readonly SessionRateLimit[] | undefined
46  let wasWarned = false
47
48  on('session.measure', ($, e, next) => {
49    limits = e.rateLimits
50    const low = lowest(limits, threshold)
51    $.ui.status(low ? `⚠ ${low.left}% left — wrapping up` : undefined)
52    if (low && !wasWarned) $.ui.toast(`usage-wrapup: ${describe(low)} Agents told to wrap up.`)
53    wasWarned = !!low
54    return next(e)
55  })
56
57  on('tool.call', async ($, e, next) => {
58    const ran = await next(e)
59    if ('deny' in ran && ran.deny !== undefined) return ran
60    if (!limits) {
61      try {
62        limits = (await $.session.usage()).rateLimits
63      } catch {
64        limits = []
65      }
66    }
67    const low = lowest(limits, threshold)
68    if (!low) return ran
69    return { ...ran, context: [...(ran.context ?? []), wrapUpNote(low)] }
70  })
71}
72
hooks/compact.ts 54 lines
1import type { Register, SessionRateLimit } from 'claude-code'
2import type { CompactedWindows } from '../types'
3import { isAtOrPast, readablePercentUsed } from './usage'
4
5export const DEFAULT_FIVE_HOUR_PERCENT_USED = 97
6export const DEFAULT_SEVEN_DAY_PERCENT_USED = 99
7
8export const COMPACT_INSTRUCTIONS =
9  'Usage is near the plan limit. Keep the current task, its exact state, the files it touched, and the next steps.'
10
11const compacted = { plugin: 'usage-wrapup', key: 'compacted' } as const
12
13type Window = { kind: string; resetsAt: string }
14
15/** The first window at or past its percent-used threshold that has not compacted since it last reset. */
16export function windowToCompact(
17  limits: readonly SessionRateLimit[],
18  thresholdByKind: Readonly<Record<string, number>>,
19  alreadyCompacted: CompactedWindows,
20): Window | undefined {
21  for (const limit of limits) {
22    const threshold = thresholdByKind[limit.kind]
23    const used = readablePercentUsed(limit)
24    if (threshold === undefined || used === undefined || !isAtOrPast(used, threshold)) continue
25    const resetsAt = limit.resetsAt ?? ''
26    if (alreadyCompacted[limit.kind] !== resetsAt) return { kind: limit.kind, resetsAt }
27  }
28  return undefined
29}
30
31export const register: Register = (on, options) => {
32  const thresholdByKind = {
33    five_hour: Number(options.compactFiveHour ?? DEFAULT_FIVE_HOUR_PERCENT_USED),
34    seven_day: Number(options.compactSevenDay ?? DEFAULT_SEVEN_DAY_PERCENT_USED),
35  }
36
37  on('turn.complete', async ($, e, next) => {
38    const done = await next(e)
39    if (e.agentId !== undefined || e.reason === 'aborted') return done
40    try {
41      const { rateLimits } = await $.session.usage()
42      const { value: alreadyCompacted = {} } = await $.state.get(compacted)
43      const window = windowToCompact(rateLimits, thresholdByKind, alreadyCompacted)
44      if (window) {
45        await $.session.compact({ instructions: COMPACT_INSTRUCTIONS })
46        await $.state.set(compacted, { ...alreadyCompacted, [window.kind]: window.resetsAt })
47      }
48    } catch {
49      return done
50    }
51    return done
52  })
53}
54
hooks/usage.ts 19 lines
1import type { SessionRateLimit } from 'claude-code'
2
3const THRESHOLD_TOLERANCE = 1e-9
4
5/** The window's percent used, or undefined when the host sent no finite reading. */
6export function readablePercentUsed(limit: SessionRateLimit): number | undefined {
7  return Number.isFinite(limit.percentUsed) ? limit.percentUsed : undefined
8}
9
10/** Whether value is at or below bound within float error; false when either is NaN. */
11export function isAtOrBelow(value: number, bound: number): boolean {
12  return value <= bound + THRESHOLD_TOLERANCE
13}
14
15/** Whether value has reached bound within float error; false when either is NaN. */
16export function isAtOrPast(value: number, bound: number): boolean {
17  return isAtOrBelow(bound, value)
18}
19
types/index.d.ts 8 lines
1export type CompactedWindows = Record<string, string>
2
3declare module 'claude-code' {
4  interface PluginState {
5    'usage-wrapup': { compacted: CompactedWindows }
6  }
7}
8