@devries-cameron20 / t3code Mirror · read-only / commits / 5ff3d3c0ec
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 19:45 UTC

fix(server): Claude 1-hour cache writes are priced at the 1-hour rate (#35423)

Julius Marminge committed Nov 12, 2026 at 10:01 UTC 5ff3d3c0eccdc5aea62e177967c992fccddecb44
9 files changed +143 -43
apps/server/src/provider/Drivers/claudeUsage.test.ts
+46 -2
@@ -1,5 +1,6 @@
1 import { describe, expect, it } from "@effect/vitest";
2
3 +import { parseRateTable, priceUsage } from "../../usage/usagePricing.ts";
4 import { claudeUsageFormat, parseClaudeLine } from "./claudeUsage.ts";
5
6 function claudeLine(overrides: {
@@ -115,7 +116,8 @@ describe("Claude advisor iterations", () => {
116 model: "claude-fable-5-1",
117 input_tokens: 54204,
118 cache_read_input_tokens: 0,
118 - cache_creation_input_tokens: 0,
119 + cache_creation_input_tokens: 500,
120 + cache_creation: { ephemeral_5m_input_tokens: 200, ephemeral_1h_input_tokens: 300 },
121 output_tokens: 4229,
122 },
123 {
@@ -160,10 +162,11 @@ describe("Claude advisor iterations", () => {
162 totals: {
163 uncachedInputTokens: 54204,
164 cachedInputTokens: 0,
163 - cacheCreationTokens: 0,
165 + cacheCreationTokens: 500,
166 outputTokens: 4229,
167 reasoningTokens: 0,
168 },
169 + cacheCreation1hTokens: 300,
170 dedupeKey: expect.any(String),
171 },
172 ];
@@ -216,3 +219,44 @@ describe("Claude advisor iterations", () => {
219 expect(parse(unusable).map((record) => record.model)).toEqual(["claude-opus-5-5"]);
220 });
221 });
222 +
223 +describe("Claude cache write pricing", () => {
224 + it("prices one-hour cache writes at the one-hour rate and the rest at the five-minute rate", () => {
225 + const model = "claude-opus-4-5-20251101";
226 + const rates = parseRateTable({
227 + [model]: {
228 + litellm_provider: "anthropic",
229 + input_cost_per_token: 5e-6,
230 + output_cost_per_token: 25e-6,
231 + cache_read_input_token_cost: 5e-7,
232 + cache_creation_input_token_cost: 6.25e-6,
233 + cache_creation_input_token_cost_above_1hr: 1e-5,
234 + },
235 + });
236 + const [record] = parseClaudeLine(
237 + JSON.stringify({
238 + type: "assistant",
239 + timestamp: "2026-08-07T04:05:13.944Z",
240 + sessionId: "s",
241 + message: {
242 + id: "msg_ttl",
243 + model,
244 + usage: {
245 + input_tokens: 0,
246 + cache_read_input_tokens: 0,
247 + cache_creation_input_tokens: 1_000_000,
248 + cache_creation: {
249 + ephemeral_5m_input_tokens: 400_000,
250 + ephemeral_1h_input_tokens: 600_000,
251 + },
252 + output_tokens: 0,
253 + },
254 + },
255 + }),
256 + );
257 +
258 + if (record === undefined) throw new Error("expected a usage record");
259 + // 0.4M x $6.25/M + 0.6M x $10/M
260 + expect(priceUsage(rates, record).costUsd).toBeCloseTo(8.5, 9);
261 + });
262 +});
apps/server/src/provider/Drivers/claudeUsage.ts
+9
@@ -30,6 +30,13 @@ export function parseClaudeLine(line: string): readonly UsageRecord[] {
30 return parseClaudeRecord(parsed);
31 }
32
33 +function cacheCreation1h(usage: Record<string, unknown>): { cacheCreation1hTokens?: number } {
34 + const split = usage["cache_creation"];
35 + if (typeof split !== "object" || split === null) return {};
36 + const tokens = tokenCount((split as Record<string, unknown>)["ephemeral_1h_input_tokens"]);
37 + return tokens > 0 ? { cacheCreation1hTokens: tokens } : {};
38 +}
39 +
40 function parseClaudeRecord(parsed: unknown): readonly UsageRecord[] {
41 if (typeof parsed !== "object" || parsed === null) return [];
42
@@ -70,6 +77,7 @@ function parseClaudeRecord(parsed: unknown): readonly UsageRecord[] {
77 // Anthropic folds thinking tokens into output and does not break them out.
78 reasoningTokens: 0,
79 },
80 + ...cacheCreation1h(usageRecord),
81 reportedCostUsd: typeof cost === "number" && Number.isFinite(cost) ? cost : null,
82 speed: usageRecord["speed"] === "fast" ? "fast" : "standard",
83 dedupeKey,
@@ -101,6 +109,7 @@ function parseClaudeRecord(parsed: unknown): readonly UsageRecord[] {
109 outputTokens: tokenCount(advisor["output_tokens"]),
110 reasoningTokens: 0,
111 },
112 + ...cacheCreation1h(advisor),
113 reportedCostUsd: null,
114 speed: executor.speed,
115 dedupeKey: dedupeKey === null ? null : `${dedupeKey}:advisor:${index}`,
apps/server/src/usage/UsageService.test.ts
+1 -1
@@ -2890,7 +2890,7 @@ describe("UsageService", () => {
2890 60 + 20 * (version === 4 ? 1 : 2),
2891 );
2892 yield* upgraded.awaitPersisted;
2893 - assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v12.json");
2893 + assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v13.json");
2894 assert.deepStrictEqual(
2895 (yield* Effect.promise(() => NodeFSP.readdir(stateDir)))
2896 .filter((name) => name.startsWith("usage-scan-cache"))
apps/server/src/usage/usageAggregation.test.ts
+2
@@ -12,11 +12,13 @@ const rates: RateTable = new Map([
12 outputCostPerToken: 5e-5,
13 cacheReadCostPerToken: 1e-6,
14 cacheCreationCostPerToken: 1.25e-5,
15 + cacheCreation1hCostPerToken: 2e-5,
16 fast: {
17 inputCostPerToken: 2e-5,
18 outputCostPerToken: 1e-4,
19 cacheReadCostPerToken: 2e-6,
20 cacheCreationCostPerToken: 2.5e-5,
21 + cacheCreation1hCostPerToken: 4e-5,
22 },
23 ultrafast: null,
24 contextTiers: [],
apps/server/src/usage/usageAggregation.ts
+6
@@ -147,12 +147,18 @@ export function mergeUsageCopies(held: UsageRecord, copy: UsageRecord): UsageRec
147 b.cacheCreationTokens <= a.cacheCreationTokens &&
148 b.outputTokens <= a.outputTokens &&
149 b.reasoningTokens <= a.reasoningTokens &&
150 + (copy.cacheCreation1hTokens ?? 0) <= (held.cacheCreation1hTokens ?? 0) &&
151 reportedCostUsd === held.reportedCostUsd
152 ) {
153 return held;
154 }
155 + const cacheCreation1hTokens = Math.max(
156 + held.cacheCreation1hTokens ?? 0,
157 + copy.cacheCreation1hTokens ?? 0,
158 + );
159 return {
160 ...held,
161 + ...(cacheCreation1hTokens > 0 ? { cacheCreation1hTokens } : {}),
162 totals: {
163 uncachedInputTokens: Math.max(a.uncachedInputTokens, b.uncachedInputTokens),
164 cachedInputTokens: Math.max(a.cachedInputTokens, b.cachedInputTokens),
apps/server/src/usage/usagePricing.ts
+48 -26
@@ -12,6 +12,8 @@ export interface TokenRates {
12 readonly outputCostPerToken: number;
13 readonly cacheReadCostPerToken: number;
14 readonly cacheCreationCostPerToken: number;
15 + /** Cache writes with a 1-hour TTL; the 5-minute rate when unpublished. */
16 + readonly cacheCreation1hCostPerToken: number;
17 }
18
19 interface SpeedRates extends TokenRates {
@@ -37,20 +39,24 @@ export function createOverrideRateTable(
39 overrides: Readonly<Record<string, UsageModelPriceOverride>>,
40 ): RateTable {
41 return new Map(
40 - Object.entries(overrides).map(([model, prices]) => [
41 - model.trim(),
42 - {
43 - inputCostPerToken: prices.inputCostPerMillionTokens / 1_000_000,
44 - outputCostPerToken: prices.outputCostPerMillionTokens / 1_000_000,
45 - cacheReadCostPerToken:
46 - (prices.cacheReadCostPerMillionTokens ?? prices.inputCostPerMillionTokens) / 1_000_000,
47 - cacheCreationCostPerToken:
48 - (prices.cacheWriteCostPerMillionTokens ?? prices.inputCostPerMillionTokens) / 1_000_000,
49 - fast: null,
50 - ultrafast: null,
51 - contextTiers: [],
52 - },
53 - ]),
42 + Object.entries(overrides).map(([model, prices]) => {
43 + const cacheCreationCostPerToken =
44 + (prices.cacheWriteCostPerMillionTokens ?? prices.inputCostPerMillionTokens) / 1_000_000;
45 + return [
46 + model.trim(),
47 + {
48 + inputCostPerToken: prices.inputCostPerMillionTokens / 1_000_000,
49 + outputCostPerToken: prices.outputCostPerMillionTokens / 1_000_000,
50 + cacheReadCostPerToken:
51 + (prices.cacheReadCostPerMillionTokens ?? prices.inputCostPerMillionTokens) / 1_000_000,
52 + cacheCreationCostPerToken,
53 + cacheCreation1hCostPerToken: cacheCreationCostPerToken,
54 + fast: null,
55 + ultrafast: null,
56 + contextTiers: [],
57 + },
58 + ];
59 + }),
60 );
61 }
62
@@ -60,7 +66,7 @@ function finiteNumber(value: unknown): number | null {
66 return typeof value === "number" && Number.isFinite(value) ? value : null;
67 }
68
63 -// Missing cache rates fall back to input pricing; Anthropic normally discounts reads and charges extra for writes.
69 +// Missing cache rates fall back to input pricing, and a missing 1-hour write rate to the 5-minute one.
70 function readTokenRates(
71 entry: LiteLlmEntry,
72 suffix: string,
@@ -70,18 +76,28 @@ function readTokenRates(
76 const input = finiteNumber(entry[`input_cost_per_token${suffix}`]);
77 const output = finiteNumber(entry[`output_cost_per_token${suffix}`]) ?? outputFallback ?? null;
78 if (input === null || output === null) return null;
73 - const cacheRate = (name: string, field: "cacheReadCostPerToken" | "cacheCreationCostPerToken") =>
79 + const cacheRate = (
80 + name: string,
81 + field: "cacheReadCostPerToken" | "cacheCreationCostPerToken" | "cacheCreation1hCostPerToken",
82 + fallback = input,
83 + ) =>
84 finiteNumber(entry[`${name}${suffix}`]) ??
85 (standard !== undefined && standard.inputCostPerToken > 0
86 ? (standard[field] / standard.inputCostPerToken) * input
77 - : input);
87 + : fallback);
88 + const cacheCreationCostPerToken = cacheRate(
89 + "cache_creation_input_token_cost",
90 + "cacheCreationCostPerToken",
91 + );
92 return {
93 inputCostPerToken: input,
94 outputCostPerToken: output,
95 cacheReadCostPerToken: cacheRate("cache_read_input_token_cost", "cacheReadCostPerToken"),
82 - cacheCreationCostPerToken: cacheRate(
83 - "cache_creation_input_token_cost",
84 - "cacheCreationCostPerToken",
96 + cacheCreationCostPerToken,
97 + cacheCreation1hCostPerToken: cacheRate(
98 + "cache_creation_input_token_cost_above_1hr",
99 + "cacheCreation1hCostPerToken",
100 + cacheCreationCostPerToken,
101 ),
102 };
103 }
@@ -92,6 +108,7 @@ function scaleTokenRates(rates: TokenRates, multiple: number): TokenRates {
108 outputCostPerToken: rates.outputCostPerToken * multiple,
109 cacheReadCostPerToken: rates.cacheReadCostPerToken * multiple,
110 cacheCreationCostPerToken: rates.cacheCreationCostPerToken * multiple,
111 + cacheCreation1hCostPerToken: rates.cacheCreation1hCostPerToken * multiple,
112 };
113 }
114
@@ -192,7 +209,8 @@ function sameTokenRates(a: TokenRates | null, b: TokenRates | null): boolean {
209 a.inputCostPerToken === b.inputCostPerToken &&
210 a.outputCostPerToken === b.outputCostPerToken &&
211 a.cacheReadCostPerToken === b.cacheReadCostPerToken &&
195 - a.cacheCreationCostPerToken === b.cacheCreationCostPerToken
212 + a.cacheCreationCostPerToken === b.cacheCreationCostPerToken &&
213 + a.cacheCreation1hCostPerToken === b.cacheCreation1hCostPerToken
214 );
215 }
216
@@ -301,7 +319,7 @@ function resolveRate(table: RateTable, model: string): ModelRate | null {
319
320 export type PricedRecord = Pick<
321 UsageRecord,
304 - "model" | "rateModel" | "totals" | "speed" | "reportedCostUsd"
322 + "model" | "rateModel" | "totals" | "cacheCreation1hTokens" | "speed" | "reportedCostUsd"
323 >;
324
325 function recordRate(table: RateTable, record: PricedRecord): ModelRate | null {
@@ -316,11 +334,15 @@ export interface PricedUsage {
334 readonly speedPremiumUsd: number;
335 }
336
319 -function costByCategory(totals: UsageTokenTotals, rates: TokenRates): UsageCategoryCost {
337 +function costByCategory(record: PricedRecord, rates: TokenRates): UsageCategoryCost {
338 + const { totals } = record;
339 + const oneHour = Math.min(record.cacheCreation1hTokens ?? 0, totals.cacheCreationTokens);
340 return {
341 input: totals.uncachedInputTokens * rates.inputCostPerToken,
342 cacheRead: totals.cachedInputTokens * rates.cacheReadCostPerToken,
323 - cacheWrite: totals.cacheCreationTokens * rates.cacheCreationCostPerToken,
343 + cacheWrite:
344 + (totals.cacheCreationTokens - oneHour) * rates.cacheCreationCostPerToken +
345 + oneHour * rates.cacheCreation1hCostPerToken,
346 output: totals.outputTokens * rates.outputCostPerToken,
347 };
348 }
@@ -352,13 +374,13 @@ export function priceUsage(
374 return reported === null ? unsplit(0, "unpriced") : unsplit(reported, "providerReported");
375 }
376
355 - const listCost = costByCategory(totals, ratesAt(rate, record.speed, totals));
377 + const listCost = costByCategory(record, ratesAt(rate, record.speed, totals));
378 const listCostUsd = sumCategories(listCost);
379 if (reported !== null && listCostUsd <= 0) return unsplit(reported, "providerReported");
380 const premiumUsd =
381 record.speed === "standard"
382 ? 0
361 - : listCostUsd - sumCategories(costByCategory(totals, ratesAt(rate, "standard", totals)));
383 + : listCostUsd - sumCategories(costByCategory(record, ratesAt(rate, "standard", totals)));
384 const scale = reported === null ? 1 : reported / listCostUsd;
385 return {
386 costUsd: reported ?? listCostUsd,
apps/server/src/usage/usageScanCache.test.ts
+12 -4
@@ -65,7 +65,15 @@ describe("scan cache round trip", () => {
65 [
66 "/a.jsonl",
67 100,
68 - [record(), record({ dedupeKey: "msg_2:", model: "claude-opus-5-5", speed: "fast" })],
68 + [
69 + record(),
70 + record({
71 + dedupeKey: "msg_2:",
72 + model: "claude-opus-5-5",
73 + speed: "fast",
74 + cacheCreation1hTokens: 8,
75 + }),
76 + ],
77 ],
78 [
79 "/b.jsonl",
@@ -244,11 +252,11 @@ describe("scan cache round trip", () => {
252 },
253 );
254
247 - it("re-parses a Claude transcript cached before partial copies were merged", () => {
255 + it("re-parses a Claude transcript cached before 1-hour cache writes were counted", () => {
256 const original = cacheWith([["/claude.jsonl", 100, [record()]]]);
249 - const v11 = { ...parse(makeScanCacheWriter()(original, {})), version: 11 };
257 + const v12 = { ...parse(makeScanCacheWriter()(original, {})), version: 12 };
258
251 - const restored = decodeScanCache(JSON.parse(JSON.stringify(v11)), TEST_FORMAT_MAP);
259 + const restored = decodeScanCache(JSON.parse(JSON.stringify(v12)), TEST_FORMAT_MAP);
260 expect(restored.get("/claude.jsonl")).toEqual({
261 ...original.get("/claude.jsonl"),
262 size: -1,
apps/server/src/usage/usageScanCache.ts
+17 -10
@@ -10,7 +10,7 @@ import { CLAUDE_SYNTHETIC_MODEL } from "../provider/Drivers/claudeUsage.ts";
10 import { mergeUsageCopies } from "./usageAggregation.ts";
11 import { GUARD_LENGTH, type TranscriptParsePosition } from "./usageTranscriptReader.ts";
12
13 -const USAGE_SCAN_CACHE_VERSION = 12 as const;
13 +const USAGE_SCAN_CACHE_VERSION = 13 as const;
14 const SPEED_COMPATIBLE_SINCE_VERSION = 4;
15
16 /**
@@ -18,8 +18,9 @@ const SPEED_COMPATIBLE_SINCE_VERSION = 4;
18 * replace it, dropping saved usage for deleted transcripts. Separate files keep
19 * the newer one; older files are removed once it is saved.
20 */
21 -export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v12.json";
21 +export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v13.json";
22 export const LEGACY_SCAN_CACHE_FILE_NAMES = [
23 + "usage-scan-cache-v12.json",
24 "usage-scan-cache-v11.json",
25 "usage-scan-cache-v10.json",
26 "usage-scan-cache-v9.json",
@@ -59,7 +60,8 @@ type SerializedRecord = readonly [
60 reportedCostUsd: number | null,
61 speed: number,
62 rateModelIndex?: number | null,
62 - unresolvedServiceTierTurnId?: string,
63 + unresolvedServiceTierTurnId?: string | null,
64 + cacheCreation1hTokens?: number,
65 ];
66
67 interface SerializedFile {
@@ -118,9 +120,11 @@ function serializeFile(entry: CachedFile, tables: InternTables): SerializedFile
120 record.rateModel === undefined
121 ? null
122 : intern(tables.models, tables.modelIndex, record.rateModel),
121 - ...(record.unresolvedServiceTierTurnId === undefined
122 - ? ([] as const)
123 - : ([record.unresolvedServiceTierTurnId] as const)),
123 + ...(record.cacheCreation1hTokens !== undefined
124 + ? ([record.unresolvedServiceTierTurnId ?? null, record.cacheCreation1hTokens] as const)
125 + : record.unresolvedServiceTierTurnId !== undefined
126 + ? ([record.unresolvedServiceTierTurnId] as const)
127 + : ([] as const)),
128 ];
129 return {
130 s: entry.size,
@@ -211,6 +215,7 @@ export function decodeScanCache(
215 speedIndex,
216 rateModelIndex,
217 unresolvedServiceTierTurnId,
218 + cacheCreation1h = 0,
219 ] = row as SerializedRecord;
220 const speed = typeof speedIndex === "number" ? SPEEDS[speedIndex] : undefined;
221
@@ -220,8 +225,8 @@ export function decodeScanCache(
225 typeof timestampMs !== "number" ||
226 !Number.isFinite(timestampMs) ||
227 model === undefined ||
223 - (unresolvedServiceTierTurnId !== undefined &&
224 - typeof unresolvedServiceTierTurnId !== "string") ||
228 + (unresolvedServiceTierTurnId != null && typeof unresolvedServiceTierTurnId !== "string") ||
229 + !Number.isFinite(cacheCreation1h) ||
230 (rateModelIndex != null && rateModel === undefined) ||
231 !Number.isFinite(uncached) ||
232 !Number.isFinite(cached) ||
@@ -250,9 +255,10 @@ export function decodeScanCache(
255 outputTokens: output,
256 reasoningTokens: reasoning,
257 },
258 + ...(cacheCreation1h > 0 ? { cacheCreation1hTokens: cacheCreation1h } : {}),
259 reportedCostUsd: typeof reportedCostUsd === "number" ? reportedCostUsd : null,
260 speed,
255 - ...(version >= 7 && unresolvedServiceTierTurnId !== undefined
261 + ...(version >= 7 && unresolvedServiceTierTurnId != null
262 ? { unresolvedServiceTierTurnId }
263 : {}),
264 dedupeKey: typeof dedupeKey === "string" ? dedupeKey : null,
@@ -292,8 +298,9 @@ export function decodeScanCache(
298 // Keep history for deleted rollouts, but re-parse incompatible reducer state.
299 // v9 and v11 repair skipped Codex requests; clean v8+ entries stay served while unchanged.
300 // v10 adds Claude advisor calls; before v12 a repeated Claude key kept its first, possibly partial, copy.
301 + // v13 adds Claude 1-hour cache writes.
302 const legacy =
296 - (entry.p === "claude" && version < 12) ||
303 + (entry.p === "claude" && version < 13) ||
304 (format.state !== undefined &&
305 (version < 6 ||
306 (entry.p === "codex" && (version < 8 || (version < 11 && malformedRecords > 0)))));
packages/provider-core/src/server/usage.ts
+2
@@ -18,6 +18,8 @@ export interface UsageRecord {
18 readonly rateModel?: string;
19 readonly sessionId: string;
20 readonly totals: UsageTokenTotals;
21 + /** The part of `totals.cacheCreationTokens` written with a 1-hour TTL. */
22 + readonly cacheCreation1hTokens?: number;
23 readonly reportedCostUsd: number | null;
24 readonly speed: UsageSpeed;
25 readonly unresolvedServiceTierTurnId?: string;