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:

2. 404 and /v1/v1/ — wrong path prefix

Every client appends its own path. The Base URL is the prefix only.

ClientSettingClient appendsTypical Base URL
Claude CodeANTHROPIC_BASE_URL/v1/messageshttps://gateway.example.com (no /v1)
Codex CLIbase_url in [model_providers.<id>]/responseshttps://gateway.example.com/v1
OpenAI SDKs, Cursor, Cline (OpenAI Compatible)base_url / Base URL/chat/completions, /modelsusually 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:

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.

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

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"

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.