fix: retain separate cache reads in historical Claude totals

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Devin FoleyandPaperclip committed 2026-10-03 22:58:30 -07:00
1 parent 532ba38263
commit 525d7b8da6
2 files changed
+14 -3

No files matched your search

+8
View File
@@ -19,6 +19,14 @@ describe("cost presentation", () => {
expect(visibleRunCostUsd({ billingType: "subscription_included", costUsd: 50 })).toBe(0);
});
it("includes separate legacy Claude cache reads without changing legacy Codex totals", () => {
const tokens = { inputTokens: 100, cachedInputTokens: 80, outputTokens: 10 };
expect(visibleRunTokenTotal({ ...tokens, provider: "anthropic" })).toBe(190);
expect(visibleRunTokenTotal({ ...tokens, provider: "openai" })).toBe(110);
expect(visibleRunTokenTotal({ ...tokens, provider: "anthropic", accountingReceiptId: "saved-receipt" })).toBe(190);
expect(visibleRunTokenTotal({ provider: "anthropic", input_tokens: 100, cache_read_input_tokens: 80, output_tokens: 10 })).toBe(190);
});
it("formats finance amounts in their recorded currency", () => {
expect(formatCents(123, "USD")).toBe("$1.23");
expect(formatCents(123, "EUR")).toBe("€1.23");
+6 -3
View File
@@ -179,8 +179,9 @@ function readRunCostUsd(payload: Record<string, unknown> | null): number | null
return null;
}
/** Receipt-backed runs store uncached input separately. Older run snapshots
* retain their original input total (Codex included cached input in it). */
/** Receipt-backed runs and legacy Claude snapshots store uncached input
* separately. Older Codex input already includes its cached tokens. Unknown
* legacy formats retain their original input-plus-output interpretation. */
export function visibleRunTokenTotal(usage: Record<string, unknown> | null | undefined): number {
const count = (...keys: string[]) => {
for (const key of keys) {
@@ -192,7 +193,9 @@ export function visibleRunTokenTotal(usage: Record<string, unknown> | null | und
const input = count("inputTokens", "input_tokens");
const output = count("outputTokens", "output_tokens");
const cached = count("cachedInputTokens", "cached_input_tokens", "cache_read_input_tokens");
return input + output + (typeof usage?.accountingReceiptId === "string" && usage.accountingReceiptId.length > 0 ? cached : 0);
const inputExcludesCached = usage?.provider === "anthropic"
|| (typeof usage?.accountingReceiptId === "string" && usage.accountingReceiptId.length > 0);
return input + output + (inputExcludesCached ? cached : 0);
}
export function visibleRunCostUsd(