Something went wrong. Try again.
a digital entity named phi that roams bsky phi.zzstoatzz.io
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501<script lang="ts"> // prompt cache instrument. // // the question this answers is "is the caching strategy in agent.py worth // it", so the headline is money, not tokens: what the input bill would // have been with caching off, against what phi was actually billed. // every label here names a thing that happened — no jargon that needs a // glossary, and no facts restated in prose (the TTLs and prices come // from the API, which reads the same CACHE_TTLS dict the agent // configures from). import { onMount } from 'svelte'; import { getCacheStability } from '$lib/api'; import type { CacheRun, CacheStability } from '$lib/types'; import { relativeWhen, whenTooltip } from '$lib/time';
let data = $state<CacheStability | null>(null); let loaded = $state(false); let now = $state(Date.now()); let inFlight = false; let expanded = $state<string | null>(null); // the per-run list is the evidence, not the reading — closed until asked for let showRuns = $state(false);
async function load() { if (inFlight) return; inFlight = true; const latest = await getCacheStability(); if (latest) data = latest; loaded = true; inFlight = false; } onMount(() => { void load(); const clock = setInterval(() => { now = Date.now(); }, 10_000); const refreshVisible = () => { if (document.visibilityState === 'visible') void load(); }; const poll = setInterval(refreshVisible, 60_000); document.addEventListener('visibilitychange', refreshVisible); return () => { clearInterval(clock); clearInterval(poll); document.removeEventListener('visibilitychange', refreshVisible); }; });
const pct = (n: number) => `${Math.round(n * 100)}%`; const num = (n: number) => n.toLocaleString('en-US');
function tokens(n: number): string { if (n >= 1000) return `${(n / 1000).toFixed(n >= 10_000 ? 0 : 1)}k`; return String(n); }
const total = (r: CacheRun) => r.cache_read + r.cache_write + r.uncached; const share = (part: number, r: CacheRun) => (total(r) ? (part / total(r)) * 100 : 0);
// each segment says what it is, how much of the run it was, and what it // cost — the thing you actually want when you hover a colored bar function segTitle(kind: 'reused' | 'stored' | 'full', part: number, r: CacheRun): string { if (!data?.prices || r.saved === null) return `${num(part)} tokens · ${Math.round(share(part, r))}% of this run; cost estimate unavailable`; const rate = kind === 'reused' ? data.prices.read : kind === 'stored' ? data.prices.write : 1; const what = kind === 'reused' ? 'read back from cache' : kind === 'stored' ? 'written into the cache' : 'sent uncached'; return `${num(part)} tokens ${what} — ${Math.round(share(part, r))}% of this run, billed at ${rate}× (${num(Math.round(part * rate))} tokens' worth)`; }
const runKey = (r: CacheRun) => `${r.started_at}:${r.label}`;</script>
<section class="cache"> <h2>prompt cache</h2>
{#if !loaded} <div class="status">loading…</div> {:else if !data || !data.runs.length} <div class="status">no runs recorded yet</div> {:else} {#if data.strategy} <p class="strategy"> caching what phi re-sends every request: {#each Object.entries(data.strategy) as [what, ttl], i (what)}<span class="ttl" >{what.replace('_', ' ')} <b>{ttl}</b></span >{i < Object.entries(data.strategy).length - 1 ? ' · ' : ''}{/each} </p>
{/if} <div class="headline"> <div class="verdict"> {#if data.saved !== null && data.billed_tokens !== null} <span class="big">{pct(data.saved)}</span> <span class="big-label">estimated input savings</span> <span class="sub"> {tokens(data.uncached_cost_tokens)} tokens of context, billed as {tokens(data.billed_tokens)} — across {data.window_runs} run{data.window_runs === 1 ? '' : 's'} </span> {:else} <span class="big">{pct(data.hit_rate)}</span> <span class="big-label">input served from cache</span> <span class="sub">Cost estimate unavailable for mixed models, other providers, or older records without provider metadata.</span> {/if} </div> </div>
<div class="facts"> <div class="fact"> <span class="fact-n">{data.warm_starts}<span class="of">/{data.window_runs}</span></span> <span class="fact-t" >runs reused at least 1,024 input tokens on their first request</span > </div> <div class="fact {data.collapses ? 'fact-bad' : ''}"> <span class="fact-n">{data.collapses}</span> <span class="fact-t" >Anthropic requests showed a large drop in cache reads; a changed prefix or expiry may explain it</span > </div> </div>
<button class="runs-toggle" onclick={() => (showRuns = !showRuns)} aria-expanded={showRuns}> {showRuns ? 'hide' : 'show'} the last {data.runs.length} runs </button> {#if showRuns} <div class="legend"> <span><i class="sw sw-read"></i>reused</span> <span><i class="sw sw-write"></i>stored</span> <span><i class="sw sw-cold"></i>uncached</span> </div>
<ul class="runs"> {#each data.runs as run (runKey(run))} <li class="run"> <div class="run-head"> <button class="opener" onclick={() => (expanded = expanded === runKey(run) ? null : runKey(run))} title="show each model request in this run" > <span class="start" class:warm={run.warm_start}> {run.warm_start ? 'warm' : 'cold'} </span> <span class="label">{run.label}</span> </button> <span class="when" title={whenTooltip(run.started_at)} >{relativeWhen(run.started_at, now)}</span > <span class="reqs">{run.requests} req · {tokens(total(run))}</span> {#if run.saved !== null}<span class="saved" class:bad={run.saved < 0.2}>{pct(run.saved)} estimated savings</span>{:else}<span class="saved">cost unavailable</span>{/if} {#if run.trace_url} <a class="trace" href={run.trace_url} target="_blank" rel="noopener" title="open this run's trace in logfire — every tool call it made">trace ↗</a > {/if} </div>
<div class="bar"> <span class="seg seg-read" style="width:{share(run.cache_read, run)}%" title={segTitle('reused', run.cache_read, run)} ></span> <span class="seg seg-write" style="width:{share(run.cache_write, run)}%" title={segTitle('stored', run.cache_write, run)} ></span> <span class="seg seg-cold" style="width:{share(run.uncached, run)}%" title={segTitle('full', run.uncached, run)} ></span> </div>
{#if run.collapses} <div class="collapse-note"> lost the cache {run.collapses} {run.collapses > 1 ? 'times' : 'time'} mid-run — the start of the prompt changed, or the provider's copy expired underneath it </div> {/if}
{#if expanded === runKey(run)} <table class="samples"> <thead> <tr> <th>request</th> <th>reused</th> <th>stored</th> <th>total input</th> <th>since last</th> <th></th> </tr> </thead> <tbody> {#each run.samples as s, i (s.at + i)} <tr class:collapsed={s.collapsed}> <td>{i + 1}</td> <td>{tokens(s.cache_read)}</td> <td>{tokens(s.cache_write)}</td> <td>{tokens(s.input_tokens)}</td> <td>{s.gap_seconds === null ? 'first' : `${Math.round(s.gap_seconds)}s`}</td> <td class="verdict-cell"> {#if s.collapsed} {s.maybe_expiry ? 'lost the cache (probably expired)' : 'lost the cache'} {/if} </td> </tr> {/each} </tbody> </table> {/if} </li> {/each} </ul> {/if} {/if}</section>
<style> .runs-toggle { background: none; border: 1px solid var(--line-mid); color: var(--text-mid); font-family: var(--font-mono); font-size: 0.78rem; padding: 0.15rem 0.6rem; cursor: pointer; margin-top: 0.5rem; } .cache { margin-top: 3rem; border-top: 1px solid var(--line-dim); padding-top: 1.5rem; } h2 { font-family: var(--font-chrome); text-transform: uppercase; letter-spacing: 0.12em; font-weight: 500; font-size: 1.1rem; margin: 0 0 0.75rem; color: var(--hud-hot); } .status { color: var(--text-dim); font-family: var(--font-mono); }
.strategy { color: var(--text-dim); font-size: 0.85rem; margin: 0 0 1.25rem; } .ttl b { font-family: var(--font-mono); color: var(--scan-hot); font-weight: 400; }
.headline { margin-bottom: 1.25rem; } .big { font-family: var(--font-chrome); font-size: 2.6rem; line-height: 1; color: var(--scan-hot); } .big-label { font-family: var(--font-chrome); text-transform: uppercase; letter-spacing: 0.1em; color: var(--text); margin-left: 0.5rem; } .sub { display: block; color: var(--text-dim); font-size: 0.8rem; margin-top: 0.4rem; }
.facts { display: flex; gap: 1.5rem; margin-bottom: 1.25rem; } .fact { display: flex; gap: 0.6rem; align-items: baseline; flex: 1; max-width: 34ch; } .fact-n { font-family: var(--font-chrome); font-size: 1.5rem; line-height: 1; color: var(--scan-hot); white-space: nowrap; } .fact-n .of { color: var(--text-dim); font-size: 0.6em; } .fact-bad .fact-n { color: var(--warn-hot); } .fact-t { font-size: 0.75rem; color: var(--text-dim); line-height: 1.35; }
.legend { display: flex; gap: 1rem; font-size: 0.75rem; color: var(--text-dim); margin-bottom: 0.75rem; font-family: var(--font-mono); } .legend span { display: flex; align-items: center; gap: 0.35rem; cursor: help; } .sw { width: 10px; height: 10px; display: inline-block; } .sw-read, .seg-read { background: var(--scan-mid); } .sw-write, .seg-write { background: var(--hud-mid); } .sw-cold, .seg-cold { background: var(--text-dim); }
.runs { list-style: none; margin: 0; padding: 0; } .run { padding: 0.5rem 0; border-bottom: 1px solid var(--grid); } .run-head { display: flex; align-items: baseline; gap: 0.5rem; padding: 0 2px 0.35rem 0; } /* the label is the only elastic part — everything to its right is a * fixed readout and must never be pushed off the edge */ .when, .reqs, .saved, .trace { flex-shrink: 0; } .opener { display: flex; align-items: baseline; gap: 0.5rem; flex: 1; min-width: 0; background: none; border: none; padding: 0; color: inherit; font: inherit; text-align: left; cursor: pointer; } .opener:hover .label { color: var(--hud-hot); } .start { font-family: var(--font-mono); font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-dim); border: 1px solid var(--line-dim); padding: 0 0.3rem; } .start.warm { color: var(--scan-hot); border-color: var(--line-scan); } .label { /* min-width:0 is what lets a flex item shrink below its content * width — without it the row overflows and the trace link clips */ min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .when, .reqs { font-family: var(--font-mono); font-size: 0.72rem; color: var(--text-dim); white-space: nowrap; } .saved { font-family: var(--font-mono); font-size: 0.78rem; color: var(--scan-hot); white-space: nowrap; } .saved.bad { color: var(--warn-hot); } .trace { font-family: var(--font-mono); font-size: 0.7rem; color: var(--text-dim); text-decoration: none; white-space: nowrap; } .trace:hover { color: var(--hud-hot); }
.bar { display: flex; height: 8px; background: var(--bg-elev); overflow: hidden; } .seg { height: 100%; cursor: help; }
.collapse-note { font-size: 0.75rem; color: var(--warn); margin-top: 0.35rem; }
.samples { width: 100%; margin-top: 0.6rem; border-collapse: collapse; font-family: var(--font-mono); font-size: 0.72rem; color: var(--text-mid); } .samples th { text-align: right; font-weight: 400; color: var(--text-dim); border-bottom: 1px solid var(--grid); padding: 0.15rem 0.4rem; } .samples td { text-align: right; padding: 0.15rem 0.4rem; } .samples tr.collapsed td { color: var(--warn); } .verdict-cell { text-align: left; white-space: nowrap; }
@media (max-width: 640px) { .facts { flex-direction: column; gap: 0.75rem; } .when { display: none; } }</style>