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) {
1342 bodyParams['reasoning'] = { effort: request.body.reasoning_effort };1342 bodyParams['reasoning'] = { effort: request.body.reasoning_effort };
1343 }1343 }
13441344
1345 let cachingAtDepth = getConfigValue('claude.cachingAtDepth', -1, 'number');1345 const cachingAtDepth = getConfigValue('claude.cachingAtDepth', -1, 'number');
1346 const isClaude3or4 = /anthropic\/claude-(3|opus-4|sonnet-4)/.test(request.body.model);1346 const isClaude3or4 = /anthropic\/claude-(3|opus-4|sonnet-4)/.test(request.body.model);
1347 const cacheTTL = getConfigValue('claude.extendedTTL', false, 'boolean') ? '1h' : '5m';
1347 if (Number.isInteger(cachingAtDepth) && cachingAtDepth >= 0 && isClaude3or4) {1348 if (Number.isInteger(cachingAtDepth) && cachingAtDepth >= 0 && isClaude3or4) {
1348 cachingAtDepthForOpenRouterClaude(request.body.messages, cachingAtDepth);1349 cachingAtDepthForOpenRouterClaude(request.body.messages, cachingAtDepth, cacheTTL);
1349 }1350 }
1350 } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.CUSTOM) {1351 } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.CUSTOM) {
1351 apiUrl = request.body.custom_url;1352 apiUrl = request.body.custom_url;
src/prompt-converters.js+4 -2
@@ -906,8 +906,9 @@ export function cachingAtDepthForClaude(messages, cachingAtDepth, ttl) {
906 * messages array.906 * messages array.
907 * @param {object[]} messages Array of messages907 * @param {object[]} messages Array of messages
908 * @param {number} cachingAtDepth Depth at which caching is supposed to occur908 * @param {number} cachingAtDepth Depth at which caching is supposed to occur
909 * @param {string} ttl TTL value
909 */910 */
910export function cachingAtDepthForOpenRouterClaude(messages, cachingAtDepth) {911export function cachingAtDepthForOpenRouterClaude(messages, cachingAtDepth, ttl) {
911 //caching the prefill is a terrible idea in general912 //caching the prefill is a terrible idea in general
912 let passedThePrefill = false;913 let passedThePrefill = false;
913 //depth here is the number of message role switches914 //depth here is the number of message role switches
@@ -927,12 +928,13 @@ export function cachingAtDepthForOpenRouterClaude(messages, cachingAtDepth) {
927 messages[i].content = [{928 messages[i].content = [{
928 type: 'text',929 type: 'text',
929 text: content,930 text: content,
930 cache_control: { type: 'ephemeral' },931 cache_control: { type: 'ephemeral', ttl: ttl },
931 }];932 }];
932 } else {933 } else {
933 const contentPartCount = content.length;934 const contentPartCount = content.length;
934 content[contentPartCount - 1].cache_control = {935 content[contentPartCount - 1].cache_control = {
935 type: 'ephemeral',936 type: 'ephemeral',
937 ttl: ttl,
936 };938 };
937 }939 }
938 }940 }