---
title: "LangChain base_url: Custom Endpoints for ChatOpenAI and ChatAnthropic"
description: "Switch LangChain's endpoint with ChatOpenAI and ChatAnthropic: parameters, env vars, a 10-line check, and a 404/401 troubleshooting order."
url: https://cdnping.org/en/blog/langchain-custom-base-url
language: en
published: 2026-11-28
category: "Developer tools"
tags: ["LangChain","base_url","ChatOpenAI","ChatAnthropic","OpenAI-compatible API"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# LangChain base_url: Custom Endpoints for ChatOpenAI and ChatAnthropic

To point LangChain at a different API endpoint, pass `base_url` and `api_key` to `ChatOpenAI`. `ChatAnthropic` has a matching endpoint parameter. Below: the syntax first, then a short verification snippet, then the order in which to check things when you get an error.

This article covers the Python packages `langchain-openai` and `langchain-anthropic`. Parameter aliases and default behavior change between versions. If a snippet below fails on your version, defer to the official docs for that package.

## Where does langchain base_url go?

`ChatOpenAI` is built on the official openai SDK, so the endpoint parameter means the same thing it does there: give the address up to the version prefix, for example `https://example.com/v1`. Don't include `/chat/completions`; the SDK appends it.

```python
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="your-model-name",
    base_url="https://example.com/v1",
    api_key="your-key",
)
print(llm.invoke("Introduce yourself in one sentence").content)
```

Notes:

- Older versions called this parameter `openai_api_base`. `base_url` is the common spelling now, and most versions accept both, but go by the version you have installed.
- Set `model` to a name from your provider's docs. If the provider isn't OpenAI, don't copy OpenAI model names from tutorials.
- Don't commit your key to a repository. The next section uses environment variables instead.

## Can I use environment variables instead of changing code?

Yes. The openai SDK reads `OPENAI_BASE_URL` and `OPENAI_API_KEY`, and LangChain usually falls back to them when you don't pass the arguments explicitly. That lets the same code point at different endpoints in different environments.

```bash
export OPENAI_BASE_URL="https://example.com/v1"
export OPENAI_API_KEY="your-key"
```

```python
llm = ChatOpenAI(model="your-model-name")
```

To see which tools read this variable, see [How to set OPENAI_BASE_URL and which tools read it](/en/blog/openai-base-url-env-variable). The three ways to set it in the SDK itself are compared in [How to change base_url in the openai Python library](/en/blog/openai-python-base-url-setting). Explicit arguments normally take priority over environment variables, so check both places when debugging.

## How do I change the endpoint in ChatAnthropic?

For services that speak the Anthropic API format, use `ChatAnthropic` from `langchain-anthropic`:

```python
from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(
    model="your-model-name",
    base_url="https://example.com",
    api_key="your-key",
)
print(llm.invoke("Hello").content)
```

Two things trip people up here. First, whether an Anthropic-format address includes `/v1` varies by service. The SDK adds its own path, so use the root address your provider's docs give you. Second, the parameter has aliases such as `anthropic_api_url` in some versions; if you're unsure, check the docs for your installed version. For how the underlying SDK handles this, see [How to change the endpoint in the Anthropic Python SDK](/en/blog/anthropic-python-sdk-base-url).

## How do I confirm requests actually go to the new address?

Verify with a minimal call before wiring up a chain or agent.

1. Print the address in use: `print(llm.openai_api_base)`. The field name may change between versions, so check the official docs. It reflects only explicitly passed arguments; a value that came from an environment variable may not show up.
2. Send a single `invoke` and see whether content comes back.
3. If it fails, send one request with curl using the same address and key. If curl works but LangChain doesn't, the problem is in your parameters. If curl fails too, the problem is the address or the key. For the curl syntax, see [Send your first LLM request with curl](/en/blog/curl-first-chat-completions-request).

## What order should I check things in when it errors?

| Symptom | Check first |
| --- | --- |
| 404 | Extra or missing `/v1` in the address; `/chat/completions` included in base_url |
| 401 | Key copied incompletely or with stray spaces; key belongs to a different service |
| Model not found | `model` is another provider's name; go by the provider's model list |
| Constant timeouts | No network path to that address, or timeout and retry settings too small |
| Change has no effect | An explicit argument overrides the environment variable, or the variable wasn't exported in the other shell |

For timeout and retry defaults and production recommendations, see [How to configure OpenAI SDK timeouts and retries](/en/blog/openai-anthropic-sdk-timeout-retry-config). If you suspect a network problem, cdnping's [online speed test](/en) shows connection latency from your browser to each provider's API, and [API status](/en/status) shows whether the official entry points have been stable recently. For why overseas APIs are slow from mainland China, see [AI API latency](/en/learn/ai-api-latency-china).

## What if I don't have a compatible endpoint yet?

The syntax above works with any service that is compatible with the OpenAI or Anthropic API format. If you don't have a usable endpoint yet, you can look at AGI Plan. AGI Plan is a commercial partner of cdnping: a monthly-subscription gateway that aggregates multiple LLM providers, payable in RMB with no overseas credit card needed. It is compatible with both the OpenAI and Anthropic API formats, and one subscription lets you switch among several capability groups. Tools like Claude Code and Codex work by changing a single endpoint address, and LangChain is the same: replace the `base_url` above with the address shown in the console. Some models are marked "overseas only"; see the [models page](https://agiplan.dev/models) for specifics. The service states that it is not an officially authorized channel.

Getting started takes roughly three steps: sign up with an emailed verification code, pick a plan and create a key, then change one setting. The entry point is the [AGI Plan beginner guide](https://agiplan.dev/startup).

## What should I do next?

1. Use the minimal code above to confirm a single call succeeds.
2. Move the address and key into environment variables so no plaintext remains in your code.
3. Then plug in your chain or agent, with suitable timeout and retry settings.
