Skip to content

Commit 05826ad

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. Entries already in the scan cache drop them when the cache loads, so already-scanned transcripts lose them too without a cold scan. Fixes #14670
1 parent 81493ef commit 05826ad

4 files changed

Lines changed: 33 additions & 1 deletion

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,15 @@ describe("parseClaudeLine", () => {
7171
expect(parseClaudeLine(JSON.stringify({ type: "user", message: {} }))).toBeNull();
7272
expect(parseClaudeLine("not json")).toBeNull();
7373
});
74+
75+
it("ignores messages Claude Code generated locally", () => {
76+
const line = (model: string) =>
77+
parseClaudeLine(
78+
claudeLine({ messageId: "msg_3", contentType: "text", outputTokens: 0, model }),
79+
);
80+
81+
expect(line("<synthetic>")).toBeNull();
82+
// A real response is billed under its own model, whatever it says.
83+
expect(line("claude-fable-5")?.model).toBe("claude-fable-5");
84+
});
7485
});

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ import {
1919
} from "@t3tools/provider-core/server/usage";
2020
import * as HostProcess from "@t3tools/shared/HostProcess";
2121

22+
/**
23+
* Claude Code stamps this model on messages it writes locally, such as the
24+
* placeholder after an interrupted turn. They never reached the API, so they
25+
* are not usage.
26+
*/
27+
export const CLAUDE_SYNTHETIC_MODEL = "<synthetic>";
28+
2229
/**
2330
* Parses one line of a Claude Code transcript.
2431
*
@@ -55,7 +62,7 @@ function parseClaudeRecord(parsed: unknown): UsageRecord | null {
5562
if (timestampMs === null) return null;
5663

5764
const model = typeof messageRecord["model"] === "string" ? messageRecord["model"] : "";
58-
if (model.length === 0) return null;
65+
if (model.length === 0 || model === CLAUDE_SYNTHETIC_MODEL) return null;
5966

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ describe("scan cache round trip", () => {
161161
expect(decodeScanCache(JSON.parse(JSON.stringify(previous)), TEST_FORMAT_MAP).size).toBe(0);
162162
});
163163

164+
it("drops Claude's locally generated messages from entries cached with them", () => {
165+
const kept = record();
166+
const encoded = encodeScanCache(
167+
cacheWith([["/a.jsonl", 100, [kept, record({ model: "<synthetic>", dedupeKey: "msg_2:" })]]]),
168+
);
169+
170+
const restored = decodeScanCache(JSON.parse(JSON.stringify(encoded)), TEST_FORMAT_MAP);
171+
expect(restored.get("/a.jsonl")?.records).toEqual([kept]);
172+
});
173+
164174
it("rewrites only changed entries and still restores the whole cache", () => {
165175
const write = makeScanCacheWriter();
166176
const cache = cacheWith([

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type {
2222
} from "@t3tools/provider-core/server/usage";
2323
import * as Schema from "effect/Schema";
2424

25+
import { CLAUDE_SYNTHETIC_MODEL } from "../provider/Drivers/claudeUsage.ts";
2526
import { GUARD_LENGTH, type TranscriptParsePosition } from "./usageTranscriptReader.ts";
2627

2728
// v2: Codex fork-copy suppression changed what a file parses to, so v1
@@ -289,6 +290,9 @@ export function decodeScanCache(
289290
) {
290291
return null;
291292
}
293+
// Entries cached before the parser skipped these still hold them, and a
294+
// transcript that never grows is never re-parsed.
295+
if (provider === "claude" && model === CLAUDE_SYNTHETIC_MODEL) continue;
292296

293297
records.push({
294298
provider,

0 commit comments

Comments
 (0)