mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
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:
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
@@ -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}%`
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import type { ChartLine } from '@/lib/overview-metrics'
|
||||
import { parseScope, scopeBudgetLimits, scopeId, scopeMtdMicros } from '@/lib/scope'
|
||||
import { seriesToStacked } from '@/lib/series'
|
||||
import { breakdownCsv } from '@/lib/usage-csv'
|
||||
import { formatHitRate, promptCacheHitRate, promptCacheSavedMicros } from '@/lib/prompt-cache'
|
||||
import { wbColumns } from '@/lib/wb-columns'
|
||||
import { useCatalogStore } from '@/stores/catalog'
|
||||
import { useDashboardStore } from '@/stores/dashboard'
|
||||
@@ -164,6 +165,8 @@ interface BreakdownRow {
|
||||
share: number | null
|
||||
/** Set for a zero-valued model row with detail: what to show instead of a zero cost. */
|
||||
zeroText: string | null
|
||||
/** Prompt-cache hit rate; null when unknown or not a model row. */
|
||||
cacheRate: number | null
|
||||
}
|
||||
|
||||
const columns = computed(() =>
|
||||
@@ -171,11 +174,12 @@ const columns = computed(() =>
|
||||
{ key: 'name', header: BREAKDOWN_LABEL[by.value], kind: 'text', sortable: false },
|
||||
{ key: 'value', header: SPEND_METRIC_LABEL[metric.value], kind: 'number', sortable: false },
|
||||
{ key: 'share', header: 'Share', kind: 'share', sortable: false },
|
||||
...(by.value === 'model' && spend.detailAvailable ? [{ key: 'cacheRate', header: 'Prompt cache', kind: 'number' as const, sortable: false }] : []),
|
||||
]),
|
||||
)
|
||||
|
||||
const sourceRows = computed<{ id: string; value: number; free?: boolean }[]>(() => {
|
||||
if (by.value === 'model') return spend.modelRanking.map((m) => ({ id: m.id, value: m.value, free: m.free }))
|
||||
const sourceRows = computed<{ id: string; value: number; free?: boolean; cacheRate?: number | null }[]>(() => {
|
||||
if (by.value === 'model') return spend.modelRanking.map((m) => ({ id: m.id, value: m.value, free: m.free, cacheRate: promptCacheHitRate(m) }))
|
||||
if (by.value === 'group') return spend.drilldownRows.map((r) => ({ id: r.id, value: r.values[metric.value] ?? 0 }))
|
||||
return users.rows
|
||||
})
|
||||
@@ -189,7 +193,7 @@ const tableRows = computed<BreakdownRow[]>(() => {
|
||||
const rows = sourceRows.value.filter((r) => r.value > 0 || by.value === 'model').sort((a, b) => b.value - a.value)
|
||||
const sum = rows.reduce((acc, r) => acc + r.value, 0)
|
||||
const total = filters.scope === 'all' && totalInRange.value > 0 ? totalInRange.value : sum
|
||||
return rows.map((r) => ({ id: r.id, name: r.id, value: r.value, share: total > 0 ? Math.min(1, r.value / total) : null, zeroText: zeroText(r) }))
|
||||
return rows.map((r) => ({ id: r.id, name: r.id, value: r.value, share: total > 0 ? Math.min(1, r.value / total) : null, zeroText: zeroText(r), cacheRate: r.cacheRate ?? null }))
|
||||
})
|
||||
|
||||
const tablePage = ref(1)
|
||||
@@ -254,6 +258,7 @@ const freeTokens = computed(() => ({
|
||||
}))
|
||||
const comparisons = computed(() => costComparisons(freeUsage.value, catalog.modelsById))
|
||||
const rateText = (inP: number | null, outP: number | null): string => (inP === null || outP === null ? 'n/a' : `$${inP} / $${outP} per 1M`)
|
||||
const promptCacheSaved = computed(() => promptCacheSavedMicros(spend.modelRanking))
|
||||
const cacheSavedMicros = computed(() => spend.modelRanking.reduce((acc, m) => acc + (m.cacheSavedMicroUsd ?? 0), 0))
|
||||
const avoidedState = computed<ViewState>(() => {
|
||||
if (spend.rankingError && spend.modelRanking.length === 0) return 'error'
|
||||
@@ -415,6 +420,9 @@ const money = (micros: number | null): string | null => (micros === null ? null
|
||||
<template v-else>{{ value }}</template>
|
||||
</template>
|
||||
<template #cell-value="{ value, row }">{{ (row as BreakdownRow).zeroText ?? fmtValue(value) }}</template>
|
||||
<template #cell-cacheRate="{ value }">
|
||||
<span class="font-mono tabular-nums">{{ formatHitRate(value as number | null) }}</span>
|
||||
</template>
|
||||
<template #cell-share="{ value, row }">
|
||||
<ShareCell v-if="!(row as BreakdownRow).zeroText" :value="value as number" />
|
||||
<span v-else aria-hidden="true" />
|
||||
@@ -473,14 +481,23 @@ const money = (micros: number | null): string | null => (micros === null ? null
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<StatGroup v-if="cacheEnabled" :columns="1" gap="sm" aria-label="Savings">
|
||||
<StatGroup v-if="cacheEnabled || spend.detailAvailable" :columns="1" gap="sm" aria-label="Savings">
|
||||
<KpiTile
|
||||
v-if="cacheEnabled"
|
||||
label="Cache savings"
|
||||
size="compact"
|
||||
:value="spend.detailAvailable && cacheSavings.available ? formatCostDisplay(cacheSavedMicros) : 'n/a'"
|
||||
good-when="none"
|
||||
:hint="cacheSavings.note ?? rangeLabel"
|
||||
/>
|
||||
<KpiTile
|
||||
v-if="spend.detailAvailable"
|
||||
label="Prompt-cache savings"
|
||||
size="compact"
|
||||
:value="formatCostDisplay(promptCacheSaved)"
|
||||
good-when="none"
|
||||
hint="Paid providers: cached input vs full price"
|
||||
/>
|
||||
</StatGroup>
|
||||
<p v-if="!spend.detailAvailable" class="text-muted-foreground m-0 text-xs">{{ DETAIL_NOTICE }}</p>
|
||||
</StateView>
|
||||
|
||||
@@ -8,6 +8,7 @@ import { adminFetch, isAuthRejection, messageOf } from '@/lib/api'
|
||||
import { formatCompactCount, formatContextWindow, formatLatencyMs, formatModelCostHover } from '@/lib/format'
|
||||
import { priceStatus } from '@/lib/models-view'
|
||||
import { formatSel } from '@/lib/panel-selection'
|
||||
import { formatHitRate, promptCacheHitRate } from '@/lib/prompt-cache'
|
||||
import { providerRateStatus, providerSuccessRate } from '@/lib/provider-rate'
|
||||
import { seriesUrl } from '@/lib/range'
|
||||
import { routersByModel } from '@/lib/router-index'
|
||||
@@ -152,6 +153,9 @@ function openRouter(name: string): void {
|
||||
<DescriptionItem term="Tokens in / out">
|
||||
<span class="font-mono tabular-nums">{{ formatCompactCount(row.tokensIn) }} / {{ formatCompactCount(row.tokensOut) }}</span>
|
||||
</DescriptionItem>
|
||||
<DescriptionItem term="Prompt cache hit rate">
|
||||
<span class="font-mono tabular-nums">{{ formatHitRate(promptCacheHitRate(row)) }}</span>
|
||||
</DescriptionItem>
|
||||
<DescriptionItem term="Latency p50 / p95">
|
||||
<span class="font-mono tabular-nums">{{ row.p50Ms !== undefined ? formatLatencyMs(row.p50Ms) : '—' }} / {{ row.p95Ms !== undefined ? formatLatencyMs(row.p95Ms) : '—' }}</span>
|
||||
</DescriptionItem>
|
||||
|
||||
@@ -561,6 +561,10 @@ export interface AdminUsageModelEntry {
|
||||
/** Cache hits/savings for this model today — present only when `detail=1` AND AdminFeaturesView.cacheStats is on (admin.go). cacheSavedMicroUsd is micro-USD, same unit as costMicroUsd. */
|
||||
cacheHits?: number
|
||||
cacheSavedMicroUsd?: number
|
||||
/** Provider prompt-cache usage — `detail=1` only (admin.go). Read/write are input-token counts; saved is micro-USD vs full input price. */
|
||||
promptCacheReadTokens?: number
|
||||
promptCacheWriteTokens?: number
|
||||
promptCacheSavedMicroUsd?: number
|
||||
/** Count of 402 (unpriced-refusal) responses for this model today — `detail=1` only (admin.go). */
|
||||
r402?: number
|
||||
/** This model's billing price source — `detail=1` only (admin.go). */
|
||||
|
||||
Reference in New Issue
Block a user