feat(admin): show prompt-cache hit rate per model

The usage model table and the model panel now show the prompt-cache hit rate. The Cost avoided card shows the prompt-cache saving as a separate line. The mock admin API fixtures carry the new fields.
This commit is contained in:
2026-11-26 01:15:54 +00:00
parent fd8ca1e95a
commit c14ce25ce0
31 changed files with 2416 additions and 586 deletions
+7
View File
@@ -42,6 +42,10 @@ export interface ModelCatalogRow {
tokensIn: number
tokensOut: number
costMicroUsd: number
/** Detail-only; undefined when the backend omits it. */
promptCacheReadTokens?: number
promptCacheWriteTokens?: number
promptCacheSavedMicroUsd?: number
p50Ms?: number
p95Ms?: number
attempts: number
@@ -84,6 +88,9 @@ export function buildModelCatalogRows(
tokensIn: usage?.tokensIn ?? 0,
tokensOut: usage?.tokensOut ?? 0,
costMicroUsd: usage?.costMicroUsd ?? 0,
promptCacheReadTokens: usage?.promptCacheReadTokens,
promptCacheWriteTokens: usage?.promptCacheWriteTokens,
promptCacheSavedMicroUsd: usage?.promptCacheSavedMicroUsd,
p50Ms: perfRow?.p50Ms,
p95Ms: perfRow?.p95Ms,
attempts: perfRow?.attempts ?? 0,
+28
View File
@@ -0,0 +1,28 @@
import { describe, expect, it } from 'vitest'
import { promptCacheHitRate, promptCacheSavedMicros } from './prompt-cache'
describe('promptCacheHitRate', () => {
it('returns read tokens over input tokens', () => {
expect(promptCacheHitRate({ tokensIn: 1000, promptCacheReadTokens: 900 })).toBe(0.9)
})
it('returns null when no input tokens', () => {
expect(promptCacheHitRate({ tokensIn: 0, promptCacheReadTokens: 0 })).toBeNull()
})
it('returns null when the backend omits the fields', () => {
expect(promptCacheHitRate({ tokensIn: 1000 })).toBeNull()
expect(promptCacheHitRate({ promptCacheReadTokens: 5 })).toBeNull()
})
it('caps at 1 when read tokens exceed input tokens', () => {
expect(promptCacheHitRate({ tokensIn: 100, promptCacheReadTokens: 150 })).toBe(1)
})
})
describe('promptCacheSavedMicros', () => {
it('sums saved micro-USD, treating missing as 0', () => {
expect(promptCacheSavedMicros([{ promptCacheSavedMicroUsd: 40 }, {}, { promptCacheSavedMicroUsd: 2 }])).toBe(42)
})
})
+21
View File
@@ -0,0 +1,21 @@
import type { AdminUsageModelEntry } from '@/types/api'
type HitRateInput = Pick<AdminUsageModelEntry, 'tokensIn' | 'promptCacheReadTokens'>
/** Share of input tokens served from the provider prompt cache; null when unknown (no input, or an older backend / non-detail row). */
export function promptCacheHitRate(row: HitRateInput): number | null {
if (row.promptCacheReadTokens === undefined || !row.tokensIn) return null
return Math.min(1, row.promptCacheReadTokens / row.tokensIn)
}
/** Total real paid-provider saving from prompt caching, in micro-USD. */
export function promptCacheSavedMicros(rows: Pick<AdminUsageModelEntry, 'promptCacheSavedMicroUsd'>[]): number {
let total = 0
for (const r of rows) total += r.promptCacheSavedMicroUsd ?? 0
return total
}
/** Hit rate as a percentage string, or an em dash when unknown. */
export function formatHitRate(rate: number | null): string {
return rate === null ? '—' : `${Math.round(rate * 1000) / 10}%`
}