From e6184c1cc688b00efc7db1485d4427a19ec17ebe Mon Sep 17 00:00:00 2001 From: Jan Renz Date: Sun, 31 May 2026 14:32:16 +0200 Subject: [PATCH] fix: allow disabling prompt caching (cherry picked from commit c1c1a12fe61399acd696886efb441a3445f8b5e3) --- agent/agent_init.py | 5 ++++- hermes_cli/config.py | 5 ++++- tests/run_agent/test_run_agent.py | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/agent/agent_init.py b/agent/agent_init.py index 5d65c4176e3..7546cb130ec 100644 --- a/agent/agent_init.py +++ b/agent/agent_init.py @@ -521,7 +521,10 @@ def init_agent( from hermes_cli.config import load_config as _load_pc_cfg _pc_cfg = _load_pc_cfg().get("prompt_caching", {}) or {} - _ttl = _pc_cfg.get("cache_ttl", "5m") + if isinstance(_pc_cfg, dict) and _pc_cfg.get("enabled") is False: + agent._use_prompt_caching = False + agent._use_native_cache_layout = False + _ttl = _pc_cfg.get("cache_ttl", "5m") if isinstance(_pc_cfg, dict) else "5m" if _ttl in {"5m", "1h"}: agent._cache_ttl = _ttl except Exception: diff --git a/hermes_cli/config.py b/hermes_cli/config.py index 050f1975db9..115096fda2c 100644 --- a/hermes_cli/config.py +++ b/hermes_cli/config.py @@ -1410,8 +1410,11 @@ DEFAULT_CONFIG = { }, # Anthropic prompt caching (Claude via OpenRouter or native Anthropic API). - # cache_ttl must be "5m" or "1h" (Anthropic-supported tiers); other values are ignored. + # Set enabled: false as an escape hatch for strict providers that reject + # cache_control markers; cache_ttl must be "5m" or "1h" (Anthropic-supported + # tiers), other values are ignored. "prompt_caching": { + "enabled": True, "cache_ttl": "5m", }, diff --git a/tests/run_agent/test_run_agent.py b/tests/run_agent/test_run_agent.py index da8a446bf8d..69b02c9cd61 100644 --- a/tests/run_agent/test_run_agent.py +++ b/tests/run_agent/test_run_agent.py @@ -1026,6 +1026,30 @@ class TestInit: ) assert a._cache_ttl == "5m" + def test_prompt_caching_enabled_false_disables_cache_markers(self): + """prompt_caching.enabled=false is an escape hatch for strict providers.""" + with ( + patch("run_agent.get_tool_definitions", return_value=[]), + patch("run_agent.check_toolset_requirements", return_value={}), + patch("agent.anthropic_adapter._anthropic_sdk"), + patch( + "hermes_cli.config.load_config", + return_value={"prompt_caching": {"enabled": False}}, + ), + ): + a = AIAgent( + api_key="test-key-1234567890", + provider="anthropic", + model="claude-sonnet-4-6", + base_url="https://api.anthropic.com/v1/", + quiet_mode=True, + skip_context_files=True, + skip_memory=True, + ) + assert a.api_mode == "anthropic_messages" + assert a._use_prompt_caching is False + assert a._use_native_cache_layout is False + def test_valid_tool_names_populated(self): """valid_tool_names should contain names from loaded tools.""" tools = _make_tool_defs("web_search", "terminal")