Skip to content

Commit 3d7b402

Browse files
fix(usage): ignore Claude local placeholder models
1 parent 1e3a6b2 commit 3d7b402

4 files changed

Lines changed: 70 additions & 38 deletions

File tree

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

Lines changed: 30 additions & 12 deletions
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,19 +66,37 @@ 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();
72101
});
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-
});
84102
});

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ 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-
*/
15+
// Claude Code uses this model for local placeholders that never reached the API.
2016
export const CLAUDE_SYNTHETIC_MODEL = "<synthetic>";
2117

2218
/**

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

Lines changed: 36 additions & 19 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", () => {
@@ -157,16 +184,6 @@ describe("scan cache round trip", () => {
157184
expect(decodeScanCache(JSON.parse(JSON.stringify(previous)), TEST_FORMAT_MAP).size).toBe(0);
158185
});
159186

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-
170187
it("rewrites only changed entries and still restores the whole cache", () => {
171188
const write = makeScanCacheWriter();
172189
const cache = cacheWith([

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ export function decodeScanCache(
237237
) {
238238
return null;
239239
}
240-
// Entries cached before the parser skipped these still hold them, and a
241-
// transcript that never grows is never re-parsed.
240+
241+
// Unchanged and deleted transcripts may only exist in the cache, so the
242+
// parser cannot remove their previously saved local placeholders.
242243
if (provider === "claude" && model === CLAUDE_SYNTHETIC_MODEL) continue;
243244

244245
records.push({

0 commit comments

Comments
 (0)