@devries-cameron20 / t3code Mirror · read-only / commits / adba9a31d0
Fork 0

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 22, 12:00 UTC

fix(usage): count Claude advisor calls under the advisor model (#24139)

fixfon committed Oct 30, 2026 at 08:12 UTC adba9a31d021ed9c9cc3205aa01d7ad670568e86
5 files changed +205 -26
apps/server/src/provider/Drivers/claudeUsage.test.ts
+134 -4
@@ -32,9 +32,9 @@ function claudeLine(overrides: {
32
33 describe("parseClaudeLine", () => {
34 it("extracts token totals and a dedupe key", () => {
35 - const record = parseClaudeLine(claudeLine({ messageId: "msg_1", contentType: "text" }));
35 + const [record] = parseClaudeLine(claudeLine({ messageId: "msg_1", contentType: "text" }));
36
37 - expect(record).not.toBeNull();
37 + expect(record).toBeDefined();
38 expect(record?.provider).toBe("claude");
39 expect(record?.model).toBe("claude-fable-5");
40 expect(record?.totals).toEqual({
@@ -50,8 +50,8 @@ describe("parseClaudeLine", () => {
50
51 it("gives every content block of one message the same dedupe key", () => {
52 // Content-block records repeat full parent usage; summing them overcounts.
53 - const text = parseClaudeLine(claudeLine({ messageId: "msg_2", contentType: "text" }));
54 - const toolUse = parseClaudeLine(claudeLine({ messageId: "msg_2", contentType: "tool_use" }));
53 + const [text] = parseClaudeLine(claudeLine({ messageId: "msg_2", contentType: "text" }));
54 + const [toolUse] = parseClaudeLine(claudeLine({ messageId: "msg_2", contentType: "tool_use" }));
55
56 expect(text?.dedupeKey).toBe(toolUse?.dedupeKey);
57 expect(text?.totals).toEqual(toolUse?.totals);
@@ -86,3 +86,133 @@ describe("parseClaudeLine", () => {
86 expect(claudeUsageFormat.parseProjected(synthetic, undefined)).toEqual([]);
87 });
88 });
89 +
90 +describe("Claude advisor iterations", () => {
91 + const advisorTurn = {
92 + type: "assistant",
93 + timestamp: "2026-10-22T01:31:11.984Z",
94 + sessionId: "5a128faa-8253-489e-b935-6c08e8e670c0",
95 + requestId: "req_1",
96 + message: {
97 + id: "msg_1",
98 + model: "claude-opus-5-5",
99 + usage: {
100 + input_tokens: 4,
101 + cache_read_input_tokens: 118320,
102 + cache_creation_input_tokens: 2676,
103 + output_tokens: 4506,
104 + speed: "standard",
105 + iterations: [
106 + {
107 + type: "message",
108 + input_tokens: 2,
109 + cache_read_input_tokens: 58352,
110 + cache_creation_input_tokens: 1616,
111 + output_tokens: 141,
112 + },
113 + {
114 + type: "advisor_message",
115 + model: "claude-fable-5-1",
116 + input_tokens: 54204,
117 + cache_read_input_tokens: 0,
118 + cache_creation_input_tokens: 0,
119 + output_tokens: 4229,
120 + },
121 + {
122 + type: "message",
123 + input_tokens: 2,
124 + cache_read_input_tokens: 59968,
125 + cache_creation_input_tokens: 1060,
126 + output_tokens: 4365,
127 + },
128 + ],
129 + },
130 + },
131 + };
132 + const withIterations = (iterations: readonly unknown[]) => ({
133 + ...advisorTurn,
134 + message: { ...advisorTurn.message, usage: { ...advisorTurn.message.usage, iterations } },
135 + });
136 + const parse = (turn: unknown) => claudeUsageFormat.parseLine(JSON.stringify(turn), undefined);
137 +
138 + it("reports the advisor model's tokens as their own record beside the unchanged executor record", () => {
139 + const executor = {
140 + provider: "claude",
141 + timestampMs: Date.parse("2026-10-22T01:31:11.984Z"),
142 + model: "claude-opus-5-5",
143 + sessionId: "5a128faa-8253-489e-b935-6c08e8e670c0",
144 + totals: {
145 + uncachedInputTokens: 4,
146 + cachedInputTokens: 118320,
147 + cacheCreationTokens: 2676,
148 + outputTokens: 4506,
149 + reasoningTokens: 0,
150 + },
151 + reportedCostUsd: null,
152 + speed: "standard",
153 + dedupeKey: "msg_1:req_1",
154 + };
155 + const expected = [
156 + executor,
157 + {
158 + ...executor,
159 + model: "claude-fable-5-1",
160 + totals: {
161 + uncachedInputTokens: 54204,
162 + cachedInputTokens: 0,
163 + cacheCreationTokens: 0,
164 + outputTokens: 4229,
165 + reasoningTokens: 0,
166 + },
167 + dedupeKey: expect.any(String),
168 + },
169 + ];
170 +
171 + expect(parse(advisorTurn)).toEqual(expected);
172 + expect(claudeUsageFormat.parseProjected(advisorTurn, undefined)).toEqual(expected);
173 + });
174 +
175 + it("keys the advisor record apart from the executor and the same across a message's content blocks", () => {
176 + const block = (type: string) =>
177 + parse({ ...advisorTurn, message: { ...advisorTurn.message, content: [{ type }] } });
178 + const [executorKey, advisorKey] = block("text").map((record) => record.dedupeKey);
179 +
180 + expect(advisorKey).toEqual(expect.any(String));
181 + expect(advisorKey).not.toBe(executorKey);
182 + expect(block("tool_use").map((record) => record.dedupeKey)).toEqual([executorKey, advisorKey]);
183 +
184 + const unkeyed = parse({
185 + ...advisorTurn,
186 + requestId: undefined,
187 + message: { ...advisorTurn.message, id: undefined },
188 + });
189 + expect(unkeyed.map((record) => record.dedupeKey)).toEqual([null, null]);
190 + });
191 +
192 + it("emits only the executor record without an advisor iteration", () => {
193 + const executorIterations = advisorTurn.message.usage.iterations.filter(
194 + (iteration) => iteration.type === "message",
195 + );
196 +
197 + expect(parse(withIterations(executorIterations))).toHaveLength(1);
198 + expect(claudeUsageFormat.parseProjected(withIterations([]), undefined)).toHaveLength(1);
199 + expect(
200 + claudeUsageFormat.parseLine(
201 + claudeLine({ messageId: "msg_3", contentType: "text" }),
202 + undefined,
203 + ),
204 + ).toHaveLength(1);
205 + });
206 +
207 + it("ignores advisor iterations without a usable model and malformed entries", () => {
208 + const unusable = withIterations([
209 + null,
210 + "advisor_message",
211 + { type: "advisor_message", input_tokens: 5 },
212 + { type: "advisor_message", model: "", input_tokens: 5 },
213 + { type: "advisor_message", model: "<synthetic>", input_tokens: 5 },
214 + ]);
215 +
216 + expect(parse(unusable).map((record) => record.model)).toEqual(["claude-opus-5-5"]);
217 + });
218 +});
apps/server/src/provider/Drivers/claudeUsage.ts
+45 -15
@@ -20,35 +20,35 @@ export const CLAUDE_SYNTHETIC_MODEL = "<synthetic>";
20 * the same complete `usage` object for the parent message. Summing them overcounts by roughly
21 * 2.4x on a real workload, so the caller must drop repeats by `dedupeKey` and keep the first.
22 */
23 -export function parseClaudeLine(line: string): UsageRecord | null {
23 +export function parseClaudeLine(line: string): readonly UsageRecord[] {
24 let parsed: unknown;
25 try {
26 parsed = JSON.parse(line);
27 } catch {
28 - return null;
28 + return [];
29 }
30 return parseClaudeRecord(parsed);
31 }
32
33 -function parseClaudeRecord(parsed: unknown): UsageRecord | null {
34 - if (typeof parsed !== "object" || parsed === null) return null;
33 +function parseClaudeRecord(parsed: unknown): readonly UsageRecord[] {
34 + if (typeof parsed !== "object" || parsed === null) return [];
35
36 const record = parsed as Record<string, unknown>;
37 - if (record["type"] !== "assistant") return null;
37 + if (record["type"] !== "assistant") return [];
38
39 const message = record["message"];
40 - if (typeof message !== "object" || message === null) return null;
40 + if (typeof message !== "object" || message === null) return [];
41 const messageRecord = message as Record<string, unknown>;
42
43 const usage = messageRecord["usage"];
44 - if (typeof usage !== "object" || usage === null) return null;
44 + if (typeof usage !== "object" || usage === null) return [];
45 const usageRecord = usage as Record<string, unknown>;
46
47 const timestampMs = parseTimestampMs(record["timestamp"]);
48 - if (timestampMs === null) return null;
48 + if (timestampMs === null) return [];
49
50 const model = typeof messageRecord["model"] === "string" ? messageRecord["model"] : "";
51 - if (model.length === 0 || model === CLAUDE_SYNTHETIC_MODEL) return null;
51 + if (model.length === 0 || model === CLAUDE_SYNTHETIC_MODEL) return [];
52
53 const messageId = typeof messageRecord["id"] === "string" ? messageRecord["id"] : null;
54 const requestId = typeof record["requestId"] === "string" ? record["requestId"] : null;
@@ -57,7 +57,7 @@ function parseClaudeRecord(parsed: unknown): UsageRecord | null {
57
58 const cost = record["costUSD"];
59
60 - return {
60 + const executor: UsageRecord = {
61 provider: "claude",
62 timestampMs,
63 model,
@@ -74,10 +74,40 @@ function parseClaudeRecord(parsed: unknown): UsageRecord | null {
74 speed: usageRecord["speed"] === "fast" ? "fast" : "standard",
75 dedupeKey,
76 };
77 -}
77
79 -const orEmpty = (record: UsageRecord | null): readonly UsageRecord[] =>
80 - record === null ? [] : [record];
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 + const records = [executor];
81 + const iterations: readonly unknown[] = Array.isArray(usageRecord["iterations"])
82 + ? usageRecord["iterations"]
83 + : [];
84 + 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;
91 +
92 + records.push({
93 + provider: "claude",
94 + timestampMs,
95 + model: advisorModel,
96 + 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 + },
104 + reportedCostUsd: null,
105 + speed: executor.speed,
106 + dedupeKey: dedupeKey === null ? null : `${dedupeKey}:advisor:${index}`,
107 + });
108 + }
109 + return records;
110 +}
111
112 export const claudeUsageFormat: TranscriptUsageFormat<void> = {
113 selectFields: {
@@ -89,8 +119,8 @@ export const claudeUsageFormat: TranscriptUsageFormat<void> = {
119 message: { id: true, model: true, usage: true },
120 },
121 usageMarkers: ['"usage"'],
92 - parseLine: (line) => orEmpty(parseClaudeLine(line)),
93 - parseProjected: (projected) => orEmpty(parseClaudeRecord(projected)),
122 + parseLine: (line) => parseClaudeLine(line),
123 + parseProjected: (projected) => parseClaudeRecord(projected),
124 };
125
126 export const claudeUsageReader: ProviderUsageReader<ClaudeSettings, Path.Path> = {
apps/server/src/usage/UsageService.test.ts
+1 -1
@@ -2299,7 +2299,7 @@ describe("UsageService", () => {
2299 60 + 20 * (version === 4 ? 1 : 2),
2300 );
2301 yield* upgraded.awaitPersisted;
2302 - assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v9.json");
2302 + assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v10.json");
2303 assert.strictEqual(
2304 yield* Effect.promise(() => NodeFSP.readFile(legacyPath, "utf8")),
2305 legacy,
apps/server/src/usage/usageScanCache.test.ts
+17 -1
@@ -218,7 +218,11 @@ describe("scan cache round trip", () => {
218 };
219
220 const restored = decodeScanCache(JSON.parse(JSON.stringify(legacy)), TEST_FORMAT_MAP);
221 - expect(restored.get("/gone.jsonl")).toEqual(original.get("/gone.jsonl"));
221 + expect(restored.get("/gone.jsonl")).toEqual({
222 + ...original.get("/gone.jsonl"),
223 + size: -1,
224 + position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
225 + });
226 expect(restored.get("/gone-codex.jsonl")).toEqual({
227 ...original.get("/gone-codex.jsonl"),
228 size: -1,
@@ -245,6 +249,18 @@ describe("scan cache round trip", () => {
249 },
250 );
251
252 + it("re-parses a Claude transcript cached before advisor usage was counted", () => {
253 + const original = cacheWith([["/claude.jsonl", 100, [record()]]]);
254 + const v9 = { ...encodeScanCache(original), version: 9 };
255 +
256 + const restored = decodeScanCache(JSON.parse(JSON.stringify(v9)), TEST_FORMAT_MAP);
257 + expect(restored.get("/claude.jsonl")).toEqual({
258 + ...original.get("/claude.jsonl"),
259 + size: -1,
260 + position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
261 + });
262 + });
263 +
264 it("rewrites only changed entries and still restores the whole cache", () => {
265 const write = makeScanCacheWriter();
266 const cache = cacheWith([
apps/server/src/usage/usageScanCache.ts
+8 -5
@@ -9,7 +9,7 @@ import * as Schema from "effect/Schema";
9 import { CLAUDE_SYNTHETIC_MODEL } from "../provider/Drivers/claudeUsage.ts";
10 import { GUARD_LENGTH, type TranscriptParsePosition } from "./usageTranscriptReader.ts";
11
12 -const USAGE_SCAN_CACHE_VERSION = 9 as const;
12 +const USAGE_SCAN_CACHE_VERSION = 10 as const;
13 const SPEED_COMPATIBLE_SINCE_VERSION = 4;
14
15 /**
@@ -17,8 +17,9 @@ const SPEED_COMPATIBLE_SINCE_VERSION = 4;
17 * replace it, dropping saved usage for deleted transcripts. Separate files keep
18 * both.
19 */
20 -export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v9.json";
20 +export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v10.json";
21 export const LEGACY_SCAN_CACHE_FILE_NAMES = [
22 + "usage-scan-cache-v9.json",
23 "usage-scan-cache-v8.json",
24 "usage-scan-cache-v7.json",
25 "usage-scan-cache-v6.json",
@@ -291,10 +292,12 @@ export function decodeScanCache(
292 }
293 // Keep history for deleted rollouts, but re-parse incompatible reducer state.
294 // v9 repairs skipped initial Codex requests; clean v8 entries can still resume.
295 + // v10 adds Claude advisor calls, which no older Claude entry holds.
296 const legacy =
295 - format.state !== undefined &&
296 - (version < 6 ||
297 - (entry.p === "codex" && (version < 8 || (version < 9 && malformedRecords > 0))));
297 + (entry.p === "claude" && version < 10) ||
298 + (format.state !== undefined &&
299 + (version < 6 ||
300 + (entry.p === "codex" && (version < 8 || (version < 9 && malformedRecords > 0)))));
301 // A corrupt state disqualifies the entry: resuming with it would attach
302 // appended usage to the wrong model or replay fork-copied history.
303 if (!legacy && !isValidState.get(entry.p)?.(entry.cs)) continue;