Skip to content

The agent that grows with you

fix: keep API-key xai auto routes off the xai-oauth refresh row

teknium12ba6fcd750ce
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index dfbe139c4c2..b98ea49d278 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -3663,13 +3663,20 @@ def _refresh_provider_credentials(provider: str, *, failed_api_key: str = "") ->
return False
-def _auth_refresh_provider_for_route(resolved_provider: Optional[str], client_base_url: str) -> str:
+def _auth_refresh_provider_for_route(
+ resolved_provider: Optional[str], client_base_url: str, effective_provider: str = "",
+) -> str:
"""Provider whose short-lived credentials should be refreshed; auto-routed calls keep
``resolved_provider == "auto"``, so infer the backend from the client's base URL."""
normalized = _normalize_aux_provider(resolved_provider)
if normalized and normalized != "auto":
return normalized
- return _provider_for_host(client_base_url, _AUTH_REFRESH_PROVIDER_BY_HOST) or normalized
+ host_provider = _provider_for_host(client_base_url, _AUTH_REFRESH_PROVIDER_BY_HOST)
+ # The auto client already knows it runs on the host's API-key sibling (``xai`` on api.x.ai):
+ # an XAI_API_KEY 401 must not spend a stale ``xai-oauth`` grant's refresh and switch routes.
+ if host_provider and host_provider == f"{_normalize_aux_provider(effective_provider)}-oauth":
+ return normalized
+ return host_provider or normalized
def _fallback_chain_entry(task: Optional[str], fb_label: str) -> Optional[Dict[str, Any]]:
@@ -7189,7 +7196,8 @@ def _ladder_credential_rungs(
"""OAuth credential refresh + same-provider retry, then credential-pool rotation.
Returns ``(response, None)`` or ``(None, first_err)`` to fall through."""
client, task, tag, resolved_provider = route.client, route.task, route.tag, route.resolved_provider
- auth_refresh_provider = _auth_refresh_provider_for_route(resolved_provider, route.base_info)
+ auth_refresh_provider = _auth_refresh_provider_for_route(
+ resolved_provider, route.base_info, _effective_provider_for_client(client, ""))
if (_is_auth_error(first_err) and auth_refresh_provider not in {"auto", "", None}
and not client_is_nous):
refresh_kwargs = ({"failed_api_key": getattr(client, "api_key", "")}
diff --git a/tests/agent/test_auxiliary_auto_route_xai_oauth_refresh.py b/tests/agent/test_auxiliary_auto_route_xai_oauth_refresh.py
index 39cc6b96d2a..7e496ad1654 100644
--- a/tests/agent/test_auxiliary_auto_route_xai_oauth_refresh.py
+++ b/tests/agent/test_auxiliary_auto_route_xai_oauth_refresh.py
@@ -3,13 +3,21 @@
A plugin/hook ``call_llm`` with no provider override inherits the main ``xai-oauth`` route as
``resolved_provider == "auto"``; the refresh rung must still resolve the concrete backend from the
client's host so the expired bearer is refreshed and retried instead of benching the only grant.
+An auto client tagged with the API-key ``xai`` backend must NOT borrow that row: an XAI_API_KEY 401
+would otherwise spend a stale ``xai-oauth`` grant's refresh rotation and silently switch routes.
"""
+import pytest
+
from agent.auxiliary_client import _auth_refresh_provider_for_route
-def test_auto_route_on_xai_host_refreshes_xai_oauth():
- assert _auth_refresh_provider_for_route("auto", "https://api.x.ai/v1") == "xai-oauth"
+ ("effective_provider", "expected"),
+ [("", "xai-oauth"), ("auto", "xai-oauth"), ("xai-oauth", "xai-oauth"), ("xai", "auto")],
+)
+def test_auto_route_on_xai_host_refreshes_xai_oauth_unless_api_key_backend(effective_provider, expected):
+ assert _auth_refresh_provider_for_route("auto", "https://api.x.ai/v1", effective_provider) == expected
def test_unknown_host_on_auto_route_stays_auto():