Skip to content

Commit e8cb5ed

Browse files
committed
fix(server): include Claude advisor calls in usage totals
1 parent c0d4e95 commit e8cb5ed

6 files changed

Lines changed: 206 additions & 20 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ describe("scan cache round trip", () => {
125125

126126
it("rejects a document from the previous cache version", () => {
127127
const encoded = encodeScanCache(cacheWith([["/a.jsonl", 100, [record()]]]));
128-
const previous = { ...encoded, version: 2 };
128+
const previous = { ...encoded, version: 3 };
129129

130130
expect(decodeScanCache(JSON.parse(JSON.stringify(previous))).size).toBe(0);
131131
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import type { CodexScanState, UsageRecord } from "./usageTranscripts.ts";
2626
// entries would keep serving double-counted records forever.
2727
// v3: entries carry the parse position and reducer state so a grown file
2828
// re-parses only its appended bytes instead of starting over.
29-
export const USAGE_SCAN_CACHE_VERSION = 3 as const;
29+
// v4: re-parse existing Claude transcripts to include nested advisor usage.
30+
export const USAGE_SCAN_CACHE_VERSION = 4 as const;
3031

3132
export interface CachedFile {
3233
readonly size: number;

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import * as NodePath from "node:path";
88
import { afterEach, assert, beforeEach, describe, it } from "@effect/vitest";
99

1010
import { readTranscriptRecords } from "./usageTranscriptReader.ts";
11+
import { UsageAggregator } from "./usageAggregation.ts";
12+
import { dedupeWithinFile } from "./usageScanCache.ts";
13+
import { parseRateTable } from "./usagePricing.ts";
1114

1215
let dir: string;
1316

@@ -61,6 +64,65 @@ function codexUsageLine(outputTokens: number, secondsOffset: number): string {
6164
}
6265

6366
describe("readTranscriptRecords resume", () => {
67+
it("counts and prices an advisor arriving after the executor, once across replays", async () => {
68+
const path = NodePath.join(dir, "advisor.jsonl");
69+
const executor = JSON.parse(claudeLine(1, 5));
70+
executor.message.usage.iterations = [
71+
{ type: "message", input_tokens: 10, output_tokens: 5 },
72+
{ type: "advisor_message", model: "claude-opus-5" },
73+
];
74+
await NodeFSP.writeFile(path, JSON.stringify(executor) + "\n");
75+
const first = await readTranscriptRecords(path, "claude");
76+
assert.isNotNull(first);
77+
assert.strictEqual(first.records.length, 1);
78+
79+
executor.message.usage.iterations = [
80+
{ type: "message", input_tokens: 10, output_tokens: 5 },
81+
{ type: "advisor_message", model: "claude-opus-5", input_tokens: 100, output_tokens: 20 },
82+
];
83+
const advisorLine = JSON.stringify(executor) + "\n";
84+
executor.message.usage.iterations.push({
85+
type: "advisor_message",
86+
model: "claude-opus-5",
87+
input_tokens: 200,
88+
output_tokens: 40,
89+
});
90+
const secondAdvisorLine = JSON.stringify(executor) + "\n";
91+
await NodeFSP.appendFile(path, advisorLine + secondAdvisorLine + secondAdvisorLine);
92+
const appended = await readTranscriptRecords(path, "claude", first.position);
93+
assert.isNotNull(appended);
94+
assert.isTrue(appended.resumed);
95+
const records = dedupeWithinFile([...first.records, ...appended.records]);
96+
assert.strictEqual(records.length, 3);
97+
98+
const replayPath = NodePath.join(dir, "replayed.jsonl");
99+
await NodeFSP.writeFile(replayPath, secondAdvisorLine);
100+
const replay = await readTranscriptRecords(replayPath, "claude");
101+
assert.isNotNull(replay);
102+
const aggregator = new UsageAggregator({
103+
timeZone: "UTC",
104+
sinceDay: "2026-08-01",
105+
untilDay: "2026-08-01",
106+
rates: parseRateTable({
107+
"claude-fable-5": { input_cost_per_token: 1e-5, output_cost_per_token: 5e-5 },
108+
"claude-opus-5": { input_cost_per_token: 5e-6, output_cost_per_token: 2.5e-5 },
109+
}),
110+
});
111+
for (const record of [...records, ...replay.records]) aggregator.add(record);
112+
const buckets = aggregator.finish().buckets;
113+
assert.strictEqual(buckets.length, 2);
114+
const advisor = buckets.find((bucket) => bucket.model === "claude-opus-5");
115+
assert.isDefined(advisor);
116+
assert.strictEqual(advisor.records, 2);
117+
assert.strictEqual(advisor.totals.uncachedInputTokens, 300);
118+
assert.strictEqual(advisor.totals.outputTokens, 60);
119+
assert.approximately(advisor.costUsd, 0.003, 1e-10);
120+
assert.strictEqual(
121+
buckets.find((bucket) => bucket.model === "claude-fable-5")?.totals.outputTokens,
122+
5,
123+
);
124+
});
125+
64126
it("parses only appended lines when resuming a grown file", async () => {
65127
const path = NodePath.join(dir, "claude.jsonl");
66128
await NodeFSP.writeFile(path, claudeLine(1, 5) + claudeLine(2, 7));

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ export async function readTranscriptRecords(
235235
for (const grokRecord of parseGrokLine(line)) out.push(grokRecord);
236236
return;
237237
}
238-
const record = parseClaudeLine(line);
239-
if (record !== null) out.push(record);
238+
for (const record of parseClaudeLine(line)) out.push(record);
240239
};
241240

242241
const toLineString = (lineBuffer: Buffer): string => {

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

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ function claudeLine(overrides: {
3838

3939
describe("parseClaudeLine", () => {
4040
it("extracts token totals and a dedupe key", () => {
41-
const record = parseClaudeLine(claudeLine({ messageId: "msg_1", contentType: "text" }));
41+
const [record] = parseClaudeLine(claudeLine({ messageId: "msg_1", contentType: "text" }));
4242

43-
expect(record).not.toBeNull();
43+
expect(record).toBeDefined();
4444
expect(record?.provider).toBe("claude");
4545
expect(record?.model).toBe("claude-fable-5");
4646
expect(record?.totals).toEqual({
@@ -56,16 +56,110 @@ describe("parseClaudeLine", () => {
5656
it("gives every content block of one message the same dedupe key", () => {
5757
// T3 Code writes one record per content block, each repeating the parent
5858
// message's full usage. Summing them would overcount ~2.4x on real data.
59-
const text = parseClaudeLine(claudeLine({ messageId: "msg_2", contentType: "text" }));
60-
const toolUse = parseClaudeLine(claudeLine({ messageId: "msg_2", contentType: "tool_use" }));
59+
const [text] = parseClaudeLine(claudeLine({ messageId: "msg_2", contentType: "text" }));
60+
const [toolUse] = parseClaudeLine(claudeLine({ messageId: "msg_2", contentType: "tool_use" }));
6161

6262
expect(text?.dedupeKey).toBe(toolUse?.dedupeKey);
6363
expect(text?.totals).toEqual(toolUse?.totals);
6464
});
6565

6666
it("ignores records that are not assistant messages", () => {
67-
expect(parseClaudeLine(JSON.stringify({ type: "user", message: {} }))).toBeNull();
68-
expect(parseClaudeLine("not json")).toBeNull();
67+
expect(parseClaudeLine(JSON.stringify({ type: "user", message: {} }))).toEqual([]);
68+
expect(parseClaudeLine("not json")).toEqual([]);
69+
});
70+
71+
it("separates advisor calls without counting executor iterations twice", () => {
72+
const records = parseClaudeLine(
73+
JSON.stringify({
74+
type: "assistant",
75+
timestamp: "2026-08-07T04:05:13.944Z",
76+
sessionId: "session",
77+
requestId: "request",
78+
costUSD: 0.5,
79+
message: {
80+
id: "message",
81+
model: "claude-sonnet-5",
82+
usage: {
83+
input_tokens: 30,
84+
output_tokens: 10,
85+
iterations: [
86+
{ type: "message", input_tokens: 10, output_tokens: 4 },
87+
{
88+
type: "advisor_message",
89+
model: "claude-opus-5",
90+
input_tokens: 100,
91+
cache_read_input_tokens: 50,
92+
cache_creation_input_tokens: 20,
93+
output_tokens: 40,
94+
},
95+
{ type: "message", input_tokens: 20, output_tokens: 6 },
96+
{
97+
type: "advisor_message",
98+
model: "claude-opus-5",
99+
input_tokens: 200,
100+
output_tokens: 60,
101+
},
102+
],
103+
},
104+
},
105+
}),
106+
);
107+
108+
expect(records.map((record) => record.model)).toEqual([
109+
"claude-sonnet-5",
110+
"claude-opus-5",
111+
"claude-opus-5",
112+
]);
113+
expect(records.map((record) => totalTokens(record.totals))).toEqual([40, 210, 260]);
114+
expect(new Set(records.map((record) => record.dedupeKey)).size).toBe(3);
115+
expect(records.map((record) => record.reportedCostUsd)).toEqual([0.5, null, null]);
116+
expect(records[1]?.totals).toEqual({
117+
uncachedInputTokens: 100,
118+
cachedInputTokens: 50,
119+
cacheCreationTokens: 20,
120+
outputTokens: 40,
121+
reasoningTokens: 0,
122+
});
123+
});
124+
125+
it("ignores malformed advisor entries and preserves calls without message IDs", () => {
126+
const line = {
127+
type: "assistant",
128+
timestamp: "2026-08-07T04:05:13.944Z",
129+
message: {
130+
model: "claude-sonnet-5",
131+
usage: {
132+
input_tokens: 1,
133+
iterations: [
134+
null,
135+
5,
136+
{ type: "advisor_message" },
137+
{ type: "advisor_message", model: " " },
138+
{
139+
type: "advisor_message",
140+
model: "claude-opus-5",
141+
input_tokens: 100,
142+
output_tokens: 20,
143+
},
144+
],
145+
},
146+
},
147+
};
148+
const records = parseClaudeLine(JSON.stringify(line));
149+
expect(records).toHaveLength(2);
150+
expect(records.map((record) => record.dedupeKey)).toEqual([null, null]);
151+
expect(records[1]?.totals.outputTokens).toBe(20);
152+
expect(
153+
parseClaudeLine(
154+
JSON.stringify({
155+
...line,
156+
message: {
157+
...line.message,
158+
usage: { input_tokens: 1, iterations: {} },
159+
},
160+
}),
161+
),
162+
).toHaveLength(1);
69163
});
70164
});
71165

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

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,38 +89,38 @@ export function grokCostTicksToUsd(ticks: unknown): number | null {
8989
/* -------------------------------------------------------------------------- */
9090

9191
/**
92-
* Parses one line of a Claude Code transcript.
92+
* Parses the executor and any advisor usage from a Claude Code transcript line.
9393
*
9494
* T3 Code writes one record per assistant *content block*, and every one of
9595
* those records repeats the same complete `usage` object for the parent
9696
* message. Summing them overcounts by roughly 2.4x on a real workload, so the
9797
* caller must drop repeats by `dedupeKey` and keep the first.
9898
*/
99-
export function parseClaudeLine(line: string): UsageRecord | null {
99+
export function parseClaudeLine(line: string): readonly UsageRecord[] {
100100
let parsed: unknown;
101101
try {
102102
parsed = JSON.parse(line);
103103
} catch {
104-
return null;
104+
return [];
105105
}
106-
if (typeof parsed !== "object" || parsed === null) return null;
106+
if (typeof parsed !== "object" || parsed === null) return [];
107107

108108
const record = parsed as Record<string, unknown>;
109-
if (record["type"] !== "assistant") return null;
109+
if (record["type"] !== "assistant") return [];
110110

111111
const message = record["message"];
112-
if (typeof message !== "object" || message === null) return null;
112+
if (typeof message !== "object" || message === null) return [];
113113
const messageRecord = message as Record<string, unknown>;
114114

115115
const usage = messageRecord["usage"];
116-
if (typeof usage !== "object" || usage === null) return null;
116+
if (typeof usage !== "object" || usage === null) return [];
117117
const usageRecord = usage as Record<string, unknown>;
118118

119119
const timestampMs = parseTimestampMs(record["timestamp"]);
120-
if (timestampMs === null) return null;
120+
if (timestampMs === null) return [];
121121

122122
const model = typeof messageRecord["model"] === "string" ? messageRecord["model"] : "";
123-
if (model.length === 0) return null;
123+
if (model.length === 0) return [];
124124

125125
const messageId = typeof messageRecord["id"] === "string" ? messageRecord["id"] : null;
126126
const requestId = typeof record["requestId"] === "string" ? record["requestId"] : null;
@@ -131,7 +131,7 @@ export function parseClaudeLine(line: string): UsageRecord | null {
131131

132132
const cost = record["costUSD"];
133133

134-
return {
134+
const executor: UsageRecord = {
135135
provider: "claude",
136136
timestampMs,
137137
model,
@@ -147,6 +147,36 @@ export function parseClaudeLine(line: string): UsageRecord | null {
147147
reportedCostUsd: typeof cost === "number" && Number.isFinite(cost) ? cost : null,
148148
dedupeKey,
149149
};
150+
151+
const records = [executor];
152+
const iterations = usageRecord["iterations"];
153+
if (!Array.isArray(iterations)) return records;
154+
155+
// Top-level usage includes executor iterations only. Advisor sub-inferences
156+
// have their own model and rates; ordinary "message" iterations are already counted.
157+
for (const [index, raw] of iterations.entries()) {
158+
if (typeof raw !== "object" || raw === null) continue;
159+
const iteration = raw as Record<string, unknown>;
160+
if (iteration["type"] !== "advisor_message") continue;
161+
const advisorModel = iteration["model"];
162+
if (typeof advisorModel !== "string" || advisorModel.trim().length === 0) continue;
163+
const advisor: UsageRecord = {
164+
...executor,
165+
model: advisorModel,
166+
totals: {
167+
uncachedInputTokens: int(iteration["input_tokens"]),
168+
cachedInputTokens: int(iteration["cache_read_input_tokens"]),
169+
cacheCreationTokens: int(iteration["cache_creation_input_tokens"]),
170+
outputTokens: int(iteration["output_tokens"]),
171+
reasoningTokens: 0,
172+
},
173+
// Legacy costUSD remains executor cost; do not copy it to advisor calls.
174+
reportedCostUsd: null,
175+
dedupeKey: dedupeKey === null ? null : `${dedupeKey}:advisor:${index}`,
176+
};
177+
if (totalTokens(advisor.totals) > 0) records.push(advisor);
178+
}
179+
return records;
150180
}
151181

152182
/* -------------------------------------------------------------------------- */

0 commit comments

Comments
 (0)