modscode EN

modscode / 図鑑 / cache-clock

cache-clock

Shows how long Claude Code's prompt cache stays warm, how many tokens your next message re-sends, and what it costs now versus after the cache expires.

確かめやすいコードターミナル+デスクトップペインBoxText

何をするか

  • 欄を描く
  • /cache を足す
  • classic.SessionStart・session.start・turn.complete・turn.start ほか 1 個にフックする
  • ターンをまたいで自分の状態を持つ
  • ネットワークに出る

良いところ

  • プロセスを起こさない
  • TypeScript で書かれている
  • ファイルを書かない

作り始めるためのコード

2 種類あります。上のデザインから modscode が書いた雛形と、その Mod 自身のコードのうち描いている部分です。

雛形(register.tsx)

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

// Drawn like cache-clock by XYZ Makes (pane), as modscode read it from the mod's code:
// https://modscode.com/gallery/xyzbuilds-cache-clock/
// 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 pane: open it with $.ui.open({ id: 'my-pane', title: "Prompt cache" })
  on('ui.render', { component: 'Pane', requestId: 'my-pane' }, async ($, e) => {
    const { Box, Text } = $.ui.resolve(e)
    const label = '…'
    const why = '…'
    const bar = '…'
    const tokensText = '…'
    const moneyText = '…'
    const moneyText2 = '…'
    const name = '…'
    const o = '…'
    const hit = '…'
    const requests = '…'
    const durationText = '…'
    const cause = '…'
    const shortModel = '…'
    return (
      <Box flexDirection="column" paddingX={1}>
        <Box flexDirection="row" justifyContent="space-between">
          <Text bold>
            ⏱ Prompt cache
          </Text>
          <Text dimColor>
            {`${label} · ${why}`}
          </Text>
        </Box>
        <Text dimColor>
          ────────
        </Text>
        <Text>
          {`${bar} expired`}
        </Text>
        <Box flexDirection="column" marginTop={1}>
          <Text bold color="blue">
            NEXT MESSAGE
          </Text>
          <Text wrap="wrap">
            {`Re-sends ${tokensText} tokens of the ${tokensText} window.`}
          </Text>
          <Text>
            <Text color="green">
              {`cache warm    ${bar} `}
            </Text>
            <Text>
              {moneyText}
            </Text>
          </Text>
          <Text>
            <Text color="red">
              {`cache expired ${bar} `}
            </Text>
            <Text>
              {moneyText2}
            </Text>
          </Text>
          <Text dimColor wrap="wrap">
            {`${name} (price unknown, Opus 5.5 assumed) at API prices, before Claude's reply. Prices: Anthropic's pricing page, read ‹…`}
          </Text>
        </Box>
        <Box flexDirection="column" marginTop={1}>
          <Text bold color="cyan">
            WHAT TO DO
          </Text>
          <Text wrap="wrap">
            {`• ${o}`}
          </Text>
          <Text wrap="wrap">
            {`• ${o}`}
          </Text>
          <Text wrap="wrap">
            {`• ${o}`}
          </Text>
        </Box>
        <Text dimColor>
          ────────
        </Text>
        <Box flexDirection="column">
          <Text bold color="magenta">
            THIS SESSION
          </Text>
          <Text>
            <Text color="red">
              ○
            </Text>
            <Text color="red">
              ○
            </Text>
            <Text color="red">
              ○
            </Text>
          </Text>
          <Text wrap="wrap">
            {`hit ${hit}% · ${requests} requests · ${tokensText} read from cache · ${tokensText} written · ≈ ${moneyText} at API prices`}
          </Text>
          <Box flexDirection="column" marginTop={1}>
            <Text dimColor>
              Rebuilt:
            </Text>
            <Text wrap="wrap">
              {`${durationText} ago · ${tokensText} tokens · ${cause} · ${moneyText}`}
            </Text>
            <Text wrap="wrap">
              {`${durationText} ago · ${tokensText} tokens · ${cause} · ${moneyText}`}
            </Text>
            <Text wrap="wrap">
              {`${durationText} ago · ${tokensText} tokens · ${cause} · ${moneyText}`}
            </Text>
          </Box>
        </Box>
        <Text dimColor>
          ────────
        </Text>
        <Text wrap="wrap">
          <Text bold>
            {"Resets the cache: "}
          </Text>
          <Text>
            /model, fast mode on, adding an MCP server, /compact, a long pause.
          </Text>
        </Text>
        <Text wrap="wrap">
          <Text bold>
            {"Keeps it: "}
          </Text>
          <Text>
            editing files, skills, /rewind, subagents, permission modes.
          </Text>
        </Text>
        <Text dimColor wrap="wrap">
          {`Last request on ${shortModel} · context ${tokensText} tokens.`}
        </Text>
      </Box>
    )
  })
}

Mod 自身のコード

Mod のライセンス(MIT)に従って載せています。全文は下にあります。写すときは著作権表示を残してください。

hooks/register.tsx 383〜387 行 · ペイン
  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
    const clock = await read($, clockAtom)
    const now = (await read($, nowAtom)) || (await $.clock.now())
    return paneView($, e, clock, now, Math.max(20, e.props.bodyColumns - 2))
  })
hooks/register.tsx 213〜311 行 · ペイン
function paneView($: EngineInterface, e: View, clock: Clock, now: number, width: number) {
  const { Box, Text } = $.ui.resolve(e)
  const rule = '─'.repeat(width)
  const last = clock.last
  const header = (
    <Box flexDirection="row" justifyContent="space-between">
      <Text bold>⏱ Prompt cache</Text>
      <Text dimColor>{`${clock.ttl.label} · ${clock.ttl.why}`}</Text>
    </Box>
  )
  if (!last) {
    return (
      <Box flexDirection="column" paddingX={1}>
        {header}
        <Text dimColor>{rule}</Text>
        <Text dimColor>Nothing cached yet: the first reply starts the clock.</Text>
      </Box>
    )
  }
  const price = priceFor(last.model)
  const next = nextCost(last, price, clock.ttl.ms)
  const phase = phaseOf(last, clock.ttl, clock.isWorking, now)
  const left = Math.max(0, last.at + clock.ttl.ms - now)
  const barWidth = Math.max(10, width - 22)
  const costWidth = Math.max(10, width - 26)
  const t = clock.totals
  const hit = Math.round((100 * t.read) / Math.max(1, t.read + t.write + t.input))
  const fresh = clock.base > 0 ? freshCost(clock.base, price, clock.ttl.ms) : undefined
  const compact = compactCost(last, price)
  const options: string[] =
    phase === 'expired'
      ? [
          `Continue: ≈ ${moneyText(next.cold)} for the next message, then ≈ ${moneyText(next.warm)} a message while warm.`,
          ...(fresh !== undefined ? [`Start fresh with /clear: ≈ ${moneyText(fresh)}. Claude forgets this conversation; save what you need to a file first.`] : []),
          `/compact first: re-reads all ${tokensText(next.tokens)} tokens without the cache to summarize them, then each message is small.`,
        ]
      : [
          `Keep going: ≈ ${moneyText(next.warm)} a short message while warm.`,
          `Stepping away for longer than ${phase === 'working' ? (clock.ttl.ms >= HOUR ? 'an hour' : '5 minutes') : durationText(left)}? /compact now ≈ ${moneyText(compact)}; once cold, the next message is ≈ ${moneyText(next.cold)}.`,
        ]
  return (
    <Box flexDirection="column" paddingX={1}>
      {header}
      <Text dimColor>{rule}</Text>
      <Text color={COLOR[phase]}>
        {phase === 'expired' ? `${bar(0, barWidth)} expired` : `${bar(phase === 'working' ? 1 : left / clock.ttl.ms, barWidth)} ${phase === 'working' ? 'in use' : `${durationText(left)} left`}`}
      </Text>
      <Box flexDirection="column" marginTop={1}>
        <Text bold color="blue">NEXT MESSAGE</Text>
        <Text wrap="wrap">{`Re-sends ${tokensText(next.tokens)} tokens${clock.window ? ` of the ${tokensText(clock.window)} window` : ''}.`}</Text>
        <Text>
          <Text color="green">{`cache warm    ${bar(next.warm / Math.max(next.cold, 1e-9), costWidth)} `}</Text>
          <Text>{moneyText(next.warm)}</Text>
        </Text>
        <Text>
          <Text color="red">{`cache expired ${bar(1, costWidth)} `}</Text>
          <Text>{moneyText(next.cold)}</Text>
        </Text>
        <Text dimColor wrap="wrap">{`${price.name}${price.isGuess ? ' (price unknown, Opus 5.5 assumed)' : ''} at API prices, before Claude's reply. Prices: ${pricedAt ? `Anthropic's pricing page, read ${durationText(now - pricedAt)} ago` : `built-in list of ${BUILT_IN_DATE}`}.`}</Text>
      </Box>
      <Box flexDirection="column" marginTop={1}>
        <Text bold color="cyan">WHAT TO DO</Text>
        {options.map((o, i) => (
          <Text key={`o-${i}`} wrap="wrap">{`• ${o}`}</Text>
        ))}
      </Box>
      <Text dimColor>{rule}</Text>
      <Box flexDirection="column">
        <Text bold color="magenta">THIS SESSION</Text>
        <Text>
          {clock.history.map((r, i) => (
            <Text key={`h-${i}`} color={r.isMiss ? 'red' : 'green'}>
              {r.isMiss ? '○' : '●'}
            </Text>
          ))}
        </Text>
        <Text wrap="wrap">{`hit ${hit}% · ${t.requests} requests · ${tokensText(t.read)} read from cache · ${tokensText(t.write)} written · ≈ ${moneyText(t.usd)} at API prices`}</Text>
        {clock.misses.length > 0 && (
          <Box flexDirection="column" marginTop={1}>
            <Text dimColor>Rebuilt:</Text>
            {clock.misses.map((m, i) => (
              <Text key={`m-${i}`} wrap="wrap">{`${durationText(now - m.at)} ago · ${tokensText(m.write)} tokens · ${m.cause ?? 'unknown'} · ${moneyText(m.usd)}`}</Text>
            ))}
          </Box>
        )}
      </Box>
      <Text dimColor>{rule}</Text>
      <Text wrap="wrap">
        <Text bold>Resets the cache: </Text>
        <Text>/model, fast mode on, adding an MCP server, /compact, a long pause.</Text>
      </Text>
      <Text wrap="wrap">
        <Text bold>Keeps it: </Text>
        <Text>editing files, skills, /rewind, subagents, permission modes.</Text>
      </Text>
      <Text dimColor wrap="wrap">{`Last request on ${shortModel(last.model)} · context ${tokensText(contextOf(last))} tokens.`}</Text>
    </Box>
  )
}
ライセンスの全文(MIT)
MIT License

Copyright (c) 2026 XYZ Makes LLC

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.

コードが呼ぶもの

通信環境変数保存

コードから読み取ったデザイン

読み取ったコミットのソースから描いたもので、動かした結果ではありません。一覧は 3 回描き、条件はデスクトップアプリで描く最初の分岐を取り、動かしたときにだけ決まる値は仮の例(点線の下線)にするか省きます。色と書体はこのサイトのものです。

コードから読めること

2026-10-06 に読んだコミット 1447f239ed52 のソースから。最初のまとまりが載せる条件で、残りは自分で判断するための材料です。後のコミットは、次に読むまで見ていません。

載せる条件: 確かめやすいコード
入口を読んだhooks/hooks.json が名指すモジュールをすべて読んだ。hooks/register.tsx
解析できるすべてのファイルが TypeScript か JavaScript として解析できる。3 ファイルを解析
コードが Mod の中で完結import は Mod 自身のファイルかエンジンのモジュールだけ。パッケージも require もない。コードはすべて Mod の中
読める圧縮した行・eval・new Function・計算した道の import()・符号化した塊がない。圧縮・eval・符号化した塊なし
呼び出しが見える通信とプログラム起動はその場で $.http.fetch・$.process.run と書き、他へ渡さない。外への呼び出しはすべてその場の $ で
入れる前に自分で判断する材料
描く場所コードから読んだ、描く先。ターミナル・デスクトップアプリ・両方・何も描かない(フックだけ: 見張りやプロンプトの書き換え)のどれか。出すだけで、外す理由にはしない。ターミナルとデスクトップアプリに同じように描く
入れる手順リポジトリ自身の .claude-plugin/marketplace.json にこの Mod が載っていて、ページに書いた手順でそのマーケットプレイスから入れられるか。リポジトリの .claude-plugin/marketplace.json に載っておらず、入れる手順は出せない
Windows のプログラム$.process で起動するプログラムが Windows にあるか(tail・date・open・osascript・/usr/… などは無い)。Windows にないプログラムを使わない
通信fetch の送り先(https のホスト)がコードに書いてあるか。https://platform.claude.com
プログラム起動するプログラムの名前がコードに書いてあり、シェル・インタプリタ・通信用の道具でないか。プログラムを起動しない
読んで送るファイルや環境変数を読み、かつ通信するか。読んだうえで通信する
ソースの鍵ソースに API キーや秘密鍵らしきものがあるか。ソースに鍵なし
ライセンスオープンなライセンス(MIT・Apache-2.0・BSD・ISC・Unlicense・0BSD・CC0)で全文があれば、modscode にコードを載せる。違えばリンクだけ。MIT

コードの文面を読んだだけで、安全の審査ではありません。動かしてはおらず、起動するプログラムの中で何が起きるかも見えません。入れる前の確認はあなたの役目です。 載せる条件

入れる

入れる前に、必ず自分で確かめてください。Mod はサンドボックスなしで、あなたの権限で動きます。modscode はこのコードの安全を保証しません。下のコードを読み(またはフォルダーで claude plugin validate .)、触るものを見て、読んだコミットを入れてください。

リポジトリの .claude-plugin/marketplace.json にこの Mod が載っていないため、入れる手順は出せません。コードとして読むか、作者の README に従ってください。

Claude に聞く

modscode のコネクタを入れると、Claude がこのデザイン・コードが触るもの・コードを読んで、それをもとに作れます。たとえば:

modscode のデザイン「xyzbuilds-cache-clock」をコードごと読んで、同じようなペインを作って

Claude から探す: MCP コネクタ

似たデザイン