Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(composer): show input cache as a percentage, not a raw count

M3gA-Mind7309447583ce
diff --git a/app/src/features/conversations/components/composer/ContextWindowPill.test.ts b/app/src/features/conversations/components/composer/ContextWindowPill.test.ts
index 68ec94a652..751b7516ae 100644
--- a/app/src/features/conversations/components/composer/ContextWindowPill.test.ts
+++ b/app/src/features/conversations/components/composer/ContextWindowPill.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';
import { emptySessionTokenUsage } from '../../../../store/chatRuntimeSlice';
-import { contextUsageFromTokenUsage } from './ContextWindowPill';
+import { cacheHitLabel, contextUsageFromTokenUsage } from './ContextWindowPill';
describe('contextUsageFromTokenUsage', () => {
it('keeps cached input separate from fresh input', () => {
@@ -35,3 +35,44 @@ describe('contextUsageFromTokenUsage', () => {
expect(contextUsageFromTokenUsage(usage, null).limit).toBe(0);
});
});
+
+describe('cacheHitLabel', () => {
+ // These assert the STRING the "Cached input" row renders, not the
+ // `cachedInput` field. A field assertion passes while the row is still an
+ // absolute count, which is the bug (#6461) — so it would be vacuous.
+ it('renders cache reads as a share of the turn total input', () => {
+ // 400 cached out of 1000 reported input. Note the denominator is
+ // `input + cachedInput`, NOT `input` (600) — against `input` this would
+ // read 67%.
+ expect(cacheHitLabel({ input: 600, cachedInput: 400 })).toBe('40%');
+ });
+
+ it('renders an em dash, not 0%, when no input has been reported', () => {
+ expect(cacheHitLabel({ input: 0, cachedInput: 0 })).toBe('—');
+ expect(cacheHitLabel(contextUsageFromTokenUsage(emptySessionTokenUsage()))).toBe('—');
+ });
+
+ it('renders 0% for a reported turn that got no cache hit', () => {
+ // Distinct from the em dash above: this turn measured a real 0% hit rate.
+ expect(cacheHitLabel({ input: 1_000, cachedInput: 0 })).toBe('0%');
+ });
+
+ it('rounds a negligible cache hit down to 0% rather than up', () => {
+ expect(cacheHitLabel({ input: 999, cachedInput: 1 })).toBe('0%');
+ });
+
+ it('never exceeds 100% when a provider over-reports cached input', () => {
+ // `cachedTokens` (500) > `inputTokens` (100). `contextUsageFromTokenUsage`
+ // floors `input` at 0, so the share lands on 100% instead of 500%. This is
+ // the reachable form of the over-report; the `Math.min` in `cacheHitLabel`
+ // is unreachable through this path because the denominator contains the
+ // numerator.
+ const usage = contextUsageFromTokenUsage({
+ ...emptySessionTokenUsage(),
+ inputTokens: 100,
+ cachedTokens: 500,
+ });
+ expect(usage.input).toBe(0);
+ expect(cacheHitLabel(usage)).toBe('100%');
+ });
+});
diff --git a/app/src/features/conversations/components/composer/ContextWindowPill.tsx b/app/src/features/conversations/components/composer/ContextWindowPill.tsx
index 9d1471bd87..6dbd6ff3b4 100644
--- a/app/src/features/conversations/components/composer/ContextWindowPill.tsx
+++ b/app/src/features/conversations/components/composer/ContextWindowPill.tsx
@@ -53,6 +53,34 @@ export function contextUsageFromTokenUsage(
const compact = (n: number): string =>
n >= 1000 ? `${(n / 1000).toFixed(n >= 10_000 ? 0 : 1)}k` : String(n);
+/**
+ * Cache reads as a share of the thread's input, or an em dash when no input has
+ * been reported yet.
+ *
+ * A share rather than a count because the two rows are disjoint:
+ * `contextUsageFromTokenUsage` has already subtracted the cached portion out of
+ * `input`, so rendering both as absolutes read as if the cache were larger than
+ * the input it came from. The denominator is therefore the ORIGINAL input total
+ * (`input + cachedInput`, i.e. `SessionTokenUsage.inputTokens`) — not `input`,
+ * which would let the ratio exceed 100%, and not `limit`, which a thread's
+ * cumulative input outgrows after a few turns.
+ *
+ * An em dash rather than `0%` when nothing has been reported: `0%` asserts a
+ * measured miss rate that no turn ever produced. `0%` on a turn that *did*
+ * report input and got no cache hit is correct and shows.
+ *
+ * The clamp is defence in depth only, and is unreachable by construction here —
+ * the denominator contains the numerator. It guards a caller that builds a
+ * `ContextUsage` by hand with a negative `input`; the provider-reported
+ * over-report (`cachedTokens > inputTokens`) is already absorbed upstream by the
+ * `Math.max(0, …)` on `input` and lands on 100%.
+ */
+export function cacheHitLabel(usage: Pick<ContextUsage, 'input' | 'cachedInput'>): string {
+ const total = usage.input + usage.cachedInput;
+ if (total <= 0) return '—';
+ return `${Math.min(100, Math.round((usage.cachedInput / total) * 100))}%`;
+}
+
const Row = ({ label, value }: { label: string; value: string }) => (
<div className="flex items-baseline justify-between gap-6">
<span className="text-muted-foreground">{label}</span>
@@ -105,10 +133,7 @@ export function ContextWindowPill({ usage }: { usage: ContextUsage }) {
<p className="mb-1.5 font-medium">{t('conversations.composer.context.title')}</p>
<div className="flex flex-col gap-1">
<Row label={t('conversations.composer.context.input')} value={compact(usage.input)} />
- <Row
- label={t('conversations.composer.context.cached')}
- value={compact(usage.cachedInput)}
- />
+ <Row label={t('conversations.composer.context.cached')} value={cacheHitLabel(usage)} />
<Row label={t('conversations.composer.context.output')} value={compact(usage.output)} />
<Row
label={t('conversations.composer.context.cost')}