usage: the cache hit rate is of the whole prompt, what was written to the cache in it too (Chapin on Discord: with Claude Code started anew at every AmpCode tool round, writing its prompt to the cache again each time, the Usage page still showed a hit rate near 100%) — the Usage page's Overview divided cache_read by input + cache_read, as did the Sessions tab's totals, the TUI's Sessions page and magpie sessions' hitRate; input is what was neither read from the cache nor written to it (the gateway's Usage.prompt() is input + cache_read + cache_write, and the sessions readers subtract both, as alma's and cline's do), so a prompt mostly re-written showed as mostly read. Each now divides by input + cache_read + cache_write, as the tray panel's Usage tab and the Requests tab's strip and rows already did. number-units.test.cjs checks the Overview's rate on its fake ledger (2B read of 1B + 2B + 30M written: 66%, where the old formula said 67%; fails on the old code), TestHitRateCountsCacheWrites checks hitRate (20% of 10 + 20 + 70), and the TUI Sessions page's fixture, which has writes, now says 67% where it said 69%. GUI: number-units, panel-usage, usage-ledger-chart, usage-refresh and mobile-web pass on WebKit; go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.

This commit is contained in:
yetone
2026-10-04 14:14:43 +08:00
parent 2ef38a381a
commit 1565bb9ce1
6 changed files with 27 additions and 8 deletions
+7 -4
View File
@@ -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"));
+3
View File
@@ -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();
+3 -1
View File
@@ -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)
+1 -1
View File
@@ -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)
+4 -2
View File
@@ -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 ""
+9
View File
@@ -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)
}
}