mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
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.
22 lines
977 B
TypeScript
22 lines
977 B
TypeScript
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}%`
|
|
}
|