MCPcopy Create free account
hub / github.com/Noumena-Network/code / should1hCacheTTL

Function should1hCacheTTL

src/services/api/claude.ts:400–442  ·  view source on GitHub ↗

* Determines if 1h TTL should be used for prompt caching. * * Only applied when: * 1. User is eligible (ant or subscriber within rate limits) * 2. The query source matches a pattern in the GrowthBook allowlist * * GrowthBook config shape: { allowlist: string[] } * Patterns support trailing '*

(querySource?: QuerySource)

Source from the content-addressed store, hash-verified

398 * TTLs when GrowthBook's disk cache updates mid-request.
399 */
400function should1hCacheTTL(querySource?: QuerySource): boolean {
401 // 3P Bedrock users get 1h TTL when opted in via env var — they manage their own billing
402 // No GrowthBook gating needed since 3P users don't have GrowthBook configured
403 if (
404 getAPIProvider() === 'bedrock' &&
405 isEnvTruthy(process.env.ENABLE_PROMPT_CACHING_1H_BEDROCK)
406 ) {
407 return true
408 }
409
410 // Latch eligibility in bootstrap state for session stability — prevents
411 // mid-session overage flips from changing the cache_control TTL, which
412 // would bust the server-side prompt cache (~20K tokens per flip).
413 let userEligible = getPromptCache1hEligible()
414 if (userEligible === null) {
415 userEligible =
416 isInternalBuild() ||
417 (getCurrentOauthBackedFirstPartyInferenceSession() !== null &&
418 !currentLimits.isUsingOverage)
419 setPromptCache1hEligible(userEligible)
420 }
421 if (!userEligible) return false
422
423 // Cache allowlist in bootstrap state for session stability — prevents mixed
424 // TTLs when GrowthBook's disk cache updates mid-request
425 let allowlist = getPromptCache1hAllowlist()
426 if (allowlist === null) {
427 const config = getFeatureValue_CACHED_MAY_BE_STALE<{
428 allowlist?: string[]
429 }>('ncode_prompt_cache_1h_config', {})
430 allowlist = config.allowlist ?? []
431 setPromptCache1hAllowlist(allowlist)
432 }
433
434 return (
435 querySource !== undefined &&
436 allowlist.some(pattern =>
437 pattern.endsWith('*')
438 ? querySource.startsWith(pattern.slice(0, -1))
439 : querySource === pattern,
440 )
441 )
442}
443
444/**
445 * Configure effort parameters for API request.

Callers 1

getCacheControlFunction · 0.85

Calls 9

isEnvTruthyFunction · 0.90
getAPIProviderFunction · 0.85
getPromptCache1hEligibleFunction · 0.85
isInternalBuildFunction · 0.85
setPromptCache1hEligibleFunction · 0.85

Tested by

no test coverage detected