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

usageBreakdown.ts

81 lines3.0 KB

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