Skip to content

Commit bcd0a49

Browse files
committed
fix(cli): preserve claude cache creation tokens
1 parent b0f4af3 commit bcd0a49

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/agents/cli-output.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,43 @@ describe("parseCliJsonl", () => {
157157
});
158158
});
159159

160+
it("preserves Claude cache creation tokens instead of flattening them to zero", () => {
161+
const result = parseCliJsonl(
162+
[
163+
JSON.stringify({ type: "init", session_id: "session-cache-123" }),
164+
JSON.stringify({
165+
type: "result",
166+
session_id: "session-cache-123",
167+
result: "Claude says hello",
168+
usage: {
169+
input_tokens: 12,
170+
output_tokens: 3,
171+
cache_read_input_tokens: 4,
172+
cache_creation_input_tokens: 7,
173+
},
174+
}),
175+
].join("\n"),
176+
{
177+
command: "claude",
178+
output: "jsonl",
179+
sessionIdFields: ["session_id"],
180+
},
181+
"claude-cli",
182+
);
183+
184+
expect(result).toEqual({
185+
text: "Claude says hello",
186+
sessionId: "session-cache-123",
187+
usage: {
188+
input: 12,
189+
output: 3,
190+
cacheRead: 4,
191+
cacheWrite: 7,
192+
total: undefined,
193+
},
194+
});
195+
});
196+
160197
it("preserves Claude session metadata even when the final result text is empty", () => {
161198
const result = parseCliJsonl(
162199
[

src/agents/cli-output.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ function toCliUsage(raw: Record<string, unknown>): CliUsage | undefined {
114114
(Object.hasOwn(raw, "cached") && typeof totalInput === "number"
115115
? Math.max(0, totalInput - (cacheRead ?? 0))
116116
: totalInput);
117-
const cacheWrite = pick("cache_write_input_tokens") ?? pick("cacheWrite");
117+
const cacheWrite =
118+
pick("cache_creation_input_tokens") ?? pick("cache_write_input_tokens") ?? pick("cacheWrite");
118119
const total = pick("total_tokens") ?? pick("total");
119120
if (!input && !output && !cacheRead && !cacheWrite && !total) {
120121
return undefined;

0 commit comments

Comments
 (0)