Tutorial

ANTHROPIC_BASE_URL: What It Is and How to Set It Correctly

2026-06-28 6

TL;DRANTHROPIC_BASE_URL is the environment variable that tells Claude Code and the Anthropic SDK where to send requests. Point it at the official endpoint to use Anthropic directly, or at a compatible relay's host root to route elsewhere. The single most common mistake is adding an API path yourself. Claude Code appends /v1/messages, so the base URL should contain the host only.

What ANTHROPIC_BASE_URL actually does

By default, Claude Code and the anthropic SDK send every request to Anthropic's API host. ANTHROPIC_BASE_URL overrides that target. Set it, and the exact same client — same model IDs, same streaming, same tool calls — talks to whatever endpoint you point it at, as long as that endpoint speaks the Anthropic Messages protocol.

That's the whole mechanism behind Claude-compatible relays and proxies: they implement the official protocol, and you redirect the client with this one variable. Nothing about your code or prompts changes (more on the concept in what a Claude-compatible API relay is).

The #1 mistake: the trailing /messages 404

This trips up almost everyone, so it's worth being precise. Claude Code constructs the final request URL by appending the endpoint path itself — it adds /v1/messages to whatever base URL you give it.

  • ✅ Correct: ANTHROPIC_BASE_URL=https://llmapi.pro → request goes to …/v1/messages
  • ❌ Wrong: ANTHROPIC_BASE_URL=https://llmapi.pro/v1 → request can go to …/v1/v1/messages404

The fix is simple: set the Claude Code base URL to the host only, with no /v1 or /messages suffix. A 404 mid-session that looks like a generic "connection error" is very often this. (For the full fault-finding ladder, see fix a Claude Code connection error or timeout.)

How to set it

You set two variables — the base URL and your key:

export ANTHROPIC_BASE_URL=https://llmapi.pro
export ANTHROPIC_API_KEY=sk-relay-your-key

macOS / Linux (persist it): add those two lines to ~/.zshrc or ~/.bashrc, then source the file or open a new terminal.

Windows (PowerShell, persist it):

setx ANTHROPIC_BASE_URL "https://llmapi.pro/v1"
setx ANTHROPIC_API_KEY "sk-relay-your-key"

Open a new terminal after setx — it doesn't affect the current session. On Windows you can also use our one-line installer, which sets both for you:

irm llmapi.pro/setup.ps1 | iex

Full per-client setup is in the Claude Code API key setup guide.

Verify it's working

Take Claude Code out of the equation and hit the endpoint directly. A passing curl proves the base URL, key, and network are all good:

curl $ANTHROPIC_BASE_URL/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-haiku-4-5",
    "max_tokens": 64,
    "messages": [{"role": "user", "content": "reply with: ok"}]
  }'

A normal JSON reply means everything's wired correctly. A 401 means the key got mangled (stray quotes/spaces); a 404 sends you back to the /messages rule above; a connection failure with no HTTP status is a network/proxy issue.

Switching back to official

There's zero lock-in. Unset the variable and the client immediately talks to Anthropic again:

unset ANTHROPIC_BASE_URL

That reversibility is the point — ANTHROPIC_BASE_URL is a redirect, not a rewrite.

FAQ

What is ANTHROPIC_BASE_URL? It's the environment variable that sets where Claude Code and the Anthropic SDK send API requests. Override it to route to a relay or proxy that speaks the Anthropic protocol; leave it unset to use Anthropic directly.

What value should ANTHROPIC_BASE_URL have? The host root — for example https://llmapi.pro. Do not append /v1 or /messages; Claude Code adds the complete endpoint path.

Why do I get a 404 after setting ANTHROPIC_BASE_URL? Almost always because the Claude Code base URL includes a path. Claude Code appends /v1/messages itself, so use the host root with no /v1 or /messages suffix.

Can I switch back to the official Anthropic endpoint? Yes, instantly — unset ANTHROPIC_BASE_URL (or remove it from your shell profile). There's no lock-in.

llmapi.pro is an independent, Claude-compatible API relay; we are not affiliated with Anthropic. Claude Code and Claude are used for identification only.

分享这篇文章

开始使用 LLM API

免费套餐可用。Claude Code 一行配置。

免费开始