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(proxy): keep tool deferral on Copilot Business/Enterprise/Individual hosts (#4608)
Tejas Choprabe456d4e3f46
diff --git a/headroom/copilot_auth.py b/headroom/copilot_auth.pyindex d2290759b..f431c9bfc 100644--- a/headroom/copilot_auth.py+++ b/headroom/copilot_auth.py@@ -157,7 +157,7 @@ def _github_host() -> str:configured_url = os.environ.get("GITHUB_COPILOT_API_URL", "").strip()if configured_url:hostname = _configured_url_hostname(configured_url)- if _is_public_copilot_api_host(hostname):+ if is_public_copilot_api_host(hostname):return DEFAULT_GITHUB_HOSTfor prefix in ("copilot-api.", "api."):if hostname.startswith(prefix):@@ -231,7 +231,7 @@ def _copilot_subdomain_enterprise_host(enterprise_url: str) -> str | None:if (not hostor host in {"github.com", "www.github.com", "api.github.com"}- or _is_public_copilot_api_host(host)+ or is_public_copilot_api_host(host)):return Nonereturn host@@ -361,7 +361,7 @@ def copilot_nes_base_url(model: Any, upstream_base_url: str) -> str | None:if not (isinstance(model, str) and model.startswith("copilot-nes-")):return None- if not _is_public_copilot_api_host(_url_host(upstream_base_url)):+ if not is_public_copilot_api_host(_url_host(upstream_base_url)):return Noneif is_copilot_completions_host(upstream_base_url):return None@@ -401,7 +401,7 @@ def copilot_completions_base_url() -> str:if _observed_completions_base_url:return _observed_completions_base_urlconfigured = _configured_api_url_override()- if configured and not _is_public_copilot_api_host(_url_host(configured)):+ if configured and not is_public_copilot_api_host(_url_host(configured)):return configuredreturn DEFAULT_COMPLETIONS_PROXY_URL@@ -1318,7 +1318,7 @@ def is_copilot_api_url(url: str | None) -> bool:if configured_host and host == configured_host:return Truehostname = (parsed.hostname or host.split("/", 1)[0]).lower()- return _is_public_copilot_api_host(hostname) or _is_ghe_copilot_api_host(hostname)+ return is_public_copilot_api_host(hostname) or _is_ghe_copilot_api_host(hostname)def is_copilot_upstream_url(url: str | None) -> bool:@@ -1340,7 +1340,7 @@ def is_copilot_upstream_url(url: str | None) -> bool:return is_copilot_api_url(url) or is_copilot_completions_host(url)-def _is_public_copilot_api_host(host: str) -> bool:+def is_public_copilot_api_host(host: str) -> bool:"""Return True for GitHub-hosted Copilot API domains."""return host == "githubcopilot.com" or host.endswith(".githubcopilot.com")diff --git a/headroom/proxy/helpers.py b/headroom/proxy/helpers.pyindex d4c09bb82..f138c5024 100644--- a/headroom/proxy/helpers.py+++ b/headroom/proxy/helpers.py@@ -4119,20 +4119,29 @@ def _warn_deferred_without_search_tool(tools: Any) -> None:)-#: Non-Anthropic upstreams that accept the first-party deferred tool shape.-#: These bill deferred schemas compactly, so downgrading deferral for them-#: inlines the whole catalog and multiplies billed prompt tokens.-_DEFERRAL_CAPABLE_HOSTS: frozenset[str] = frozenset({"api.githubcopilot.com"})--def anthropic_first_party_tool_search_supported(api_base_url: str | None) -> bool:- """Return whether Anthropic server-side tool search is valid for this upstream."""+ """Return whether Anthropic server-side tool search is valid for this upstream.++ GitHub's Copilot API also accepts the first-party deferred shape and bills+ deferred schemas compactly, so downgrading deferral there inlines the whole+ catalog and multiplies billed prompt tokens (#4356). That holds for the+ generic host and for the plan hosts Business/Enterprise/Individual seats+ are routed to (``api.<plan>.githubcopilot.com``, #4155), so any public+ ``*.githubcopilot.com`` host qualifies. GHE data-residency hosts+ (``copilot-api.<tenant>.ghe.com``) still downgrade: nobody has verified+ that they accept the shape.+ """+ from headroom.copilot_auth import is_copilot_completions_host, is_public_copilot_api_hostfrom headroom.providers.claude.runtime import (_gate_view_host,is_custom_anthropic_base_url,)- if _gate_view_host((api_base_url or "").strip()) in _DEFERRAL_CAPABLE_HOSTS:+ # The ``proxy.*`` hosts are the inline-completions surface: nobody has measured+ # the deferred shape there.+ if is_public_copilot_api_host(+ _gate_view_host((api_base_url or "").strip())+ ) and not is_copilot_completions_host(api_base_url):return Truereturn not is_custom_anthropic_base_url(api_base_url)diff --git a/tests/test_tool_search_client_deferral.py b/tests/test_tool_search_client_deferral.pyindex ba3cfcc60..d8d7af98f 100644--- a/tests/test_tool_search_client_deferral.py+++ b/tests/test_tool_search_client_deferral.py@@ -411,21 +411,35 @@ def test_nothing_to_strip_returns_the_same_list() -> None:assert out is tools-def test_github_copilot_upstream_keeps_deferral() -> None:- """Copilot accepts the deferred shape and bills deferred schemas compactly."""+ "url",+ [+ "https://api.githubcopilot.com",+ "https://api.business.githubcopilot.com",+ "https://api.enterprise.githubcopilot.com",+ "https://api.individual.githubcopilot.com/v1",+ "https://api.business.githubcopilot.com:443",+ ],+)+def test_copilot_hosts_keep_deferral(url: str) -> None:+ """Copilot accepts the deferred shape and bills deferred schemas compactly,+ on the generic host and on the plan hosts seats are sent to (#4155)."""tools = [ANTHROPIC_SEARCH_REGEX, {"name": "mcp__srv__a", "defer_loading": True}]- out = strip_first_party_tool_search_tools_for_third_party_upstream(- tools, "https://api.githubcopilot.com"- )-- assert out is tools- assert anthropic_first_party_tool_search_supported("https://api.githubcopilot.com")+ assert strip_first_party_tool_search_tools_for_third_party_upstream(tools, url) is tools-def test_copilot_host_match_is_exact() -> None:- """A lookalike host must not inherit the exemption."""- assert not anthropic_first_party_tool_search_supported("https://api.githubcopilot.com.evil.com")+ "url",+ [+ "https://api.githubcopilot.com.evil.com",+ "https://api.business.githubcopilot.com.evil.com",+ "https://evilgithubcopilot.com",+ "https://copilot-api.acme.ghe.com",+ ],+)+def test_copilot_lookalike_hosts_still_downgrade(url: str) -> None:+ assert not anthropic_first_party_tool_search_supported(url)def test_other_third_party_upstreams_still_downgrade() -> None:@@ -727,3 +741,12 @@ def test_the_orphan_warning_is_throttled(caplog: pytest.LogCaptureFixture) -> Nolines = [r for r in caplog.records if "tool_search_deferred_orphan" in r.getMessage()]assert len(lines) == 1+++def test_copilot_completions_hosts_do_not_keep_deferral() -> None:+ tools = [ANTHROPIC_SEARCH_REGEX, {"name": "mcp__srv__a", "defer_loading": True}]++ out = strip_first_party_tool_search_tools_for_third_party_upstream(+ tools, "https://proxy.business.githubcopilot.com"+ )+ assert out is not tools