Skip to content

The agent that grows with you

fix(compressor): thread custom_providers into context-length resolution

dhruv kejriwal71516214c3d7
diff --git a/agent/agent_init.py b/agent/agent_init.py
index 5f04649c364..30157f735ff 100644
--- a/agent/agent_init.py
+++ b/agent/agent_init.py
@@ -1851,6 +1851,7 @@ def _build_context_engine(agent, _agent_cfg, cs, _custom_providers, _effective_c
proactive_prune_min_result_chars=cs.proactive_prune_min_chars,
proactive_prune_min_reclaim_tokens=cs.proactive_prune_min_reclaim,
min_tail_user_messages=cs.min_tail_users, tail_mode=cs.tail_mode,
+ custom_providers=_custom_providers,
)
_bind_session_state = getattr(agent.context_compressor, "bind_session_state", None)
if callable(_bind_session_state):
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 2f247c65c68..eb1a314cc1f 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -1745,6 +1745,7 @@ class ContextCompressor(SummaryDispatchMixin, MicroCompactionMixin, ContextEngin
self._resolved_context_length = get_model_context_length(
self.model, base_url=self.base_url, api_key=self.api_key,
config_context_length=self._config_context_length, provider=self.provider,
+ custom_providers=self.custom_providers,
)
# Raise-only small-context floor; must run after context_length resolves and before threshold_tokens derives.
self.threshold_percent = self._effective_threshold_percent(self._resolved_context_length, self._base_threshold_percent)
@@ -2264,10 +2265,14 @@ class ContextCompressor(SummaryDispatchMixin, MicroCompactionMixin, ContextEngin
model_thresholds: dict[str, float] | None = None, threshold_tokens_cap: Any = None,
proactive_prune_tokens: int = 0, proactive_prune_min_result_chars: int = 8000,
proactive_prune_min_reclaim_tokens: int = 4096, min_tail_user_messages: int = 1, tail_mode: str = "lean",
+ custom_providers: list | None = None,
):
self.model, self.base_url, self.api_key, self.provider, self.api_mode = model, base_url, api_key, provider, api_mode
# "lean" = small clamped tail + verbatim-user summary section; "legacy" = 0.20*window tail.
self.tail_mode = tail_mode if tail_mode in ("legacy", "lean") else "lean"
+ # Per-model context_length overrides live in custom_providers; without them the deferred
+ # resolution below falls back to the hardcoded family catalog (#83324).
+ self.custom_providers = list(custom_providers) if custom_providers else None
# Per-model overrides (longest substring match wins); floor applied on top.
self.model_thresholds = model_thresholds or {}
# Raw config value, before override/floor; fallback when switching to a model with no override.
diff --git a/tests/test_compressor_custom_providers.py b/tests/test_compressor_custom_providers.py
new file mode 100644
index 00000000000..9a04177b269
--- /dev/null
+++ b/tests/test_compressor_custom_providers.py
@@ -0,0 +1,62 @@
+"""Regression tests for custom_providers context-length threading (#83324)."""
+
+from agent.context_compressor import ContextCompressor
+from agent.model_metadata import get_model_context_length
+
+
+def test_custom_providers_override_wins_over_catalog(monkeypatch, tmp_path):
+ """A per-model context_length in custom_providers must beat the hardcoded
+ catalog default for the same model (the #15779 / #83324 gap)."""
+ providers = [
+ {
+ "name": "myproxy",
+ "base_url": "https://proxy.example.com/v1",
+ "api_key": "sk-test",
+ "models": [
+ {"model": "kimi-k2", "context_length": 262144},
+ ],
+ }
+ ]
+ ctx = get_model_context_length(
+ "kimi-k2",
+ base_url="https://proxy.example.com/v1",
+ api_key="sk-test",
+ provider="custom",
+ custom_providers=providers,
+ )
+ assert ctx == 262144
+
+
+def test_compressor_threads_custom_providers(tmp_path, monkeypatch):
+ """ContextCompressor must pass its custom_providers snapshot into
+ _resolve_context_length so per-model overrides apply."""
+ captured = {}
+
+ def fake_resolve(self):
+ from agent import model_metadata
+
+ captured["custom_providers"] = self.custom_providers
+ self._resolved_context_length = model_metadata.get_model_context_length(
+ self.model,
+ base_url=self.base_url,
+ api_key=self.api_key,
+ config_context_length=self._config_context_length,
+ provider=self.provider,
+ custom_providers=self.custom_providers,
+ )
+ return self._resolved_context_length
+
+ monkeypatch.setattr(ContextCompressor, "_resolve_context_length", fake_resolve)
+
+ providers = [{"name": "p1"}]
+ comp = ContextCompressor(
+ model="m",
+ base_url="https://x.example.com/v1",
+ api_key="k",
+ provider="custom",
+ custom_providers=providers,
+ )
+ # Caller mutates the list after construction — compressor view must not change.
+ providers.append({"name": "p2"})
+ comp.context_length
+ assert captured["custom_providers"] == [{"name": "p1"}]