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(client): store metrics at HeadroomConfig.store_url (#4270)
Satya Bharadwaj47fd8cb3ba25
diff --git a/headroom/client.py b/headroom/client.pyindex 8ae403f10..4d5ed50da 100644--- a/headroom/client.py+++ b/headroom/client.py@@ -307,11 +307,11 @@ class HeadroomClient:# Use provided config or build from individual parametersif config is not None:self._config = config- # Override store_url and mode if explicitly provided in config+ # Override store_url and mode if explicitly provided in config.+ # The resolved URL is not written back: the caller may share this+ # config with other clients, which would then read it as explicit.if config.store_url:self._store_url = config.store_url- else:- self._config.store_url = store_urlself._default_mode = config.default_modeelse:# Build config from individual parameters@@ -325,7 +325,7 @@ class HeadroomClient:self._config.model_context_limits.update(model_context_limits)# Initialize storage- self._storage = create_storage(store_url)+ self._storage = create_storage(self._store_url)# Initialize transform pipelineself._pipeline = TransformPipeline(self._config, provider=self._provider)diff --git a/headroom/config.py b/headroom/config.pyindex 11a507e50..25d8e5b78 100644--- a/headroom/config.py+++ b/headroom/config.py@@ -923,7 +923,9 @@ class PrefixFreezeConfig:class HeadroomConfig:"""Main configuration for HeadroomClient."""- store_url: str = "sqlite:///headroom.db"+ # None defers to HeadroomClient: its ``store_url`` argument, else a+ # headroom.db in the system temp directory.+ store_url: str | None = Nonedefault_mode: HeadroomMode = HeadroomMode.AUDITmodel_context_limits: dict[str, int] = field(default_factory=lambda: DEFAULT_MODEL_CONTEXT_LIMITS.copy()diff --git a/tests/test_client_store_url.py b/tests/test_client_store_url.pynew file mode 100644index 000000000..eea978dd1--- /dev/null+++ b/tests/test_client_store_url.py@@ -0,0 +1,104 @@+"""``HeadroomConfig.store_url`` must decide where ``HeadroomClient`` stores metrics.++The client copied ``config.store_url`` into ``self._store_url`` (which+``validate_setup()`` reports) but opened storage with its own ``store_url``+argument, so metrics went to the temp-directory default while the client+reported the configured path.+"""++from __future__ import annotations++import tempfile+from pathlib import Path+from unittest.mock import MagicMock++import pytest++from headroom.client import HeadroomClient+from headroom.config import HeadroomConfig+from headroom.providers.openai import OpenAIProvider+++def temp_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:+ """Point the client's temp-directory default at an isolated folder."""+ temp = tmp_path / "system-temp"+ temp.mkdir()+ monkeypatch.setattr(tempfile, "tempdir", str(temp))+ return temp+++def _client(**kwargs: object) -> HeadroomClient:+ return HeadroomClient(+ original_client=MagicMock(),+ provider=OpenAIProvider(),+ enable_cache_optimizer=False,+ **kwargs,+ )+++def _storage_path(client: HeadroomClient) -> Path:+ # "sqlite:////abs/path" parses to "//abs/path"; resolve() normalizes it.+ return Path(client._storage.db_path).resolve()+++def test_config_store_url_is_where_metrics_are_stored(tmp_path: Path, temp_dir: Path) -> None:+ db = tmp_path / "mine.db"+ client = _client(config=HeadroomConfig(store_url=f"sqlite:///{db}"))++ assert _storage_path(client) == db.resolve()+ assert db.exists()+ assert not (temp_dir / "headroom.db").exists()+ assert client.validate_setup()["storage"]["url"] == f"sqlite:///{db}"+++def test_config_store_url_takes_precedence_over_the_argument(+ tmp_path: Path, temp_dir: Path+) -> None:+ from_config = tmp_path / "from-config.db"+ from_argument = tmp_path / "from-argument.db"+ client = _client(+ store_url=f"sqlite:///{from_argument}",+ config=HeadroomConfig(store_url=f"sqlite:///{from_config}"),+ )++ assert _storage_path(client) == from_config.resolve()+ assert not from_argument.exists()+++def test_argument_is_used_when_config_has_no_store_url(tmp_path: Path, temp_dir: Path) -> None:+ from_argument = tmp_path / "from-argument.db"+ config = HeadroomConfig()+ client = _client(store_url=f"sqlite:///{from_argument}", config=config)++ assert _storage_path(client) == from_argument.resolve()+ assert config.store_url is None+++def test_shared_config_does_not_leak_one_clients_url_into_another(+ tmp_path: Path, temp_dir: Path+) -> None:+ """One config object reused for two clients, each with its own database."""+ shared = HeadroomConfig()+ a_db = tmp_path / "a.db"+ b_db = tmp_path / "b.db"++ client_a = _client(store_url=f"sqlite:///{a_db}", config=shared)+ client_b = _client(store_url=f"sqlite:///{b_db}", config=shared)++ assert _storage_path(client_a) == a_db.resolve()+ assert _storage_path(client_b) == b_db.resolve()+ assert shared.store_url is None+++def test_default_config_stores_in_temp_dir_not_cwd(+ tmp_path: Path, temp_dir: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ cwd = tmp_path / "cwd"+ cwd.mkdir()+ monkeypatch.chdir(cwd)++ client = _client(config=HeadroomConfig())++ assert _storage_path(client) == (temp_dir / "headroom.db").resolve()+ assert not (cwd / "headroom.db").exists()diff --git a/tests/test_config.py b/tests/test_config.pyindex 65d0835c0..5d413681d 100644--- a/tests/test_config.py+++ b/tests/test_config.py@@ -151,7 +151,7 @@ class TestHeadroomConfig:def test_default_values(self):"""Default values are correctly set."""config = HeadroomConfig()- assert config.store_url == "sqlite:///headroom.db"+ assert config.store_url is Noneassert config.default_mode == HeadroomMode.AUDITassert config.generate_diff_artifact is False# Nested configs exist