feat(cache): 1h-Prompt-Cache-TTL für API und Claude CLI

Anthropic hat Anfang März 2026 die Default-Cache-TTL stillschweigend
von 1h auf 5min gesenkt. Bei Sessions mit Pausen >5min wird der Cache
jedes Mal neu geschrieben (teuer). Dieser Commit aktiviert die
extended 1h-TTL über beide Pfade.

API-Pfad (AnthropicService):
- Beta-Header "extended-cache-ttl-2025-04-11" hinzugefügt
- Falsches Top-Level cache_control (Root-Level wurde ignoriert) entfernt
- Neue Helper ApplyCacheControlToLast() setzt cache_control idempotent
  auf das letzte Element via JSON-Roundtrip zu Dictionary
- 2 Cache-Breakpoints pro Iteration:
  * Letztes Tool (cached gesamten Tool-Block)
  * Letzter System-Prompt-Block (cached System-Prompt)
- Sub-Agent-Loop (Preparer/Review/Finalizer): String-System-Prompt wird
  bei Bedarf in Block-Form mit cache_control konvertiert

CLI-Pfad (ClaudeCliService):
- Env-Var ENABLE_PROMPT_CACHING_1H=1 für jeden CLI-Child-Prozess gesetzt
  (offizieller Opt-in seit Claude CLI 2.1.108, gilt für API-Key,
  Bedrock, Vertex, Foundry)
This commit is contained in:
Meleki committed 2026-05-13 06:46:26 +02:00
1 parent e87d7c4eec
commit 1ece62ce8c
4 files changed
+56 -3

No files matched your search

+5 -1
View File
@@ -93,13 +93,17 @@ public partial class AnthropicService
{
["model"] = modelId,
["max_tokens"] = 16384,
["cache_control"] = new { type = "ephemeral" },
["messages"] = messages,
["stream"] = true
};
if (systemPrompt != null && systemPrompt.Count > 0)
{
// Cache-Breakpoint auf den letzten System-Prompt-Block (1h TTL).
// Idempotent über Iterationen.
ApplyCacheControlToLast(systemPrompt);
payload["system"] = systemPrompt;
}
var remoteConn = remoteMgr?.GetBySessionId(sessionId);
var hasRemote = remoteConn != null;