How to Call Domestic Chinese Models with LangChain: Avoid These Common Pitfalls
Last week a friend came to me venting: he wanted to use LangChain with domestic Chinese models, spent two nights struggling, and finally got stuck on a 401 Unauthorized error, nearly giving up in frustration. I asked him to send me his code—the problem was obvious at a glance: he had copied the OpenAI pattern straight from the official docs without changing the base_url, sending his domestic model's key to OpenAI's endpoint.
This kind of failure is all too typical. Today, instead of walking through the usual "register → get key → run code" sequence, I'll first lay out the common failure patterns, then give you the correct path. The mistakes you've stumbled through are the ones you remember best.
Detour #1: Changing Only the Model Name, Not the base_url
The most common beginner code looks like this:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="deepseek-chat", api_key="sk-xxx")The error is usually "model not found" or an authentication failure. The reason is simple: ChatOpenAI sends requests to the official OpenAI endpoint by default, where your domestic model key is meaningless.
Domestic model providers almost all offer OpenAI-compatible endpoints—that is, the API protocol is compatible, but the address is different. So the correct approach is to change the base_url, not just the model name.
Detour #2: Writing Your Own HTTP Requests, Bypassing LangChain
Some people, after failing to get things working, simply hand-write HTTP calls with requests. It works, but the cost is giving up LangChain's entire ecosystem—Chains, message templates, streaming output, and Agent tool calls all have to be reimplemented from scratch. For indie developers, that's trading flexibility for grunt work—a bad deal.
Detour #3: Registering an Account for Every Provider, Turning Key Management into a Disaster
A common state for small teams: one key for DeepSeek, one for Tongyi, one for Zhipu—environment variables in chaos, and comparing models means editing code. This is exactly why I recommend using an aggregation gateway below.
The Correct Path: One OpenAI-Compatible Gateway Handles Everything
ThisToken.AI is an OpenAI-compatible gateway that aggregates multiple domestic Chinese models. You only need to register one account and get one key, and you can switch between different models under the same base_url. For specific pricing, refer to the pricing page on the official website.
Step 1: Register and Get Your API Key
- Open the ThisToken.AI registration page and sign up with your email;
- Go to the console and find the API Key management page;
- Create a new key and store it securely—it is only displayed in full once, at creation time.
It's recommended to store the key in an environment variable rather than hardcoding it:
export THISTOKEN_API_KEY="sk-your-key"Step 2: Install Dependencies
pip install langchain langchain-openaiStep 3: Run Your First Piece of Code
import os
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
llm = ChatOpenAI(
model="deepseek-chat", # 换成你想用的模型名即可
api_key=os.environ["THISTOKEN_API_KEY"],
base_url="https://api.thistoken.ai/v1", # 关键:指向网关端点
)
messages = [
SystemMessage(content="你是一个简洁的中文技术助手。"),
HumanMessage(content="用一句话解释什么是 OpenAI 兼容端点。"),
]
response = llm.invoke(messages)
print(response.content)If you can see the answer printed after running it, the whole pipeline is working.
Note the two key points in the code: base_url="https://api.thistoken.ai/v1" tells LangChain to send requests to the gateway instead of OpenAI; and changing the model parameter to another model name supported by the gateway is all it takes to switch models—not a single other line of code needs to change.
Step 4: Experience Zero-Cost Switching
Want to compare another model? Change just one line:
llm = ChatOpenAI(
model="qwen-plus", # 只改这里
api_key=os.environ["THISTOKEN_API_KEY"],
base_url="https://api.thistoken.ai/v1",
)This is the solution to Detour #3: no more registering separately and managing keys separately for every provider.
Quick Reference for Common Errors
- 401 / Authentication failure: Check whether the environment variable is set (
echo $THISTOKEN_API_KEY) and whether the key was copied in full. - Model not found: The model name must exactly match the name in the gateway's documentation—watch out for case and hyphens.
- Connection timeout: Make sure the base_url includes the
/v1suffix; without it, requests hit the wrong path. - Package version conflicts:
langchain-openairequires version 0.1 or above; the oldlangchain.chat_modelspattern from outdated tutorials has been deprecated.
Final Thoughts
Looking back, these detours share a common root: they all stem from "copying example code without understanding the protocol structure." The whole point of OpenAI compatibility is that—as long as the endpoint is right—all of LangChain's capabilities (streaming output, tool calls, Agent orchestration) can be reused directly with domestic Chinese models.
If you haven't registered yet, you can go here to create an account and get a key: https://api.thistoken.ai/register —you'll have your first piece of code running within five minutes.
---
Tired of juggling provider integrations? Register at https://api.thistoken.ai/register and call every model through one base_url.
Bạn muốn thử Token.AI?
Tạo API Key cấp dự án, bật kênh trong bảng điều khiển và định cấu hình định tuyến, ngân sách và nhật ký kiểm tra.
注册 ThisToken.AI 并获取 API Key