Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🪙 fix: Price 1-Hour Prompt Cache Writes at the 1-Hour Rate (#17208)
Marco Beretta472d29a3ee58
diff --git a/api/server/controllers/agents/__tests__/modelEndHandler.spec.js b/api/server/controllers/agents/__tests__/modelEndHandler.spec.jsindex e2d0880ae9..5f794347f2 100644--- a/api/server/controllers/agents/__tests__/modelEndHandler.spec.js+++ b/api/server/controllers/agents/__tests__/modelEndHandler.spec.js@@ -2,13 +2,14 @@ jest.mock('@librechat/data-schemas', () => ({logger: { error: jest.fn(), debug: jest.fn(), info: jest.fn() },}));jest.mock('@librechat/api', () => {- const { getModelRefusalInfo } = jest.requireActual('@librechat/api');+ const { getModelRefusalInfo, getModelEndUsage } = jest.requireActual('@librechat/api');return {sendEvent: jest.fn(),emitEvent: jest.fn(),createToolExecuteHandler: jest.fn(),markSummarizationUsage: (usage) => usage,getModelRefusalInfo,+ getModelEndUsage,};});jest.mock('~/server/services/Files/Citations', () => ({@@ -235,6 +236,44 @@ describe('ModelEndHandler — Vertex thoughtSignature capture (issue #13006 follexpect(emitUsage).toHaveBeenCalledWith(expect.objectContaining({ agentId: undefined }));});+ it('carries the Anthropic 1-hour cache write count into collected and emitted usage', async () => {+ const collectedUsage = [];+ const emitUsage = jest.fn();+ const handler = new ModelEndHandler(collectedUsage, null, emitUsage);++ await handler.handle(+ 'on_chat_model_end',+ {+ output: {+ usage_metadata: {+ input_tokens: 3510,+ output_tokens: 40,+ total_tokens: 3550,+ input_token_details: { cache_creation: 3000, cache_read: 500 },+ },+ response_metadata: {+ usage: {+ cache_creation: { ephemeral_5m_input_tokens: 1000, ephemeral_1h_input_tokens: 2000 },+ },+ },+ },+ },+ { ls_model_name: 'claude-sonnet-5-5', run_id: 'r1', user_id: 'u1' },+ buildGraph(),+ );++ expect(collectedUsage[0].input_token_details).toEqual({+ cache_creation: 3000,+ cache_creation_1h: 2000,+ cache_read: 500,+ });+ expect(emitUsage).toHaveBeenCalledWith(+ expect.objectContaining({+ input_token_details: { cache_creation: 3000, cache_creation_1h: 2000, cache_read: 500 },+ }),+ );+ });+it('throws when collectedUsage is not an array (existing contract)', () => {expect(() => new ModelEndHandler(null)).toThrow('collectedUsage must be an array');});diff --git a/api/server/controllers/agents/__tests__/responses.unit.spec.js b/api/server/controllers/agents/__tests__/responses.unit.spec.jsindex a6923e0145..237062a6e6 100644--- a/api/server/controllers/agents/__tests__/responses.unit.spec.js+++ b/api/server/controllers/agents/__tests__/responses.unit.spec.js@@ -203,6 +203,7 @@ jest.mock('@librechat/api', () => ({getConversationWriteContext: (...args) =>jest.requireActual('@librechat/api').getConversationWriteContext(...args),isTemporaryTurn: (...args) => jest.requireActual('@librechat/api').isTemporaryTurn(...args),+ getModelEndUsage: (...args) => jest.requireActual('@librechat/api').getModelEndUsage(...args),announceReply: jest.fn().mockResolvedValue(undefined),/* Provisioning moved into this package; the controllers build the callback from it. */createProvisionFilesCallback: () => async () => {},diff --git a/api/server/controllers/agents/callbacks.js b/api/server/controllers/agents/callbacks.jsindex 767c5ebc0a..37ac16d90a 100644--- a/api/server/controllers/agents/callbacks.js+++ b/api/server/controllers/agents/callbacks.js@@ -19,6 +19,7 @@ const {} = require('@librechat/agents');const {sendEvent,+ getModelEndUsage,computeUsageCostUSD,GenerationJobManager,waitForGenerationSettled,@@ -151,7 +152,7 @@ class ModelEndHandler {});}- const usage = data?.output?.usage_metadata;+ const usage = getModelEndUsage(data?.output);if (!usage) {return;}@@ -188,7 +189,11 @@ class ModelEndHandler {total_tokens: taggedUsage.total_tokens,input_token_details:cache_creation != null || cache_read != null- ? { cache_creation, cache_read }+ ? {+ cache_creation,+ cache_creation_1h: taggedUsage.input_token_details?.cache_creation_1h,+ cache_read,+ }: undefined,model: taggedUsage.model,provider: taggedUsage.provider,diff --git a/api/server/controllers/agents/openai.js b/api/server/controllers/agents/openai.jsindex 5d5697a837..e83e337afe 100644--- a/api/server/controllers/agents/openai.js+++ b/api/server/controllers/agents/openai.js@@ -76,6 +76,7 @@ const {resolveAdmittedCodeEnvironmentDecision,createTerminalRunErrorObserver,isTemporaryTurn,+ getModelEndUsage,} = require('@librechat/api');const {buildSummarizationHandlers,@@ -1054,7 +1055,7 @@ const executeOpenAIChatCompletion = async (envelope, { req, res }) => {// Usage trackingon_chat_model_end: {handle: (_event, data, metadata, graph) => {- const usage = data?.output?.usage_metadata;+ const usage = getModelEndUsage(data?.output);if (usage) {const agentContext = graph?.getAgentContext?.(metadata);const taggedUsage = contextualizeModelUsage(usage, metadata, agentContext);diff --git a/api/server/controllers/agents/responses.js b/api/server/controllers/agents/responses.jsindex ad183b9406..5bcbfe0868 100644--- a/api/server/controllers/agents/responses.js+++ b/api/server/controllers/agents/responses.js@@ -92,6 +92,7 @@ const {announceReply,getConversationWriteContext,isTemporaryTurn,+ getModelEndUsage,} = require('@librechat/api');const {createResponsesToolEndCallback,@@ -1315,7 +1316,7 @@ const executeResponse = async (envelope, { req, res }) => {on_chat_model_end: {handle: (event, data, metadata, graph) => {responsesHandlers.on_chat_model_end.handle(event, data);- const usage = data?.output?.usage_metadata;+ const usage = getModelEndUsage(data?.output);if (usage) {const agentContext = graph?.getAgentContext?.(metadata);const taggedUsage = contextualizeModelUsage(usage, metadata, agentContext);@@ -1558,7 +1559,7 @@ const executeResponse = async (envelope, { req, res }) => {on_chat_model_end: {handle: (event, data, metadata, graph) => {aggregatorHandlers.on_chat_model_end.handle(event, data);- const usage = data?.output?.usage_metadata;+ const usage = getModelEndUsage(data?.output);if (usage) {const agentContext = graph?.getAgentContext?.(metadata);const taggedUsage = contextualizeModelUsage(usage, metadata, agentContext);diff --git a/e2e/setup/fake-model.js b/e2e/setup/fake-model.jsindex 7035ba3932..3fcbbb0cb6 100644--- a/e2e/setup/fake-model.js+++ b/e2e/setup/fake-model.js@@ -58,6 +58,41 @@ const MCP_LINK_APP_MARKER = 'E2E_MCP_LINK_APP:';const MCP_LEGACY_MARKER = 'E2E_MCP_LEGACY:';const MCP_LEGACY_MIMELESS_MARKER = 'E2E_MCP_LEGACY_MIMELESS:';const MCP_LEGACY_ACTION_MARKER = 'E2E_LEGACY_ACTION';+/** `E2E_CACHE_USAGE: 1h` or `5m`: the final usage reports Anthropic-style cache+ * writes (3000, of which 2000 are 1-hour for `1h`) and 500 cache reads, with+ * the TTL split on `response_metadata.usage` where the agents SDK leaves it. */+const CACHE_USAGE_MARKER = 'E2E_CACHE_USAGE:';+const CACHE_USAGE_WRITES = 3000;+const CACHE_USAGE_READS = 500;++function cacheUsageFields(messages, input_tokens, output_tokens) {+ const ttl = getMarkerValue(getLatestUserText(messages), CACHE_USAGE_MARKER);+ if (ttl !== '1h' && ttl !== '5m') {+ return {+ usage_metadata: { input_tokens, output_tokens, total_tokens: input_tokens + output_tokens },+ };+ }+ const oneHour = ttl === '1h' ? 2000 : 0;+ const cachedInput = input_tokens + CACHE_USAGE_WRITES + CACHE_USAGE_READS;+ return {+ usage_metadata: {+ input_tokens: cachedInput,+ output_tokens,+ total_tokens: cachedInput + output_tokens,+ input_token_details: { cache_creation: CACHE_USAGE_WRITES, cache_read: CACHE_USAGE_READS },+ },+ response_metadata: {+ usage: {+ cache_creation_input_tokens: CACHE_USAGE_WRITES,+ cache_read_input_tokens: CACHE_USAGE_READS,+ cache_creation: {+ ephemeral_5m_input_tokens: CACHE_USAGE_WRITES - oneHour,+ ephemeral_1h_input_tokens: oneHour,+ },+ },+ },+ };+}const STEER_SPLIT_REPLY_MARKER = 'E2E_STEER_SPLIT_REPLY:';const STEER_LATE_REPLY_MARKER = 'E2E_STEER_LATE_REPLY:';const ACTIVITY_REPLY_MARKER = 'E2E_ACTIVITY_REPLY:';@@ -964,7 +999,7 @@ class UsageEmittingFakeChatModel extends FakeChatModel {text: '',message: new AIMessageChunk({content: '',- usage_metadata: { input_tokens, output_tokens, total_tokens: input_tokens + output_tokens },+ ...cacheUsageFields(messages, input_tokens, output_tokens),}),});}diff --git a/e2e/specs/mock/scenarios/cache-ttl-pricing.spec.ts b/e2e/specs/mock/scenarios/cache-ttl-pricing.spec.tsnew file mode 100644index 0000000000..53b44c3f63--- /dev/null+++ b/e2e/specs/mock/scenarios/cache-ttl-pricing.spec.ts@@ -0,0 +1,51 @@+import { expect, test } from '@playwright/test';+import type { Page } from '@playwright/test';+import type { TMessage } from 'librechat-data-provider';+import {+ MOCK_ENDPOINTS,+ NEW_CHAT_PATH,+ fetchJson,+ getAccessToken,+ selectMockEndpoint,+ sendMessageAndWaitForCompletion,+} from '../helpers';++/** `mock-model-a` has no price table, so its prompt costs the default rate (6 per+ * million), its 5-minute writes fall back to that input rate, and its 1-hour writes+ * cost twice the prompt rate, as Anthropic bills them. */+const DEFAULT_RATE = 6;+const WRITE_5M = DEFAULT_RATE;+const WRITE_1H = DEFAULT_RATE * 2;+/** The fake model reports 2000 of its cache writes as 1-hour for `E2E_CACHE_USAGE: 1h` */+const ONE_HOUR_WRITES = 2000;++/** The persisted cost of the reply to `text`, sent in a new chat */+async function replyCost(page: Page, text: string): Promise<number> {+ await page.goto(NEW_CHAT_PATH, { timeout: 10000 });+ await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);+ const response = await sendMessageAndWaitForCompletion(page, text);+ const { conversationId } = (await response.json()) as { conversationId: string };+ const token = await getAccessToken(page);+ const messages = await fetchJson<TMessage[]>(+ page,+ `/api/messages/${encodeURIComponent(conversationId)}`,+ token,+ );+ const reply = messages.find((message) => !message.isCreatedByUser);+ const usage = (reply?.metadata as { usage?: { cost?: number; cacheWrite?: number } } | undefined)+ ?.usage;+ expect(usage?.cacheWrite).toBe(3000);+ expect(typeof usage?.cost).toBe('number');+ return usage?.cost as number;+}++test.describe('Prompt cache write pricing', () => {+ test('1-hour cache writes cost the 1-hour rate, the rest of the call unchanged @scenario:cache-1h-writes-priced-at-1h-rate', async ({+ page,+ }) => {+ const fiveMinute = await replyCost(page, 'E2E_CACHE_USAGE: 5m price check');+ const oneHour = await replyCost(page, 'E2E_CACHE_USAGE: 1h price check');++ expect(oneHour - fiveMinute).toBeCloseTo((ONE_HOUR_WRITES * (WRITE_1H - WRITE_5M)) / 1e6, 9);+ });+});diff --git a/packages/api/src/agents/transactions.bulk-parity.spec.ts b/packages/api/src/agents/transactions.bulk-parity.spec.tsindex 6f22cbcfb0..4c6fc24c67 100644--- a/packages/api/src/agents/transactions.bulk-parity.spec.ts+++ b/packages/api/src/agents/transactions.bulk-parity.spec.ts@@ -412,6 +412,60 @@ describe('Structured token parity', () => {});});+describe('One-hour cache write parity', () => {+ test('bulk and legacy paths bill 1-hour writes identically, long-context tier included', async () => {+ const model = 'claude-haiku-5-5';+ const { threshold } = premiumTokenValues[model];+ const tokenUsage = {+ promptTokens: { input: threshold, write: 3000, write1h: 2000, read: 500 },+ completionTokens: 40,+ };+ const bulkUser = new mongoose.Types.ObjectId().toString();+ const legacyUser = new mongoose.Types.ObjectId().toString();+ await Balance.create({ user: bulkUser, tokenCredits: 10000000 });+ await Balance.create({ user: legacyUser, tokenCredits: 10000000 });++ const entries = prepareStructuredTokenSpend(txMeta(bulkUser, { model }), tokenUsage, pricing);+ await bulkWriteTransactions({ user: bulkUser, docs: entries }, dbOps());+ await dbMethods.spendStructuredTokens(+ {+ user: legacyUser,+ conversationId: 'test-convo',+ context: 'test',+ model,+ balance: { enabled: true },+ },+ tokenUsage,+ );++ const inputTokenCount = threshold + 3000 + 500;+ const promptRate = getMultiplier({ model, tokenType: 'prompt', inputTokenCount });+ const expectedPrompt =+ threshold * promptRate ++ 1000 * getCacheMultiplier({ model, cacheType: 'write', inputTokenCount })! ++ 2000 * getCacheMultiplier({ model, cacheType: 'write1h', inputTokenCount })! ++ 500 * getCacheMultiplier({ model, cacheType: 'read', inputTokenCount })!;+ expect(getCacheMultiplier({ model, cacheType: 'write1h', inputTokenCount })).toBe(+ premiumTokenValues[model].prompt * 2,+ );++ const prompts = (await Transaction.find({ tokenType: 'prompt' }).lean()) as Record<+ string,+ unknown+ >[];+ expect(prompts).toHaveLength(2);+ for (const tx of prompts) {+ expect(tx.tokenValue as number).toBeCloseTo(-expectedPrompt);+ expect(tx.writeTokens).toBe(-3000);+ }+ const [bulkBalance, legacyBalance] = (await Promise.all([+ Balance.findOne({ user: bulkUser }).lean(),+ Balance.findOne({ user: legacyUser }).lean(),+ ])) as Record<string, number>[];+ expect(bulkBalance.tokenCredits).toBeCloseTo(legacyBalance.tokenCredits);+ });+});+describe('Premium pricing parity', () => {test('standard pricing below threshold — identical to legacy', async () => {const userId = new mongoose.Types.ObjectId().toString();diff --git a/packages/api/src/agents/transactions.ts b/packages/api/src/agents/transactions.tsindex 2a62c4a0bb..f673235432 100644--- a/packages/api/src/agents/transactions.ts+++ b/packages/api/src/agents/transactions.ts@@ -14,7 +14,8 @@ interface GetMultiplierParams {}interface GetCacheMultiplierParams {- cacheType: 'write' | 'read';+ /** `write1h` is Anthropic's 1-hour cache write; `write` is the 5-minute rate */+ cacheType: 'write' | 'read' | 'write1h';model?: string;endpointTokenConfig?: EndpointTokenConfig;inputTokenCount?: number;@@ -47,6 +48,8 @@ interface StructuredTxData extends BaseTxData {tokenType: TokenType;inputTokens?: number;writeTokens?: number;+ /** Subset of `writeTokens` written to the 1-hour cache */+ write1hTokens?: number;readTokens?: number;inputTokenCount?: number;rawAmount?: number;@@ -66,6 +69,8 @@ export interface TokenUsage {export interface StructuredPromptTokens {input?: number;write?: number;+ /** Subset of `write` written to the 1-hour cache, billed at its own rate */+ write1h?: number;read?: number;}@@ -141,20 +146,31 @@ function calculateStructuredTokenValue(const inputAbs = Math.abs(txData.inputTokens ?? 0);const writeAbs = Math.abs(txData.writeTokens ?? 0);+ const write1hAbs = Math.min(Math.abs(txData.write1hTokens ?? 0), writeAbs);const readAbs = Math.abs(txData.readTokens ?? 0);const totalPromptTokens = inputAbs + writeAbs + readAbs;-+ const write1hMultiplier =+ write1hAbs > 0+ ? (pricing.getCacheMultiplier({+ cacheType: 'write1h',+ model,+ endpointTokenConfig,+ inputTokenCount,+ }) ?? writeMultiplier)+ : writeMultiplier;++ const promptValue =+ inputAbs * Math.abs(inputMultiplier) ++ (writeAbs - write1hAbs) * Math.abs(writeMultiplier) ++ write1hAbs * Math.abs(write1hMultiplier) ++ readAbs * Math.abs(readMultiplier);const rate =- totalPromptTokens > 0- ? (Math.abs(inputMultiplier * (txData.inputTokens ?? 0)) +- Math.abs(writeMultiplier * (txData.writeTokens ?? 0)) +- Math.abs(readMultiplier * (txData.readTokens ?? 0))) /- totalPromptTokens- : Math.abs(inputMultiplier);+ totalPromptTokens > 0 ? promptValue / totalPromptTokens : Math.abs(inputMultiplier);const tokenValue = -(inputAbs * inputMultiplier +- writeAbs * writeMultiplier ++ (writeAbs - write1hAbs) * writeMultiplier ++ write1hAbs * write1hMultiplier +readAbs * readMultiplier);@@ -162,7 +178,12 @@ function calculateStructuredTokenValue(tokenValue,rate,rawAmount: -totalPromptTokens,- rateDetail: { input: inputMultiplier, write: writeMultiplier, read: readMultiplier },+ rateDetail: {+ input: inputMultiplier,+ write: writeMultiplier,+ read: readMultiplier,+ ...(write1hAbs > 0 && { write1h: write1hMultiplier }),+ },};}@@ -289,6 +310,7 @@ export function prepareStructuredTokenSpend(if (promptTokens) {const input = Math.max(promptTokens.input ?? 0, 0);const write = Math.max(promptTokens.write ?? 0, 0);+ const write1h = Math.min(Math.max(promptTokens.write1h ?? 0, 0), write);const read = Math.max(promptTokens.read ?? 0, 0);const totalInputTokens = input + write + read;const entry = prepareStructuredTx(@@ -297,6 +319,7 @@ export function prepareStructuredTokenSpend(tokenType: 'prompt',inputTokens: -input,writeTokens: -write,+ ...(write1h > 0 && { write1hTokens: -write1h }),readTokens: -read,inputTokenCount: totalInputTokens,},diff --git a/packages/api/src/agents/usage.bulk-parity.spec.ts b/packages/api/src/agents/usage.bulk-parity.spec.tsindex 5520b66794..68b3408731 100644--- a/packages/api/src/agents/usage.bulk-parity.spec.ts+++ b/packages/api/src/agents/usage.bulk-parity.spec.ts@@ -254,6 +254,33 @@ describe('recordCollectedUsage — bulk path parity', () => {});});+ describe('cache token handling - Anthropic 1-hour writes', () => {+ it('prices the 1-hour share of the writes at the 1-hour rate in bulk docs', async () => {+ mockPricing.getMultiplier = jest.fn().mockReturnValue(2);+ mockPricing.getCacheMultiplier = jest.fn(+ ({ cacheType }) => ({ write: 2.5, write1h: 4, read: 0.2 })[cacheType],+ );+ const collectedUsage: UsageMetadata[] = [+ {+ input_tokens: 3600,+ output_tokens: 50,+ model: 'claude-sonnet-5-5',+ provider: 'anthropic',+ input_token_details: { cache_creation: 3000, cache_creation_1h: 2000, cache_read: 500 },+ },+ ];++ await recordCollectedUsage(deps, { ...baseParams, collectedUsage });++ const docs = mockInsertMany.mock.calls[0][0];+ const promptDoc = docs.find((d: { tokenType: string }) => d.tokenType === 'prompt');+ expect(promptDoc.writeTokens).toBe(-3000);+ expect(promptDoc.write1hTokens).toBe(-2000);+ expect(promptDoc.tokenValue).toBeCloseTo(-(100 * 2 + 1000 * 2.5 + 2000 * 4 + 500 * 0.2));+ expect(promptDoc.rateDetail).toEqual({ input: 2, write: 2.5, read: 0.2, write1h: 4 });+ });+ });+describe('mixed cache and non-cache entries', () => {it('should handle mixed entries — same output_tokens as legacy', async () => {const collectedUsage: UsageMetadata[] = [diff --git a/packages/api/src/agents/usage.spec.ts b/packages/api/src/agents/usage.spec.tsindex 94fa217441..7dbda10b09 100644--- a/packages/api/src/agents/usage.spec.ts+++ b/packages/api/src/agents/usage.spec.ts@@ -3,6 +3,7 @@ import type { RecordUsageDeps, RecordUsageParams, SubagentUsageEvent } from './uimport type { UsageMetadata } from '../stream/interfaces/IJobStore';import type { BulkWriteDeps, PricingFns } from './transactions';import {+ getModelEndUsage,computeUsageCostUSD,aggregateEmittedUsage,createDetachedSubagentUsageRecorder,@@ -687,6 +688,51 @@ describe('recordCollectedUsage', () => {});});+ describe('cache token handling - Anthropic 1-hour writes', () => {+ it('passes the 1-hour share of the writes to spendStructuredTokens', async () => {+ const collectedUsage: UsageMetadata[] = [+ {+ input_tokens: 3600,+ output_tokens: 50,+ model: 'claude-sonnet-5-5',+ provider: 'anthropic',+ input_token_details: { cache_creation: 3000, cache_creation_1h: 2000, cache_read: 500 },+ },+ ];++ await recordCollectedUsage(deps, { ...baseParams, collectedUsage });++ expect(mockSpendStructuredTokens).toHaveBeenCalledWith(+ expect.objectContaining({ model: 'claude-sonnet-5-5' }),+ {+ promptTokens: { input: 100, write: 3000, write1h: 2000, read: 500 },+ completionTokens: 50,+ },+ );+ });++ it('caps the 1-hour share at the reported writes', async () => {+ const collectedUsage: UsageMetadata[] = [+ {+ input_tokens: 1100,+ output_tokens: 10,+ model: 'claude-sonnet-5-5',+ provider: 'anthropic',+ input_token_details: { cache_creation: 1000, cache_creation_1h: 4000 },+ },+ ];++ await recordCollectedUsage(deps, { ...baseParams, collectedUsage });++ expect(mockSpendStructuredTokens.mock.calls[0][1].promptTokens).toEqual({+ input: 100,+ write: 1000,+ write1h: 1000,+ read: 0,+ });+ });+ });+describe('reasoning token handling - issue #13006', () => {it('uses total - input when output_tokens undercounts (Vertex stream undercount with details present)', async () => {const collectedUsage: UsageMetadata[] = [@@ -1895,6 +1941,82 @@ describe('computeUsageCostUSD', () => {);expect(cost).toBeCloseTo((260000 * 8 + 5000 * 3.75 + 15000 * 0.3 + 500 * 40) / 1e6);});++ it('prices the 1-hour share of Anthropic cache writes at the 1-hour rate', () => {+ /** 13000 = 1000 fresh + 2000 write (1500 of them 1-hour) + 10000 read */+ const cost = computeUsageCostUSD(+ {+ input_tokens: 13000,+ output_tokens: 500,+ model: 'claude-sonnet-5-5',+ provider: 'anthropic',+ input_token_details: { cache_creation: 2000, cache_creation_1h: 1500, cache_read: 10000 },+ },+ {+ ...pricing,+ getCacheMultiplier: ({ cacheType }) => ({ write: 3.75, write1h: 6, read: 0.3 })[cacheType],+ },+ );+ expect(cost).toBeCloseTo((1000 * 3 + 500 * 3.75 + 1500 * 6 + 10000 * 0.3 + 500 * 15) / 1e6);+ });+});++describe('getModelEndUsage', () => {+ const usage_metadata: UsageMetadata = {+ input_tokens: 3510,+ output_tokens: 40,+ total_tokens: 3550,+ input_token_details: { cache_creation: 3000, cache_read: 500 },+ };++ it('folds the Anthropic 1-hour write count into the usage', () => {+ const usage = getModelEndUsage({+ usage_metadata,+ response_metadata: {+ usage: {+ cache_creation: { ephemeral_5m_input_tokens: 1000, ephemeral_1h_input_tokens: 2000 },+ },+ },+ });+ expect(usage?.input_token_details).toEqual({+ cache_creation: 3000,+ cache_creation_1h: 2000,+ cache_read: 500,+ });+ expect(usage_metadata.input_token_details).not.toHaveProperty('cache_creation_1h');+ });++ it('reads the 1-hour writes from Bedrock cacheDetails, streamed or not', () => {+ const cacheDetails = [+ { ttl: '1h', inputTokens: 2000 },+ { ttl: '5m', inputTokens: 1000 },+ ];+ for (const response_metadata of [+ { usage: { cacheDetails } },+ { metadata: { usage: { cacheDetails } } },+ ]) {+ expect(getModelEndUsage({ usage_metadata, response_metadata })?.input_token_details).toEqual({+ cache_creation: 3000,+ cache_creation_1h: 2000,+ cache_read: 500,+ });+ }+ });++ it('returns the usage unchanged without a 1-hour breakdown', () => {+ expect(getModelEndUsage({ usage_metadata })).toBe(usage_metadata);+ expect(+ getModelEndUsage({+ usage_metadata,+ response_metadata: { usage: { cache_creation: { ephemeral_1h_input_tokens: 0 } } },+ }),+ ).toBe(usage_metadata);+ });++ it('returns undefined when the call reported no usage', () => {+ expect(getModelEndUsage(undefined)).toBeUndefined();+ expect(getModelEndUsage({ response_metadata: null })).toBeUndefined();+ });});describe('aggregateEmittedUsage', () => {diff --git a/packages/api/src/agents/usage.ts b/packages/api/src/agents/usage.tsindex c68d30a543..52158b175c 100644--- a/packages/api/src/agents/usage.ts+++ b/packages/api/src/agents/usage.ts@@ -50,6 +50,78 @@ function getCacheCreationTokens(usage: UsageMetadata): number {);}+/** The 1-hour share of the cache writes, never more than the writes themselves */+function getCacheCreation1hTokens(usage: UsageMetadata, cacheCreation: number): number {+ return Math.min(+ Math.max(Number(usage.input_token_details?.cache_creation_1h) || 0, 0),+ cacheCreation,+ );+}++interface RawProviderUsage {+ /** Anthropic (direct and Vertex) */+ cache_creation?: {+ ephemeral_5m_input_tokens?: unknown;+ ephemeral_1h_input_tokens?: unknown;+ } | null;+ /** Bedrock Converse */+ cacheDetails?: ReadonlyArray<{ ttl?: string; inputTokens?: unknown } | null> | null;+}++interface ModelEndOutput {+ usage_metadata?: UsageMetadata;+ response_metadata?: {+ usage?: RawProviderUsage | null;+ /** Bedrock's streamed `metadata` event */+ metadata?: { usage?: RawProviderUsage | null } | null;+ } | null;+}++function getOneHourCacheWrites(usage?: RawProviderUsage | null): number {+ const anthropic = Number(usage?.cache_creation?.ephemeral_1h_input_tokens);+ if (anthropic > 0) {+ return anthropic;+ }+ if (!Array.isArray(usage?.cacheDetails)) {+ return 0;+ }+ let total = 0;+ for (const detail of usage.cacheDetails) {+ if (detail?.ttl === '1h') {+ total += Number(detail.inputTokens) || 0;+ }+ }+ return total;+}++/**+ * Usage of one finished model call. Providers split cache writes by TTL only in+ * their raw usage (Anthropic's `cache_creation.ephemeral_1h_input_tokens`, Bedrock's+ * `cacheDetails`), which the agents SDK keeps on `response_metadata` but leaves out+ * of `usage_metadata`. The 1-hour count is folded into+ * `input_token_details.cache_creation_1h` so those writes are billed at the 1-hour+ * rate; without a breakdown the usage is returned unchanged.+ */+export function getModelEndUsage(output?: ModelEndOutput | null): UsageMetadata | undefined {+ const usage = output?.usage_metadata;+ if (!usage) {+ return undefined;+ }+ const metadata = output?.response_metadata;+ const oneHour =+ getOneHourCacheWrites(metadata?.usage) || getOneHourCacheWrites(metadata?.metadata?.usage);+ if (!(oneHour > 0)) {+ return usage;+ }+ return {+ ...usage,+ input_token_details: {+ ...usage.input_token_details,+ cache_creation_1h: Math.min(oneHour, getCacheCreationTokens(usage)),+ },+ };+}+/*** Resolves `completionTokens` for billing, repairing providers whose* `usage_metadata.output_tokens` undercounts.@@ -100,6 +172,8 @@ interface SplitUsage {/** Non-cached input portion — what gets billed at the standard input rate */inputOnly: number;cacheCreation: number;+ /** Subset of `cacheCreation` written to the 1-hour cache */+ cacheCreation1h: number;cacheRead: number;/** Total prompt tokens including cached portion */totalInput: number;@@ -123,6 +197,7 @@ export interface CollectedUsageBreakdown {function splitUsage(usage: UsageMetadata): SplitUsage {const cacheCreation = getCacheCreationTokens(usage);+ const cacheCreation1h = getCacheCreation1hTokens(usage, cacheCreation);const cacheRead =Number(usage.input_token_details?.cache_read) || Number(usage.cache_read_input_tokens) || 0;const rawInput = Number(usage.input_tokens) || 0;@@ -131,6 +206,7 @@ function splitUsage(usage: UsageMetadata): SplitUsage {return {inputOnly: Math.max(0, rawInput - cacheCreation - cacheRead),cacheCreation,+ cacheCreation1h,cacheRead,totalInput: rawInput,completion,@@ -139,6 +215,7 @@ function splitUsage(usage: UsageMetadata): SplitUsage {return {inputOnly: rawInput,cacheCreation,+ cacheCreation1h,cacheRead,totalInput: rawInput + cacheCreation + cacheRead,completion,@@ -217,7 +294,7 @@ export function computeUsageCostUSD(pricing: PricingFns,endpointTokenConfig?: EndpointTokenConfig,): number {- const { inputOnly, cacheCreation, cacheRead, completion } = splitUsage(usage);+ const { inputOnly, cacheCreation, cacheCreation1h, cacheRead, completion } = splitUsage(usage);/** user/context/conversationId only populate the transaction doc, which is* discarded here — only `tokenValue` (credits) is summed */const txData: TxMetadata = {@@ -232,7 +309,12 @@ export function computeUsageCostUSD(? prepareStructuredTokenSpend(txData,{- promptTokens: { input: inputOnly, write: cacheCreation, read: cacheRead },+ promptTokens: {+ input: inputOnly,+ write: cacheCreation,+ ...(cacheCreation1h > 0 && { write1h: cacheCreation1h }),+ read: cacheRead,+ },completionTokens: completion,},pricing,@@ -871,7 +953,8 @@ export async function recordCollectedUsage(continue;}- const { inputOnly, cacheCreation, cacheRead, completion } = splitUsage(usage);+ const { inputOnly, cacheCreation, cacheCreation1h, cacheRead, completion } =+ splitUsage(usage);if (options?.excludeFromOutputTotal !== true) {total_output_tokens += completion;@@ -902,6 +985,7 @@ export async function recordCollectedUsage(promptTokens: {input: inputOnly,write: cacheCreation,+ ...(cacheCreation1h > 0 && { write1h: cacheCreation1h }),read: cacheRead,},completionTokens: completion,@@ -926,6 +1010,7 @@ export async function recordCollectedUsage(promptTokens: {input: inputOnly,write: cacheCreation,+ ...(cacheCreation1h > 0 && { write1h: cacheCreation1h }),read: cacheRead,},completionTokens: completion,diff --git a/packages/api/src/endpoints/custom/initialize.spec.ts b/packages/api/src/endpoints/custom/initialize.spec.tsindex c671ddf0bf..7eaa3d6f91 100644--- a/packages/api/src/endpoints/custom/initialize.spec.ts+++ b/packages/api/src/endpoints/custom/initialize.spec.ts@@ -511,7 +511,14 @@ describe('initializeCustom – token-config fetch header forwarding', () => {it('uses a static tokenConfig for billing and skips the model fetch', async () => {const tokenConfig = {- 'gpt-4': { prompt: 1.5, completion: 4.5, context: 32000, cacheRead: 0.3, cacheWrite: 1.8 },+ 'gpt-4': {+ prompt: 1.5,+ completion: 4.5,+ context: 32000,+ cacheRead: 0.3,+ cacheWrite: 1.8,+ cacheWrite1h: 2.9,+ },};mockGetCustomEndpointConfig.mockReturnValue({apiKey: 'sk-test-key',@@ -546,7 +553,9 @@ describe('initializeCustom – token-config fetch header forwarding', () => {context: 32000,cacheRead: 0.3,cacheWrite: 1.8,+ cacheWrite1h: 2.9,write: 1.8,+ write1h: 2.9,read: 0.3,});});diff --git a/packages/api/src/endpoints/custom/initialize.ts b/packages/api/src/endpoints/custom/initialize.tsindex 476dd212f4..ae9a138a65 100644--- a/packages/api/src/endpoints/custom/initialize.ts+++ b/packages/api/src/endpoints/custom/initialize.ts@@ -69,7 +69,8 @@ export function getTokenConfigKey(/*** Maps an admin-facing static `tokenConfig` to the billing shape: the UI uses- * `cacheWrite`/`cacheRead`, but `getCacheMultiplier` indexes `write`/`read`.+ * `cacheWrite`/`cacheWrite1h`/`cacheRead`, but `getCacheMultiplier` indexes+ * `write`/`write1h`/`read`.* Adds those keys (preserving the originals) so cache tokens bill at the* configured rate instead of the prompt-rate fallback.*/@@ -82,6 +83,9 @@ function toBillingTokenConfig(if (rates.cacheWrite != null) {mapped.write = rates.cacheWrite;}+ if (rates.cacheWrite1h != null) {+ mapped.write1h = rates.cacheWrite1h;+ }if (rates.cacheRead != null) {mapped.read = rates.cacheRead;}diff --git a/packages/api/src/stream/interfaces/IJobStore.ts b/packages/api/src/stream/interfaces/IJobStore.tsindex f9e040252c..6ff437d7cf 100644--- a/packages/api/src/stream/interfaces/IJobStore.ts+++ b/packages/api/src/stream/interfaces/IJobStore.ts@@ -822,6 +822,8 @@ export interface UsageMetadata {input_token_details?: {/** Tokens written to cache */cache_creation?: number;+ /** Subset of `cache_creation` written to Anthropic's 1-hour cache (see `getModelEndUsage`) */+ cache_creation_1h?: number;/** Tokens read from cache */cache_read?: number;/** OpenAI GPT-5.6+ cache-write tokens (billed above the input rate) */diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.tsindex d25a2790b7..f77d22eaf8 100644--- a/packages/data-provider/src/config.ts+++ b/packages/data-provider/src/config.ts@@ -2205,6 +2205,8 @@ export const endpointSchema = baseEndpointSchema.merge(context: z.number(),cacheRead: z.number().optional(),cacheWrite: z.number().optional(),+ /** 1-hour prompt cache writes; defaults to twice `prompt` */+ cacheWrite1h: z.number().optional(),}),).optional(),diff --git a/packages/data-provider/src/types/runs.ts b/packages/data-provider/src/types/runs.tsindex 10bf09519d..f5358d4654 100644--- a/packages/data-provider/src/types/runs.ts+++ b/packages/data-provider/src/types/runs.ts@@ -333,6 +333,8 @@ export type TTokenUsageEvent = {total_tokens?: number;input_token_details?: {cache_creation?: number;+ /** Subset of `cache_creation` written to Anthropic's 1-hour cache */+ cache_creation_1h?: number;cache_read?: number;};model?: string;diff --git a/packages/data-schemas/src/methods/spendTokens.spec.ts b/packages/data-schemas/src/methods/spendTokens.spec.tsindex 975b907547..2993684421 100644--- a/packages/data-schemas/src/methods/spendTokens.spec.ts+++ b/packages/data-schemas/src/methods/spendTokens.spec.ts@@ -665,6 +665,57 @@ describe('spendTokens', () => {expect(balance!.tokenCredits).toBeLessThan(10000); // Balance should be reduced});+ describe('one-hour cache writes', () => {+ const model = 'claude-sonnet-5-5';+ const txData = () => ({+ user: userId,+ conversationId: 'convo-1h',+ model,+ context: 'message',+ balance: { enabled: true },+ });++ beforeEach(async () => {+ await Balance.create({ user: userId, tokenCredits: 100_000_000 });+ });++ it('bills the 1-hour share of the writes at the 1-hour rate', async () => {+ const promptTokens = { input: 100, write: 3000, write1h: 2000, read: 500 };+ const result = await spendStructuredTokens(txData(), { promptTokens });++ const prompt = tokenValues[model].prompt;+ const write5m = getCacheMultiplier({ model, cacheType: 'write' })!;+ const write1h = getCacheMultiplier({ model, cacheType: 'write1h' })!;+ const read = getCacheMultiplier({ model, cacheType: 'read' })!;+ const expected = -(100 * prompt + 1000 * write5m + 2000 * write1h + 500 * read);+ expect(result.prompt?.prompt).toBeCloseTo(expected);++ const [doc] = await Transaction.find({ user: userId, tokenType: 'prompt' }).lean();+ expect(doc.writeTokens).toBe(-3000);+ expect(doc.tokenValue).toBeCloseTo(expected);+ });++ it('keeps 5-minute pricing when no 1-hour writes are reported', async () => {+ const promptTokens = { input: 100, write: 3000, read: 500 };+ const result = await spendStructuredTokens(txData(), { promptTokens });++ const expected = -(+ 100 * tokenValues[model].prompt ++ 3000 * getCacheMultiplier({ model, cacheType: 'write' })! ++ 500 * getCacheMultiplier({ model, cacheType: 'read' })!+ );+ expect(result.prompt?.prompt).toBeCloseTo(expected);+ });++ it('never bills more 1-hour writes than writes', async () => {+ const promptTokens = { input: 0, write: 1000, write1h: 5000, read: 0 };+ const result = await spendStructuredTokens(txData(), { promptTokens });+ expect(result.prompt?.prompt).toBeCloseTo(+ -1000 * getCacheMultiplier({ model, cacheType: 'write1h' })!,+ );+ });+ });+describe('premium token pricing', () => {it('should charge standard rates for gemini-3.1 when prompt tokens are below threshold', async () => {const initialBalance = 100000000;diff --git a/packages/data-schemas/src/methods/spendTokens.ts b/packages/data-schemas/src/methods/spendTokens.tsindex 07c6d3818a..02b338385b 100644--- a/packages/data-schemas/src/methods/spendTokens.ts+++ b/packages/data-schemas/src/methods/spendTokens.ts@@ -27,7 +27,7 @@ export function createSpendTokensMethods(spendStructuredTokens: (txData: SpendTxData,tokenUsage: {- promptTokens?: { input?: number; write?: number; read?: number };+ promptTokens?: { input?: number; write?: number; write1h?: number; read?: number };completionTokens?: number;},) => Promise<{@@ -93,7 +93,7 @@ export function createSpendTokensMethods(async function spendStructuredTokens(txData: SpendTxData,tokenUsage: {- promptTokens?: { input?: number; write?: number; read?: number };+ promptTokens?: { input?: number; write?: number; write1h?: number; read?: number };completionTokens?: number;},): Promise<{@@ -112,6 +112,7 @@ export function createSpendTokensMethods(if (promptTokens) {const input = Math.max(promptTokens.input ?? 0, 0);const write = Math.max(promptTokens.write ?? 0, 0);+ const write1h = Math.min(Math.max(promptTokens.write1h ?? 0, 0), write);const read = Math.max(promptTokens.read ?? 0, 0);const totalInputTokens = input + write + read;prompt = await transactionMethods.createStructuredTransaction({@@ -119,6 +120,7 @@ export function createSpendTokensMethods(tokenType: 'prompt',inputTokens: -input,writeTokens: -write,+ ...(write1h > 0 && { write1hTokens: -write1h }),readTokens: -read,inputTokenCount: totalInputTokens,});diff --git a/packages/data-schemas/src/methods/transaction.ts b/packages/data-schemas/src/methods/transaction.tsindex b0ea64483a..01874c090a 100644--- a/packages/data-schemas/src/methods/transaction.ts+++ b/packages/data-schemas/src/methods/transaction.ts@@ -29,7 +29,7 @@ type MultiplierParams = {};type CacheMultiplierParams = {- cacheType?: 'write' | 'read';+ cacheType?: 'write' | 'read' | 'write1h';model?: string;endpointTokenConfig?: Record<string, Record<string, number>>;inputTokenCount?: number;@@ -49,6 +49,8 @@ interface InternalTxDoc {rateDetail?: Record<string, number>;inputTokens?: number;writeTokens?: number;+ /** Subset of `writeTokens` written to the 1-hour cache */+ write1hTokens?: number;readTokens?: number;}@@ -65,6 +67,8 @@ export interface TxData {inputTokenCount?: number;inputTokens?: number;writeTokens?: number;+ /** Subset of `writeTokens` written to the 1-hour cache */+ write1hTokens?: number;readTokens?: number;balance?: { enabled?: boolean };transactions?: { enabled?: boolean };@@ -169,31 +173,42 @@ export function createTransactionMethods(inputTokenCount,}) ?? inputMultiplier;+ const inputAbs = Math.abs(txn.inputTokens ?? 0);+ const writeAbs = Math.abs(txn.writeTokens ?? 0);+ const write1hAbs = Math.min(Math.abs(txn.write1hTokens ?? 0), writeAbs);+ const readAbs = Math.abs(txn.readTokens ?? 0);+ const write1hMultiplier =+ write1hAbs > 0+ ? (txMethods.getCacheMultiplier({+ cacheType: 'write1h',+ model,+ endpointTokenConfig: etConfig,+ inputTokenCount,+ }) ?? writeMultiplier)+ : writeMultiplier;+txn.rateDetail = {input: inputMultiplier,write: writeMultiplier,read: readMultiplier,+ ...(write1hAbs > 0 && { write1h: write1hMultiplier }),};- const totalPromptTokens =- Math.abs(txn.inputTokens ?? 0) +- Math.abs(txn.writeTokens ?? 0) +- Math.abs(txn.readTokens ?? 0);-- if (totalPromptTokens > 0) {- txn.rate =- (Math.abs(inputMultiplier * (txn.inputTokens ?? 0)) +- Math.abs(writeMultiplier * (txn.writeTokens ?? 0)) +- Math.abs(readMultiplier * (txn.readTokens ?? 0))) /- totalPromptTokens;- } else {- txn.rate = Math.abs(inputMultiplier);- }+ const totalPromptTokens = inputAbs + writeAbs + readAbs;+ const promptValue =+ inputAbs * Math.abs(inputMultiplier) ++ (writeAbs - write1hAbs) * Math.abs(writeMultiplier) ++ write1hAbs * Math.abs(write1hMultiplier) ++ readAbs * Math.abs(readMultiplier);++ txn.rate =+ totalPromptTokens > 0 ? promptValue / totalPromptTokens : Math.abs(inputMultiplier);txn.tokenValue = -(- Math.abs(txn.inputTokens ?? 0) * inputMultiplier +- Math.abs(txn.writeTokens ?? 0) * writeMultiplier +- Math.abs(txn.readTokens ?? 0) * readMultiplier+ inputAbs * inputMultiplier ++ (writeAbs - write1hAbs) * writeMultiplier ++ write1hAbs * write1hMultiplier ++ readAbs * readMultiplier);txn.rawAmount = -totalPromptTokens;@@ -654,6 +669,7 @@ export function createTransactionMethods(const transaction = new Transaction(txData);transaction.endpointTokenConfig = txData.endpointTokenConfig;transaction.inputTokenCount = txData.inputTokenCount;+ transaction.write1hTokens = txData.write1hTokens;calculateStructuredTokenValue(transaction);diff --git a/packages/data-schemas/src/methods/tx.spec.ts b/packages/data-schemas/src/methods/tx.spec.tsindex 9f91c39952..17fb5ee291 100644--- a/packages/data-schemas/src/methods/tx.spec.ts+++ b/packages/data-schemas/src/methods/tx.spec.ts@@ -6,6 +6,7 @@ import {cacheTokenValues,premiumTokenValues,premiumCacheTokenValues,+ ONE_HOUR_CACHE_WRITE_FACTOR,defaultRate,} from './tx';import { matchModelName, findMatchingPattern } from './test-helpers';@@ -3085,6 +3086,67 @@ describe('Long-Context Premium Cache Pricing', () => {});});+describe('One-Hour Cache Write Pricing', () => {+ const claudeModels = [+ 'claude-sonnet-5-5',+ 'claude-opus-5-5',+ 'claude-haiku-4-5',+ 'claude-haiku-5-5',+ ];++ it('prices 1-hour writes at the prompt rate times the 1-hour factor', () => {+ for (const model of claudeModels) {+ expect(getCacheMultiplier({ model, cacheType: 'write1h' })).toBeCloseTo(+ getMultiplier({ model, tokenType: 'prompt' }) * ONE_HOUR_CACHE_WRITE_FACTOR,+ );+ }+ });++ it('keeps the 5-minute write rate below the 1-hour rate', () => {+ for (const model of claudeModels) {+ expect(getCacheMultiplier({ model, cacheType: 'write' })).toBeLessThan(+ getCacheMultiplier({ model, cacheType: 'write1h' }) as number,+ );+ }+ });++ it('follows the long-context prompt tier above the premium threshold', () => {+ const model = 'claude-haiku-5-5';+ const { threshold } = premiumTokenValues[model];+ expect(+ getCacheMultiplier({ model, cacheType: 'write1h', inputTokenCount: threshold + 1 }),+ ).toBe(premiumTokenValues[model].prompt * ONE_HOUR_CACHE_WRITE_FACTOR);+ expect(getCacheMultiplier({ model, cacheType: 'write1h', inputTokenCount: threshold })).toBe(+ tokenValues[model].prompt * ONE_HOUR_CACHE_WRITE_FACTOR,+ );+ });++ it('uses an endpoint write1h override, else twice the configured prompt rate', () => {+ const model = 'custom-claude';+ const prompt = tokenValues['claude-sonnet-5-5'].prompt;+ expect(+ getCacheMultiplier({+ model,+ cacheType: 'write1h',+ endpointTokenConfig: { [model]: { prompt, completion: prompt, write1h: prompt * 3 } },+ }),+ ).toBe(prompt * 3);+ expect(+ getCacheMultiplier({+ model,+ cacheType: 'write1h',+ endpointTokenConfig: { [model]: { prompt, completion: prompt } },+ }),+ ).toBe(prompt * ONE_HOUR_CACHE_WRITE_FACTOR);+ });++ it('leaves the 5-minute write rate unchanged', () => {+ for (const model of claudeModels) {+ expect(getCacheMultiplier({ model, cacheType: 'write' })).toBe(cacheTokenValues[model].write);+ }+ });+});+describe('Newer model pricing', () => {const newlyPriced = ['llama4-scout',diff --git a/packages/data-schemas/src/methods/tx.ts b/packages/data-schemas/src/methods/tx.tsindex 0d06e6afec..7aa6a5d45f 100644--- a/packages/data-schemas/src/methods/tx.ts+++ b/packages/data-schemas/src/methods/tx.ts@@ -507,6 +507,12 @@ export const premiumCacheTokenValues: Record<'grok-4-7': { threshold: 199999, write: 4, read: 1 },};+/** `write1h` prices Anthropic's 1-hour prompt cache writes; `write` is the 5-minute rate */+type CacheType = 'write' | 'read' | 'write1h';++/** Anthropic bills 1-hour cache writes at 2x the input rate on every model and tier */+export const ONE_HOUR_CACHE_WRITE_FACTOR = 2;+export function createTxMethods(_mongoose: typeof import('mongoose'),txDeps: TxDeps,@@ -556,7 +562,7 @@ export function createTxMethods(inputTokenCount,}: {valueKey?: string;- cacheType?: 'write' | 'read';+ cacheType?: CacheType;model?: string;endpoint?: string;endpointTokenConfig?: Record<string, Record<string, number>>;@@ -704,7 +710,9 @@ export function createTxMethods(/*** Retrieves the cache multiplier for a given value key and token type.* When `inputTokenCount` crosses a model's long-context threshold, the- * premium cache rate applies instead of the standard one.+ * premium cache rate applies instead of the standard one. A `write1h`+ * rate comes from an explicit endpoint override, otherwise it is the+ * prompt rate of the same tier times {@link ONE_HOUR_CACHE_WRITE_FACTOR}.*/function getCacheMultiplier({valueKey,@@ -715,12 +723,28 @@ export function createTxMethods(inputTokenCount,}: {valueKey?: string;- cacheType?: 'write' | 'read';+ cacheType?: CacheType;model?: string;endpoint?: string;endpointTokenConfig?: Record<string, Record<string, number>>;inputTokenCount?: number | null;}): number | null {+ if (cacheType === 'write1h') {+ const override = model ? endpointTokenConfig?.[model]?.write1h : undefined;+ if (override != null) {+ return override;+ }+ const promptRate = getMultiplier({+ model,+ valueKey,+ endpoint,+ tokenType: 'prompt',+ inputTokenCount: inputTokenCount ?? undefined,+ endpointTokenConfig,+ });+ return promptRate * ONE_HOUR_CACHE_WRITE_FACTOR;+ }+if (endpointTokenConfig && model) {const modelConfig = endpointTokenConfig[model];/** Models absent from a partial override fall through to standard