Open History to see the last commit
1import type { UsageTokenTotals } from "@t3tools/contracts";
2import {
3 isModelCostUnknown,
4 type CategoryCost,
5 type ModelTotals,
6 type SpeedCost,
7} from "@t3tools/shared/usageMerge";
8
9import type { ShareSegment } from "./UsageShareBar";
10
11export function sortModelsByTokens(models: readonly ModelTotals[]) {
12 return models.toSorted(
13 (left, right) => right.totalTokens - left.totalTokens || right.costUsd - left.costUsd,
14 );
15}
16
17/**
18 * Share of a model's input read from cache, or `null` without input. Cache
19 * writes count as misses: that input was processed in full.
20 */
21export function cacheHitRate({ tokens }: ModelTotals): number | null {
22 const input = tokens.uncachedInputTokens + tokens.cachedInputTokens + tokens.cacheCreationTokens;
23 return input === 0 ? null : tokens.cachedInputTokens / input;
24}
25
26/** Effective USD per million priced tokens, or `null` when none were priced. */
27export function costPerMillionTokens(model: ModelTotals): number | null {
28 const pricedTokens = model.totalTokens - model.unpricedTokens;
29 return pricedTokens <= 0 || isModelCostUnknown(model)
30 ? null
31 : (model.costUsd / pricedTokens) * 1_000_000;
32}
33
34/** A neutral step between background and foreground, so mixes never borrow a provider's color. */
35const ink = (percent: number) =>
36 `color-mix(in oklab, var(--foreground) ${percent}%, var(--background))`;
37
38/** Adjacent segments stay above the 15 ΔE separation floor in both themes. */
39const TYPE_COLORS = {
40 input: ink(60),
41 cacheRead: ink(30),
42 cacheWrite: ink(72),
43 output: ink(100),
44 other: ink(44),
45};
46
47export function costTypeSegments(cost: CategoryCost): readonly ShareSegment[] {
48 return [
49 { label: "Input", value: cost.input, color: TYPE_COLORS.input },
50 { label: "Cache read", value: cost.cacheRead, color: TYPE_COLORS.cacheRead },
51 { label: "Cache write", value: cost.cacheWrite, color: TYPE_COLORS.cacheWrite },
52 { label: "Output", value: cost.output, color: TYPE_COLORS.output },
53 // Reported cost with no rates to split it, or from older servers. Below a
54 // cent it is rounding, not usage.
55 { label: "Other", value: cost.unsplit >= 0.005 ? cost.unsplit : 0, color: TYPE_COLORS.other },
56 ];
57}
58
59export function tokenTypeSegments(
60 tokens: Omit<UsageTokenTotals, "reasoningTokens">,
61): readonly ShareSegment[] {
62 return [
63 { label: "Input", value: tokens.uncachedInputTokens, color: TYPE_COLORS.input },
64 { label: "Cache read", value: tokens.cachedInputTokens, color: TYPE_COLORS.cacheRead },
65 { label: "Cache write", value: tokens.cacheCreationTokens, color: TYPE_COLORS.cacheWrite },
66 { label: "Output", value: tokens.outputTokens, color: TYPE_COLORS.output },
67 ];
68}
69
70/** Speeds are ordered by price, so they brighten from standard to ultrafast. */
71export function speedCostSegments(cost: SpeedCost): readonly ShareSegment[] {
72 return [
73 { label: "Standard", value: cost.standard, color: ink(34) },
74 { label: "Fast", value: cost.fast, color: ink(66) },
75 { label: "Ultrafast", value: cost.ultrafast, color: ink(100) },
76 ];
77}