Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usageBreakdown.ts

100 lines3.7 KB

1import type { UsageTokenTotals } from "@t3tools/contracts";
2import {
3 isModelCostUnknown,
4 type CategoryCost,
5 type ModelTotals,
6 type SpeedCost,
7} from "@t3tools/shared/usageMerge";
8import { formatDateTimeShort, formatHourShort } from "@t3tools/shared/usageFormat";
9​
10import { msg } from "gt-react";
11​
12import type { ShareSegment } from "./UsageShareBar";
13​
14// A 24-hour window across spring-forward opens and closes on the same wall hour; date those two rows.
15export function breakdownHourLabel(
16 hourStart: string,
17 hours: readonly string[],
18 timeZone: string,
19 hour12: boolean,
20 locale?: string,
21) {
22 const label = (hour: string) => formatHourShort(hour, timeZone, hour12, locale);
23 const first = hours[0];
24 const last = hours.at(-1);
25 const endsCollide =
26 first !== undefined && last !== undefined && first !== last && label(first) === label(last);
27 return endsCollide && (hourStart === first || hourStart === last)
28 ? formatDateTimeShort(hourStart, timeZone, hour12, locale)
29 : label(hourStart);
31​
32export function sortModelsByTokens(models: readonly ModelTotals[]) {
33 return models.toSorted(
34 (left, right) => right.totalTokens - left.totalTokens || right.costUsd - left.costUsd,
35 );
37​
38/** Unknown cost yields null cost share; unpriced models still have token shares. */
39export function modelShare(model: ModelTotals, metric: "cost" | "tokens"): number | null {
40 if (metric === "tokens") return model.tokenShare;
41 return isModelCostUnknown(model) ? null : model.costShare;
43​
44/** Returns null without input; cache writes count as misses because input was fully processed. */
45export function cacheHitRate({ tokens }: ModelTotals): number | null {
46 const input = tokens.uncachedInputTokens + tokens.cachedInputTokens + tokens.cacheCreationTokens;
47 return input === 0 ? null : tokens.cachedInputTokens / input;
49​
50/** Effective USD per million priced tokens, or `null` when none were priced. */
51export function costPerMillionTokens(model: ModelTotals): number | null {
52 const pricedTokens = model.totalTokens - model.unpricedTokens;
53 return pricedTokens <= 0 || isModelCostUnknown(model)
54 ? null
55 : (model.costUsd / pricedTokens) * 1_000_000;
57​
58const ink = (percent: number) =>
59 `color-mix(in oklab, var(--foreground) ${percent}%, var(--background))`;
60​
61const TYPE_COLORS = {
62 input: ink(60),
63 cacheRead: ink(30),
64 cacheWrite: ink(72),
65 output: ink(100),
66 other: ink(44),
67};
68​
69export function costTypeSegments(cost: CategoryCost): readonly ShareSegment[] {
70 return [
71 { label: msg("Input"), value: cost.input, color: TYPE_COLORS.input },
72 { label: msg("Cache read"), value: cost.cacheRead, color: TYPE_COLORS.cacheRead },
73 { label: msg("Cache write"), value: cost.cacheWrite, color: TYPE_COLORS.cacheWrite },
74 { label: msg("Output"), value: cost.output, color: TYPE_COLORS.output },
75 {
76 label: msg("Other"),
77 value: cost.unsplit >= 0.005 ? cost.unsplit : 0,
78 color: TYPE_COLORS.other,
79 },
80 ];
82​
83export function tokenTypeSegments(
84 tokens: Omit<UsageTokenTotals, "reasoningTokens">,
85): readonly ShareSegment[] {
86 return [
87 { label: msg("Input"), value: tokens.uncachedInputTokens, color: TYPE_COLORS.input },
88 { label: msg("Cache read"), value: tokens.cachedInputTokens, color: TYPE_COLORS.cacheRead },
89 { label: msg("Cache write"), value: tokens.cacheCreationTokens, color: TYPE_COLORS.cacheWrite },
90 { label: msg("Output"), value: tokens.outputTokens, color: TYPE_COLORS.output },
91 ];
93​
94export function speedCostSegments(cost: SpeedCost): readonly ShareSegment[] {
95 return [
96 { label: msg("Standard"), value: cost.standard, color: ink(34) },
97 { label: msg("Fast"), value: cost.fast, color: ink(66) },
98 { label: msg("Ultrafast"), value: cost.ultrafast, color: ink(100) },
99 ];