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(providers): share proxy_base_url and the project-attribution setup text (#4085)
Tejas Chopra0494edcc5e8e
diff --git a/headroom/providers/_setup_text.py b/headroom/providers/_setup_text.pynew file mode 100644index 000000000..52ac0f606--- /dev/null+++ b/headroom/providers/_setup_text.py@@ -0,0 +1,15 @@+"""Setup-text lines shared by the provider ``render_setup_lines`` helpers."""++from __future__ import annotations+++def project_attribution_lines(project: str | None) -> list[str]:+ """Setup-text lines telling the user which project the proxy URL attributes savings to."""+ if not project:+ return []+ return [+ "",+ f" Dashboard savings will be attributed to project '{project}'",+ " (the directory this command was run from). Re-run from another",+ " project directory to get that project's URL.",+ ]diff --git a/headroom/providers/antigravity/runtime.py b/headroom/providers/antigravity/runtime.pyindex 4da900bc6..ccc19bd8b 100644--- a/headroom/providers/antigravity/runtime.py+++ b/headroom/providers/antigravity/runtime.py@@ -4,6 +4,7 @@ from __future__ import annotationsfrom dataclasses import dataclass+from headroom.providers._setup_text import project_attribution_linesfrom headroom.providers.claude import proxy_base_url as claude_proxy_base_urlfrom headroom.providers.codex import proxy_base_url as codex_proxy_base_urlfrom headroom.proxy.project_context import with_project_prefix@@ -47,11 +48,4 @@ def render_setup_lines(port: int, project: str | None = None) -> list[str]:" Antigravity fetches the model list from GET /v1/models automatically,"," so there is nothing else to register.",]- if project:- lines += [- "",- f" Dashboard savings will be attributed to project '{project}'",- " (the directory this command was run from). Re-run from another",- " project directory to get that project's URL.",- ]- return lines+ return lines + project_attribution_lines(project)diff --git a/headroom/providers/cortex_code/install.py b/headroom/providers/cortex_code/install.pyindex c0da2ee51..093ef862d 100644--- a/headroom/providers/cortex_code/install.py+++ b/headroom/providers/cortex_code/install.py@@ -2,6 +2,8 @@from __future__ import annotations+from headroom.providers._setup_text import project_attribution_lines+from .runtime import build_launch_env, proxy_base_url@@ -20,11 +22,4 @@ def render_setup_lines(port: int, project: str | None = None) -> list[str]:" Set the following environment variable before launching cortex:",]lines += [f" {line}" for line in env_lines]- if project:- lines += [- "",- f" Dashboard savings will be attributed to project '{project}'",- " (the directory this command was run from). Re-run from another",- " project directory to get that project's URL.",- ]- return lines+ return lines + project_attribution_lines(project)diff --git a/headroom/providers/cortex_code/runtime.py b/headroom/providers/cortex_code/runtime.pyindex 110ec5af7..cbfc888a3 100644--- a/headroom/providers/cortex_code/runtime.py+++ b/headroom/providers/cortex_code/runtime.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport osfrom collections.abc import Mapping+from headroom.providers.codex import proxy_base_urlfrom headroom.proxy.project_context import with_project_prefixSNOWFLAKE_ACCOUNT_ENV = "SNOWFLAKE_ACCOUNT"@@ -30,11 +31,6 @@ def default_api_url(environ: Mapping[str, str] | None = None) -> str:return _FALLBACK_API_URL-def proxy_base_url(port: int) -> str:- """Return the local proxy base URL for OpenAI-compatible Cortex requests."""- return f"http://127.0.0.1:{port}/v1"--def build_launch_env(port: int,environ: Mapping[str, str] | None = None,diff --git a/headroom/providers/cursor/runtime.py b/headroom/providers/cursor/runtime.pyindex b67f36456..0f4f86b06 100644--- a/headroom/providers/cursor/runtime.py+++ b/headroom/providers/cursor/runtime.py@@ -4,6 +4,7 @@ from __future__ import annotationsfrom dataclasses import dataclass+from headroom.providers._setup_text import project_attribution_linesfrom headroom.providers.claude import proxy_base_url as claude_proxy_base_urlfrom headroom.providers.codex import proxy_base_url as codex_proxy_base_urlfrom headroom.proxy.project_context import with_project_prefix@@ -48,11 +49,4 @@ def render_setup_lines(port: int, project: str | None = None) -> list[str]:" Settings > Models > OpenAI API Key > Override OpenAI Base URL",f" Set to: {targets.openai_base_url}",]- if project:- lines += [- "",- f" Dashboard savings will be attributed to project '{project}'",- " (the directory this command was run from). Re-run from another",- " project directory to get that project's URL.",- ]- return lines+ return lines + project_attribution_lines(project)diff --git a/headroom/providers/grok/runtime.py b/headroom/providers/grok/runtime.pyindex ddf3d1acd..1867941c4 100644--- a/headroom/providers/grok/runtime.py+++ b/headroom/providers/grok/runtime.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport osfrom collections.abc import Mapping+from headroom.providers.codex import proxy_base_urlfrom headroom.proxy.project_context import with_project_prefixDEFAULT_API_URL = "https://api.x.ai"@@ -51,11 +52,6 @@ def is_grok_cli_request(headers: Mapping[str, str]) -> bool:return any(token.startswith(_GROK_UA_PREFIXES) for token in user_agent.lower().split())-def proxy_base_url(port: int) -> str:- """Return the local proxy base URL used by Grok CLI integrations."""- return f"http://127.0.0.1:{port}/v1"--def build_launch_env(port: int,environ: Mapping[str, str] | None = None,diff --git a/headroom/providers/grok_build/runtime.py b/headroom/providers/grok_build/runtime.pyindex 26ca11e4e..3fc8827ed 100644--- a/headroom/providers/grok_build/runtime.py+++ b/headroom/providers/grok_build/runtime.py@@ -4,15 +4,12 @@ from __future__ import annotationsfrom dataclasses import dataclass+from headroom.providers._setup_text import project_attribution_lines+from headroom.providers.codex import proxy_base_urlfrom headroom.providers.grok.runtime import DEFAULT_API_URLfrom headroom.proxy.project_context import with_project_prefix-def proxy_base_url(port: int) -> str:- """Return the local proxy base URL for OpenAI-compatible Grok traffic."""- return f"http://127.0.0.1:{port}/v1"--@dataclass(frozen=True)class GrokBuildProxyTarget:"""Resolved local proxy target shown in Grok Build setup instructions."""@@ -50,11 +47,4 @@ def render_setup_lines(port: int, project: str | None = None) -> list[str]:" Or switch models in an existing session:"," /model grok-build",]- if project:- lines += [- "",- f" Dashboard savings will be attributed to project '{project}'",- " (the directory this command was run from). Re-run from another",- " project directory to get that project's URL.",- ]- return lines+ return lines + project_attribution_lines(project)diff --git a/headroom/providers/opencode/runtime.py b/headroom/providers/opencode/runtime.pyindex 85511e38a..66c11eea7 100644--- a/headroom/providers/opencode/runtime.py+++ b/headroom/providers/opencode/runtime.py@@ -11,15 +11,10 @@ from pathlib import Pathfrom headroom._subprocess import runfrom headroom.mcp_registry.install import DEFAULT_PROXY_URL+from headroom.providers.codex import proxy_base_urlfrom .config import HEADROOM_OPENCODE_PLUGIN, headroom_provider_entry--def proxy_base_url(port: int) -> str:- """Return the local proxy base URL used by OpenCode integrations."""- return f"http://127.0.0.1:{port}/v1"--# OpenCode 2.x subcommands. Of these only the interactive ones (the root TUI,# ``run`` and ``mini``) get ``--standalone``; the rest are management commands._OPENCODE_SUBCOMMANDS = frozenset(