From 4b45f65858321483567b978fe57a4734d580e6b6 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 25 Mar 2026 18:23:03 -0700 Subject: [PATCH] fix: update api_key in _try_activate_fallback for subagent auth (#3103) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When fallback activates (e.g. minimax → OpenRouter), self.provider, self.base_url, self.api_mode, and self._client_kwargs were all updated but self.api_key was not. delegate_tool.py reads parent_agent.api_key to pass credentials to child agents, so subagents inherited the stale pre-fallback key (e.g. a minimax key sent to OpenRouter), causing 401 Missing Authentication errors. Add self.api_key = ... in both the anthropic_messages and chat_completions branches of _try_activate_fallback(). --- run_agent.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/run_agent.py b/run_agent.py index a0e3f2cec..0d98b7954 100644 --- a/run_agent.py +++ b/run_agent.py @@ -4018,6 +4018,7 @@ class AIAgent: # Build native Anthropic client instead of using OpenAI client from agent.anthropic_adapter import build_anthropic_client, resolve_anthropic_token, _is_oauth_token effective_key = (fb_client.api_key or resolve_anthropic_token() or "") if fb_provider == "anthropic" else (fb_client.api_key or "") + self.api_key = effective_key self._anthropic_api_key = effective_key self._anthropic_base_url = getattr(fb_client, "base_url", None) self._anthropic_client = build_anthropic_client(effective_key, self._anthropic_base_url) @@ -4026,6 +4027,7 @@ class AIAgent: self._client_kwargs = {} else: # Swap OpenAI client and config in-place + self.api_key = fb_client.api_key self.client = fb_client self._client_kwargs = { "api_key": fb_client.api_key,