OpenRouter: add cache TTL control for Claude
| @@ -1342,10 +1342,11 @@ router.post('/generate', function (request, response) { | ||
| 1342 | 1342 | bodyParams['reasoning'] = { effort: request.body.reasoning_effort }; |
| 1343 | 1343 | } |
| 1344 | 1344 | |
| 1345 | 1345 | letconst cachingAtDepth = getConfigValue('claude.cachingAtDepth', -1, 'number'); |
| 1346 | 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 | 1348 | if (Number.isInteger(cachingAtDepth) && cachingAtDepth >= 0 && isClaude3or4) { |
| 1348 | 1349 | cachingAtDepthForOpenRouterClaude(request.body.messages, cachingAtDepth, cacheTTL); |
| 1349 | 1350 | } |
| 1350 | 1351 | } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.CUSTOM) { |
| 1351 | 1352 | apiUrl = request.body.custom_url; |
| @@ -906,8 +906,9 @@ export function cachingAtDepthForClaude(messages, cachingAtDepth, ttl) { | ||
| 906 | 906 | * messages array. |
| 907 | 907 | * @param {object[]} messages Array of messages |
| 908 | 908 | * @param {number} cachingAtDepth Depth at which caching is supposed to occur |
| 909 | + * @param {string} ttl TTL value | |
| 909 | 910 | */ |
| 910 | 911 | export function cachingAtDepthForOpenRouterClaude(messages, cachingAtDepth, ttl) { |
| 911 | 912 | //caching the prefill is a terrible idea in general |
| 912 | 913 | let passedThePrefill = false; |
| 913 | 914 | //depth here is the number of message role switches |
| @@ -927,12 +928,13 @@ export function cachingAtDepthForOpenRouterClaude(messages, cachingAtDepth) { | ||
| 927 | 928 | messages[i].content = [{ |
| 928 | 929 | type: 'text', |
| 929 | 930 | text: content, |
| 930 | 931 | cache_control: { type: 'ephemeral', ttl: ttl }, |
| 931 | 932 | }]; |
| 932 | 933 | } else { |
| 933 | 934 | const contentPartCount = content.length; |
| 934 | 935 | content[contentPartCount - 1].cache_control = { |
| 935 | 936 | type: 'ephemeral', |
| 937 | + ttl: ttl, | |
| 936 | 938 | }; |
| 937 | 939 | } |
| 938 | 940 | } |