---
title: "Anthropic SDK base_url in Python: Two Ways and the Extra /v1 404 Fix"
description: "Set the Anthropic SDK base_url by passing it to Anthropic() or using ANTHROPIC_BASE_URL. Includes a minimal example and how to debug a 404 from an extra /v1."
url: https://cdnping.org/en/blog/anthropic-python-sdk-base-url
language: en
published: 2026-11-28
category: "Developer tools"
tags: ["Anthropic","Python","SDK","base_url","Claude API"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# 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:

```bash
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:

```python
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`:

```bash
# macOS / Linux
export ANTHROPIC_BASE_URL="https://your-api-domain"
export ANTHROPIC_API_KEY="your API key"
```

```powershell
# 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](/en/blog/anthropic-auth-token-vs-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 write | Request path the SDK sends (illustrative) | Result |
| --- | --- | --- |
| `https://domain` | `https://domain/v1/messages` | Correct |
| `https://domain/v1` | `https://domain/v1/v1/messages` | Most likely 404 |
| `https://domain/` | A trailing slash is generally fine, but test to confirm | Usually works |

For the OpenAI side, see [How to change base_url in the openai Python library: three ways](/en/blog/openai-python-base-url-setting). 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:

```bash
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"}]}'
```

4. If you get a connection timeout rather than an error code, it is usually a network problem. Try the [cdnping online speed test](/en) to see first-connection time and round trip for each API entry point, or check the [API status page](/en/status/anthropic) to see whether the official endpoint has had recent issues. For background on latency from mainland China, see [AI API latency](/en/learn/ai-api-latency-china).
5. Toolchain issues: if you also use Claude Code, its configuration lives in settings.json; see [Claude Code settings.json hierarchy, precedence, and env configuration](/en/blog/claude-code-settings-json-guide).

## 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:

```python
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](/en/blog/how-to-get-claude-api-key), and for payment options see [Which payment methods the Anthropic API supports](/en/blog/anthropic-api-payment-methods). 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](https://agiplan.dev/models) 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](https://agiplan.dev/startup).

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?

- Get the minimal example from this article working, then confirm the address with `print(client.base_url)`.
- For endpoint settings in command-line tools, see [Claude Code custom API, and Codex and OpenCode endpoint configuration](/en/blog/configure-custom-api-endpoint-claude-code-codex).
- If you are new to the Claude API, read [How to use the Claude API: from getting a key to your first request](/en/blog/claude-api-beginner-getting-started).
