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

+16 -1
View File
@@ -466,9 +466,24 @@ public partial class AnthropicService
};
if (systemPrompt is List<object> spBlocks && spBlocks.Count > 0)
{
// Cache-Breakpoint auf den letzten System-Prompt-Block (1h TTL).
ApplyCacheControlToLast(spBlocks);
payload["system"] = spBlocks;
}
else if (systemPrompt is string spText && !string.IsNullOrEmpty(spText))
payload["system"] = spText;
{
// String-System-Prompt → in Block-Form mit cache_control konvertieren.
payload["system"] = new List<object>
{
new Dictionary<string, object>
{
["type"] = "text",
["text"] = spText,
["cache_control"] = new { type = "ephemeral", ttl = "1h" }
}
};
}
AddToolsToPayload(payload, toolOpts with { Iteration = iterations });