Come back to big chats for less. When a large chat (150k+ tokens) has been idle for 25 minutes, it is compacted while the prompt cache is still warm, so…

Come back to big chats for less. Claude Code's prompt cache expires after an hour. Return to a 300k-token chat after lunch and the first reply pays to reload all of it at full price.
idle-compact runs /compact on a large chat (150k+ tokens) after 25 minutes idle, while the cache is still warm and before the desktop app disconnects the idle chat (30 minutes). Compacting is cheap then, and the chat you come back to is small.
Run this in a terminal claude session:
/plugin install idle-compact --marketplace Ryan-LT/claude-mods
Answer y to add the marketplace, then choose the user scope.
hooks/register.ts 76 lines1import type { EngineInterface, Register } from 'claude-code'
2
3const MINUTE = 60_000
4/**
5 * Quiet this long after the last reply: compact before the prompt cache (1 hour) goes cold, and
6 * before the desktop app disconnects an idle chat (30 minutes), which ends this process and its timer.
7 */
8const IDLE = 25 * MINUTE
9/** A smaller chat is cheap to come back to as it is. */
10const MIN_TOKENS = 150_000
11const CHECK_EVERY = MINUTE
12const KEEP =
13 'Keep what was asked last and any open tasks, decisions and why they were made, exact file paths, links, ' +
14 'commands, and error messages, so the work can resume without re-explaining.'
15
16/**
17 * When this process last saw a main-thread reply end, or 0. Zero until a reply in this process,
18 * so a chat reopened cold (cache already gone) is never compacted: that would pay full price.
19 */
20let lastReplyAt = 0
21let isBusy = false
22let isCompacting = false
23let isTicking = false
24/** The /compact we start runs as a turn of its own here; its end is not a reply to arm on. */
25let isOwnTurnNext = false
26
27const check = async ($: EngineInterface) => {
28 if (!lastReplyAt || isBusy || isCompacting) return
29 if ((await $.clock.now()) - lastReplyAt < IDLE) return
30 // One try per idle stretch: the next reply arms it again.
31 lastReplyAt = 0
32 const { context } = await $.session.usage()
33 if ((context.tokens ?? 0) < MIN_TOKENS) return
34 isCompacting = true
35 try {
36 isOwnTurnNext = true
37 // In the desktop app and SDK, compaction runs as a /compact turn, not a direct call.
38 await $.session.compact({ instructions: KEEP }).catch(() => $.command.run({ command: 'compact', args: KEEP }))
39 } catch {
40 isOwnTurnNext = false
41 // A turn started meanwhile, or compaction isn't possible now: leave the chat as it is.
42 } finally {
43 isCompacting = false
44 }
45}
46
47const ensureTicker = ($: EngineInterface) => {
48 if (isTicking) return
49 isTicking = true
50 $.clock.every(CHECK_EVERY, () => void check($).catch(() => {}))
51}
52
53export const register: Register = on => {
54 on('turn.start', ($, e, next) => {
55 ensureTicker($)
56 isBusy = true
57
58 return next(e)
59 })
60
61 on('turn.complete', async ($, e, next) => {
62 const done = await next(e)
63 if (!e.agentId) {
64 isBusy = false
65 if (isOwnTurnNext) {
66 isOwnTurnNext = false
67 return done
68 }
69 lastReplyAt = await $.clock.now()
70 ensureTicker($)
71 }
72
73 return done
74 })
75}
76