Skip to content

Commit 172b99f

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 2577baf commit 172b99f

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
@@ -70,4 +70,15 @@ describe("parseClaudeLine", () => {
7070
expect(parseClaudeLine(JSON.stringify({ type: "user", message: {} }))).toBeNull();
7171
expect(parseClaudeLine("not json")).toBeNull();
7272
});
73+
74+
it("ignores messages Claude Code generated locally", () => {
75+
const line = (model: string) =>
76+
parseClaudeLine(
77+
claudeLine({ messageId: "msg_3", contentType: "text", outputTokens: 0, model }),
78+
);
79+
80+
expect(line("<synthetic>")).toBeNull();
81+
// A real response is billed under its own model, whatever it says.
82+
expect(line("claude-fable-5")?.model).toBe("claude-fable-5");
83+
});
7384
});

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

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

15+
/**
16+
* Claude Code stamps this model on messages it writes locally, such as the
17+
* placeholder after an interrupted turn. They never reached the API, so they
18+
* are not usage.
19+
*/
20+
export const CLAUDE_SYNTHETIC_MODEL = "<synthetic>";
21+
1522
/**
1623
* T3 Code writes one record per assistant *content block*, and every one of those records repeats
1724
* the same complete `usage` object for the parent message. Summing them overcounts by roughly
@@ -45,7 +52,7 @@ function parseClaudeRecord(parsed: unknown): UsageRecord | null {
4552
if (timestampMs === null) return null;
4653

4754
const model = typeof messageRecord["model"] === "string" ? messageRecord["model"] : "";
48-
if (model.length === 0) return null;
55+
if (model.length === 0 || model === CLAUDE_SYNTHETIC_MODEL) return null;
4956

5057
const messageId = typeof messageRecord["id"] === "string" ? messageRecord["id"] : null;
5158
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
@@ -157,6 +157,16 @@ describe("scan cache round trip", () => {
157157
expect(decodeScanCache(JSON.parse(JSON.stringify(previous)), TEST_FORMAT_MAP).size).toBe(0);
158158
});
159159

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

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

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

9+
import { CLAUDE_SYNTHETIC_MODEL } from "../provider/Drivers/claudeUsage.ts";
910
import { GUARD_LENGTH, type TranscriptParsePosition } from "./usageTranscriptReader.ts";
1011

1112
const USAGE_SCAN_CACHE_VERSION = 5 as const;
@@ -236,6 +237,9 @@ export function decodeScanCache(
236237
) {
237238
return null;
238239
}
240+
// Entries cached before the parser skipped these still hold them, and a
241+
// transcript that never grows is never re-parsed.
242+
if (provider === "claude" && model === CLAUDE_SYNTHETIC_MODEL) continue;
239243

240244
records.push({
241245
provider,

0 commit comments

Comments
 (0)