modscode JA

modscode / Gallery / prompt-cache-control

prompt-cache-control

Prompt-cache meter above the Claude Code prompt: how many tokens each request read from, wrote to and sent past the cache, a live countdown to the cache's expiry, and what to do about it (keep going, compact or clear). The TTL follows Clau…

Code you can checkTerminal + desktopPaneBandStatus lineToastBoxButtonText

What it does

  • Draws a band above the prompt, a pane, toasts
  • Hooks session.end, session.start, turn.step, ui.close and 1 more
  • Keeps its own state between turns

Good points

  • No network calls
  • Starts no processes
  • Written in TypeScript
  • Has a desktop-specific branch

Code to start from

Two kinds: a starter modscode writes from the design above, and the lines of the mod's own code that draw it.

Starter (register.tsx)

register.tsx
import type { Register } from 'claude-code'

// Drawn like prompt-cache-control by Jake Rains (adapted from claude-code-templates by Daniel Áv… (pane, band, status, toast), as modscode read it from the mod's code:
// https://modscode.com/gallery/jakerains-prompt-cache-control/
// Written by modscode from that reading, not copied from the mod: use it as you like.
// Each value below stands for one the mod works out at run time; put in your own.
export const register: Register = on => {
  // the band above the prompt; what else goes there is drawn under it
  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
    const { Box, Text } = $.ui.resolve(e)
    const ratio = '…'
    const fmtTokens = '…'
    const fmtClock = '…'
    const ttl = '…'
    const text = '…'
    return (
      <Box flexDirection="column">
        <Box flexDirection="row" columnGap={1}>
          <Text bold>
            ●
          </Text>
          <Text bold color="cyan">
            cache
          </Text>
          <Text>
            ████████░░░░░░░░
          </Text>
          <Text bold>
            {`${ratio}%`}
          </Text>
          <Box flexDirection="row" columnGap={1}>
            <Text color="green">
              {`read ${fmtTokens}`}
            </Text>
            <Text color="yellow">
              {`wrote ${fmtTokens}`}
            </Text>
            <Text color="cyan">
              {`new ${fmtTokens}`}
            </Text>
          </Box>
          <Text bold color="red">
            {`⏱ ${fmtClock}`}
          </Text>
          <Text dimColor wrap="truncate-end">
            {`${ttl} · ${text}`}
          </Text>
        </Box>
        {await next(e)}
      </Box>
    )
  })

  // the pane: open it with $.ui.open({ id: 'my-pane', title: "cache" })
  on('ui.render', { component: 'Pane', requestId: 'my-pane' }, async ($, e) => {
    const { Box, Text, Button } = $.ui.resolve(e)
    const ttl = '…'
    const ttlSource = '…'
    const life = '…'
    const icon = '…'
    const text = '…'
    const model = '…'
    const fmtTokens = '…'
    const hitRatio = '…'
    const sp = '…'
    const sp2 = '…'
    const sp3 = '…'
    const sp4 = '…'
    const sp5 = '…'
    const pct = '…'
    const sp6 = '…'
    const sp7 = '…'
    const sp8 = '…'
    const sp9 = '…'
    const sp10 = '…'
    const sp11 = '…'
    const sp12 = '…'
    const sp13 = '…'
    const sp14 = '…'
    const sp15 = '…'
    return (
      <Box flexDirection="column">
        <Box flexDirection="row" columnGap={1}>
          <Text bold color="cyan">
            ⚡ PROMPT CACHE
          </Text>
          <Text dimColor>
            {`· ${ttl} lifetime (${ttlSource})`}
          </Text>
        </Box>
        <Box flexDirection="column" marginTop={1}>
          <Text bold>
            ⏱ 0:00
          </Text>
          <Box flexDirection="row" columnGap={1}>
            <Box flexDirection="row" height={1} flexShrink={0}>
              <Box height={1} flexShrink={0} />
              <Box height={1} flexShrink={0} />
              <Box height={1} flexShrink={0} />
            </Box>
            <Text dimColor>
              {`${life}%`}
            </Text>
          </Box>
        </Box>
        <Box marginTop={1} flexDirection="column">
          <Text bold>
            {`${icon} ${text}`}
          </Text>
          <Text dimColor>
            {`${model} · prompt ${fmtTokens} tokens`}
          </Text>
        </Box>
        <Box flexDirection="column" marginTop={1}>
          <Box flexDirection="row" columnGap={1}>
            <Box flexDirection="row" height={1} flexShrink={0}>
              <Box height={1} flexShrink={0} />
              <Box height={1} flexShrink={0} />
              <Box height={1} flexShrink={0} />
            </Box>
            <Text bold>
              {`${hitRatio}% hit`}
            </Text>
          </Box>
          <Box flexDirection="row" columnGap={2}>
            <Text color="green">
              {`■ read ${fmtTokens}`}
            </Text>
            <Text color="yellow">
              {`■ wrote ${fmtTokens}`}
            </Text>
            <Text color="cyan">
              {`■ new ${fmtTokens}`}
            </Text>
          </Box>
        </Box>
        <Box flexDirection="column" marginTop={1}>
          <Box flexDirection="row" columnGap={1}>
            <Box width="4" flexShrink={0} justifyContent="flex-end">
              <Text color="cyan">
                turn
              </Text>
            </Box>
            <Box width="5" flexShrink={0} justifyContent="flex-end">
              <Text color="cyan">
                steps
              </Text>
            </Box>
            <Box width="6" flexShrink={0} justifyContent="flex-end">
              <Text color="green">
                read
              </Text>
            </Box>
            <Box width="6" flexShrink={0} justifyContent="flex-end">
              <Text color="yellow">
                wrote
              </Text>
            </Box>
            <Box width="5" flexShrink={0} justifyContent="flex-end">
              <Text color="cyan">
                new
              </Text>
            </Box>
            <Box width="4" flexShrink={0} justifyContent="flex-end">
              <Text color="magenta">
                hit
              </Text>
            </Box>
          </Box>
          <Text dimColor>
            no requests yet
          </Text>
          <Box flexDirection="row" columnGap={1}>
            <Box width="4" flexShrink={0} justifyContent="flex-end">
              <Text>
                {sp}
              </Text>
            </Box>
            <Box width="5" flexShrink={0} justifyContent="flex-end">
              <Text>
                {sp2}
              </Text>
            </Box>
            <Box width="6" flexShrink={0} justifyContent="flex-end">
              <Text color="green">
                {sp3}
              </Text>
            </Box>
            <Box width="6" flexShrink={0} justifyContent="flex-end">
              <Text color="yellow">
                {sp4}
              </Text>
            </Box>
            <Box width="5" flexShrink={0} justifyContent="flex-end">
              <Text color="cyan">
                {sp5}
              </Text>
            </Box>
            <Box width="4" flexShrink={0} justifyContent="flex-end">
              <Text>
                {`${pct}%`}
              </Text>
            </Box>
          </Box>
          <Box flexDirection="row" columnGap={1}>
            <Box width="4" flexShrink={0} justifyContent="flex-end">
              <Text>
                {sp6}
              </Text>
            </Box>
            <Box width="5" flexShrink={0} justifyContent="flex-end">
              <Text>
                {sp7}
              </Text>
            </Box>
            <Box width="6" flexShrink={0} justifyContent="flex-end">
              <Text color="green">
                {sp8}
              </Text>
            </Box>
            <Box width="6" flexShrink={0} justifyContent="flex-end">
              <Text color="yellow">
                {sp9}
              </Text>
            </Box>
            <Box width="5" flexShrink={0} justifyContent="flex-end">
              <Text color="cyan">
                {sp10}
              </Text>
            </Box>
            <Box width="4" flexShrink={0} justifyContent="flex-end">
              <Text>
                {`${pct}%`}
              </Text>
            </Box>
          </Box>
          <Box flexDirection="row" columnGap={1}>
            <Box width="4" flexShrink={0} justifyContent="flex-end">
              <Text>
                {sp11}
              </Text>
            </Box>
            <Box width="5" flexShrink={0} justifyContent="flex-end">
              <Text>
                {sp12}
              </Text>
            </Box>
            <Box width="6" flexShrink={0} justifyContent="flex-end">
              <Text color="green">
                {sp13}
              </Text>
            </Box>
            <Box width="6" flexShrink={0} justifyContent="flex-end">
              <Text color="yellow">
                {sp14}
              </Text>
            </Box>
            <Box width="5" flexShrink={0} justifyContent="flex-end">
              <Text color="cyan">
                {sp15}
              </Text>
            </Box>
            <Box width="4" flexShrink={0} justifyContent="flex-end">
              <Text>
                {`${pct}%`}
              </Text>
            </Box>
          </Box>
        </Box>
        <Box marginTop={1} flexDirection="column">
          <Button label="close" />
          <Box marginTop={1} flexDirection="column">
            <Text color="green">
              ■ read: served by the cache
            </Text>
            <Text color="yellow">
              ■ wrote: new cache entry
            </Text>
            <Text color="cyan">
              ■ new: sent uncached
            </Text>
          </Box>
        </Box>
      </Box>
    )
  })

  on('session.start', async ($, e, next) => {
    const secs = '…'
    const tail = '…'
    await $.ui.toast(`cache expires in ${secs}s: ${tail}`)
    return next(e)
  })
}

The mod's own code

Shown under the mod's licence (MIT); its text is below. Keep the notice if you copy these lines.

prompt-cache-control/hooks/prompt-cache-control.tsx lines 335–386 · Band
  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
    const others = await next(e)
    if (!(await read($, barOn)) || e.props.hasSurvey || (await read($, paneOpen))) return others
    const m = await read($, meter)
    const { last, advice, left } = current(m, policy, Date.now())
    if (!last && advice.kind !== 'off') return others
    const { Box, Text } = $.ui.resolve(e)
    const columns = e.props.bodyColumns ?? e.viewport?.columns ?? 100
    const color = COLOR[advice.kind]
    const ttl = m.ttl

    if (!last) {
      return (
        <Box flexDirection="column">
          {others}
          <Text dimColor>{fit(`cache: ${advice.text}`, columns)}</Text>
        </Box>
      )
    }

    const ratio = hitRatio(last)
    const wide = columns >= 90
    const meterRow = (
      <Box flexDirection="row" columnGap={1}>
        <Text bold color={color}>{advice.kind === 'warm' ? '●' : advice.kind === 'soon' ? '▲' : advice.kind === 'off' || advice.kind === 'cold' || advice.kind === 'uncached' ? '○' : '✖'}</Text>
        <Text bold color="cyan">cache</Text>
        <Text color={color}>{bar(ratio, wide ? 10 : 6)}</Text>
        <Text bold>{`${Math.round(ratio * 100)}%`}</Text>
        {wide ? (
          // a row of its own: a fragment here stacks its children as a column
          <Box flexDirection="row" columnGap={1}>
            <Text color="green">{`read ${fmtTokens(last.read)}`}</Text>
            <Text color="yellow">{`wrote ${fmtTokens(last.write)}`}</Text>
            <Text color="cyan">{`new ${fmtTokens(last.fresh)}`}</Text>
          </Box>
        ) : (
          <Text dimColor>{`${fmtTokens(promptTokens(last))} tok`}</Text>
        )}
        {advice.kind !== 'uncached' && advice.kind !== 'off' && (
          <Text bold color={left > 0 ? lifeColor(left, ttl, policy.warnMs) : 'red'}>{left > 0 ? `⏱ ${fmtClock(left)}` : '⏱ 0:00'}</Text>
        )}
        <Text dimColor wrap="truncate-end">{`${ttl} · ${advice.text}`}</Text>
      </Box>
    )

    return (
      <Box flexDirection="column">
        {others}
        {meterRow}
      </Box>
    )
  })
prompt-cache-control/hooks/prompt-cache-control.tsx lines 388–495 · Pane
  on('ui.render', { component: 'Pane' }, async ($, e, next) => {
    if (e.requestId !== PANE) return next(e)
    const { Box, Text, Button } = $.ui.resolve(e)
    const width = Math.max(30, e.props.bodyColumns - 1)
    // HTML collapses runs of spaces and trims a text's ends; a no-break space keeps them
    const sp = (t: string) => (e.surface === 'terminal' ? t : t.replace(/ /g, ' '))
    const now = Date.now()
    const m = await read($, meter)
    const { ttl, ttlSource } = m
    const { last, advice, left } = current(m, policy, now)
    const all = byTurn(m.samples)
    const counting = !!last && advice.kind !== 'uncached' && advice.kind !== 'off'
    // the countdown goes green, then yellow, then red as the cache runs out
    const clockColor = counting ? lifeColor(left, ttl, policy.warnMs) : undefined
    const stateColor = advice.kind === 'expired' || advice.kind === 'miss' ? 'red' : (clockColor ?? COLOR[advice.kind])
    const hitColor = (pct: number) => (pct >= 80 ? 'green' : pct >= 40 ? 'yellow' : 'red')
    // solid bars are filled Boxes, not block characters, so HTML draws no seams between cells
    const solid = (key: string, parts: [number, string | undefined][]) => (
      <Box key={key} flexDirection="row" height={1} flexShrink={0}>
        {parts.map(([w, c], i) => (w > 0 ? <Box key={`${key}:${i}`} width={w} height={1} flexShrink={0} backgroundColor={c} /> : null))}
      </Box>
    )
    const cell = (key: string, w: number, text: string, c?: string, bold = false) => (
      <Box key={key} width={w} flexShrink={0} justifyContent="flex-end">
        <Text color={c} bold={bold} dimColor={!c}>{sp(text)}</Text>
      </Box>
    )

    const barW = Math.min(width, 40)
    const life = lifeRatio(left, ttl)
    const lifeFilled = Math.round(life * barW)
    const [sr, sw, sn] = last ? segments(last.read, last.write, last.fresh, barW) : [0, 0, 0]
    const rows = all.slice(-Math.max(3, (e.viewport?.rows ?? 24) - 16))
    const icon = advice.kind === 'warm' ? '●' : advice.kind === 'soon' ? '▲' : advice.kind === 'expired' || advice.kind === 'miss' ? '✖' : '○'

    return (
      <Box flexDirection="column">
        <Box key="title" flexDirection="row" columnGap={1}>
          <Text bold color="cyan">{sp('⚡ PROMPT CACHE')}</Text>
          <Text dimColor>{sp(`· ${ttl} lifetime (${ttlSource})`)}</Text>
        </Box>

        <Box key="clock" flexDirection="column" marginTop={1}>
          <Text bold color={clockColor}>{sp(counting ? `⏱ ${left > 0 ? fmtClock(left) : '0:00'}` : '⏱ --:--')}</Text>
          {counting ? (
            <Box flexDirection="row" columnGap={1}>
              {solid('life', [[lifeFilled, clockColor], [barW - lifeFilled, 'gray']])}
              <Text dimColor>{sp(`${Math.round(life * 100)}%`)}</Text>
            </Box>
          ) : null}
        </Box>

        <Box key="advice" marginTop={1} flexDirection="column">
          <Text bold color={stateColor}>{sp(`${icon} ${advice.text}`)}</Text>
          {last ? <Text dimColor>{sp(fit(`${last.model} · prompt ${fmtTokens(promptTokens(last))} tokens`, width))}</Text> : null}
        </Box>

        {last ? (
          <Box key="stack" flexDirection="column" marginTop={1}>
            <Box flexDirection="row" columnGap={1}>
              {solid('stack', [[sr, 'green'], [sw, 'yellow'], [sn, 'cyan']])}
              <Text bold color={hitColor(Math.round(hitRatio(last) * 100))}>{sp(`${Math.round(hitRatio(last) * 100)}% hit`)}</Text>
            </Box>
            <Box flexDirection="row" columnGap={2}>
              <Text color="green">{sp(`■ read ${fmtTokens(last.read)}`)}</Text>
              <Text color="yellow">{sp(`■ wrote ${fmtTokens(last.write)}`)}</Text>
              <Text color="cyan">{sp(`■ new ${fmtTokens(last.fresh)}`)}</Text>
            </Box>
          </Box>
        ) : null}

        <Box key="table" flexDirection="column" marginTop={1}>
          <Box key="head" flexDirection="row" columnGap={1}>
            {cell('h:turn', 4, 'turn', 'cyan', true)}
            {cell('h:steps', 5, 'steps', 'cyan', true)}
            {cell('h:read', 6, 'read', 'green', true)}
            {cell('h:wrote', 6, 'wrote', 'yellow', true)}
            {cell('h:new', 5, 'new', 'cyan', true)}
            {cell('h:hit', 4, 'hit', 'magenta', true)}
          </Box>
          {rows.length === 0 ? <Text dimColor>{sp('no requests yet')}</Text> : null}
          {rows.map((row, i) => {
            const n = all.length - rows.length + i + 1
            const pct = Math.round(rowRatio(row) * 100)
            return (
              <Box key={`t:${row.turnId}`} flexDirection="row" columnGap={1}>
                {cell(`c:turn:${row.turnId}`, 4, String(n))}
                {cell(`c:steps:${row.turnId}`, 5, String(row.steps))}
                {cell(`c:read:${row.turnId}`, 6, fmtTokens(row.read), 'green')}
                {cell(`c:wrote:${row.turnId}`, 6, fmtTokens(row.write), 'yellow')}
                {cell(`c:new:${row.turnId}`, 5, fmtTokens(row.fresh), 'cyan')}
                {cell(`c:hit:${row.turnId}`, 4, `${pct}%`, hitColor(pct), true)}
              </Box>
            )
          })}
        </Box>

        <Box key="foot" marginTop={1} flexDirection="column">
          <Button key="close" label="close" onPress={() => {}} />
          <Box key="legend" marginTop={1} flexDirection="column">
            <Text color="green">{sp('■ read: served by the cache')}</Text>
            <Text color="yellow">{sp('■ wrote: new cache entry')}</Text>
            <Text color="cyan">{sp('■ new: sent uncached')}</Text>
          </Box>
        </Box>
      </Box>
    )
  })
Licence text (MIT)
MIT License

Copyright (c) 2026 Jake Rains

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

What its code calls

reads filesenv varsremembers

The design, read from code

Drawn from the mod's source at the commit read, not from a run: a list is drawn three times, a condition takes its first branch that draws in the desktop app, and a value only the run time knows is shown as a made-up sample (dotted underline) or left out. Colours and type are this site's.

What its code shows

Read from the source at commit 814b7ca1ba85 on 2026-10-06. The first group decides whether a mod is listed; the rest is for you to weigh. A later commit is not read until the next crawl.

Decides the listing: code you can check
Entry readEvery module hooks/hooks.json names was read.prompt-cache-control/hooks/prompt-cache-control.tsx
ParsesEvery file parses as TypeScript or JavaScript.2 files parsed
All its code is in the modEvery import is a file of the mod or the engine's own module; no packages, no require.all its code is in the mod
ReadableNo minified lines, no eval or new Function, no import of a computed path, no encoded blobs.no minified code, no eval, no encoded blobs
Calls in the openNetwork and programs are reached as $.http.fetch and $.process.run in place, never passed around.every call outside goes through $ in the open
For you to weigh before installing
Where it drawsThe terminal, the desktop app, both, or nothing (hooks only: a guard or a prompt rewriter), as its code says. Shown, never a reason to leave a mod out.draws in the terminal and the desktop app, the same in both
Install lineWhether its repository's own .claude-plugin/marketplace.json lists it, so it installs from that marketplace with the lines on its page.listed in its repository's .claude-plugin/marketplace.json: installs from there
Programs on WindowsWhether every program it starts through $.process exists on Windows (tail, date, open, osascript, /usr/… do not).runs no program a Windows PC lacks
NetworkWhether every fetch names its https host in the code.no network
ProgramsWhether every program it starts is named in the code and is not a shell, an interpreter or a network tool.starts no programs
Reads and sendsWhether it both reads files or environment variables and reaches the network.reaches no network
Keys in the sourceWhether the source holds what looks like an API key or a private key.no keys in the source
LicenceAn open licence (MIT, Apache-2.0, BSD, ISC, Unlicense, 0BSD, CC0) with its text lets modscode show its code; otherwise the code is linked.MIT

This is a reading of the code text, not a security review: it does not run the mod, and it cannot see what a program the mod starts does. The check before installing is yours. How mods are chosen

Install it

Check it yourself before you install it. Mods run unsandboxed, with your permissions, and modscode does not vouch for what this code does. Read the code below (or run claude plugin validate . in its folder), weigh what it reaches, and install the commit you read.

Its repository is its marketplace. Add that marketplace (in the terminal with these commands, or in the desktop app's plugin settings) and install the mod from it:

/plugin marketplace add jakerains/claudemods
/plugin install prompt-cache-control@claudemods

This installs the marketplace's latest commit, which may not be the one read here (814b7ca1ba85). Compare them before you install.

Ask Claude about it

With the modscode connector added, Claude can read this design, what its code reaches and its code, and build on them. Ask it like this:

Read the modscode design "jakerains-prompt-cache-control" with its code and make me a pane like it

Search them from Claude: the MCP connector

Similar designs