The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(xai): send Hermes-Agent User-Agent on chat/completions
Jaaneek5cffc5319406
diff --git a/agent/agent_init.py b/agent/agent_init.pyindex 22f617b7f1c..a99a0de8991 100644--- a/agent/agent_init.py+++ b/agent/agent_init.py@@ -1156,6 +1156,10 @@ def init_agent(elif base_url_host_matches(effective_base, "chatgpt.com"):from agent.auxiliary_client import _codex_cloudflare_headersclient_kwargs["default_headers"] = _codex_cloudflare_headers(api_key)+ elif base_url_host_matches(effective_base, "x.ai"):+ from tools.xai_http import hermes_xai_default_headers++ client_kwargs["default_headers"] = hermes_xai_default_headers()elif "default_headers" not in client_kwargs:# Fall back to profile.default_headers for providers that# declare custom headers (e.g. Kimi User-Agent on non-kimi.comdiff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.pyindex 46b40b2a58a..fa5bc6b80d7 100644--- a/agent/auxiliary_client.py+++ b/agent/auxiliary_client.py@@ -2794,7 +2794,13 @@ def _build_xai_oauth_aux_client(model: str) -> Tuple[Optional[Any], Optional[strreturn None, Noneapi_key, base_url = resolvedlogger.debug("Auxiliary client: xAI OAuth (%s via Responses API)", model)- real_client = _create_openai_client(api_key=api_key, base_url=base_url)+ from tools.xai_http import hermes_xai_default_headers++ real_client = _create_openai_client(+ api_key=api_key,+ base_url=base_url,+ default_headers=hermes_xai_default_headers(),+ )return CodexAuxiliaryClient(real_client, model), model@@ -4892,6 +4898,10 @@ def _to_async_client(sync_client, model: str, is_vision: bool = False):async_kwargs["default_headers"] = {"User-Agent": "claude-code/0.1.0"}elif base_url_host_matches(sync_base_url, "integrate.api.nvidia.com"):async_kwargs["default_headers"] = build_nvidia_nim_headers(sync_base_url)+ elif base_url_host_matches(sync_base_url, "x.ai"):+ from tools.xai_http import hermes_xai_default_headers++ async_kwargs["default_headers"] = hermes_xai_default_headers()else:# Fall back to profile.default_headers for providers that declare# client-level headers on their ProviderProfile (e.g. attribution@@ -5515,6 +5525,10 @@ def resolve_provider_client())elif base_url_host_matches(base_url, "integrate.api.nvidia.com"):headers.update(build_nvidia_nim_headers(base_url))+ elif base_url_host_matches(base_url, "x.ai"):+ from tools.xai_http import hermes_xai_default_headers++ headers.update(hermes_xai_default_headers())else:# Fall back to profile.default_headers for providers that declare# client-level attribution headers on their profile (e.g. GMIdiff --git a/plugins/model-providers/xai/__init__.py b/plugins/model-providers/xai/__init__.pyindex 8d73ae0199e..273890dde95 100644--- a/plugins/model-providers/xai/__init__.py+++ b/plugins/model-providers/xai/__init__.py@@ -1,5 +1,6 @@"""xAI (Grok) provider profile."""+from hermes_cli import __version__ as _HERMES_VERSIONfrom providers import register_providerfrom providers.base import ProviderProfile@@ -10,6 +11,11 @@ xai = ProviderProfile(env_vars=("XAI_API_KEY",),base_url="https://api.x.ai/v1",auth_type="api_key",+ # Attribution so xAI can identify Hermes chat/completions traffic.+ # Must match tools.xai_http.hermes_xai_user_agent() (Hermes-Agent/<ver>).+ # Host-based api.x.ai matching in run_agent / auxiliary_client also+ # covers provider=xai-oauth.+ default_headers={"User-Agent": f"Hermes-Agent/{_HERMES_VERSION}"},)register_provider(xai)diff --git a/run_agent.py b/run_agent.pyindex 71176691839..f4d501914fe 100644--- a/run_agent.py+++ b/run_agent.py@@ -5087,6 +5087,11 @@ class AIAgent:self._client_kwargs["default_headers"] = _codex_cloudflare_headers(self._client_kwargs.get("api_key", ""))+ elif base_url_host_matches(base_url, "x.ai"):+ # Cover both provider=xai and provider=xai-oauth (api.x.ai).+ from tools.xai_http import hermes_xai_default_headers++ self._client_kwargs["default_headers"] = hermes_xai_default_headers()else:# No URL-specific headers — check profile.default_headers before clearing._ph_headers = Nonediff --git a/tests/run_agent/test_provider_attribution_headers.py b/tests/run_agent/test_provider_attribution_headers.pyindex dec932e34e0..109f8bd4e9c 100644--- a/tests/run_agent/test_provider_attribution_headers.py+++ b/tests/run_agent/test_provider_attribution_headers.py@@ -159,6 +159,46 @@ def test_gmi_base_url_picks_up_profile_user_agent(mock_openai):assert headers["User-Agent"].startswith("HermesAgent/")+@patch("run_agent.OpenAI")+def test_xai_base_url_applies_hermes_user_agent(mock_openai):+ """Direct xAI chat must send Hermes-Agent/* instead of OpenAI/Python."""+ mock_openai.return_value = MagicMock()+ agent = AIAgent(+ api_key="test-key",+ base_url="https://api.x.ai/v1",+ model="grok-4",+ provider="xai",+ quiet_mode=True,+ skip_context_files=True,+ skip_memory=True,+ )++ agent._apply_client_headers_for_base_url("https://api.x.ai/v1")++ headers = agent._client_kwargs["default_headers"]+ assert headers["User-Agent"].startswith("Hermes-Agent/")+++@patch("run_agent.OpenAI")+def test_xai_oauth_base_url_applies_hermes_user_agent(mock_openai):+ """xai-oauth uses the same api.x.ai host and must get the Hermes UA."""+ mock_openai.return_value = MagicMock()+ agent = AIAgent(+ api_key="oauth-token",+ base_url="https://api.x.ai/v1",+ model="grok-4",+ provider="xai-oauth",+ quiet_mode=True,+ skip_context_files=True,+ skip_memory=True,+ )++ agent._apply_client_headers_for_base_url("https://api.x.ai/v1")++ headers = agent._client_kwargs["default_headers"]+ assert headers["User-Agent"].startswith("Hermes-Agent/")++@patch("run_agent.OpenAI")def test_unknown_base_url_clears_default_headers(mock_openai):mock_openai.return_value = MagicMock()diff --git a/tools/xai_http.py b/tools/xai_http.pyindex 6d5287e4d86..81f2e264ca0 100644--- a/tools/xai_http.py+++ b/tools/xai_http.py@@ -97,6 +97,16 @@ def hermes_xai_user_agent() -> str:return f"Hermes-Agent/{__version__}"+def hermes_xai_default_headers() -> Dict[str, str]:+ """Default headers for OpenAI-SDK and raw HTTP clients talking to xAI.++ Replaces the OpenAI Python SDK's identifying ``User-Agent: OpenAI/Python …``+ so chat/completions and Responses traffic is attributed as Hermes Agent,+ matching the direct HTTP integrations (search, TTS, STT, image, video).+ """+ return {"User-Agent": hermes_xai_user_agent()}++def _load_config_section(section_name: str) -> Dict[str, Any]:"""Return a top-level Hermes config section as a dict, or empty."""try: