Skip to content

fix: add configurable User-Agent for custom providers - #24306

Closed
wait4xx wants to merge 1 commit into
NousResearch:mainfrom
wait4xx:fix/custom-provider-user-agent
Closed

fix: add configurable User-Agent for custom providers#24306
wait4xx wants to merge 1 commit into
NousResearch:mainfrom
wait4xx:fix/custom-provider-user-agent

Conversation

@wait4xx

@wait4xx wait4xx commented May 12, 2026

Copy link
Copy Markdown

Summary

Fixes #24293

Third-party API proxies behind Cloudflare WAFs block SDK User-Agent
strings, causing HTTP 403 errors. This PR adds an optional user_agent
field to custom_providers config that overrides the SDK's default
User-Agent.

Changes

File Change
agent/anthropic_adapter.py Add user_agent param to build_anthropic_client()
hermes_cli/runtime_provider.py Read user_agent from config (both providers: dict and custom_providers: list paths)
gateway/run.py Pass user_agent through runtime kwargs + fallback provider
run_agent.py Accept and propagate user_agent to all 8 build_anthropic_client() call sites

Config Usage

custom_providers:
- name: my-proxy
  base_url: https://proxy.example.com
  api_key: sk-xxx
  api_mode: anthropic_messages
  user_agent: my-custom-agent  # optional, defaults to 'hermes-agent'

Safety

  • Input validation: User-Agent truncated to 256 chars, CR/LF stripped (prevents HTTP header injection)
  • Type hints: str | None = None
  • Backward compatible: existing configs work without changes
  • Built-in providers (Anthropic, OpenRouter, Kimi, etc.) are unaffected

Test Plan

  • Verified with wiki proxy (https://ai.tvt.wiki) behind Cloudflare — requests now return 200 instead of 403
  • Verified anthropic_messages mode (Anthropic SDK path)
  • Verified chat_completions mode (OpenAI SDK path)
  • Verified gateway/Feishu path receives and uses the User-Agent
  • Verified default value 'hermes-agent' is used when user_agent is not configured

…dpoints

Third-party API proxies often sit behind Cloudflare or similar WAFs
that block SDK User-Agent strings (Anthropic/Python, OpenAI/Python),
causing HTTP 403 errors on every request.

Add optional  field to custom_providers config, defaulting
to 'hermes-agent' for third-party endpoints.

Changes across 4 files (37 lines):
- agent/anthropic_adapter.py: add user_agent param to
  build_anthropic_client(), with input validation (truncate 256 chars,
  strip CR/LF to prevent HTTP header injection)
- hermes_cli/runtime_provider.py: read user_agent from custom provider
  config in both new-style (providers: dict) and legacy
  (custom_providers: list) resolution paths
- gateway/run.py: pass user_agent through runtime kwargs pipeline
  including the fallback provider path
- run_agent.py: accept user_agent in AIAgent.__init__(), store as
  self._user_agent, and propagate to all 8 build_anthropic_client()
  call sites (init, OAuth refresh, credential pool rotation, rebuild,
  fallback activation, session recovery)

Config example:
  custom_providers:
  - name: my-proxy
    base_url: https://example.com
    api_mode: anthropic_messages
    user_agent: my-custom-agent  # optional, defaults to 'hermes-agent'
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint area/config Config system, migrations, profiles labels May 12, 2026
@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Closing as superseded by #41096, which adds model.default_headers support for custom OpenAI-compatible providers across both the main and auxiliary client paths (overriding the OpenAI SDK's User-Agent/X-Stainless-* headers that some gateways/WAFs reject — #40033). Thanks for the contribution! 🙏

@wait4xx
wait4xx deleted the fix/custom-provider-user-agent branch June 7, 2026 09:26
@wait4xx
wait4xx restored the fix/custom-provider-user-agent branch June 14, 2026 04:02
@wait4xx
wait4xx deleted the fix/custom-provider-user-agent branch July 25, 2026 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config Config system, migrations, profiles comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom providers behind Cloudflare WAF return 403 due to SDK User-Agent

3 participants