Skip to content

Commit f34ba43

Browse files
fix(usage): Claude usage no longer lists a <synthetic> model (#14672)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 5a264a8 commit f34ba43

4 files changed

Lines changed: 75 additions & 11 deletions

File tree

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "@effect/vitest";
22

3-
import { parseClaudeLine } from "./claudeUsage.ts";
3+
import { claudeUsageFormat, parseClaudeLine } from "./claudeUsage.ts";
44

55
function claudeLine(overrides: {
66
messageId: string;
@@ -66,6 +66,35 @@ describe("parseClaudeLine", () => {
6666
expect(text?.totals).toEqual(toolUse?.totals);
6767
});
6868

69+
it("skips local placeholders in both reader paths but keeps real zero-token responses", () => {
70+
const projected = {
71+
type: "assistant",
72+
timestamp: "2026-08-07T04:05:13.944Z",
73+
message: {
74+
model: "claude-fable-5",
75+
usage: { input_tokens: 0, output_tokens: 0 },
76+
},
77+
};
78+
const expected = [
79+
expect.objectContaining({
80+
model: "claude-fable-5",
81+
totals: {
82+
uncachedInputTokens: 0,
83+
cachedInputTokens: 0,
84+
cacheCreationTokens: 0,
85+
outputTokens: 0,
86+
reasoningTokens: 0,
87+
},
88+
}),
89+
];
90+
expect(claudeUsageFormat.parseLine(JSON.stringify(projected), undefined)).toEqual(expected);
91+
expect(claudeUsageFormat.parseProjected(projected, undefined)).toEqual(expected);
92+
93+
const synthetic = { ...projected, message: { ...projected.message, model: "<synthetic>" } };
94+
expect(claudeUsageFormat.parseLine(JSON.stringify(synthetic), undefined)).toEqual([]);
95+
expect(claudeUsageFormat.parseProjected(synthetic, undefined)).toEqual([]);
96+
});
97+
6998
it("ignores records that are not assistant messages", () => {
7099
expect(parseClaudeLine(JSON.stringify({ type: "user", message: {} }))).toBeNull();
71100
expect(parseClaudeLine("not json")).toBeNull();

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

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

15+
// Claude Code uses this model for local placeholders that never reached the API.
16+
export const CLAUDE_SYNTHETIC_MODEL = "<synthetic>";
17+
1518
/**
1619
* T3 Code writes one record per assistant *content block*, and every one of those records repeats
1720
* the same complete `usage` object for the parent message. Summing them overcounts by roughly
@@ -45,7 +48,7 @@ function parseClaudeRecord(parsed: unknown): UsageRecord | null {
4548
if (timestampMs === null) return null;
4649

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

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

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,40 @@ function cacheWith(entries: readonly [string, number, readonly UsageRecord[]][])
5959
}
6060

6161
describe("scan cache round trip", () => {
62-
it("restores records unchanged", () => {
62+
it("restores cached history unchanged except for Claude local placeholders", () => {
6363
const original = cacheWith([
6464
[
6565
"/a.jsonl",
6666
100,
6767
[record(), record({ dedupeKey: "msg_2:", model: "claude-opus-5-5", speed: "fast" })],
6868
],
69-
["/b.jsonl", 200, [record({ sessionId: "session-b", reportedCostUsd: 1.5 })]],
69+
[
70+
"/b.jsonl",
71+
200,
72+
[
73+
record({
74+
sessionId: "session-b",
75+
reportedCostUsd: 1.5,
76+
totals: {
77+
uncachedInputTokens: 0,
78+
cachedInputTokens: 0,
79+
cacheCreationTokens: 0,
80+
outputTokens: 0,
81+
reasoningTokens: 0,
82+
},
83+
}),
84+
],
85+
],
7086
]);
7187
original.set("/grok.jsonl", {
7288
size: 40,
7389
mtimeMs: 300,
7490
provider: "grok",
7591
records: [
7692
record({ provider: "grok", model: "grok-4.5-build", dedupeKey: "s:p:grok-4.5-build" }),
93+
record({ provider: "grok", model: "<synthetic>", dedupeKey: "s:p:<synthetic>" }),
7794
],
78-
tailRecords: [record({ provider: "grok", model: "grok-4.5-build", dedupeKey: null })],
95+
tailRecords: [record({ provider: "grok", model: "<synthetic>", dedupeKey: null })],
7996
position: position({ resumeOffset: 30, guardLength: 30, guardHash: 123 }),
8097
});
8198
original.set("/codex.jsonl", {
@@ -99,16 +116,26 @@ describe("scan cache round trip", () => {
99116
}),
100117
});
101118

119+
original.set("/a.jsonl", {
120+
...original.get("/a.jsonl")!,
121+
tailRecords: [record({ dedupeKey: "msg_tail:" })],
122+
});
123+
const claude = original.get("/a.jsonl")!;
124+
const cached = new Map(original);
125+
cached.set("/a.jsonl", {
126+
...claude,
127+
records: [...claude.records, record({ model: "<synthetic>" })],
128+
tailRecords: [
129+
...claude.tailRecords,
130+
record({ model: "<synthetic>", dedupeKey: "msg_tail:" }),
131+
],
132+
});
102133
const restored = decodeScanCache(
103-
JSON.parse(JSON.stringify(encodeScanCache(original))),
134+
JSON.parse(JSON.stringify(encodeScanCache(cached))),
104135
TEST_FORMAT_MAP,
105136
);
106137

107-
expect(restored.size).toBe(4);
108-
expect(restored.get("/a.jsonl")).toEqual(original.get("/a.jsonl"));
109-
expect(restored.get("/b.jsonl")).toEqual(original.get("/b.jsonl"));
110-
expect(restored.get("/grok.jsonl")).toEqual(original.get("/grok.jsonl"));
111-
expect(restored.get("/codex.jsonl")).toEqual(original.get("/codex.jsonl"));
138+
expect(restored).toEqual(original);
112139
});
113140

114141
it("drops an entry whose persisted parse state is corrupt", () => {

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

Lines changed: 5 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;
@@ -237,6 +238,10 @@ export function decodeScanCache(
237238
return null;
238239
}
239240

241+
// Unchanged and deleted transcripts may only exist in the cache, so the
242+
// parser cannot remove their previously saved local placeholders.
243+
if (provider === "claude" && model === CLAUDE_SYNTHETIC_MODEL) continue;
244+
240245
records.push({
241246
provider,
242247
timestampMs,

0 commit comments

Comments
 (0)