Skip to content

Commit bbc718f

Browse files
fix(usage): price Claude fallbacks under the models that ran them
1 parent f6433db commit bbc718f

5 files changed

Lines changed: 145 additions & 37 deletions

File tree

‎apps/server/src/provider/Drivers/claudeUsage.test.ts‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,89 @@ describe("Claude advisor iterations", () => {
216216
expect(parse(unusable).map((record) => record.model)).toEqual(["claude-opus-5-5"]);
217217
});
218218
});
219+
220+
describe("Claude fallback iterations", () => {
221+
// Shaped like Claude Code's records of a server-side fallback: after a mid-stream decline the
222+
// top-level usage names the requested model and merges cache writes across both attempts.
223+
const fallbackTurn = (declinedOutputTokens: number) => ({
224+
type: "assistant",
225+
timestamp: "2026-11-02T03:04:05.000Z",
226+
sessionId: "5a128faa-8253-489e-b935-6c08e8e670c0",
227+
requestId: "req_fallback",
228+
message: {
229+
id: "msg_fallback",
230+
model: declinedOutputTokens > 0 ? "claude-opus-5-5" : "claude-opus-4-8",
231+
usage: {
232+
input_tokens: 6,
233+
cache_read_input_tokens: 0,
234+
cache_creation_input_tokens: declinedOutputTokens > 0 ? 30000 : 10000,
235+
output_tokens: 900,
236+
iterations: [
237+
{
238+
type: "message",
239+
model: "claude-opus-5-5",
240+
input_tokens: 3,
241+
cache_read_input_tokens: 0,
242+
cache_creation_input_tokens: 20000,
243+
output_tokens: declinedOutputTokens,
244+
},
245+
{
246+
type: "fallback_message",
247+
model: "claude-opus-4-8",
248+
input_tokens: 6,
249+
cache_read_input_tokens: 0,
250+
cache_creation_input_tokens: 10000,
251+
output_tokens: 900,
252+
},
253+
],
254+
},
255+
},
256+
});
257+
const served = {
258+
provider: "claude",
259+
timestampMs: Date.parse("2026-11-02T03:04:05.000Z"),
260+
model: "claude-opus-4-8",
261+
sessionId: "5a128faa-8253-489e-b935-6c08e8e670c0",
262+
totals: {
263+
uncachedInputTokens: 6,
264+
cachedInputTokens: 0,
265+
cacheCreationTokens: 10000,
266+
outputTokens: 900,
267+
reasoningTokens: 0,
268+
},
269+
reportedCostUsd: null,
270+
speed: "standard",
271+
dedupeKey: "msg_fallback:req_fallback",
272+
};
273+
274+
it.each([
275+
{ declined: "before any output", declinedOutputTokens: 0, expected: [served] },
276+
{
277+
declined: "mid-stream",
278+
declinedOutputTokens: 250,
279+
expected: [
280+
served,
281+
{
282+
...served,
283+
model: "claude-opus-5-5",
284+
totals: {
285+
uncachedInputTokens: 3,
286+
cachedInputTokens: 0,
287+
cacheCreationTokens: 20000,
288+
outputTokens: 250,
289+
reasoningTokens: 0,
290+
},
291+
dedupeKey: "msg_fallback:req_fallback:attempt:0",
292+
},
293+
],
294+
},
295+
])(
296+
"prices the serving fallback under its own model when the request declined $declined",
297+
({ declinedOutputTokens, expected }) => {
298+
const turn = fallbackTurn(declinedOutputTokens);
299+
300+
expect(claudeUsageFormat.parseLine(JSON.stringify(turn), undefined)).toEqual(expected);
301+
expect(claudeUsageFormat.parseProjected(turn, undefined)).toEqual(expected);
302+
},
303+
);
304+
});

‎apps/server/src/provider/Drivers/claudeUsage.ts‎

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,59 +56,80 @@ function parseClaudeRecord(parsed: unknown): readonly UsageRecord[] {
5656
messageId === null && requestId === null ? null : `${messageId ?? ""}:${requestId ?? ""}`;
5757

5858
const cost = record["costUSD"];
59+
const iterations: readonly unknown[] = Array.isArray(usageRecord["iterations"])
60+
? usageRecord["iterations"]
61+
: [];
62+
// After a server-side fallback the top-level usage can name the declined model and merge
63+
// attempts; the serving `fallback_message` iteration holds what its own model billed.
64+
const servingIndex = iterations.findLastIndex(
65+
(iteration) => iterationRecord(iteration)?.["type"] === "fallback_message",
66+
);
67+
const serving = iterationRecord(iterations[servingIndex]);
5968

6069
const executor: UsageRecord = {
6170
provider: "claude",
6271
timestampMs,
63-
model,
72+
model: (serving === null ? null : iterationModel(serving)) ?? model,
6473
sessionId: typeof record["sessionId"] === "string" ? record["sessionId"] : "",
65-
totals: {
66-
uncachedInputTokens: tokenCount(usageRecord["input_tokens"]),
67-
cachedInputTokens: tokenCount(usageRecord["cache_read_input_tokens"]),
68-
cacheCreationTokens: tokenCount(usageRecord["cache_creation_input_tokens"]),
69-
outputTokens: tokenCount(usageRecord["output_tokens"]),
70-
// Anthropic folds thinking tokens into output and does not break them out.
71-
reasoningTokens: 0,
72-
},
74+
totals: usageTotals(serving ?? usageRecord),
7375
reportedCostUsd: typeof cost === "number" && Number.isFinite(cost) ? cost : null,
7476
speed: usageRecord["speed"] === "fast" ? "fast" : "standard",
7577
dedupeKey,
7678
};
7779

78-
// Top-level `usage` is the executor's alone. Advisor calls run on another model at its own rates
79-
// and appear only in `iterations`, so each one is its own record under that model.
80+
// Advisor calls and declined attempts bill at their own model's rates. Anthropic bills a
81+
// declined attempt that produced output; most declines before any output are free.
8082
const records = [executor];
81-
const iterations: readonly unknown[] = Array.isArray(usageRecord["iterations"])
82-
? usageRecord["iterations"]
83-
: [];
8483
for (const [index, iteration] of iterations.entries()) {
85-
if (typeof iteration !== "object" || iteration === null) continue;
86-
const advisor = iteration as Record<string, unknown>;
87-
if (advisor["type"] !== "advisor_message") continue;
88-
89-
const advisorModel = typeof advisor["model"] === "string" ? advisor["model"] : "";
90-
if (advisorModel.length === 0 || advisorModel === CLAUDE_SYNTHETIC_MODEL) continue;
84+
const entry = iterationRecord(iteration);
85+
if (entry === null) continue;
86+
const kind =
87+
entry["type"] === "advisor_message"
88+
? "advisor"
89+
: entry["type"] === "message" &&
90+
index < servingIndex &&
91+
tokenCount(entry["output_tokens"]) > 0
92+
? "attempt"
93+
: null;
94+
const entryModel = iterationModel(entry);
95+
if (kind === null || entryModel === null) continue;
9196

9297
records.push({
9398
provider: "claude",
9499
timestampMs,
95-
model: advisorModel,
100+
model: entryModel,
96101
sessionId: executor.sessionId,
97-
totals: {
98-
uncachedInputTokens: tokenCount(advisor["input_tokens"]),
99-
cachedInputTokens: tokenCount(advisor["cache_read_input_tokens"]),
100-
cacheCreationTokens: tokenCount(advisor["cache_creation_input_tokens"]),
101-
outputTokens: tokenCount(advisor["output_tokens"]),
102-
reasoningTokens: 0,
103-
},
102+
totals: usageTotals(entry),
104103
reportedCostUsd: null,
105104
speed: executor.speed,
106-
dedupeKey: dedupeKey === null ? null : `${dedupeKey}:advisor:${index}`,
105+
dedupeKey: dedupeKey === null ? null : `${dedupeKey}:${kind}:${index}`,
107106
});
108107
}
109108
return records;
110109
}
111110

111+
function iterationRecord(value: unknown): Record<string, unknown> | null {
112+
return typeof value === "object" && value !== null ? (value as Record<string, unknown>) : null;
113+
}
114+
115+
function iterationModel(iteration: Record<string, unknown>): string | null {
116+
const model = iteration["model"];
117+
return typeof model === "string" && model.length > 0 && model !== CLAUDE_SYNTHETIC_MODEL
118+
? model
119+
: null;
120+
}
121+
122+
function usageTotals(usage: Record<string, unknown>): UsageRecord["totals"] {
123+
return {
124+
uncachedInputTokens: tokenCount(usage["input_tokens"]),
125+
cachedInputTokens: tokenCount(usage["cache_read_input_tokens"]),
126+
cacheCreationTokens: tokenCount(usage["cache_creation_input_tokens"]),
127+
outputTokens: tokenCount(usage["output_tokens"]),
128+
// Anthropic folds thinking tokens into output and does not break them out.
129+
reasoningTokens: 0,
130+
};
131+
}
132+
112133
export const claudeUsageFormat: TranscriptUsageFormat<void> = {
113134
selectFields: {
114135
type: true,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2782,7 +2782,7 @@ describe("UsageService", () => {
27822782
60 + 20 * (version === 4 ? 1 : 2),
27832783
);
27842784
yield* upgraded.awaitPersisted;
2785-
assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v12.json");
2785+
assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v13.json");
27862786
assert.deepStrictEqual(
27872787
(yield* Effect.promise(() => NodeFSP.readdir(stateDir)))
27882788
.filter((name) => name.startsWith("usage-scan-cache"))

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ describe("scan cache round trip", () => {
244244
},
245245
);
246246

247-
it("re-parses a Claude transcript cached before partial copies were merged", () => {
247+
it.each([11, 12])("re-parses v%s Claude transcripts for corrected attempt usage", (version) => {
248248
const original = cacheWith([["/claude.jsonl", 100, [record()]]]);
249-
const v11 = { ...parse(makeScanCacheWriter()(original, {})), version: 11 };
249+
const legacy = { ...parse(makeScanCacheWriter()(original, {})), version };
250250

251-
const restored = decodeScanCache(JSON.parse(JSON.stringify(v11)), TEST_FORMAT_MAP);
251+
const restored = decodeScanCache(JSON.parse(JSON.stringify(legacy)), TEST_FORMAT_MAP);
252252
expect(restored.get("/claude.jsonl")).toEqual({
253253
...original.get("/claude.jsonl"),
254254
size: -1,

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ import { CLAUDE_SYNTHETIC_MODEL } from "../provider/Drivers/claudeUsage.ts";
1010
import { mergeUsageCopies } from "./usageAggregation.ts";
1111
import { GUARD_LENGTH, type TranscriptParsePosition } from "./usageTranscriptReader.ts";
1212

13-
const USAGE_SCAN_CACHE_VERSION = 12 as const;
13+
const USAGE_SCAN_CACHE_VERSION = 13 as const;
1414
const SPEED_COMPATIBLE_SINCE_VERSION = 4;
1515

1616
/**
1717
* An older server sharing that directory cannot read a newer cache and would
1818
* replace it, dropping saved usage for deleted transcripts. Separate files keep
1919
* the newer one; older files are removed once it is saved.
2020
*/
21-
export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v12.json";
21+
export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v13.json";
2222
export const LEGACY_SCAN_CACHE_FILE_NAMES = [
23+
"usage-scan-cache-v12.json",
2324
"usage-scan-cache-v11.json",
2425
"usage-scan-cache-v10.json",
2526
"usage-scan-cache-v9.json",
@@ -291,9 +292,9 @@ export function decodeScanCache(
291292
}
292293
// Keep history for deleted rollouts, but re-parse incompatible reducer state.
293294
// v9 and v11 repair skipped Codex requests; clean v8+ entries stay served while unchanged.
294-
// v10 adds Claude advisor calls; before v12 a repeated Claude key kept its first, possibly partial, copy.
295+
// Claude history before v13 lacks per-attempt fallback usage.
295296
const legacy =
296-
(entry.p === "claude" && version < 12) ||
297+
(entry.p === "claude" && version < 13) ||
297298
(format.state !== undefined &&
298299
(version < 6 ||
299300
(entry.p === "codex" && (version < 8 || (version < 11 && malformedRecords > 0)))));

0 commit comments

Comments
 (0)