Fix Claude Code and Codex CLI custom Base URL errors
I run qcode.cc, an independent API service (not official Anthropic, OpenAI or Google). The examples use QCode paths; swap the host for any compatible gateway.
A short table also lives on the Base URL guide. This page is the longer checklist: exact snippets, official variable names, and the order to test in. I run qcode.cc, an independent API service; examples use QCode paths you can swap for any compatible gateway.
Sources: Claude Code environment variables, Codex advanced configuration, Codex configuration reference, and QCode endpoints and API paths (for the worked example only).
1. Test the endpoint before blaming the client
Run these in the same terminal you will launch the tool from, so the key is actually set. Or use llm-endpoint-check, which reports status, content type, redirects and time to first byte in one pass.
# Replace the host with your gateway. A useful result is JSON (401/400/200), not HTML.
KEY="YOUR_API_KEY"
# Anthropic path (what Claude Code calls). Base URL should NOT include /v1.
curl -sS -D- -o /tmp/cc-body -X POST "$ANTHROPIC_BASE_URL/v1/messages" \
-H "Authorization: Bearer $KEY" -H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model":"MODEL_ID","max_tokens":16,"messages":[{"role":"user","content":"ping"}]}'
head -n 20 /tmp/cc-body
# OpenAI Chat Completions (SDKs, Cursor, Cline OpenAI-compatible). Base URL usually includes /v1.
curl -sS "$OPENAI_BASE_URL/models" -H "Authorization: Bearer $KEY"
# Codex CLI Responses API. Codex posts to {base_url}/responses.
curl -sS -o /dev/null -w '%{http_code} %{content_type}\n' -X POST "$CODEX_BASE_URL/responses" \
-H "Authorization: Bearer $KEY" -H "content-type: application/json" \
-d '{"model":"MODEL_ID","input":"ping"}'
How to read the result:
- JSON plus
401/400usually means the path is reachable (missing or invalid key, or missing body). - JSON plus
200onGET /modelsor a small completion means the path and key work. - HTML with HTTP 200 means you hit a landing page, not an API route — or the key was not sent.
- A
301/302is a problem: many clients drop the POST body on a redirect. Use the finalhttpsURL, with no redirect. 404is almost always the prefix (missing, doubled/v1, or a trailing slash).
2. 404 and /v1/v1/ — wrong path prefix
Every client appends its own path. The Base URL is the prefix only.
| Client | Setting | Client appends | Typical Base URL |
|---|---|---|---|
| Claude Code | ANTHROPIC_BASE_URL | /v1/messages | https://gateway.example.com (no /v1) |
| Codex CLI | base_url in [model_providers.<id>] | /responses | https://gateway.example.com/v1 |
| OpenAI SDKs, Cursor, Cline (OpenAI Compatible) | base_url / Base URL | /chat/completions, /models | usually https://gateway.example.com/v1 |
Your gateway's docs win over the typical column. QCode, for example, uses https://api.qcode.cc/api for Claude Code (the client then calls /api/v1/messages) and https://api.qcode.cc/openai for Codex (which calls /openai/responses; QCode treats that as equivalent to /openai/v1/responses).
If the error URL contains /v1/v1/, remove /v1 from the Base URL. Do not add a trailing slash — that can produce //v1/messages.
3. 401 and invalid x-api-key — the wrong header or an unset key
From the official Claude Code list:
ANTHROPIC_AUTH_TOKEN— value is sent asAuthorization: Bearer <value>. This is what most relays expect.ANTHROPIC_API_KEY— sent asX-Api-Key. When set, Claude Code uses this key instead of a Claude Pro / Max / Team / Enterprise subscription (in-pmode always; in interactive mode after you approve the key once). To go back to the subscription,unset ANTHROPIC_API_KEY.
Set only one of those two unless the gateway documents both. Check for quotes, spaces or a truncated key. Confirm with echo "$ANTHROPIC_AUTH_TOKEN" in the same shell. A settings-file env value replaces the shell value in most sessions (see environment variables).
export ANTHROPIC_BASE_URL="https://gateway.example.com" # no /v1, no trailing slash
export ANTHROPIC_AUTH_TOKEN="YOUR_API_KEY" # Authorization: Bearer …
# If the gateway wants X-Api-Key instead, unset AUTH_TOKEN and use:
# export ANTHROPIC_API_KEY="YOUR_API_KEY"
# optional:
# export ANTHROPIC_MODEL="claude-opus-5-5"
# export API_TIMEOUT_MS="1200000"
Or ~/.claude/settings.json, which applies however claude is launched (terminal, VS Code, JetBrains):
{
"env": {
"ANTHROPIC_BASE_URL": "https://gateway.example.com",
"ANTHROPIC_AUTH_TOKEN": "YOUR_API_KEY",
"API_TIMEOUT_MS": "1200000"
}
}
4. model not found / model not available on this endpoint
The ID is wrong, retired, or not served on that protocol. List models with GET /models (or the gateway's model page) and use an ID from that list, exactly.
On QCode this is a protocol mismatch, not a bad key: Claude models are served only on the Anthropic path, GPT models only on the OpenAI path, Gemini models only on the Gemini path. A claude-* ID sent to /openai/v1/chat/completions returns model_not_available_on_endpoint even with an invalid key — so do not treat that error as an auth failure. Details: endpoints and API paths. Live IDs: qcode.cc/en/models.
For a gateway-specific Claude ID that is not a built-in alias, official Claude Code docs provide ANTHROPIC_CUSTOM_MODEL_OPTION so the ID appears in the /model picker.
5. Codex CLI: the config.toml provider block
Official docs: define a custom provider under [model_providers.<id>] and point model_provider at that id. Reserved IDs you must not use: openai, ollama, lmstudio, amazon-bedrock, amazon-bedrock-runtime, and gcp.
# ~/.codex/config.toml (user-level — not a project .codex/config.toml)
model_provider = "mygateway"
model = "MODEL_ID"
[model_providers.mygateway]
name = "mygateway"
base_url = "https://gateway.example.com/v1"
wire_api = "responses"
env_key = "MYGATEWAY_API_KEY"
request_max_retries = 4
stream_max_retries = 5
stream_idle_timeout_ms = 300000
Then export MYGATEWAY_API_KEY="YOUR_API_KEY" and run codex.
wire_api— official reference: the only supported value isresponses, and that is the default if omitted. Current Codex postsPOST {base_url}/responses. Chat-Completions-only gateways will not work. If an old file still haswire_api = "chat", change it.- Auth — use
env_key, orrequires_openai_auth = trueplus~/.codex/auth.jsoncontaining{"OPENAI_API_KEY": "YOUR_API_KEY"}. Official docs also warn not to combine command-backed[model_providers.<id>.auth]withenv_key,experimental_bearer_token, orrequires_openai_auth. - Where the file lives — user-level config is
~/.codex/config.toml. Official reference: a project-local.codex/config.tomlcannot overridemodel_provider,model_providers, oropenai_base_url. Put the provider block in the user-level file.
QCode worked example (one method only — here requires_openai_auth + auth.json):
# ~/.codex/config.toml
model_provider = "qcode"
model = "gpt-6.2-sol"
[model_providers.qcode]
name = "qcode"
base_url = "https://api.qcode.cc/openai"
wire_api = "responses"
requires_openai_auth = true
# ~/.codex/auth.json
# { "OPENAI_API_KEY": "cr_your_key" }
More: Codex CLI page.
6. Streaming stalls and timeouts
If the first token never arrives, or the stream dies after a long pause, raise the documented idle and header timeouts rather than inventing client flags.
# Claude Code — official names from the env-vars docs
export API_TIMEOUT_MS="1200000" # time to receive response headers (default 600000)
export API_FORCE_IDLE_TIMEOUT="0" # turn off the 5-minute body-idle abort on a slow stream
# optional finer control (byte-level watchdog, 10s–30min, Claude Code v2.1.210+)
# export CLAUDE_BYTE_STREAM_IDLE_TIMEOUT_MS="600000"
# Codex CLI — official [model_providers] keys
# stream_idle_timeout_ms = 600000
# stream_max_retries = 8
# request_max_retries = 6
API_TIMEOUT_MS(Claude Code): milliseconds allowed for each attempt to receive response headers. It does not cap how long a stream may run after that. Official default 600000 (10 minutes). Increase it on a slow network or proxy.0removes this limit (Claude Code v2.1.305+); a non-streaming local attempt still times out after 300 seconds.API_FORCE_IDLE_TIMEOUT: official docs describe a 5-minute body-idle abort that is active on providers other than the direct Anthropic API (and some AWS paths). Set to0to turn that abort off when a slow gateway pauses longer than five minutes between chunks. Stream watchdogs can still abort a long silent pause;CLAUDE_BYTE_STREAM_IDLE_TIMEOUT_MS(v2.1.210+, clamped between 10 seconds and 30 minutes) adjusts the byte-level watchdog. Official docs also mentionCLAUDE_STREAM_IDLE_TIMEOUT_MSas the event-level counterpart.- Codex official defaults:
request_max_retries4,stream_max_retries5,stream_idle_timeout_ms300000 (5 minutes). Raisestream_idle_timeout_msif SSE goes idle on a slow path.
If every request hangs, try another region hostname of the same gateway, or check a corporate proxy (next section). 429 is a rate limit; 402 is usually quota — wait, lower concurrency, or top up. Neither is a path bug.
7. Corporate proxy (HTTPS_PROXY)
Claude Code's official env-vars list documents HTTPS_PROXY / https_proxy in the description of CLAUDE_CODE_DISABLE_PROXY_SCHEME_REPAIR: Claude Code reads that variable, and by default it can repair a bare host:port value that is missing a scheme. Set a full URL if your network requires a proxy:
export HTTPS_PROXY="http://proxy.example.com:8080"\n# optional, if the repair of a bare host:port is in the way:\n# export CLAUDE_CODE_DISABLE_PROXY_SCHEME_REPAIR=1
If the gateway itself rejects requests because of pre-release anthropic-beta headers or unexpected body fields, official docs offer CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 to strip those. If a mid-stream failure plus a non-streaming fallback causes duplicate tool runs through a proxy, CLAUDE_CODE_DISABLE_NONSTREAMING_FALLBACK=1 turns that fallback off.
8. Claude Code still uses the subscription, or settings "do nothing"
- Official precedence: when the same variable is set in the shell and in a settings file
envblock, the settings file value applies in most sessions. If an export "does nothing", look in~/.claude/settings.json,.claude/settings.json, and.claude/settings.local.json. - On Windows, User-level environment variables apply only after you open a new terminal.
- A running session may keep a startup value for some features; relaunch
claudeafter a change. - As of Claude Code v2.1.196, Remote Control is disabled when
ANTHROPIC_BASE_URLpoints at a host other thanapi.anthropic.com. That is documented behavior, not a misconfiguration.
QCode worked example:
export ANTHROPIC_BASE_URL="https://api.qcode.cc/api"
export ANTHROPIC_AUTH_TOKEN="cr_your_key"
Related pages
Environment variable reference · Cursor and Cline · How to evaluate a relay · Setup cheat sheet · Codex · Gemini · OpenCode
Facts checked against docs.qcode.cc on 2026-12-05. I run qcode.cc. Pricing: qcode.cc/pricing (no free quota). Contact: hi@qcode.cc.