Skip to content

Commit 3e4f0d1

Browse files
t3dotggclaude
andcommitted
fix(usage): Codex Fast and Ultrafast now cost what they bill
Codex usage was always priced at the standard rate. Codex records the service tier on `thread_settings_applied` events, which the usage scan skipped. Ultrafast bills at 6x and Fast (priority) at 2x for GPT-6 Astra, so tier-heavy usage was under-reported by up to 6x. The Codex reducer now carries the tier forward, and pricing reads LiteLLM's `_priority` and `_ultrafast` rates. Claude fast mode keeps its published multiple. The scan cache moves to v5; v4 Claude and Grok entries still load, and v4 Codex entries re-parse. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent fed41fa commit 3e4f0d1

13 files changed

Lines changed: 293 additions & 105 deletions

‎apps/server/src/usage/antigravityUsageReader.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ async function readDatabase(path: string, fallbackTimestamp: number): Promise<Us
240240
"antigravity-unknown",
241241
totals,
242242
reportedCostUsd: null,
243-
fast: false,
243+
speed: "standard",
244244
dedupeKey: keys[0] ?? `antigravity:${sessionId}:${source}:${index}:${usageIndex}`,
245245
};
246246
records.push({

‎apps/server/src/usage/cursorUsageReader.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export async function readCursorAccountUsage(
253253
sessionId,
254254
totals,
255255
reportedCostUsd,
256-
fast: false,
256+
speed: "standard",
257257
dedupeKey: `cursor-account:${accountKey}:${key}:${occurrence}`,
258258
});
259259
}

‎apps/server/src/usage/opencodeUsageReader.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function parseOpenCodeMessage(
6464
// OpenCode writes zero for models without a known rate, including paid
6565
// subscription models. Let the shared price table estimate those records.
6666
reportedCostUsd: typeof cost === "number" && Number.isFinite(cost) && cost > 0 ? cost : null,
67-
fast: false,
67+
speed: "standard",
6868
dedupeKey: id ? `opencode:${id}` : null,
6969
};
7070
}

‎apps/server/src/usage/usageAggregation.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const rates: RateTable = new Map([
1212
outputCostPerToken: 5e-5,
1313
cacheReadCostPerToken: 1e-6,
1414
cacheCreationCostPerToken: 1.25e-5,
15-
fastMultiplier: 1,
15+
fast: null,
16+
ultrafast: null,
1617
},
1718
],
1819
]);
@@ -32,7 +33,7 @@ function record(overrides: Partial<UsageRecord> = {}): UsageRecord {
3233
reasoningTokens: 0,
3334
},
3435
reportedCostUsd: null,
35-
fast: false,
36+
speed: "standard",
3637
dedupeKey: null,
3738
...overrides,
3839
};

‎apps/server/src/usage/usagePricing.test.ts‎

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "@effect/vitest";
22

33
import { cursorRateModel } from "./cursorUsageReader.ts";
4+
import type { UsageSpeed } from "./usageTranscripts.ts";
45
import {
56
cacheSavingsUsd,
67
createOverrideRateTable,
@@ -23,11 +24,15 @@ describe("usage pricing", () => {
2324
outputTokens: 1_000_000,
2425
reasoningTokens: 500_000,
2526
};
26-
const record = (model: string, reportedCostUsd: number | null = null, fast = false) => ({
27+
const record = (
28+
model: string,
29+
reportedCostUsd: number | null = null,
30+
speed: UsageSpeed = "standard",
31+
) => ({
2732
model,
2833
totals,
2934
reportedCostUsd,
30-
fast,
35+
speed,
3136
});
3237

3338
it("uses custom token rates ahead of public and provider-reported costs", () => {
@@ -117,20 +122,46 @@ describe("usage pricing", () => {
117122
const overrides = createOverrideRateTable({
118123
"claude-opus-5-5": { inputCostPerMillionTokens: 4, outputCostPerMillionTokens: 20 },
119124
});
120-
const cost = (model: string, fast: boolean, custom?: typeof overrides) =>
121-
priceUsage(table, record(model, null, fast), custom).costUsd;
125+
const cost = (model: string, speed: UsageSpeed, custom?: typeof overrides) =>
126+
priceUsage(table, record(model, null, speed), custom).costUsd;
122127

123-
expect(cost("claude-opus-5-5", true)).toBeCloseTo(2 * cost("claude-opus-5-5", false));
124-
expect(cacheSavingsUsd(table, record("claude-opus-5-5", null, true))).toBeCloseTo(
128+
expect(cost("claude-opus-5-5", "fast")).toBeCloseTo(2 * cost("claude-opus-5-5", "standard"));
129+
expect(cacheSavingsUsd(table, record("claude-opus-5-5", null, "fast"))).toBeCloseTo(
125130
2 * cacheSavingsUsd(table, record("claude-opus-5-5")),
126131
);
127132
// No published fast tier, and custom prices, both stay at the standard rate.
128-
expect(cost("claude-fable-5-1", true)).toBe(cost("claude-fable-5-1", false));
129-
expect(cost("claude-opus-5-5", true, overrides)).toBe(
130-
cost("claude-opus-5-5", false, overrides),
133+
expect(cost("claude-fable-5-1", "fast")).toBe(cost("claude-fable-5-1", "standard"));
134+
expect(cost("claude-opus-5-5", "fast", overrides)).toBe(
135+
cost("claude-opus-5-5", "standard", overrides),
131136
);
132137
});
133138

139+
it("prices Codex priority and ultrafast requests at their published tier rates", () => {
140+
const table = parseRateTable({
141+
"gpt-6-astra": {
142+
...rate(1e-5, 1e-6),
143+
input_cost_per_token_priority: 2e-5,
144+
output_cost_per_token_priority: 1e-4,
145+
cache_read_input_token_cost_priority: 2e-6,
146+
input_cost_per_token_ultrafast: 6e-5,
147+
output_cost_per_token_ultrafast: 3e-4,
148+
// No ultrafast cache rate: keeps the standard 10:1 input-to-cache ratio.
149+
},
150+
"gpt-6-sol": rate(2e-6, 2e-7),
151+
});
152+
const cost = (model: string, speed: UsageSpeed) =>
153+
priceUsage(table, record(model, null, speed)).costUsd;
154+
const standard = cost("gpt-6-astra", "standard");
155+
156+
expect(cost("gpt-6-astra", "fast")).toBeCloseTo(2 * standard);
157+
expect(cost("gpt-6-astra", "ultrafast")).toBeCloseTo(6 * standard);
158+
expect(cacheSavingsUsd(table, record("gpt-6-astra", null, "ultrafast"))).toBeCloseTo(
159+
6 * cacheSavingsUsd(table, record("gpt-6-astra")),
160+
);
161+
// A tier the model does not publish bills at the standard rate.
162+
expect(cost("gpt-6-sol", "ultrafast")).toBe(cost("gpt-6-sol", "standard"));
163+
});
164+
134165
it("keeps the canonical Fable rate separate from DeepInfra in either order", () => {
135166
const canonical = ["claude-fable-5", rate(1e-5, 1e-6)] as const;
136167
const deepInfra = ["deepinfra/anthropic/claude-fable-5", rate(1e-5)] as const;

‎apps/server/src/usage/usagePricing.ts‎

Lines changed: 104 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,39 @@
99
*/
1010
import type { UsageCostSource, UsageModelPriceOverride } from "@t3tools/contracts";
1111

12-
import type { UsageRecord } from "./usageTranscripts.ts";
12+
import type { UsageRecord, UsageSpeed } from "./usageTranscripts.ts";
1313

14-
/**
15-
* The subset of a LiteLLM entry we price against. All values are USD per token.
16-
*
17-
* LiteLLM also publishes tiered variants (`*_above_272k_tokens`, `*_flex`,
18-
* `*_priority`, `*_batches`). We deliberately price at the base tier: the
19-
* transcripts don't record which tier served a request, so anything else would
20-
* be a guess dressed up as precision.
21-
*/
22-
export interface ModelRate {
14+
/** Token rates for one billing speed. All values are USD per token. */
15+
export interface TokenRates {
2316
readonly inputCostPerToken: number;
2417
readonly outputCostPerToken: number;
2518
readonly cacheReadCostPerToken: number;
2619
readonly cacheCreationCostPerToken: number;
20+
}
21+
22+
/**
23+
* The subset of a LiteLLM entry we price against: standard rates, plus rates
24+
* for each faster speed the model publishes. A request at a speed with no
25+
* published rates bills at the standard rates.
26+
*
27+
* LiteLLM also publishes `*_above_272k_tokens`, `*_flex`, and `*_batches`
28+
* variants. Transcripts don't record those, so we don't price them.
29+
*/
30+
export interface ModelRate extends TokenRates {
2731
/**
28-
* Multiple of the rates above billed for a fast-mode request, from LiteLLM's
29-
* `provider_specific_entry.fast`. `1` when the model publishes no fast tier.
32+
* From LiteLLM's `provider_specific_entry.fast` multiple (Claude fast mode),
33+
* or else its `*_priority` rates (Codex `priority`).
3034
*/
31-
readonly fastMultiplier: number;
35+
readonly fast: TokenRates | null;
36+
/** From LiteLLM's `*_ultrafast` rates (Codex `ultrafast`). */
37+
readonly ultrafast: TokenRates | null;
3238
}
3339

3440
export type RateTable = ReadonlyMap<string, ModelRate>;
3541

3642
/**
3743
* Custom IDs keep their case, provider prefix, and variant suffix. Custom rates
38-
* apply as entered, fast-mode requests included.
44+
* apply as entered, at every speed.
3945
*/
4046
export function createOverrideRateTable(
4147
overrides: Readonly<Record<string, UsageModelPriceOverride>>,
@@ -50,31 +56,68 @@ export function createOverrideRateTable(
5056
(prices.cacheReadCostPerMillionTokens ?? prices.inputCostPerMillionTokens) / 1_000_000,
5157
cacheCreationCostPerToken:
5258
(prices.cacheWriteCostPerMillionTokens ?? prices.inputCostPerMillionTokens) / 1_000_000,
53-
fastMultiplier: 1,
59+
fast: null,
60+
ultrafast: null,
5461
},
5562
]),
5663
);
5764
}
5865

59-
/** Raw shape of one LiteLLM entry, narrowed to the fields we read. */
60-
interface LiteLlmEntry {
61-
readonly input_cost_per_token?: unknown;
62-
readonly output_cost_per_token?: unknown;
63-
readonly cache_read_input_token_cost?: unknown;
64-
readonly cache_creation_input_token_cost?: unknown;
65-
readonly provider_specific_entry?: unknown;
66-
}
66+
/** One raw LiteLLM entry. Field names carry a tier suffix, e.g. `_priority`. */
67+
type LiteLlmEntry = Readonly<Record<string, unknown>>;
6768

6869
function finiteNumber(value: unknown): number | null {
6970
return typeof value === "number" && Number.isFinite(value) ? value : null;
7071
}
7172

73+
/**
74+
* Reads one rate set, `suffix` selecting a tier such as `_priority`. Returns
75+
* `null` without both an input and an output rate.
76+
*
77+
* Anthropic bills cache reads at a discount and cache writes at a premium.
78+
* When the standard tier omits them, cached input is priced as plain input
79+
* rather than as free. A faster tier that omits them keeps the standard tier's
80+
* cache-to-input ratio.
81+
*/
82+
function readTokenRates(
83+
entry: LiteLlmEntry,
84+
suffix: string,
85+
standard?: TokenRates,
86+
): TokenRates | null {
87+
const input = finiteNumber(entry[`input_cost_per_token${suffix}`]);
88+
const output = finiteNumber(entry[`output_cost_per_token${suffix}`]);
89+
if (input === null || output === null) return null;
90+
const cacheRate = (name: string, field: "cacheReadCostPerToken" | "cacheCreationCostPerToken") =>
91+
finiteNumber(entry[`${name}${suffix}`]) ??
92+
(standard !== undefined && standard.inputCostPerToken > 0
93+
? (standard[field] / standard.inputCostPerToken) * input
94+
: input);
95+
return {
96+
inputCostPerToken: input,
97+
outputCostPerToken: output,
98+
cacheReadCostPerToken: cacheRate("cache_read_input_token_cost", "cacheReadCostPerToken"),
99+
cacheCreationCostPerToken: cacheRate(
100+
"cache_creation_input_token_cost",
101+
"cacheCreationCostPerToken",
102+
),
103+
};
104+
}
105+
106+
function scaleTokenRates(rates: TokenRates, multiple: number): TokenRates {
107+
return {
108+
inputCostPerToken: rates.inputCostPerToken * multiple,
109+
outputCostPerToken: rates.outputCostPerToken * multiple,
110+
cacheReadCostPerToken: rates.cacheReadCostPerToken * multiple,
111+
cacheCreationCostPerToken: rates.cacheCreationCostPerToken * multiple,
112+
};
113+
}
114+
72115
/** Reads `provider_specific_entry.fast`, e.g. `2` for Claude Opus 5.5. */
73-
function fastMultiplier(entry: LiteLlmEntry): number {
74-
const specific = entry.provider_specific_entry;
75-
if (typeof specific !== "object" || specific === null) return 1;
116+
function fastMultiplier(entry: LiteLlmEntry): number | null {
117+
const specific = entry["provider_specific_entry"];
118+
if (typeof specific !== "object" || specific === null) return null;
76119
const fast = finiteNumber((specific as Record<string, unknown>)["fast"]);
77-
return fast !== null && fast > 0 ? fast : 1;
120+
return fast !== null && fast > 0 ? fast : null;
78121
}
79122

80123
/**
@@ -94,21 +137,19 @@ export function parseRateTable(document: unknown): RateTable {
94137
for (const [name, raw] of Object.entries(document as Record<string, unknown>)) {
95138
if (typeof raw !== "object" || raw === null) continue;
96139
const entry = raw as LiteLlmEntry;
97-
const input = finiteNumber(entry.input_cost_per_token);
98-
const output = finiteNumber(entry.output_cost_per_token);
99-
if (input === null || output === null) continue;
140+
const standard = readTokenRates(entry, "");
141+
if (standard === null) continue;
100142

101143
const key = normalizeRateKey(name);
102144
if (key.length === 0) continue;
145+
const multiple = fastMultiplier(entry);
103146
table.set(key, {
104-
inputCostPerToken: input,
105-
outputCostPerToken: output,
106-
// Anthropic bills cache reads at a discount and cache writes at a
107-
// premium. When a model omits them, cached input is priced as plain
108-
// input rather than as free.
109-
cacheReadCostPerToken: finiteNumber(entry.cache_read_input_token_cost) ?? input,
110-
cacheCreationCostPerToken: finiteNumber(entry.cache_creation_input_token_cost) ?? input,
111-
fastMultiplier: fastMultiplier(entry),
147+
...standard,
148+
fast:
149+
multiple === null
150+
? readTokenRates(entry, "_priority", standard)
151+
: scaleTokenRates(standard, multiple),
152+
ultrafast: readTokenRates(entry, "_ultrafast", standard),
112153
});
113154
}
114155

@@ -131,16 +172,29 @@ export function parseRateTable(document: unknown): RateTable {
131172
return table;
132173
}
133174

134-
function sameRate(a: ModelRate, b: ModelRate): boolean {
175+
function sameTokenRates(a: TokenRates | null, b: TokenRates | null): boolean {
176+
if (a === null || b === null) return a === b;
135177
return (
136178
a.inputCostPerToken === b.inputCostPerToken &&
137179
a.outputCostPerToken === b.outputCostPerToken &&
138180
a.cacheReadCostPerToken === b.cacheReadCostPerToken &&
139-
a.cacheCreationCostPerToken === b.cacheCreationCostPerToken &&
140-
a.fastMultiplier === b.fastMultiplier
181+
a.cacheCreationCostPerToken === b.cacheCreationCostPerToken
141182
);
142183
}
143184

185+
function sameRate(a: ModelRate, b: ModelRate): boolean {
186+
return (
187+
sameTokenRates(a, b) &&
188+
sameTokenRates(a.fast, b.fast) &&
189+
sameTokenRates(a.ultrafast, b.ultrafast)
190+
);
191+
}
192+
193+
/** The rates a request at `speed` bills at. */
194+
function ratesAt(rate: ModelRate, speed: UsageSpeed): TokenRates {
195+
return (speed === "standard" ? null : rate[speed]) ?? rate;
196+
}
197+
144198
function normalizeRateKey(model: string): string {
145199
return model.trim().toLowerCase();
146200
}
@@ -186,7 +240,7 @@ export function lookupRate(table: RateTable, model: string): ModelRate | null {
186240
/** The parts of a transcript record that decide its price. */
187241
export type PricedRecord = Pick<
188242
UsageRecord,
189-
"model" | "rateModel" | "totals" | "fast" | "reportedCostUsd"
243+
"model" | "rateModel" | "totals" | "speed" | "reportedCostUsd"
190244
>;
191245

192246
export interface PricedUsage {
@@ -214,14 +268,13 @@ export function priceUsage(
214268
const rate = override ?? lookupRate(table, record.rateModel ?? model);
215269
if (rate === null) return { costUsd: 0, costSource: "unpriced" };
216270

217-
const standardCostUsd =
218-
totals.uncachedInputTokens * rate.inputCostPerToken +
219-
totals.cachedInputTokens * rate.cacheReadCostPerToken +
220-
totals.cacheCreationTokens * rate.cacheCreationCostPerToken +
221-
totals.outputTokens * rate.outputCostPerToken;
222-
271+
const rates = ratesAt(rate, record.speed);
223272
return {
224-
costUsd: standardCostUsd * (record.fast ? rate.fastMultiplier : 1),
273+
costUsd:
274+
totals.uncachedInputTokens * rates.inputCostPerToken +
275+
totals.cachedInputTokens * rates.cacheReadCostPerToken +
276+
totals.cacheCreationTokens * rates.cacheCreationCostPerToken +
277+
totals.outputTokens * rates.outputCostPerToken,
225278
costSource: "modelPriced",
226279
};
227280
}
@@ -238,9 +291,6 @@ export function cacheSavingsUsd(
238291
const rate =
239292
overrides?.get(record.model.trim()) ?? lookupRate(table, record.rateModel ?? record.model);
240293
if (rate === null) return 0;
241-
return (
242-
record.totals.cachedInputTokens *
243-
(rate.inputCostPerToken - rate.cacheReadCostPerToken) *
244-
(record.fast ? rate.fastMultiplier : 1)
245-
);
294+
const rates = ratesAt(rate, record.speed);
295+
return record.totals.cachedInputTokens * (rates.inputCostPerToken - rates.cacheReadCostPerToken);
246296
}

0 commit comments

Comments
 (0)