OpenRouter: add cache TTL control for Claude

0eff634bd063ec084eb6a8deb3f9fecfe73ad51e

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +7 -4Ignore whitespace
src/endpoints/backends/chat-completions.js+3 -2
@@ -1342,10 +1342,11 @@ router.post('/generate', function (request, response) {
13421342 bodyParams['reasoning'] = { effort: request.body.reasoning_effort };
13431343 }
13441344
13451345 letconst cachingAtDepth = getConfigValue('claude.cachingAtDepth', -1, 'number');
13461346 const isClaude3or4 = /anthropic\/claude-(3|opus-4|sonnet-4)/.test(request.body.model);
1347+ const cacheTTL = getConfigValue('claude.extendedTTL', false, 'boolean') ? '1h' : '5m';
13471348 if (Number.isInteger(cachingAtDepth) && cachingAtDepth >= 0 && isClaude3or4) {
13481349 cachingAtDepthForOpenRouterClaude(request.body.messages, cachingAtDepth, cacheTTL);
13491350 }
13501351 } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.CUSTOM) {
13511352 apiUrl = request.body.custom_url;
src/prompt-converters.js+4 -2
@@ -906,8 +906,9 @@ export function cachingAtDepthForClaude(messages, cachingAtDepth, ttl) {
906906 * messages array.
907907 * @param {object[]} messages Array of messages
908908 * @param {number} cachingAtDepth Depth at which caching is supposed to occur
909+ * @param {string} ttl TTL value
909910 */
910911export function cachingAtDepthForOpenRouterClaude(messages, cachingAtDepth, ttl) {
911912 //caching the prefill is a terrible idea in general
912913 let passedThePrefill = false;
913914 //depth here is the number of message role switches
@@ -927,12 +928,13 @@ export function cachingAtDepthForOpenRouterClaude(messages, cachingAtDepth) {
927928 messages[i].content = [{
928929 type: 'text',
929930 text: content,
930931 cache_control: { type: 'ephemeral', ttl: ttl },
931932 }];
932933 } else {
933934 const contentPartCount = content.length;
934935 content[contentPartCount - 1].cache_control = {
935936 type: 'ephemeral',
937+ ttl: ttl,
936938 };
937939 }
938940 }