Analysis: Issue #967 - Add configurable cache retention
Summary
Type: Feature request
Request: Add support for extended cache retention for Anthropic and OpenAI APIs, controlled via environment variable (not user-facing setting).
Agreed approach (from discussion): Environment variable to keep it hidden from users who don't understand cost implications.
Provider Support
Current Implementation
Anthropic (anthropic.ts):
- Uses
cache_control: { type: "ephemeral" } without TTL (defaults to 5m)
- Applied to: system prompt (line ~319-335) and last user message (line ~454-460)
OpenAI Responses (openai-responses.ts):
- Uses
prompt_cache_key: options?.sessionId (line 103)
- No
prompt_cache_retention field set (defaults to in_memory)
Proposed Implementation
Environment variable: PI_CACHE_RETENTION
- Values: unset (default behavior) or
persistent (extended retention)
- Browser-safe check:
typeof process !== "undefined" && process.env.PI_CACHE_RETENTION === "persistent"
Files to modify:
-
packages/ai/src/providers/anthropic.ts
- Add helper function to get cache control TTL from env
- Update
buildParams() (~line 319-335): Add ttl: '1h' to system prompt cache_control when persistent
- Update
convertMessages() (~line 454-460): Add ttl: '1h' to last user message cache_control when persistent
-
packages/ai/src/providers/openai-responses.ts
- Update
buildParams() (~line 97-127): Add prompt_cache_retention: '24h' when persistent
-
packages/ai/README.md
- Document
PI_CACHE_RETENTION environment variable
-
packages/ai/CHANGELOG.md
- Add entry under
[Unreleased]
Code Changes
anthropic.ts - Add helper and modify cache_control:
openai-responses.ts - Add cache retention:
Alternative: Provider-specific env vars
Could also support provider-specific overrides:
ANTHROPIC_CACHE_TTL=1h
OPENAI_CACHE_RETENTION=24h
With PI_CACHE_RETENTION as unified fallback. This gives power users more control.