Skip to content

Commit 46897b7

Browse files
committed
fix(usage): Claude usage no longer lists a <synthetic> model
Claude Code stamps `<synthetic>` on messages it writes locally, such as the placeholder after an interrupted turn. They never reached the API, so the usage parser now drops them instead of giving them a model bucket. The scan cache version is bumped so already-scanned transcripts drop them too. Fixes #14670
1 parent a3abb52 commit 46897b7

4 files changed

Lines changed: 18 additions & 3 deletions

File tree

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

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

142142
it("rejects a document from the previous cache version", () => {
143143
const encoded = encodeScanCache(cacheWith([["/a.jsonl", 100, [record()]]]));
144-
const previous = { ...encoded, version: 3 };
144+
const previous = { ...encoded, version: 4 };
145145

146146
expect(decodeScanCache(JSON.parse(JSON.stringify(previous))).size).toBe(0);
147147
});

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import type { CodexScanState, UsageRecord } from "./usageTranscripts.ts";
2424
// v3: entries carry the parse position and reducer state so a grown file
2525
// re-parses only its appended bytes instead of starting over.
2626
// v4: records carry Claude fast mode, which v3 rows never captured.
27-
const USAGE_SCAN_CACHE_VERSION = 4 as const;
27+
// v5: Claude's `<synthetic>` placeholder messages are no longer records, and
28+
// v4 entries would keep serving them for every transcript that never grows.
29+
const USAGE_SCAN_CACHE_VERSION = 5 as const;
2830

2931
export interface CachedFile {
3032
readonly size: number;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ describe("parseClaudeLine", () => {
7878
expect(parseClaudeLine(JSON.stringify({ type: "user", message: {} }))).toBeNull();
7979
expect(parseClaudeLine("not json")).toBeNull();
8080
});
81+
82+
it("ignores messages Claude Code generated locally", () => {
83+
const line = (model: string) =>
84+
parseClaudeLine(
85+
claudeLine({ messageId: "msg_3", contentType: "text", outputTokens: 0, model }),
86+
);
87+
88+
expect(line("<synthetic>")).toBeNull();
89+
// A real response is billed under its own model, whatever it says.
90+
expect(line("claude-fable-5")?.model).toBe("claude-fable-5");
91+
});
8192
});
8293

8394
describe("parseCodexLine", () => {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ export function parseClaudeRecord(parsed: unknown): UsageRecord | null {
134134
if (timestampMs === null) return null;
135135

136136
const model = typeof messageRecord["model"] === "string" ? messageRecord["model"] : "";
137-
if (model.length === 0) return null;
137+
// Claude Code stamps `<synthetic>` on messages it writes locally, such as the
138+
// placeholder after an interrupted turn. They never reached the API.
139+
if (model.length === 0 || model === "<synthetic>") return null;
138140

139141
const messageId = typeof messageRecord["id"] === "string" ? messageRecord["id"] : null;
140142
const requestId = typeof record["requestId"] === "string" ? record["requestId"] : null;

0 commit comments

Comments
 (0)