Fix Cloudflare 403s for openai-codex provider on server IPs#6391
Closed
admin28980 wants to merge 1 commit into
Closed
Fix Cloudflare 403s for openai-codex provider on server IPs#6391admin28980 wants to merge 1 commit into
admin28980 wants to merge 1 commit into
Conversation
Add ChatGPT-Account-Id and originator headers when using chatgpt.com backend-api endpoint. Matches official codex-rs CLI behavior to prevent Cloudflare JavaScript challenges on non-residential IPs (VPS, Mac Mini, always-on servers). Applied in AIAgent.__init__ and _update_base_url_headers to cover both initial setup and credential rotation paths.
saamuelng601-pixel
added a commit
to saamuelng601-pixel/hermes-agent
that referenced
this pull request
Apr 16, 2026
ChatGPT's Codex backend sits behind Cloudflare's managed challenge. When Hermes hits /responses or /chat/completions via the Python openai SDK, Cloudflare sees the default "OpenAI/Python X.X.X" User-Agent and the absence of ChatGPT-Account-Id, classifies the request as bot traffic, and returns an HTML JS-challenge page that the openai SDK cannot handle. Hermes reports this as "Non-retryable client error: <html>..." and falls back to the secondary provider even when the Codex credentials themselves are fresh and the account is under-quota. The official Rust codex_cli_rs CLI sends three headers that Cloudflare uses to verify the client: - User-Agent: codex_cli_rs/<version> - originator: codex_cli_rs - ChatGPT-Account-Id: <decoded from JWT> This change installs an httpx.Client.send / AsyncClient.send wrapper that inspects the outbound request, detects chatgpt.com hosts, and injects the three headers (decoding ChatGPT-Account-Id from the bearer JWT). This covers every OpenAI client Hermes creates — including auxiliary flows (_try_codex, resolve_provider_client, _to_async_client) that PR NousResearch#6391 doesn't reach. Tested locally: requests to /responses now return 200 (previously HTML 403). /chat/completions returns 404 (backend doesn't expose that route for Codex), which is the expected behavior — callers should use the Responses API adapter. Refs: NousResearch#6391 (upstream CF fix, scoped to main agent only), NousResearch#5169 (closed earlier attempt), NousResearch#10016 (browser-compat Codex login roadmap).
Contributor
|
Merged via PR #12664 — your commit Layered a follow-up fix on top to correct a few details vs upstream:
13 regression tests added. |
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.
Running Hermes with the
openai-codexprovider againstchatgpt.com/backend-api/codexfrom any non-residential IP (VPS, Mac Mini, etc.) hits Cloudflare 403 JavaScript challenges. Laptops on home networks work fine, which makes this annoying to diagnose.The issue is missing headers. The official codex-rs CLI sends
ChatGPT-Account-Id(extracted from the JWT) andoriginator: codex_cli_rs, which Cloudflare uses to verify the request is from an authorized client. Without them, the OpenAI SDK's defaultUser-Agent: OpenAI/Python ...looks like bot traffic and gets blocked.The fix adds three headers when
base_urlcontainschatgpt.com:ChatGPT-Account-Id(decoded from the access token),originator: hermes-agent, and a cleanUser-Agent. Applied in bothAIAgent.__init__and_update_base_url_headersto cover credential rotation. JWT decode is wrapped in try/except — if the token format changes, it skips the header rather than crashing.~30 lines across two locations in
run_agent.py. No new dependencies. Tested on a Hetzner VPS and a Mac Mini, both previously getting consistent 403s.