fix(cli): preserve claude cache creation tokens

This commit is contained in:
Vincent Koc
2026-04-05 15:09:20 +01:00
parent b0f4af3bad
commit bcd0a492a4
2 changed files with 39 additions and 1 deletions

View File

@@ -157,6 +157,43 @@ describe("parseCliJsonl", () => {
});
});
it("preserves Claude cache creation tokens instead of flattening them to zero", () => {
const result = parseCliJsonl(
[
JSON.stringify({ type: "init", session_id: "session-cache-123" }),
JSON.stringify({
type: "result",
session_id: "session-cache-123",
result: "Claude says hello",
usage: {
input_tokens: 12,
output_tokens: 3,
cache_read_input_tokens: 4,
cache_creation_input_tokens: 7,
},
}),
].join("\n"),
{
command: "claude",
output: "jsonl",
sessionIdFields: ["session_id"],
},
"claude-cli",
);
expect(result).toEqual({
text: "Claude says hello",
sessionId: "session-cache-123",
usage: {
input: 12,
output: 3,
cacheRead: 4,
cacheWrite: 7,
total: undefined,
},
});
});
it("preserves Claude session metadata even when the final result text is empty", () => {
const result = parseCliJsonl(
[

View File

@@ -114,7 +114,8 @@ function toCliUsage(raw: Record<string, unknown>): CliUsage | undefined {
(Object.hasOwn(raw, "cached") && typeof totalInput === "number"
? Math.max(0, totalInput - (cacheRead ?? 0))
: totalInput);
const cacheWrite = pick("cache_write_input_tokens") ?? pick("cacheWrite");
const cacheWrite =
pick("cache_creation_input_tokens") ?? pick("cache_write_input_tokens") ?? pick("cacheWrite");
const total = pick("total_tokens") ?? pick("total");
if (!input && !output && !cacheRead && !cacheWrite && !total) {
return undefined;