Skip to content
TypeScript

fix(usage): price Cursor cache savings by base model (#13731)

Yash Singhaade3a69a853
diff --git a/apps/server/src/usage/cursorUsageReader.ts b/apps/server/src/usage/cursorUsageReader.ts
index 81c680afff0..1e07fcc7a82 100644
--- a/apps/server/src/usage/cursorUsageReader.ts
+++ b/apps/server/src/usage/cursorUsageReader.ts
@@ -17,6 +17,18 @@ function tokens(value: unknown): number {
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.trunc(value) : 0;
}
+/**
+ * Maps Cursor's tiered names (`cursor-grok-4.6-high-fast`,
+ * `claude-fable-5-1-thinking-high`) to the base model's rate-table key.
+ * Grok resolves through xAI's first-party entry, which has no bare alias.
+ */
+export function cursorRateModel(model: string): string {
+ const base = model
+ .replace(/^cursor-/, "")
+ .replace(/(?:-thinking)?(?:-(?:none|minimal|low|medium|high|xhigh|max))?(?:-fast)?$/, "");
+ return base.startsWith("grok-") ? `xai/${base}` : base;
+}
+
export interface CursorAccountUsageReadResult {
readonly accountKey: string | null;
readonly records: readonly UsageRecord[];
@@ -232,6 +244,7 @@ export async function readCursorAccountUsage(
provider: "cursor",
timestampMs,
model: event.model,
+ rateModel: cursorRateModel(event.model),
sessionId,
totals,
reportedCostUsd,
diff --git a/apps/server/src/usage/usagePricing.test.ts b/apps/server/src/usage/usagePricing.test.ts
index ca340a44a64..db4f68c2cb4 100644
--- a/apps/server/src/usage/usagePricing.test.ts
+++ b/apps/server/src/usage/usagePricing.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from "@effect/vitest";
+import { cursorRateModel } from "./cursorUsageReader.ts";
import {
cacheSavingsUsd,
createOverrideRateTable,
@@ -49,6 +50,26 @@ describe("usage pricing", () => {
expect(cacheSavingsUsd(table, record("example-model"), overrides)).toBe(1.5);
});
+ it("prices Cursor cache savings at the base model rate", () => {
+ const table = parseRateTable({
+ "claude-fable-5-1": rate(10e-6, 1e-6),
+ "xai/grok-4.7": rate(2e-6, 0.5e-6),
+ "openrouter/x-ai/grok-4.7": rate(3e-6, 0.5e-6),
+ });
+ const cursorRecord = (model: string) => ({
+ ...record(model, 0.25),
+ rateModel: cursorRateModel(model),
+ });
+
+ expect(cacheSavingsUsd(table, cursorRecord("claude-fable-5-1-thinking-high"))).toBeCloseTo(9);
+ expect(cacheSavingsUsd(table, cursorRecord("cursor-grok-4.7-high-fast"))).toBeCloseTo(1.5);
+ expect(cacheSavingsUsd(table, cursorRecord("default"))).toBe(0);
+ expect(priceUsage(table, cursorRecord("grok-4.7-xhigh-fast"))).toEqual({
+ costUsd: 0.25,
+ costSource: "providerReported",
+ });
+ });
+
it("prices unknown models offline and uses input prices for omitted cache rates", () => {
const table = parseRateTable({});
const overrides = createOverrideRateTable({
diff --git a/apps/server/src/usage/usagePricing.ts b/apps/server/src/usage/usagePricing.ts
index 60bf31b93e3..78f6cf2c5cd 100644
--- a/apps/server/src/usage/usagePricing.ts
+++ b/apps/server/src/usage/usagePricing.ts
@@ -184,7 +184,10 @@ export function lookupRate(table: RateTable, model: string): ModelRate | null {
}
/** The parts of a transcript record that decide its price. */
-export type PricedRecord = Pick<UsageRecord, "model" | "totals" | "fast" | "reportedCostUsd">;
+export type PricedRecord = Pick<
+ UsageRecord,
+ "model" | "rateModel" | "totals" | "fast" | "reportedCostUsd"
+>;
export interface PricedUsage {
readonly costUsd: number;
@@ -208,7 +211,7 @@ export function priceUsage(
return { costUsd: reportedCostUsd, costSource: "providerReported" };
}
- const rate = override ?? lookupRate(table, model);
+ const rate = override ?? lookupRate(table, record.rateModel ?? model);
if (rate === null) return { costUsd: 0, costSource: "unpriced" };
const standardCostUsd =
@@ -232,7 +235,8 @@ export function cacheSavingsUsd(
record: PricedRecord,
overrides?: RateTable,
): number {
- const rate = overrides?.get(record.model.trim()) ?? lookupRate(table, record.model);
+ const rate =
+ overrides?.get(record.model.trim()) ?? lookupRate(table, record.rateModel ?? record.model);
if (rate === null) return 0;
return (
record.totals.cachedInputTokens *
diff --git a/apps/server/src/usage/usageTranscripts.ts b/apps/server/src/usage/usageTranscripts.ts
index 5da13168a1a..6e01c2c5a8e 100644
--- a/apps/server/src/usage/usageTranscripts.ts
+++ b/apps/server/src/usage/usageTranscripts.ts
@@ -12,6 +12,11 @@ export interface UsageRecord {
readonly provider: UsageProviderKind;
readonly timestampMs: number;
readonly model: string;
+ /**
+ * Rate-table key when the provider's display name carries tiers the table
+ * does not know, such as Cursor's `claude-opus-5-5-high`. Defaults to `model`.
+ */
+ readonly rateModel?: string;
readonly sessionId: string;
readonly totals: UsageTokenTotals;
readonly reportedCostUsd: number | null;