Skip to content

The agent that grows with you

fix(agent): default context lookup for empty model IDs

whirmill4a6d3640b99b
diff --git a/agent/model_metadata.py b/agent/model_metadata.py
index c2da434467d..bd974af5e87 100644
--- a/agent/model_metadata.py
+++ b/agent/model_metadata.py
@@ -2624,6 +2624,19 @@ def get_model_context_length(
except ValueError:
base_url = ""
+ # An empty/blank model id can't be meaningfully resolved: every probe
+ # below would either miss or — worse — fuzzy-match an arbitrary catalog
+ # entry (the endpoint matcher's `model in key` check is vacuously true
+ # for ""), returning whatever context length that random entry has and
+ # persisting it under a junk "@<base_url>" cache key. Fall back to the
+ # default immediately instead.
+ if not str(model or "").strip():
+ logger.info(
+ "No model id provided for context length resolution — defaulting to %s tokens.",
+ f"{DEFAULT_FALLBACK_CONTEXT:,}",
+ )
+ return DEFAULT_FALLBACK_CONTEXT
+
# Normalise provider-prefixed model names (e.g. "local:model-name" →
# "model-name") so cache lookups and server queries use the bare ID that
# local servers actually know about. Ollama "model:tag" colons are preserved.
diff --git a/tests/agent/test_model_metadata.py b/tests/agent/test_model_metadata.py
index 8fe08933d02..cb73c608392 100644
--- a/tests/agent/test_model_metadata.py
+++ b/tests/agent/test_model_metadata.py
@@ -285,6 +285,10 @@ class TestDefaultContextLengths:
model, provider="kimi-coding", base_url=base_url
) == 1_048_576
+ def test_empty_model_uses_fallback_context(self):
+ assert get_model_context_length("") == DEFAULT_FALLBACK_CONTEXT
+ assert get_model_context_length(None) == DEFAULT_FALLBACK_CONTEXT # type: ignore[arg-type]
+
def test_xai_oauth_grok_build_uses_xai_models_dev_context(self):
"""xAI OAuth should share the xAI provider metadata path.