Three Common Failure Scenarios First
When many indie developers first try to run Chinese domestic models with LangChain, the path usually goes like this: search for a tutorial → copy the code → change the model name → run into errors. Here are three classic failure modes—see which one you've hit.
Failure #1: Changing only the model name, not the base_url.
llm = ChatOpenAI(model="qwen-plus", api_key="sk-xxx")After running it, you get a pile of retry logs, ending with an authentication or "model not found" error. The reason is simple: this code still sends requests to the official OpenAI endpoint by default, and OpenAI doesn't have a model called qwen-plus. No matter how correct your model name is, the request never reaches the right place.
Failure #2: base_url is set, but the path is incomplete.
Some developers know they need to switch endpoints and write https://api.thistoken.ai—missing the /v1. The OpenAI client underlying LangChain then assembles an incorrect resource path and gets a 404. What makes this error so frustrating is that the key is valid, the model name is correct, and the network is fine—just one missing path suffix, and you'll spend at least half an hour debugging.
Failure #3: Mixing the official SDK with the compatible endpoint.
Some domestic models have their own official SDKs. Developers take the easy route and get things working with the official SDK first, but when they later try to switch back to the LangChain ecosystem, they find the message structures and tool-calling formats don't match, so they end up hand-writing glue code between the two systems—and it gets messier and messier.
The common root cause of all three failures is: not understanding what an "OpenAI-compatible endpoint" is actually compatible with. It's compatible with OpenAI's HTTP interface specification, so all you need to do is point LangChain's OpenAI client at the correct address, and everything else (streaming, tool calling, structured output) works as usual.
The Right Path: Three Steps to Get It Working
Step 1: Register and Get Your API Key
Open ThisToken.AI (an OpenAI-compatible gateway aggregating multiple domestic models), register an account, go to the console, and create a new key on the "API Keys" page. The key is shown in full only once at creation—make sure to save it immediately in an environment variable or a secrets management tool, and never hardcode it.
As for pricing, no need to comparison-shop around the web—the official pricing page lists the billing rules for each model clearly.
Step 2: Install Dependencies
pip install langchain langchain-openaiStep 3: Run Your First Code
import os
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
# 建议通过环境变量管理密钥,避免硬编码
os.environ["OPENAI_API_KEY"] = "sk-你的Key"
llm = ChatOpenAI(
model="qwen-plus", # 换成你在 ThisToken.AI 可用的任意模型
base_url="https://api.thistoken.ai/v1", # 关键:注意 /v1 不能少
temperature=0.7,
)
messages = [
SystemMessage(content="你是一个简洁的技术助手。"),
HumanMessage(content="用一句话解释什么是 OpenAI 兼容端点。"),
]
response = llm.invoke(messages)
print(response.content)If the response prints correctly after running, the pipeline is working. Check against the three failure scenarios above: base_url includes /v1, the model name is one the gateway supports, and the key comes from an environment variable—all three landmines cleared.
Once It Works, Two Easy Upgrades
Switch to streaming output. The compatible endpoint supports streaming just like OpenAI—one line change:
for chunk in llm.stream(messages):
print(chunk.content, end="", flush=True)Move the key out of your code. The example above uses os.environ assignment for completeness, but in real projects you should use a .env file with python-dotenv, or just rely on system environment variables. For small team projects deployed on Vercel or Netlify, put the key in the platform's environment variable configuration—never commit it to the repository.
Why Use an Aggregation Gateway Instead of Connecting to Each Provider Directly
For indie developers and small teams, registering with each provider, topping up each account separately, and maintaining SDK versions for each one quickly costs more in maintenance than the models themselves. Going through a single OpenAI-compatible gateway gives you:
- One codebase, any model: Want to switch from Tongyi to DeepSeek? Just change the
modelparameter—thebase_urland authentication stay the same; - No vendor lock-in: If a model misbehaves or gets discontinued, switching costs nearly nothing;
- Full LangChain ecosystem compatibility: Higher-level capabilities like chains, agents, and structured output work as usual, with no separate adaptation needed for domestic models.
Final Thoughts
The hard part of integrating domestic models was never the models themselves—it's environment configuration issues like "you think you're calling a domestic model, but the request is actually going somewhere else." Get the line base_url="https://api.thistoken.ai/v1" right, and the road ahead is smooth.
If you haven't registered yet, you can start right here: https://api.thistoken.ai/register —within five minutes, you'll see your first response printed in your terminal.
---
Tired of juggling provider integrations? Register at https://api.thistoken.ai/register and call every model through one base_url.
Ready to try Token.AI?
Create a project-level API Key, enable channels in the console, and configure routing, budgets, and audit logs.
注册 ThisToken.AI 并获取 API Key