Fix: Custom providers behind Cloudflare WAF blocked by SDK User-Agent header - #24308
Closed
ryptotalent wants to merge 2 commits into
Closed
Fix: Custom providers behind Cloudflare WAF blocked by SDK User-Agent header#24308ryptotalent wants to merge 2 commits into
ryptotalent wants to merge 2 commits into
Conversation
…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
Collaborator
Collaborator
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.
Problem
When using
custom_providerswith 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.Zand the OpenAI SDK sendsUser-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_agentfield tocustom_providersconfig that overrides the SDK's default User-Agent header:When
user_agentis set, it replaces the SDK's identifying User-Agent. When a custom/third-party endpoint is detected but no explicituser_agentis configured, the code defaults tohermes-agent/X.Y.Zas a safe fallback that passes Cloudflare checks.Changes
agent/anthropic_adapter.py—build_anthropic_client()acceptsuser_agentparameter; auto-setshermes-agent/X.Y.Zfor custom endpoints when no specialized UA (OAuth, kimi-coding) is already setrun_agent.py— resolvesuser_agentfrom custom_providers for bothanthropic_messagesand OpenAI-wire paths (__init__+switch_model)agent/auxiliary_client.py— resolvesuser_agentfor custom endpoint auxiliary clients (codex_responses, anthropic_messages, OpenAI fallback)Testing
ast.parseuser_agentis optional; existing configs work unchangedCloses #24293