Skip to content

fix(agent): reapply provider headers after model switch (#61099) - #61347

Closed
AlexFucuson9 wants to merge 1 commit into
NousResearch:mainfrom
AlexFucuson9:fix/openrouter-headers-lost-on-model-switch
Closed

fix(agent): reapply provider headers after model switch (#61099)#61347
AlexFucuson9 wants to merge 1 commit into
NousResearch:mainfrom
AlexFucuson9:fix/openrouter-headers-lost-on-model-switch

Conversation

@AlexFucuson9

Copy link
Copy Markdown
Contributor

Summary

switch_model() in agent/agent_runtime_helpers.py rebuilds _client_kwargs from scratch (only api_key + base_url) but does not call _apply_client_headers_for_base_url(). This drops provider-specific HTTP headers like OpenRouter's HTTP-Referer and X-Title, causing subsequent requests to show "Unknown" in OpenRouter dashboard logs.

Fixes #61099

Root Cause

In switch_model() (line ~1903), _client_kwargs is rebuilt:

agent._client_kwargs = {
    "api_key": effective_key,
    "base_url": effective_base,
}

But _apply_client_headers_for_base_url(effective_base) is never called, so headers like:

{"HTTP-Referer": "https://hermes-agent.nousresearch.com", "X-Title": "Hermes Agent"}

are lost. The client is then created without attribution headers.

During initial agent setup (__init__), _apply_client_headers_for_base_url IS called. But after any /model switch, the headers are gone.

Fix

Add agent._apply_client_headers_for_base_url(effective_base) after rebuilding _client_kwargs and before _create_openai_client().

Impact

  • Affects all providers that use URL-based attribution (OpenRouter, NVIDIA NIM, Copilot, Kimi)
  • After a model switch, all subsequent requests lose provider-specific headers
  • OpenRouter dashboard shows "Unknown" instead of "Hermes Agent"

switch_model() rebuilds _client_kwargs from scratch (api_key + base_url)
but does not call _apply_client_headers_for_base_url(), so provider-
specific headers like OpenRouter HTTP-Referer and X-Title are lost.
Subsequent requests show "Unknown" in OpenRouter dashboard logs.

Call _apply_client_headers_for_base_url() after rebuilding _client_kwargs
and before creating the new client.

Fixes NousResearch#61099
@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint provider/openrouter OpenRouter aggregator P3 Low — cosmetic, nice to have labels Jul 9, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Salvaged and merged in #61732 (rebase-merge — your commit landed on main with full authorship). Root cause was exactly as you diagnosed: switch_model() was the one client-rebuild site that skipped _apply_client_headers_for_base_url(). We added 3 regression tests on top (OpenRouter attribution, Kimi User-Agent sentinel, stale-header clearing) — 2 of 3 fail on the unpatched code, confirming the bug live. Thanks @AlexFucuson9!

@teknium1 teknium1 closed this Jul 10, 2026
santhreal pushed a commit to santhreal/hermes-agent that referenced this pull request Jul 13, 2026
justemu pushed a commit to justemu/hermes-agent that referenced this pull request Jul 18, 2026
Gravezzz pushed a commit to Gravezzz/hermes-agent that referenced this pull request Jul 21, 2026
leewenjie pushed a commit to leewenjie/hermes-agent that referenced this pull request Aug 7, 2026
randlee pushed a commit to randlee/hermes-agent that referenced this pull request Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have provider/openrouter OpenRouter aggregator type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: OpenRouter logs show 'Unknown' App for Hermes Agent requests intermittently

3 participants