From 1565bb9ce148935b2d2a3a6d51e9b0d9e4f83bfb Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 4 Oct 2026 14:13:03 +0800 Subject: [PATCH] =?UTF-8?q?usage:=20the=20cache=20hit=20rate=20is=20of=20t?= =?UTF-8?q?he=20whole=20prompt,=20what=20was=20written=20to=20the=20cache?= =?UTF-8?q?=20in=20it=20too=20(Chapin=20on=20Discord:=20with=20Claude=20Co?= =?UTF-8?q?de=20started=20anew=20at=20every=20AmpCode=20tool=20round,=20wr?= =?UTF-8?q?iting=20its=20prompt=20to=20the=20cache=20again=20each=20time,?= =?UTF-8?q?=20the=20Usage=20page=20still=20showed=20a=20hit=20rate=20near?= =?UTF-8?q?=20100%)=20=E2=80=94=20the=20Usage=20page's=20Overview=20divide?= =?UTF-8?q?d=20cache=5Fread=20by=20input=20+=20cache=5Fread,=20as=20did=20?= =?UTF-8?q?the=20Sessions=20tab's=20totals,=20the=20TUI's=20Sessions=20pag?= =?UTF-8?q?e=20and=20magpie=20sessions'=20hitRate;=20input=20is=20what=20w?= =?UTF-8?q?as=20neither=20read=20from=20the=20cache=20nor=20written=20to?= =?UTF-8?q?=20it=20(the=20gateway's=20Usage.prompt()=20is=20input=20+=20ca?= =?UTF-8?q?che=5Fread=20+=20cache=5Fwrite,=20and=20the=20sessions=20reader?= =?UTF-8?q?s=20subtract=20both,=20as=20alma's=20and=20cline's=20do),=20so?= =?UTF-8?q?=20a=20prompt=20mostly=20re-written=20showed=20as=20mostly=20re?= =?UTF-8?q?ad.=20Each=20now=20divides=20by=20input=20+=20cache=5Fread=20+?= =?UTF-8?q?=20cache=5Fwrite,=20as=20the=20tray=20panel's=20Usage=20tab=20a?= =?UTF-8?q?nd=20the=20Requests=20tab's=20strip=20and=20rows=20already=20di?= =?UTF-8?q?d.=20number-units.test.cjs=20checks=20the=20Overview's=20rate?= =?UTF-8?q?=20on=20its=20fake=20ledger=20(2B=20read=20of=201B=20+=202B=20+?= =?UTF-8?q?=2030M=20written:=2066%,=20where=20the=20old=20formula=20said?= =?UTF-8?q?=2067%;=20fails=20on=20the=20old=20code),=20TestHitRateCountsCa?= =?UTF-8?q?cheWrites=20checks=20hitRate=20(20%=20of=2010=20+=2020=20+=2070?= =?UTF-8?q?),=20and=20the=20TUI=20Sessions=20page's=20fixture,=20which=20h?= =?UTF-8?q?as=20writes,=20now=20says=2067%=20where=20it=20said=2069%.=20GU?= =?UTF-8?q?I:=20number-units,=20panel-usage,=20usage-ledger-chart,=20usage?= =?UTF-8?q?-refresh=20and=20mobile-web=20pass=20on=20WebKit;=20go=20vet,?= =?UTF-8?q?=20builds=20(darwin,=20GOOS=3Dlinux,=20GOOS=3Dwindows)=20and=20?= =?UTF-8?q?go=20test=20-tags=20nogui=20-count=3D1=20./...=20under=20a=20te?= =?UTF-8?q?mp=20HOME=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gui/assets/app.js | 11 +++++++---- internal/gui/tests/number-units.test.cjs | 3 +++ internal/tui/sessions.go | 4 +++- internal/tui/sessions_test.go | 2 +- sessions_cli.go | 6 ++++-- sessions_cli_test.go | 9 +++++++++ 6 files changed, 27 insertions(+), 8 deletions(-) diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 20ca05bef..fe260a3a9 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -11867,9 +11867,11 @@ function renderUsage() { tile(fmtN(tokensOf(u)), t("tokens"), t("{a} in · {b} out", { a: fmtN(u.input), b: fmtN(u.output) })); // cache reads are billed at a fraction of input, so how much of the prompt // came from cache is the number that explains the bill; input here already - // excludes the cached tokens (the gateway subtracts them). The written - // count is secondary and only fits in the tooltip. - const promptTokens = u.input + u.cache_read; + // excludes the cached tokens, read and written (the gateway subtracts + // them), so what was written counts in the prompt too: left out, a prompt + // written to the cache again at every turn showed a rate near 100%. The + // written count is secondary and only fits in the tooltip. + const promptTokens = u.input + u.cache_read + u.cache_write; const hit = u.cache_read && promptTokens ? t("hit rate {p}", { p: Math.round(100 * u.cache_read / promptTokens) + "%" }) : ""; tile(fmtN(u.cache_read), t("cache read"), hit, u.cache_write ? t("{n} written", { n: fmtN(u.cache_write) }) : ""); tile(fmtN(u.reasoning), t("reasoning"), t("inside output")); @@ -13411,7 +13413,8 @@ function renderSessions() { t("Tokens in and out per session: the middle session's, and what nine in ten stay under"), stale ? "stale" : ""); tile(fmtN(tot.input + tot.output), t("tokens"), t("{a} in · {b} out", { a: fmtN(tot.input), b: fmtN(tot.output) })); tile(c ? "≈" + c : "—", t("cost"), t("at effective prices"), unpricedNote); - const prompt = tot.input + tot.cache_read; + // input leaves out what was read from the cache and written to it + const prompt = tot.input + tot.cache_read + tot.cache_write; tile(fmtN(tot.cache_read), t("cache read"), tot.cache_read && prompt ? t("hit rate {p}", { p: Math.round(100 * tot.cache_read / prompt) + "%" }) : "", tot.cache_write ? t("{n} written", { n: fmtN(tot.cache_write) }) : ""); tile(active == null ? "—" : fmtDur(active), t("active"), active == null ? t("not kept by model") : t(days === 1 ? "on {n} day" : "on {n} days", { n: days }), t("The time the sessions were at work: the pauses between one message and the next, each under five minutes")); diff --git a/internal/gui/tests/number-units.test.cjs b/internal/gui/tests/number-units.test.cjs index 396637c76..6267cf4d9 100644 --- a/internal/gui/tests/number-units.test.cjs +++ b/internal/gui/tests/number-units.test.cjs @@ -113,6 +113,9 @@ async function rest(p, western, what) { const tiles = await p.locator("#stats .kpi").allInnerTexts(); assert.equal(tiles[0].split("\n")[0], western ? "1.54B" : "15.4 亿", what + ": the Overview's tokens"); for (const x of tiles) if (/\d/.test(x)) assert(has(x, western) || !/[万亿KMB]/.test(x), `${what}: Overview tile "${x}"`); + // the hit rate is of the whole prompt, what was written to the cache in + // it too: 2B read of 1B + 2B + 30M written, not of 1B + 2B (67%) + assert(tiles.some((x) => /(hit rate|命中率) 66%/.test(x)), `${what}: the Overview's hit rate in ${JSON.stringify(tiles)}`); assert.equal((await p.locator("#chart .peak").textContent()).trim(), western ? "1.46B" : "14.6 亿", what + ": the Overview chart's peak"); await p.locator("#usageTab .opt").nth(1).click(); diff --git a/internal/tui/sessions.go b/internal/tui/sessions.go index dbe774a1c..e14cf3350 100644 --- a/internal/tui/sessions.go +++ b/internal/tui/sessions.go @@ -324,7 +324,9 @@ func sessLines(st sessions.Stats, rng int, model, folder string, byCost bool, wi c = sOK.Render(fmtCost(usage.Totals{Cost: r.Cost, Unpriced: len(r.Unpriced)})) } hit := "" - if p := r.Input + r.CacheRead; r.CacheRead > 0 && p > 0 { + // of all the prompts came to: Input leaves out what was read from the + // cache and what was written to it + if p := r.Input + r.CacheRead + r.CacheWrite; r.CacheRead > 0 && p > 0 { hit = sMuted.Render(fmt.Sprintf(" (%d%% hit)", 100*r.CacheRead/p)) } active := sessions.Duration(r.Active) diff --git a/internal/tui/sessions_test.go b/internal/tui/sessions_test.go index 89932d93f..b1c57a1a3 100644 --- a/internal/tui/sessions_test.go +++ b/internal/tui/sessions_test.go @@ -55,7 +55,7 @@ func TestSessionsPage(t *testing.T) { t.Fatalf("page %d, range %d", m.page, m.srange) } v := m.View() - for _, want := range []string{"5 sessions", " all ", "all models", "all folders", "11.9K tokens", "cache read 25.2K (69% hit)", "active 7m on 2 days", + for _, want := range []string{"5 sessions", " all ", "all models", "all folders", "11.9K tokens", "cache read 25.2K (67% hit)", "active 7m on 2 days", "█", "Sep 20", "tokens · a day", "models", "gpt-6-astra", "claude-opus-5-5", "folders", "/work/app", "/work/it's", "M model", "f folder"} { if !strings.Contains(v, want) { t.Errorf("missing %q in\n%s", want, v) diff --git a/sessions_cli.go b/sessions_cli.go index 94d05232c..86bf89a12 100644 --- a/sessions_cli.go +++ b/sessions_cli.go @@ -377,9 +377,11 @@ func rollupCost(c float64, unpriced []string) string { return cost(stats.Totals{Cost: c, Unpriced: len(unpriced)}) } -// hitRate is how much of the prompts came from the cache. +// hitRate is how much of the prompts came from the cache: of all they +// came to, what was read, written to the cache and neither (Input, which +// leaves out both). func hitRate(t sessions.Tokens) string { - if p := t.Input + t.CacheRead; t.CacheRead > 0 && p > 0 { + if p := t.Input + t.CacheRead + t.CacheWrite; t.CacheRead > 0 && p > 0 { return muted.Render(fmt.Sprintf(" (%d%% hit)", 100*t.CacheRead/p)) } return "" diff --git a/sessions_cli_test.go b/sessions_cli_test.go index 245f147e0..510273bdc 100644 --- a/sessions_cli_test.go +++ b/sessions_cli_test.go @@ -130,3 +130,12 @@ func TestSessionsCmd(t *testing.T) { t.Error("--days week taken") } } + +// The hit rate is of all the prompts came to: what was written to the +// cache counts in it, as Input leaves it out — a prompt written again at +// every turn showed near 100% otherwise. +func TestHitRateCountsCacheWrites(t *testing.T) { + if got := hitRate(sessions.Tokens{Input: 10, CacheRead: 20, CacheWrite: 70}); !strings.Contains(got, "(20% hit)") { + t.Errorf("hitRate = %q, want 20%%", got) + } +}