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
refactor(proxy): delete duplicate semantic_cache_key module (#3472)
trs-80e9c6276e2491
diff --git a/headroom/proxy/semantic_cache_key.py b/headroom/proxy/semantic_cache_key.pydeleted file mode 100644index 267eddb36..000000000--- a/headroom/proxy/semantic_cache_key.py+++ /dev/null@@ -1,42 +0,0 @@-"""Pure semantic-cache key construction policy."""--from __future__ import annotations--import hashlib-import json-from typing import Any---def strip_cache_control(obj: Any) -> Any:- """Recursively drop ``cache_control`` annotations before hashing."""- if isinstance(obj, dict):- return {k: strip_cache_control(v) for k, v in obj.items() if k != "cache_control"}- if isinstance(obj, list):- return [strip_cache_control(item) for item in obj]- return obj---def compute_semantic_cache_key(- messages: list[dict],- model: str,- **key_fields: Any,-) -> str:- """Compute the proxy semantic-cache key from generation-shaping inputs.-- ``cache_control`` is stripped from ``messages`` as well as the shaping- fields: it is a prompt-caching directive for the upstream provider that- never changes the generated completion, so a moved breakpoint must not- fragment the key. Messages are the primary key component and, on the- Anthropic path, the most common place a client (e.g. Claude Code) moves a- breakpoint between turns, so leaving them un-stripped defeated the strip for- the field that matters most.- """- normalized = json.dumps(- {- "model": model,- "messages": strip_cache_control(messages),- **{k: strip_cache_control(v) for k, v in key_fields.items()},- },- sort_keys=True,- )- return hashlib.sha256(normalized.encode()).hexdigest()[:32]diff --git a/tests/test_proxy_semantic_cache_key_policy.py b/tests/test_proxy_semantic_cache_key_policy.pyindex 04239d0ac..afa16e062 100644--- a/tests/test_proxy_semantic_cache_key_policy.py+++ b/tests/test_proxy_semantic_cache_key_policy.py@@ -2,7 +2,7 @@from __future__ import annotations-from headroom.proxy.semantic_cache_key import (+from headroom.proxy.semantic_cache_key_policy import (compute_semantic_cache_key,strip_cache_control,)