Skip to content

fix(auxiliary): forward explicit_api_key to _try_openrouter for credential pool fallback#18618

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/openrouter-explicit-api-key
Closed

fix(auxiliary): forward explicit_api_key to _try_openrouter for credential pool fallback#18618
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/openrouter-explicit-api-key

Conversation

@liuhao1024
Copy link
Copy Markdown
Contributor

Summary

resolve_provider_client() receives explicit_api_key from the credential pool and correctly propagates it to most downstream provider calls. However, when routing to OpenRouter, it calls _try_openrouter() without passing explicit_api_key, silently ignoring it.

This causes auth failures when the auxiliary client falls back to OpenRouter using a runtime API key from the credential pool — the key is available but never reaches the OpenRouter client constructor.

Root Cause

In agent/auxiliary_client.py, the OpenRouter branch of resolve_provider_client() (line ~2058):

if provider == "openrouter":
    client, default = _try_openrouter()  # explicit_api_key not passed

_try_openrouter() only checked the credential pool and OPENROUTER_API_KEY env var — it never accepted an explicit key parameter.

Fix

  1. Added explicit_api_key parameter to _try_openrouter(). When provided, it's used directly (bypassing pool/env lookup).
  2. resolve_provider_client() now passes explicit_api_key to _try_openrouter().

Test Plan

  • New regression test test_explicit_api_key_used_for_openrouter — RED on main, GREEN with fix
  • All existing test_auxiliary_client.py tests pass (107 passed, 5 pre-existing failures unrelated to this change)
  • No new test regressions in tests/agent/ (2290 passed vs 2289 on main — +1 is the new test)

Closes #18338

…ntial pool fallback

resolve_provider_client() receives explicit_api_key from the credential
pool but _try_openrouter() never accepted or used it, causing OpenRouter
fallback to silently ignore runtime pool keys and fall through to
OPENROUTER_API_KEY (often empty).

Now _try_openrouter() accepts an optional explicit_api_key parameter and
uses it directly when provided, bypassing the pool/env lookup.

Closes NousResearch#18338
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder provider/openrouter OpenRouter aggregator labels May 2, 2026
@teknium1
Copy link
Copy Markdown
Contributor

teknium1 commented May 2, 2026

Closing in favour of the narrower fix in #18768 (salvage of your earlier #18341).

#18341's approach — threading explicit_api_key through the existing pool-first, env-fallback flow as explicit_api_key or ... — is cleaner than the short-circuit branch in this PR and has equivalent test coverage. Thanks for both attempts at the fix!

#18768

@teknium1 teknium1 closed this May 2, 2026
Cyrene963 pushed a commit to Cyrene963/hermes-agent that referenced this pull request May 3, 2026
Community PRs applied:
- NousResearch#18596: Enable secret redaction by default (SECURITY)
- NousResearch#18650: Sanitize malformed tool messages + auto-recover on API 400
- NousResearch#18607: Emergency compression before max_iterations exhaustion
- NousResearch#18603: Compression fallback to main model on 413 rate limit
- NousResearch#18638: Pass threshold_percent on model switch
- NousResearch#18663: Strip extra_content from tool_calls for strict APIs
- NousResearch#18618: Forward explicit_api_key to OpenRouter
- NousResearch#18632: Show cache tokens in /insights breakdown
- NousResearch#18614: Add idempotency guard for patch duplicate loops
- NousResearch#18600: Raise ValueError when HERMES_HOME unset in profile mode
- NousResearch#18616: Allow ZWJ emoji in context files
- NousResearch#18582: Reload .env on /restart
- NousResearch#18547: Stabilize system prompt prefix for KV cache reuse
- NousResearch#18692: Strip FTS5 operators from session search truncation terms

Fix: Add order_by_last_active=True to list_sessions_rich call
(pre-existing commit 142b4bf code sync)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder P2 Medium — degraded but workaround exists provider/openrouter OpenRouter aggregator type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: resolve_provider_client ignores explicit_api_key when calling _try_openrouter()

3 participants