Skip to content

The agent that grows with you

fix(errors): auth refusals from a non-stock route name the contacted host

teknium17530f4038358
diff --git a/agent/error_classifier.py b/agent/error_classifier.py
index 771631a479b..29bb23bee01 100644
--- a/agent/error_classifier.py
+++ b/agent/error_classifier.py
@@ -807,10 +807,28 @@ def classify_api_error(
anonymous=is_anonymous_request(provider, api_key),
)
verdict = next((v for v in (stage(c) for stage in _STAGES) if v is not None), _V_UNKNOWN)
- base = {"status_code": status_code, "provider": provider, "model": model, "message": _extract_message(error, body)}
+ message = _extract_message(error, body)
+ if verdict["reason"] in (_R.auth, _R.auth_permanent):
+ # An auth refusal from a non-stock route names the host, so a credential posted to the
+ # wrong endpoint (a stale ``model.base_url`` after a provider switch, #113719) reads as
+ # such — not as a bad key.
+ host = _off_route_host(c)
+ if host:
+ message = f"{message} (endpoint: {host})"
+ base = {"status_code": status_code, "provider": provider, "model": model, "message": message}
return ClassifiedError(**{**base, **verdict})
+def _off_route_host(c: _Ctx) -> str:
+ """The contacted host when ``base_url`` is set and is not the provider's own endpoint; ``""`` otherwise."""
+ from hermes_cli.route_identity import provider_owns_route
+ from utils import base_url_hostname
+ host = base_url_hostname(c.base_url)
+ if not host or provider_owns_route(c.provider_slug, c.base_url) is True:
+ return ""
+ return host
+
+
# ── Status code handlers ────────────────────────────────────────────────
def _status_403(c: _Ctx) -> Verdict:
diff --git a/tests/agent/test_error_classifier.py b/tests/agent/test_error_classifier.py
index 963fab64c0d..c71cc07f3b3 100644
--- a/tests/agent/test_error_classifier.py
+++ b/tests/agent/test_error_classifier.py
@@ -1879,3 +1879,21 @@ class TestNousWelcomeTier:
result = classify_api_error(MockAPIError("forbidden", status_code=403, body={"message": "forbidden"}), provider="nous", api_key=make_jwt())
assert result.reason == FailoverReason.auth
assert "welcome_route" not in result.error_context
+
+
+class TestAuthErrorNamesOffRouteEndpoint:
+ """#113719: an auth refusal from a route that is not the provider's own endpoint names the host."""
+
+ _BODY = {"error": {"code": "api_key_not_supported", "message": "API keys are not supported by this endpoint."}}
+
+ def test_stale_base_url_names_contacted_host(self):
+ e = MockAPIError("Unauthorized", status_code=401, body=self._BODY)
+ result = classify_api_error(e, provider="anthropic", model="claude", base_url="https://chatgpt.com/backend-api/codex")
+ assert result.reason == FailoverReason.auth
+ assert result.message == "API keys are not supported by this endpoint. (endpoint: chatgpt.com)"
+
+ def test_stock_endpoint_and_no_base_url_keep_plain_message(self):
+ e = MockAPIError("Unauthorized", status_code=401, body=self._BODY)
+ for base_url in ("", "https://api.anthropic.com/v1"):
+ result = classify_api_error(e, provider="anthropic", model="claude", base_url=base_url)
+ assert result.message == "API keys are not supported by this endpoint.", base_url