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/** Unknown cost yields null cost share; unpriced models still have token shares. */
18export function modelShare(model: ModelTotals, metric: "cost" | "tokens"): number | null {
19 if (metric === "tokens") return model.tokenShare;
20 return isModelCostUnknown(model) ? null : model.costShare;
21}
22
23/** Returns null without input; cache writes count as misses because input was fully processed. */
24export function cacheHitRate({ tokens }: ModelTotals): number | null {
25 const input = tokens.uncachedInputTokens + tokens.cachedInputTokens + tokens.cacheCreationTokens;
26 return input === 0 ? null : tokens.cachedInputTokens / input;
27}
28
29/** Effective USD per million priced tokens, or `null` when none were priced. */
30export function costPerMillionTokens(model: ModelTotals): number | null {
31 const pricedTokens = model.totalTokens - model.unpricedTokens;
32 return pricedTokens <= 0 || isModelCostUnknown(model)
33 ? null
34 : (model.costUsd / pricedTokens) * 1_000_000;
35}
36
37const ink = (percent: number) =>
38 `color-mix(in oklab, var(--foreground) ${percent}%, var(--background))`;
39
40const TYPE_COLORS = {
41 input: ink(60),
42 cacheRead: ink(30),
43 cacheWrite: ink(72),
44 output: ink(100),
45 other: ink(44),
46};
47
48export function costTypeSegments(cost: CategoryCost): readonly ShareSegment[] {
49 return [
50 { label: "Input", value: cost.input, color: TYPE_COLORS.input },
51 { label: "Cache read", value: cost.cacheRead, color: TYPE_COLORS.cacheRead },
52 { label: "Cache write", value: cost.cacheWrite, color: TYPE_COLORS.cacheWrite },
53 { label: "Output", value: cost.output, color: TYPE_COLORS.output },
54 { label: "Other", value: cost.unsplit >= 0.005 ? cost.unsplit : 0, color: TYPE_COLORS.other },
55 ];
56}
57
58export function tokenTypeSegments(
59 tokens: Omit<UsageTokenTotals, "reasoningTokens">,
60): readonly ShareSegment[] {
61 return [
62 { label: "Input", value: tokens.uncachedInputTokens, color: TYPE_COLORS.input },
63 { label: "Cache read", value: tokens.cachedInputTokens, color: TYPE_COLORS.cacheRead },
64 { label: "Cache write", value: tokens.cacheCreationTokens, color: TYPE_COLORS.cacheWrite },
65 { label: "Output", value: tokens.outputTokens, color: TYPE_COLORS.output },
66 ];
67}
68
69export function speedCostSegments(cost: SpeedCost): readonly ShareSegment[] {
70 return [
71 { label: "Standard", value: cost.standard, color: ink(34) },
72 { label: "Fast", value: cost.fast, color: ink(66) },
73 { label: "Ultrafast", value: cost.ultrafast, color: ink(100) },
74 ];
75}