Skip to content

The agent that grows with you

fix(providers): bare `provider: custom` honours model.key_env; an unset key_env is logged instead of laundered into no-key-required

teknium177d87b625d08
diff --git a/agent/model_metadata.py b/agent/model_metadata.py
index 26419717ad7..f93ecb79647 100644
--- a/agent/model_metadata.py
+++ b/agent/model_metadata.py
@@ -1329,6 +1329,7 @@ _OUTPUT_CAP_SIGNALS = (
("in the output", "maximum context length"), ("requested", "output tokens"),
("should be",), ("less than or equal",), ("must be",), ("exceeds model", "maximum output tokens"),
("output limit",), ("maximum allowed number of output tokens",),
+ ("limited to",), # Scaleway: "max_completion_tokens is limited to 16384 for <model>" (#67453)
)
_INPUT_OVERFLOW_SIGNALS = (
"prompt is too long", "prompt too long", "input is too long", "input token",
diff --git a/hermes_cli/runtime_provider_backends.py b/hermes_cli/runtime_provider_backends.py
index 6f786ca3f20..df060606059 100644
--- a/hermes_cli/runtime_provider_backends.py
+++ b/hermes_cli/runtime_provider_backends.py
@@ -155,7 +155,11 @@ def _resolve_openrouter_runtime(
if is_openrouter_context:
candidates = [explicit_api_key, get_secret_str("OPENROUTER_API_KEY"), get_secret_str("OPENAI_API_KEY")]
else:
+ # ``model.api_key`` and ``model.key_env`` back a trusted config base_url only; the key_env
+ # rung is what a bare ``provider: custom`` block relies on (#67453).
+ from hermes_cli.runtime_provider_custom import _model_cfg_key_env_for
candidates = [explicit_api_key, (cfg_api_key if use_config_base_url else ""),
+ (_model_cfg_key_env_for(model_cfg, base_url) if use_config_base_url else ""),
*rp._host_gated_env_key_candidates(base_url, ollama=True)]
api_key = next((str(c or "").strip() for c in candidates if rp.has_usable_secret(c)), "")
source = "explicit" if (explicit_api_key or explicit_base_url) else "env/config"
diff --git a/hermes_cli/runtime_provider_custom.py b/hermes_cli/runtime_provider_custom.py
index 9b4219d7e29..8826d7c8305 100644
--- a/hermes_cli/runtime_provider_custom.py
+++ b/hermes_cli/runtime_provider_custom.py
@@ -38,6 +38,34 @@ def _clean(value: Any) -> str:
return str(value or "").strip()
+def _key_env_secret(entry: Dict[str, Any], label: str) -> str:
+ """The credential named by ``key_env`` / ``api_key_env`` on a config block, or "".
+
+ A declared variable that resolves to nothing is logged: every custom rung substitutes
+ ``no-key-required`` for an empty key (keyless local servers), so a misnamed or unexported
+ variable otherwise surfaces only as the provider's 401/403 (#67453). A block with no key_env at
+ all stays silent — that IS the keyless-server configuration.
+ """
+ key_env = _clean(entry.get("key_env") or entry.get("api_key_env"))
+ if not key_env:
+ return ""
+ value = get_secret_str(key_env, "").strip()
+ if not value:
+ logger.warning("%s: key_env %s is set but the variable is empty/unset — the request will carry the "
+ "placeholder no-key-required and the endpoint will reject it", label, key_env)
+ return value
+
+
+def _model_cfg_key_env_for(model_cfg: Dict[str, Any], base_url: str) -> str:
+ """``model.key_env`` for a bare ``provider: custom`` runtime, only when ``base_url`` IS the
+ configured ``model.base_url`` — the key was declared for that endpoint, never for a direct alias
+ or CUSTOM_BASE_URL pointing elsewhere."""
+ cfg_base_url = _clean(model_cfg.get("base_url")).rstrip("/")
+ if not cfg_base_url or cfg_base_url != _clean(base_url).rstrip("/"):
+ return ""
+ return _key_env_secret(model_cfg, "model")
+
+
def _entry_url(entry: Dict[str, Any]) -> str:
return entry.get("api") or entry.get("url") or entry.get("base_url") or ""
@@ -427,7 +455,9 @@ def _resolve_direct_alias_runtime(requested_provider: str, explicit_api_key: Opt
return pool_result
# OLLAMA_API_KEY gets its own gate here: without it a `model_aliases:` entry pointing at
# Ollama Cloud resolved no key at all.
- candidates = [(explicit_api_key or "").strip(), *rp._host_gated_env_key_candidates(base_url, ollama=True)]
+ # ``model.key_env`` only when this alias endpoint IS the configured model.base_url (#67453).
+ candidates = [(explicit_api_key or "").strip(), _model_cfg_key_env_for(rp._get_model_config(), base_url),
+ *rp._host_gated_env_key_candidates(base_url, ollama=True)]
api_key = next((c for c in candidates if rp.has_usable_secret(c)), "")
return _custom_runtime(rp, base_url, api_key, None, source="direct-alias", requested_provider=requested_provider)
@@ -488,7 +518,7 @@ def _resolve_named_custom_runtime(*, requested_provider: str, explicit_api_key:
candidates = [
explicit_key,
_clean(custom_provider.get("api_key", "")),
- get_secret_str(_clean(custom_provider.get("key_env", "")), "").strip(),
+ _key_env_secret(custom_provider, f"custom provider '{custom_provider.get('name', requested_provider)}'"),
*rp._host_gated_env_key_candidates(base_url, ollama=False),
]
api_key: Any = next((c for c in candidates if rp.has_usable_secret(c)), "")
diff --git a/tests/agent/test_output_cap_parsing.py b/tests/agent/test_output_cap_parsing.py
index 745e86a0caf..6953abd8a2f 100644
--- a/tests/agent/test_output_cap_parsing.py
+++ b/tests/agent/test_output_cap_parsing.py
@@ -208,3 +208,10 @@ class TestParseVllmTokenBasedOutputCap:
cap = available
assert real_input + cap <= window, f"did not converge: cap={cap}"
+
+
+def test_limited_to_phrasing_is_an_output_cap():
+ """#67453: Scaleway rejects an oversized budget with "max_completion_tokens is limited to N for
+ <model>" — an output cap (step the budget down), not a context overflow (do not compress)."""
+ assert is_output_cap_error("max_completion_tokens is limited to 16384 for glm-5.2")
+ assert not is_output_cap_error("prompt is too long: max_tokens limited to 100 given the input")
diff --git a/tests/hermes_cli/test_runtime_provider_resolution.py b/tests/hermes_cli/test_runtime_provider_resolution.py
index 744bd7fe3f0..ebb0703a77d 100644
--- a/tests/hermes_cli/test_runtime_provider_resolution.py
+++ b/tests/hermes_cli/test_runtime_provider_resolution.py
@@ -1971,3 +1971,38 @@ def test_removed_keyless_free_provider_points_at_its_replacements(name):
assert excinfo.value.code == "invalid_provider"
message = str(excinfo.value)
assert "opencode-zen" in message and "opencode-go" in message
+
+
+def test_bare_custom_resolves_model_key_env_for_configured_base_url(monkeypatch):
+ """#67453: ``model.provider: custom`` + ``model.base_url`` + ``model.key_env`` (the setup wizard's
+ bare-custom shape) must send the named variable's value, not the ``no-key-required`` placeholder.
+ A CUSTOM_BASE_URL pointing elsewhere is a different endpoint: the declared key stays home."""
+ monkeypatch.setattr(rp, "resolve_provider", lambda *a, **k: "custom")
+ monkeypatch.setattr(rp, "load_config", lambda: {"custom_providers": []})
+ monkeypatch.setattr(rp, "_get_model_config", lambda: {
+ "provider": "custom", "base_url": "https://api.example.test/v1", "key_env": "MY_LLM_API_KEY",
+ "default": "glm-5.2", "api_mode": "chat_completions"})
+ for var in ("CUSTOM_BASE_URL", "OPENROUTER_BASE_URL", "OPENAI_API_KEY", "OPENROUTER_API_KEY"):
+ monkeypatch.delenv(var, raising=False)
+ monkeypatch.setenv("MY_LLM_API_KEY", "scw-real-key-0123456789abcdef")
+
+ resolved = rp.resolve_runtime_provider(requested="custom")
+ assert (resolved["base_url"], resolved["api_key"]) == ("https://api.example.test/v1", "scw-real-key-0123456789abcdef")
+
+ monkeypatch.setenv("CUSTOM_BASE_URL", "https://other.example.test/v1")
+ assert rp.resolve_runtime_provider(requested="custom")["api_key"] == "no-key-required"
+
+
+def test_configured_key_env_resolving_empty_is_logged(monkeypatch, caplog):
+ """#67453: a declared ``key_env`` whose variable is unset used to be laundered silently into
+ ``no-key-required`` and surface only as the provider's 403; a keyless block stays silent."""
+ monkeypatch.setattr(rp, "resolve_provider", lambda *a, **k: "custom")
+ monkeypatch.setattr(rp, "load_config", lambda: {"custom_providers": [
+ {"name": "scw", "base_url": "https://api.example.test/v1", "key_env": "UNSET_LLM_KEY", "model": "m"},
+ {"name": "local", "base_url": "http://127.0.0.1:8080/v1", "model": "m"}]})
+ monkeypatch.delenv("UNSET_LLM_KEY", raising=False)
+ with caplog.at_level("WARNING", logger="hermes_cli.runtime_provider"):
+ assert rp.resolve_runtime_provider(requested="custom:scw")["api_key"] == "no-key-required"
+ assert rp.resolve_runtime_provider(requested="custom:local")["api_key"] == "no-key-required"
+ hits = [r for r in caplog.records if "UNSET_LLM_KEY" in r.getMessage()]
+ assert len(hits) == 1 and "scw" in hits[0].getMessage()