Skip to content

Fix: Custom providers behind Cloudflare WAF blocked by SDK User-Agent header - #24308

Closed
ryptotalent wants to merge 2 commits into
NousResearch:mainfrom
ryptotalent:fix/custom-provider-user-agent
Closed

Fix: Custom providers behind Cloudflare WAF blocked by SDK User-Agent header#24308
ryptotalent wants to merge 2 commits into
NousResearch:mainfrom
ryptotalent:fix/custom-provider-user-agent

Conversation

@ryptotalent

Copy link
Copy Markdown
Contributor

Problem

When using custom_providers with third-party API proxies (e.g. self-hosted relays, GLM proxies) behind Cloudflare or similar WAFs, all API requests fail with HTTP 403.

Root Cause

The Anthropic SDK sends User-Agent: Anthropic/Python X.Y.Z and the OpenAI SDK sends User-Agent: OpenAI/Python X.Y.Z. Cloudflare's bot detection rules block these patterns, returning 403 responses for all requests through custom providers.

Solution

Add optional user_agent field to custom_providers config that overrides the SDK's default User-Agent header:

custom_providers:
- name: my-proxy
  base_url: https://proxy.example.com
  api_key: sk-xxx
  user_agent: hermes-agent    # overrides SDK default User-Agent

When user_agent is set, it replaces the SDK's identifying User-Agent. When a custom/third-party endpoint is detected but no explicit user_agent is configured, the code defaults to hermes-agent/X.Y.Z as a safe fallback that passes Cloudflare checks.

Changes

  • agent/anthropic_adapter.pybuild_anthropic_client() accepts user_agent parameter; auto-sets hermes-agent/X.Y.Z for custom endpoints when no specialized UA (OAuth, kimi-coding) is already set
  • run_agent.py — resolves user_agent from custom_providers for both anthropic_messages and OpenAI-wire paths (__init__ + switch_model)
  • agent/auxiliary_client.py — resolves user_agent for custom endpoint auxiliary clients (codex_responses, anthropic_messages, OpenAI fallback)

Testing

  • Syntax validation: all 3 modified files pass ast.parse
  • No hardcoded secrets in diff
  • Backward compatible: user_agent is optional; existing configs work unchanged

Closes #24293

…are WAF

Add optional user_agent field to custom_providers config that overrides
the SDK's default User-Agent header (e.g. "Anthropic/Python 0.101.0")
which is often blocked by Cloudflare WAF bot-detection rules.

Changes:
- build_anthropic_client(): accept user_agent parameter, inject into
  default_headers; fallback to hermes-agent/X.Y.Z for third-party endpoints
- run_agent.py: resolve custom_providers user_agent for both
  anthropic_messages and OpenAI-wire paths (__init__ + switch_model)
- auxiliary_client.py: resolve user_agent for custom endpoint clients
  (codex_responses, anthropic_messages, OpenAI fallback paths)

Usage in config.yaml:

  custom_providers:
  - name: my-proxy
    base_url: https://proxy.example.com
    api_key: sk-xxx
    api_mode: anthropic_messages
    user_agent: hermes-agent   # overrides SDK default User-Agent

Closes NousResearch#24293
@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
@alt-glitch

Copy link
Copy Markdown
Collaborator

Competing implementation with #24306 (same configurable user_agent approach for custom providers behind Cloudflare WAF). Also see #24295 (simpler hardcoded override). All fix #24293.

@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! 🙏

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