Skip to content

The agent that grows with you

fix: widen fallback warning to the sibling custom-endpoint 256K path

kshitijk4poore078c8c6ef9b
diff --git a/agent/model_metadata.py b/agent/model_metadata.py
index cd73cfc8360..42d841009fa 100644
--- a/agent/model_metadata.py
+++ b/agent/model_metadata.py
@@ -273,11 +273,27 @@ CONTEXT_PROBE_TIERS = [
# Default context length when no detection method succeeds.
DEFAULT_FALLBACK_CONTEXT = CONTEXT_PROBE_TIERS[0]
-# (model, base_url) pairs that already emitted the step-9 fallback warning.
+# (model, base_url) pairs that already emitted the fallback warning.
# The fallback result itself is deliberately never cached, so without this
# the warning would repeat on every resolution for the same unknown model.
_FALLBACK_WARNED: set = set()
+
+def _warn_context_length_fallback(model: str, base_url: str) -> None:
+ """Warn (once per model+endpoint) that context detection failed and the
+ hard default is being used, so small-context models (8K, 32K) don't
+ silently get 256K and cause hard-to-debug API failures."""
+ key = (model, base_url or "")
+ if key in _FALLBACK_WARNED:
+ return
+ _FALLBACK_WARNED.add(key)
+ logger.warning(
+ "Could not determine context length for model %r (base_url=%s) "
+ "— falling back to %s tokens. Set model.context_length in "
+ "config.yaml to override.",
+ model, base_url or "default", f"{DEFAULT_FALLBACK_CONTEXT:,}",
+ )
+
# Minimum context length required to run Hermes Agent. Models with fewer
# tokens cannot maintain enough working memory for tool-calling workflows.
# Sessions, model switches, and cron jobs should reject models below this.
@@ -2604,6 +2620,9 @@ def get_model_context_length(
f"{length:,}", model, default_model,
)
return length
+ # Same silent-256K bug class as the step-9 fallback below —
+ # warn here too so custom/local endpoints aren't left invisible.
+ _warn_context_length_fallback(model, base_url)
return DEFAULT_FALLBACK_CONTEXT
# 4. Anthropic /v1/models API (only for regular API keys, not OAuth)
@@ -2776,21 +2795,10 @@ def get_model_context_length(
if default_model in model_lower:
return length
- # 9. Default fallback — log so small-context models (8K, 32K) don't
- # silently get 256K and cause hard-to-debug API failures.
- # Warn once per (model, base_url): the fallback result is deliberately
- # never cached (a wrong value must not freeze), so without dedup this
- # would fire on every resolution — e.g. once per gateway message via
- # the session-hygiene path.
- _warn_key = (model, base_url or "")
- if _warn_key not in _FALLBACK_WARNED:
- _FALLBACK_WARNED.add(_warn_key)
- logger.warning(
- "Could not determine context length for model %r (base_url=%s) "
- "— falling back to %s tokens. Set model.context_length in "
- "config.yaml to override.",
- model, base_url or "default", f"{DEFAULT_FALLBACK_CONTEXT:,}",
- )
+ # 9. Default fallback — warn (deduped per model+endpoint) so
+ # small-context models don't silently get 256K. See
+ # _warn_context_length_fallback for rationale.
+ _warn_context_length_fallback(model, base_url)
return DEFAULT_FALLBACK_CONTEXT
diff --git a/tests/agent/test_model_metadata.py b/tests/agent/test_model_metadata.py
index a63d79cfb77..ac8ed899cc3 100644
--- a/tests/agent/test_model_metadata.py
+++ b/tests/agent/test_model_metadata.py
@@ -1247,6 +1247,24 @@ class TestFallbackWarning:
]
assert len(fallback_warnings) == 1
+ def test_warning_emitted_on_custom_endpoint_fallback(self, caplog):
+ """The sibling step-3b fallback (custom/local endpoint, probes down,
+ no catalog match) is the same silent-256K bug class and must warn too."""
+ import logging
+
+ with self._patch_all_lookups(), \
+ patch("agent.model_metadata._query_local_context_length", return_value=None):
+ with caplog.at_level(logging.WARNING, logger="agent.model_metadata"):
+ result = get_model_context_length(
+ "totally-unknown-model-xyz",
+ base_url="http://192.168.1.50:8080/v1",
+ )
+
+ assert result == DEFAULT_FALLBACK_CONTEXT
+ warning_msgs = [r for r in caplog.records if r.levelno == logging.WARNING]
+ assert any("totally-unknown-model-xyz" in r.getMessage() for r in warning_msgs)
+ assert any("model.context_length" in r.getMessage() for r in warning_msgs)
+
def test_no_warning_when_cached(self, caplog):
"""No fallback warning when the context length is found in the cache."""
import logging