The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 21:45 UTC
fix(usage): price Cursor cache savings by base model (#13731)
Yash Singh committed
Sep 25, 2026 at 19:27 UTC
aade3a69a8536be98d2fe9b0fb9193289b70e90b
4 files changed
+46
-3
apps/server/src/usage/cursorUsageReader.ts
+13
@@ -17,6 +17,18 @@ function tokens(value: unknown): number {
17
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.trunc(value) : 0;
18
}
19
20
+/**
21
+ * Maps Cursor's tiered names (`cursor-grok-4.6-high-fast`,
22
+ * `claude-fable-5-1-thinking-high`) to the base model's rate-table key.
23
+ * Grok resolves through xAI's first-party entry, which has no bare alias.
24
+ */
25
+export function cursorRateModel(model: string): string {
26
+ const base = model
27
+ .replace(/^cursor-/, "")
28
+ .replace(/(?:-thinking)?(?:-(?:none|minimal|low|medium|high|xhigh|max))?(?:-fast)?$/, "");
29
+ return base.startsWith("grok-") ? `xai/${base}` : base;
30
+}
31
+
32
export interface CursorAccountUsageReadResult {
33
readonly accountKey: string | null;
34
readonly records: readonly UsageRecord[];
@@ -232,6 +244,7 @@ export async function readCursorAccountUsage(
244
provider: "cursor",
245
timestampMs,
246
model: event.model,
247
+ rateModel: cursorRateModel(event.model),
248
sessionId,
249
totals,
250
reportedCostUsd,
apps/server/src/usage/usagePricing.test.ts
+21
@@ -1,5 +1,6 @@
1
import { describe, expect, it } from "@effect/vitest";
2
3
+import { cursorRateModel } from "./cursorUsageReader.ts";
4
import {
5
cacheSavingsUsd,
6
createOverrideRateTable,
@@ -49,6 +50,26 @@ describe("usage pricing", () => {
50
expect(cacheSavingsUsd(table, record("example-model"), overrides)).toBe(1.5);
51
});
52
53
+ it("prices Cursor cache savings at the base model rate", () => {
54
+ const table = parseRateTable({
55
+ "claude-fable-5-1": rate(10e-6, 1e-6),
56
+ "xai/grok-4.7": rate(2e-6, 0.5e-6),
57
+ "openrouter/x-ai/grok-4.7": rate(3e-6, 0.5e-6),
58
+ });
59
+ const cursorRecord = (model: string) => ({
60
+ ...record(model, 0.25),
61
+ rateModel: cursorRateModel(model),
62
+ });
63
+
64
+ expect(cacheSavingsUsd(table, cursorRecord("claude-fable-5-1-thinking-high"))).toBeCloseTo(9);
65
+ expect(cacheSavingsUsd(table, cursorRecord("cursor-grok-4.7-high-fast"))).toBeCloseTo(1.5);
66
+ expect(cacheSavingsUsd(table, cursorRecord("default"))).toBe(0);
67
+ expect(priceUsage(table, cursorRecord("grok-4.7-xhigh-fast"))).toEqual({
68
+ costUsd: 0.25,
69
+ costSource: "providerReported",
70
+ });
71
+ });
72
+
73
it("prices unknown models offline and uses input prices for omitted cache rates", () => {
74
const table = parseRateTable({});
75
const overrides = createOverrideRateTable({
apps/server/src/usage/usagePricing.ts
+7
-3
@@ -184,7 +184,10 @@ export function lookupRate(table: RateTable, model: string): ModelRate | null {
184
}
185
186
/** The parts of a transcript record that decide its price. */
187
-export type PricedRecord = Pick<UsageRecord, "model" | "totals" | "fast" | "reportedCostUsd">;
187
+export type PricedRecord = Pick<
188
+ UsageRecord,
189
+ "model" | "rateModel" | "totals" | "fast" | "reportedCostUsd"
190
+>;
191
192
export interface PricedUsage {
193
readonly costUsd: number;
@@ -208,7 +211,7 @@ export function priceUsage(
211
return { costUsd: reportedCostUsd, costSource: "providerReported" };
212
}
213
211
- const rate = override ?? lookupRate(table, model);
214
+ const rate = override ?? lookupRate(table, record.rateModel ?? model);
215
if (rate === null) return { costUsd: 0, costSource: "unpriced" };
216
217
const standardCostUsd =
@@ -232,7 +235,8 @@ export function cacheSavingsUsd(
235
record: PricedRecord,
236
overrides?: RateTable,
237
): number {
235
- const rate = overrides?.get(record.model.trim()) ?? lookupRate(table, record.model);
238
+ const rate =
239
+ overrides?.get(record.model.trim()) ?? lookupRate(table, record.rateModel ?? record.model);
240
if (rate === null) return 0;
241
return (
242
record.totals.cachedInputTokens *
apps/server/src/usage/usageTranscripts.ts
+5
@@ -12,6 +12,11 @@ export interface UsageRecord {
12
readonly provider: UsageProviderKind;
13
readonly timestampMs: number;
14
readonly model: string;
15
+ /**
16
+ * Rate-table key when the provider's display name carries tiers the table
17
+ * does not know, such as Cursor's `claude-opus-5-5-high`. Defaults to `model`.
18
+ */
19
+ readonly rateModel?: string;
20
readonly sessionId: string;
21
readonly totals: UsageTokenTotals;
22
readonly reportedCostUsd: number | null;