Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(cache): preserve cache_control schema properties (#3348)
C0sm0catsb84c4c9f7164
diff --git a/headroom/proxy/semantic_cache_key_policy.py b/headroom/proxy/semantic_cache_key_policy.pyindex 3b7d3625d..45b9818c1 100644--- a/headroom/proxy/semantic_cache_key_policy.py+++ b/headroom/proxy/semantic_cache_key_policy.py@@ -8,11 +8,25 @@ from typing import Anydef strip_cache_control(obj: Any) -> Any:- """Recursively drop ``cache_control`` annotations before hashing."""+ """Recursively drop prompt-cache annotations, preserving schema properties."""+ return _strip_cache_control(obj, preserve_key=False)+++def _strip_cache_control(obj: Any, *, preserve_key: bool) -> Any:+ """Strip directives while retaining names inside JSON Schema ``properties``.++ ``cache_control`` is also a valid user-defined JSON Schema property name.+ Its value still needs normal recursion because that property's schema may+ itself contain prompt-cache annotations.+ """if isinstance(obj, dict):- return {k: strip_cache_control(v) for k, v in obj.items() if k != "cache_control"}+ return {+ k: _strip_cache_control(v, preserve_key=k == "properties")+ for k, v in obj.items()+ if k != "cache_control" or preserve_key+ }if isinstance(obj, list):- return [strip_cache_control(item) for item in obj]+ return [_strip_cache_control(item, preserve_key=False) for item in obj]return objdiff --git a/tests/test_proxy_semantic_cache_key.py b/tests/test_proxy_semantic_cache_key.pyindex 94db0600b..7d1c9c628 100644--- a/tests/test_proxy_semantic_cache_key.py+++ b/tests/test_proxy_semantic_cache_key.py@@ -42,6 +42,28 @@ def test_different_tools_distinct_keys():assert _key(cache, tools=tools_a) != _key(cache, tools=tools_b)+def test_tool_schema_cache_control_property_produces_distinct_key():+ """A user-defined schema property is not a prompt-cache annotation."""+ tools_without = [{"name": "maintain", "input_schema": {"type": "object", "properties": {}}}]+ tools_with = [+ {+ "name": "maintain",+ "input_schema": {+ "type": "object",+ "properties": {+ "cache_control": {+ "type": "boolean",+ "description": "Flush the application cache",+ }+ },+ },+ }+ ]++ cache = SemanticCache()+ assert _key(cache, tools=tools_without) != _key(cache, tools=tools_with)++@pytest.mark.parametrize("field,a,b",[diff --git a/tests/test_proxy_semantic_cache_key_policy.py b/tests/test_proxy_semantic_cache_key_policy.pyindex afa16e062..09a817e90 100644--- a/tests/test_proxy_semantic_cache_key_policy.py+++ b/tests/test_proxy_semantic_cache_key_policy.py@@ -55,3 +55,17 @@ def test_strip_cache_control_recurses_through_dicts_and_lists() -> None:"system": [{"type": "text", "text": "sys"}],"tools": [{"name": "read"}],}+++def test_strip_cache_control_preserves_user_defined_schema_property() -> None:+ schema = {+ "type": "object",+ "properties": {+ "cache_control": {+ "type": "boolean",+ "description": "Flush the application cache",+ }+ },+ }++ assert strip_cache_control(schema) == schema