fix(agent): reapply provider headers after model switch (#61099) - #61732
Merged
Conversation
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.
This was referenced Jul 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Provider-specific default headers now survive
/modelswitches. Salvages PR #61347 by @AlexFucuson9 (fixes #61099).Root cause:
switch_model()rebuilds_client_kwargsfrom 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
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