Anthropic SDK base_url in Python: Two Ways and the Extra /v1 404 Fix

Blog / Developer tools

Anthropic SDK base_url in Python: Two Ways and the Extra /v1 404 Fix

There are two common ways to point the Anthropic Python SDK at a different API endpoint: pass the base_url argument when you create the client, or set the ANTHROPIC_BASE_URL environment variable. If both are set, the explicit argument wins. The most common mistake is the path: the Anthropic SDK appends /v1/messages itself, so the address should normally stop at the domain. Don't add /v1 by hand.

Below: how to write it, how to verify it, and where to look when it fails.

What does a minimal example look like?

Install the package and check the version first:

pip install anthropic
pip show anthropic

Here is a minimal call. Replace base_url and api_key with the values from the service you actually use, and take the model name from that service's documentation as well:

import anthropic

client = anthropic.Anthropic(
    base_url="https://your-api-domain",   # no /v1
    api_key="your API key",
)

msg = client.messages.create(
    model="the model you want to call",
    max_tokens=256,
    messages=[{"role": "user", "content": "Introduce yourself in one sentence"}],
)
print(msg.content[0].text)

If you connect directly to Anthropic, leave base_url out and the SDK uses the official address by default. You only need to change it when you go through a gateway, a proxy, or another compatible service.

How do I set it with an environment variable?

If you'd rather keep the address out of your code, use environment variables. The SDK reads ANTHROPIC_API_KEY and ANTHROPIC_BASE_URL:

# macOS / Linux
export ANTHROPIC_BASE_URL="https://your-api-domain"
export ANTHROPIC_API_KEY="your API key"
# Windows PowerShell (current window only)
$env:ANTHROPIC_BASE_URL = "https://your-api-domain"
$env:ANTHROPIC_API_KEY = "your API key"

Your code can then just call anthropic.Anthropic() with no arguments. The benefit is that the same code runs locally and in production, and only the environment variables change. Note that an environment variable only applies to the terminal session where you set it (and processes launched from it). When you run code from an IDE it is easy for the variable not to be visible, which is the classic "I set it but it has no effect" problem for newcomers.

For the difference between ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN, see What is the difference between ANTHROPIC_AUTH_TOKEN and ANTHROPIC_API_KEY.

What happens if I add an extra /v1?

This is the most common error. The OpenAI SDK's base_url usually includes /v1; the Anthropic SDK is different. Side by side:

What you writeRequest path the SDK sends (illustrative)Result
https://domainhttps://domain/v1/messagesCorrect
https://domain/v1https://domain/v1/v1/messagesMost likely 404
https://domain/A trailing slash is generally fine, but test to confirmUsually works

For the OpenAI side, see How to change base_url in the openai Python library: three ways. If one service offers both formats, the base_url you need may differ between them, so follow the provider's documentation.

How do I debug an error step by step?

Work through these in order; it usually takes a few minutes to find the cause:

  1. Print the address the client actually uses with print(client.base_url), and confirm it hasn't been overridden by an environment variable or given an extra /v1.
  2. Check the status code. A 404 is usually a wrong path or model name; 401 means an invalid key or a mismatched auth header; 429 means rate limiting or a quota problem; 5xx is a server-side problem. For the exact meaning of each error, check the official documentation of the service you are calling.
  3. Reproduce with curl to bypass the SDK and see whether the problem is the address or your code:
curl -i "$ANTHROPIC_BASE_URL/v1/messages" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model":"the model you want to call","max_tokens":64,"messages":[{"role":"user","content":"hi"}]}'
  1. If you get a connection timeout rather than an error code, it is usually a network problem. Try the cdnping online speed test to see first-connection time and round trip for each API entry point, or check the API status page to see whether the official endpoint has had recent issues. For background on latency from mainland China, see AI API latency.
  2. Toolchain issues: if you also use Claude Code, its configuration lives in settings.json; see Claude Code settings.json hierarchy, precedence, and env configuration.

Should I set timeouts, retries, and a proxy too?

After changing the address, it helps to write these parameters out explicitly so debugging is clearer:

client = anthropic.Anthropic(
    base_url="https://your-api-domain",
    api_key="your API key",
    timeout=60.0,
    max_retries=2,
)

timeout and max_retries are both client parameters of the SDK; check the official documentation for your installed version for the defaults. If you need to go through a local HTTP proxy, you can pass a custom httpx.Client when creating the client, or use the standard proxy environment variables that httpx recognizes. Again, details are in the SDK documentation.

What if I don't have an overseas credit card?

Using the official API requires setting up and topping up an account in the Anthropic Console. For the process, see How to get a Claude API key, and for payment options see Which payment methods the Anthropic API supports. The official service has supported-region restrictions, so use it in line with its terms and the laws where you live.

If you just want existing code to go through a compatible endpoint, you can look at AGI Plan. It is a multi-provider LLM aggregation gateway with monthly subscriptions, payment in RMB, and no overseas credit card required. It is compatible with both the OpenAI and Anthropic API formats, so tools such as Claude Code and Codex only need one endpoint address changed. The site states clearly that it is not an officially authorized channel, and some models are marked as available outside China only; see the models page for specifics. AGI Plan is a commercial partner of cdnping, and it is described here only factually; compare it yourself before deciding. For setup steps, see the AGI Plan quick start.

Once you have an address and a key, substitute them for base_url and api_key in the examples above, and run one small request with max_tokens=64 to confirm it works.

What should I do next?

Related articles

Keep reading