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
agent_savings: don't crash the proxy on an unknown savings profile (#1830)
gglucasscfcd40f8ac95
diff --git a/headroom/agent_savings.py b/headroom/agent_savings.pyindex 368b8e157..975f9963f 100644--- a/headroom/agent_savings.py+++ b/headroom/agent_savings.py@@ -2,10 +2,14 @@from __future__ import annotations+import loggingfrom dataclasses import dataclass, replacefrom typing import Protocol+logger = logging.getLogger(__name__)+AGENT_90_PROFILE = "agent-90"+FALLBACK_PROFILE = "balanced"class CompressConfigLike(Protocol):@@ -150,14 +154,29 @@ _PROFILES: dict[str, AgentSavingsProfile] = {def get_agent_savings_profile(name: str | None = None) -> AgentSavingsProfile:- """Return a named agent savings profile."""+ """Return a named agent savings profile.++ An unrecognized name falls back to the ``balanced`` profile with a warning+ instead of raising. The savings profile is a soft config knob, but it is+ resolved during proxy startup (``proxy_pipeline_kwargs`` -> ``create_app``),+ so raising here takes the whole proxy down before it can open its port. That+ happens on desktop/runtime version skew: a newer client requests a profile+ (e.g. ``coding``) that an older pinned or fallback runtime predates. Degrade+ to ``balanced`` rather than leaving the user with no proxy at all.+ """key = (name or AGENT_90_PROFILE).strip().lower()- try:- return _PROFILES[key]- except KeyError as exc:- valid = ", ".join(sorted(_PROFILES))- raise ValueError(f"unknown savings profile {name!r}; expected one of: {valid}") from exc+ profile = _PROFILES.get(key)+ if profile is not None:+ return profile+ valid = ", ".join(sorted(_PROFILES))+ logger.warning(+ "unknown savings profile %r; falling back to %r (known: %s)",+ name,+ FALLBACK_PROFILE,+ valid,+ )+ return _PROFILES[FALLBACK_PROFILE]def apply_agent_savings_env_defaults(diff --git a/tests/test_agent_savings.py b/tests/test_agent_savings.pyindex 15a555619..589eaf1eb 100644--- a/tests/test_agent_savings.py+++ b/tests/test_agent_savings.py@@ -1,6 +1,7 @@from __future__ import annotationsimport json+import loggingfrom importlib import import_modulefrom types import SimpleNamespace@@ -124,9 +125,17 @@ def test_agent_savings_env_defaults_preserve_user_overrides() -> None:assert env["HEADROOM_SMART_CRUSHER_COMPACTION"] == "0"-def test_unknown_agent_savings_profile_lists_valid_profiles() -> None:- with pytest.raises(ValueError, match="agent-90"):- get_agent_savings_profile("missing")+def test_unknown_agent_savings_profile_falls_back_to_balanced(+ caplog: pytest.LogCaptureFixture,+) -> None:+ # An unknown profile must NOT raise: it's resolved during proxy startup, so+ # raising takes the whole proxy down before it opens its port (desktop asked+ # for a profile a fallback runtime predates). Degrade to "balanced" instead.+ with caplog.at_level(logging.WARNING):+ profile = get_agent_savings_profile("missing")+ assert profile is get_agent_savings_profile("balanced")+ assert "unknown savings profile" in caplog.text+ assert "missing" in caplog.textdef test_with_target_savings_recomputes_target_ratio() -> None: