Skip to content

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

Merged
teknium1 merged 3 commits into
mainfrom
hermes/hermes-d13abbb5
Jul 10, 2026
Merged

fix(agent): reapply provider headers after model switch (#61099)#61732
teknium1 merged 3 commits into
mainfrom
hermes/hermes-d13abbb5

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Summary

Provider-specific default headers now survive /model switches. Salvages PR #61347 by @AlexFucuson9 (fixes #61099).

Root cause: switch_model() rebuilds _client_kwargs from scratch (api_key + base_url + TLS + timeout) but never called _apply_client_headers_for_base_url() — the one client-rebuild site that skipped it (init and credential-swap rebuilds both call it). After any model switch, OpenRouter attribution headers (HTTP-Referer / X-Title) were dropped → logs show "Unknown"; switching TO Kimi mid-session shipped a client without the required User-Agent sentinel → 403s.

Changes

  • agent/agent_runtime_helpers.py: call _apply_client_headers_for_base_url(effective_base) after rebuilding _client_kwargs, before _create_openai_client() (contributor commit, +5).
  • tests/run_agent/test_switch_model_reapplies_headers.py: 3 regression tests — OpenRouter attribution present, Kimi sentinel present, stale headers cleared on switch to an unheadered provider (follow-up commit).

Validation

Result
New regression tests on this branch 3/3 pass
Same tests on unpatched main 2/3 fail (bug confirmed)
tests/run_agent/ -k switch 27/27 pass
header suites (extra_headers, codex cloudflare) pass

Note: this restores existing header behavior on a missed rebuild path — no new telemetry surface (headers already applied at init and credential swap).

Contributor authorship preserved via cherry-pick; merge with rebase.

Infographic

switch-model-header-reapply

AlexFucuson9 and others added 3 commits July 9, 2026 17:33
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 #61099
Three tests for the #61099 salvage: OpenRouter attribution headers
present after switching to openrouter.ai, Kimi User-Agent sentinel
present after switching to api.kimi.com, and stale headers cleared
when switching to a provider with no URL-specific headers.
2/3 fail on unpatched main (DID NOT ATTACH), confirming the bug.
@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint provider/openrouter OpenRouter aggregator labels Jul 10, 2026
@teknium1
teknium1 merged commit bd16395 into main Jul 10, 2026
31 checks passed
@teknium1
teknium1 deleted the hermes/hermes-d13abbb5 branch July 10, 2026 01:23
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