Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-7638 fix(usage): include cache writes in hit rate (#8798)

Multica Eve470fd1fc0dcb
diff --git a/packages/views/issues/components/issue-usage-dialog.test.tsx b/packages/views/issues/components/issue-usage-dialog.test.tsx
index 337b6bb1f1..1025a62121 100644
--- a/packages/views/issues/components/issue-usage-dialog.test.tsx
+++ b/packages/views/issues/components/issue-usage-dialog.test.tsx
@@ -56,7 +56,24 @@ beforeEach(() => {
afterEach(cleanup);
describe("IssueUsageDialog", () => {
- it("floors the cache hit rate instead of rounding it up to 100%", () => {
+ it("includes cache writes in the cache hit-rate denominator", () => {
+ open([
+ makeTask({
+ usage: [
+ usage({
+ input_tokens: 0,
+ output_tokens: 0,
+ cache_read_tokens: 72_000,
+ cache_write_tokens: 28_000,
+ }),
+ ],
+ }),
+ ]);
+
+ expect(screen.getByText(/72% hit rate/)).toBeInTheDocument();
+ });
+
+ it("does not round an incomplete cache hit rate up to 100%", () => {
// 99.55% — rounding would print "100% hit rate" and claim every token came
// from cache on an issue that plainly read some fresh input.
open([
@@ -77,6 +94,24 @@ describe("IssueUsageDialog", () => {
expect(screen.getByText(/100% hit rate/)).toBeInTheDocument();
});
+ it("shows an unavailable hit rate when no input-side tokens were reported", () => {
+ open([
+ makeTask({
+ usage: [
+ usage({
+ input_tokens: 0,
+ output_tokens: 1_000,
+ cache_read_tokens: 0,
+ cache_write_tokens: 0,
+ }),
+ ],
+ }),
+ ]);
+
+ expect(screen.getByText("—")).toBeInTheDocument();
+ expect(screen.queryByText(/0% hit rate/)).not.toBeInTheDocument();
+ });
+
it("keeps the full model list reachable when the cell truncates", () => {
// A run that spilled across models carries ids long enough to set the
// table's width on their own; the cell is capped, so the untruncated list
diff --git a/packages/views/issues/components/issue-usage-dialog.tsx b/packages/views/issues/components/issue-usage-dialog.tsx
index fa306f8ade..e61be2fc7b 100644
--- a/packages/views/issues/components/issue-usage-dialog.tsx
+++ b/packages/views/issues/components/issue-usage-dialog.tsx
@@ -15,6 +15,7 @@ import { ActorAvatar } from "../../common/actor-avatar";
import { useT } from "../../i18n";
import { formatDuration } from "../../agents/components/agent-activity-hover-content";
import {
+ cacheHitRatePercent,
collectUnmappedModels,
formatTokens,
formatUsd,
@@ -84,14 +85,10 @@ export function IssueUsageDialog({
[priced, pricings],
);
- // Floor, not round: on a cache-heavy issue 99.55% rounds to "100% hit rate",
- // which claims every single token came from cache. Flooring only ever says
- // 100% when it is actually 100%, and one percentage point of pessimism is
- // cheaper than an impossible-looking number.
const cacheHitRate =
- total && total.input + total.cacheRead > 0
- ? Math.floor((total.cacheRead / (total.input + total.cacheRead)) * 100)
- : 0;
+ total == null
+ ? null
+ : cacheHitRatePercent(total.input, total.cacheRead, total.cacheWrite);
return (
<Dialog open={open} onOpenChange={onOpenChange}>
@@ -134,10 +131,14 @@ export function IssueUsageDialog({
label={t(($) => $.usage_detail.kpi_cache)}
value={formatUsd(total.cacheSavings)}
accent={total.cacheSavings > 0 ? "success" : "default"}
- hint={t(($) => $.usage_detail.kpi_cache_hint, {
- pct: cacheHitRate,
- reads: formatTokens(total.cacheRead),
- })}
+ hint={
+ cacheHitRate == null
+ ? "—"
+ : t(($) => $.usage_detail.kpi_cache_hint, {
+ pct: cacheHitRate,
+ reads: formatTokens(total.cacheRead),
+ })
+ }
/>
<KpiCard
label={t(($) => $.usage_detail.kpi_tokens)}
diff --git a/packages/views/runtimes/components/usage-section.test.tsx b/packages/views/runtimes/components/usage-section.test.tsx
index f16b2397c7..7f903acfe8 100644
--- a/packages/views/runtimes/components/usage-section.test.tsx
+++ b/packages/views/runtimes/components/usage-section.test.tsx
@@ -196,6 +196,51 @@ describe("UsageSection — Viewing timezone wiring", () => {
});
});
+describe("UsageSection — cache hit rate", () => {
+ beforeEach(() => {
+ usageOverride.rows = null;
+ });
+
+ it("includes cache writes in the denominator", () => {
+ usageOverride.rows = [
+ {
+ runtime_id: "r-1",
+ date: new Date().toISOString().slice(0, 10),
+ provider: "anthropic",
+ model: "claude-sonnet-4-6",
+ input_tokens: 0,
+ output_tokens: 0,
+ cache_read_tokens: 72_000,
+ cache_write_tokens: 28_000,
+ },
+ ];
+
+ render(<UsageSection runtime={RUNTIME} />, { wrapper: Wrapper });
+
+ expect(screen.getByText(/72% hit/)).toBeInTheDocument();
+ });
+
+ it("shows an unavailable rate when no input-side tokens were reported", () => {
+ usageOverride.rows = [
+ {
+ runtime_id: "r-1",
+ date: new Date().toISOString().slice(0, 10),
+ provider: "anthropic",
+ model: "claude-sonnet-4-6",
+ input_tokens: 0,
+ output_tokens: 1_000,
+ cache_read_tokens: 0,
+ cache_write_tokens: 0,
+ },
+ ];
+
+ render(<UsageSection runtime={RUNTIME} />, { wrapper: Wrapper });
+
+ expect(screen.getByText("—")).toBeInTheDocument();
+ expect(screen.queryByText(/0% hit/)).not.toBeInTheDocument();
+ });
+});
+
describe("UsageSection — custom-pricing entry point", () => {
// A model that no maintained row prices, so it lands in the unmapped
// diagnostic. `collectUnmappedModels` keys it by provider, so the saved
diff --git a/packages/views/runtimes/components/usage-section.tsx b/packages/views/runtimes/components/usage-section.tsx
index 929455a9bb..48dac7afc8 100644
--- a/packages/views/runtimes/components/usage-section.tsx
+++ b/packages/views/runtimes/components/usage-section.tsx
@@ -20,6 +20,7 @@ import {
import { useCustomPricingStore } from "@multica/core/runtimes/custom-pricing-store";
import { useViewingTimezone } from "../../common/use-viewing-timezone";
import {
+ cacheHitRatePercent,
formatTokens,
formatUsd,
estimateCost,
@@ -171,9 +172,11 @@ export function UsageSection({ runtime }: { runtime: AgentRuntime }) {
const tokensTotal =
totals.input + totals.output + totals.cacheRead + totals.cacheWrite;
- const cacheableTokens = totals.input + totals.cacheRead;
- const cacheHitRate =
- cacheableTokens > 0 ? Math.round((totals.cacheRead / cacheableTokens) * 100) : 0;
+ const cacheHitRate = cacheHitRatePercent(
+ totals.input,
+ totals.cacheRead,
+ totals.cacheWrite,
+ );
const costDelta = pctChange(totals.cost, prevTotals.cost);
const locales = i18n.resolvedLanguage ?? i18n.language;
@@ -272,10 +275,12 @@ export function UsageSection({ runtime }: { runtime: AgentRuntime }) {
accent={totals.cacheSavings > 0 ? "success" : "default"}
hint={
<span>
- {t(($) => $.usage.kpi_cache_hint, {
- pct: cacheHitRate,
- reads: formatTokens(totals.cacheRead),
- })}
+ {cacheHitRate == null
+ ? "—"
+ : t(($) => $.usage.kpi_cache_hint, {
+ pct: cacheHitRate,
+ reads: formatTokens(totals.cacheRead),
+ })}
</span>
}
/>
diff --git a/packages/views/runtimes/utils.test.ts b/packages/views/runtimes/utils.test.ts
index 9c688eae6d..8d78d317cf 100644
--- a/packages/views/runtimes/utils.test.ts
+++ b/packages/views/runtimes/utils.test.ts
@@ -7,6 +7,7 @@ import {
aggregateByDate,
aggregateByWeek,
aggregateCostByModel,
+ cacheHitRatePercent,
collectUnmappedModels,
computeCostInWindow,
estimateCost,
@@ -33,6 +34,21 @@ const zeroUsage = {
cache_write_tokens: 0,
};
+describe("cacheHitRatePercent", () => {
+ it("includes cache writes in the input-side denominator", () => {
+ expect(cacheHitRatePercent(0, 72, 28)).toBe(72);
+ });
+
+ it("does not round an incomplete hit rate up to 100%", () => {
+ expect(cacheHitRatePercent(45, 9_955, 0)).toBe(99);
+ expect(cacheHitRatePercent(0, 10_000, 0)).toBe(100);
+ });
+
+ it("returns null when there are no input-side tokens", () => {
+ expect(cacheHitRatePercent(0, 0, 0)).toBeNull();
+ });
+});
+
describe("isSelfHealingRuntime", () => {
function makeRuntime(overrides: Partial<AgentRuntime>): AgentRuntime {
return {
diff --git a/packages/views/runtimes/utils.ts b/packages/views/runtimes/utils.ts
index edda3522f7..472de94aa8 100644
--- a/packages/views/runtimes/utils.ts
+++ b/packages/views/runtimes/utils.ts
@@ -140,6 +140,27 @@ export function formatTokens(n: number): string {
return `${Number(scaled.toFixed(1))}${unit.suffix}`;
}
+/**
+ * Whole-number prompt-cache hit rate across mutually exclusive input buckets.
+ * Callers must sum each bucket before calling so the result is a ratio of sums,
+ * never an average of per-row percentages. Output tokens are not input-side
+ * traffic and therefore do not belong in the denominator.
+ *
+ * Returns null when no input-side usage was reported. Round ordinary values to
+ * the nearest percent, but reserve 100% for a genuinely complete cache hit.
+ */
+export function cacheHitRatePercent(
+ inputTokens: number,
+ cacheReadTokens: number,
+ cacheWriteTokens: number,
+): number | null {
+ const inputSideTokens = inputTokens + cacheReadTokens + cacheWriteTokens;
+ if (inputSideTokens <= 0) return null;
+
+ const percent = (cacheReadTokens / inputSideTokens) * 100;
+ return percent === 100 ? 100 : Math.min(Math.round(percent), 99);
+}
+
// Cents below $100, whole dollars above — two decimals on a four-figure spend
// is noise, and dropping them below $100 would round most single runs to $0.
export function formatUsd(n: number): string {