fix(usage): price Cursor cache savings by base model (#13731)
This commit is contained in:
1 parent
525af2d1ad
commit
aade3a69a8
4 files changed
+46
-3
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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 *
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user