Use BigModel.io with LangChain
Connect LangChain to BigModel.io with ChatOpenAI (base_url https://bigmodel.io/v1) or ChatAnthropic (https://bigmodel.io). Streaming usage, tool binding and the reasoning_content caveat.
On this page
LangChain talks to BigModel.io through its OpenAI or Anthropic integration. Use ChatOpenAI for any key; use ChatAnthropic only if the key's group serves /v1/messages.
| Class | Base URL setting | Value |
|---|---|---|
ChatOpenAI (Python) |
base_url |
https://bigmodel.io/v1 |
ChatOpenAI (JS) |
configuration.baseURL |
https://bigmodel.io/v1 |
ChatAnthropic (Python) |
anthropic_api_url |
https://bigmodel.io (no /v1) |
ChatAnthropic (JS) |
anthropicApiUrl |
https://bigmodel.io (no /v1) |
Model IDs come from the model catalog; availability depends on the key's group.
export BIGMODEL_API_KEY="sk-..."#Install
pip install langchain-openai langchain-anthropicnpm install @langchain/openai @langchain/anthropic @langchain/core zod#ChatOpenAI
import os
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="glm-5.3",
base_url="https://bigmodel.io/v1",
api_key=os.environ["BIGMODEL_API_KEY"],
stream_usage=True,
)
print(llm.invoke("List five things to verify before launch.").content)import { ChatOpenAI } from "@langchain/openai";
const llm = new ChatOpenAI({
model: "glm-5.3",
apiKey: process.env.BIGMODEL_API_KEY,
configuration: { baseURL: "https://bigmodel.io/v1" },
streamUsage: true,
});
const result = await llm.invoke("List five things to verify before launch.");
console.log(result.content);In Python, stream_usage is off by default when you set a custom base URL, so streamed responses carry no usage_metadata. Pass stream_usage=True as shown.
Passing reasoning=... or use_responses_api=True switches ChatOpenAI to /v1/responses, which GLM and DeepSeek groups do not serve (404). Leave both unset for those keys.
#ChatAnthropic
import os
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(
model="glm-5.3",
anthropic_api_url="https://bigmodel.io",
api_key=os.environ["BIGMODEL_API_KEY"],
max_tokens=1024,
)
print(llm.invoke("List five things to verify before launch.").content)import { ChatAnthropic } from "@langchain/anthropic";
const llm = new ChatAnthropic({
model: "glm-5.3",
anthropicApiUrl: "https://bigmodel.io",
apiKey: process.env.BIGMODEL_API_KEY,
maxTokens: 1024,
});
const result = await llm.invoke("List five things to verify before launch.");
console.log(result.content);ChatAnthropic also reads ANTHROPIC_API_URL / ANTHROPIC_BASE_URL from the environment; an explicit argument wins.
#Streaming
for chunk in llm.stream("Write a haiku about latency."):
print(chunk.content, end="", flush=True)const stream = await llm.stream("Write a haiku about latency.");
for await (const chunk of stream) {
process.stdout.write(typeof chunk.content === "string" ? chunk.content : "");
}#Tool calling
bind_tools sends standard OpenAI or Anthropic tool definitions; the gateway forwards them. Tool support depends on the model.
from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
return f"Sunny in {city}"
llm_with_tools = llm.bind_tools([get_weather])
msg = llm_with_tools.invoke("What's the weather in Hangzhou?")
print(msg.tool_calls)import { tool } from "@langchain/core/tools";
import { z } from "zod";
const getWeather = tool(async ({ city }) => `Sunny in ${city}`, {
name: "get_weather",
description: "Get the current weather for a city.",
schema: z.object({ city: z.string() }),
});
const msg = await llm.bindTools([getWeather]).invoke("What's the weather in Hangzhou?");
console.log(msg.tool_calls);#reasoning_content caveat
Thinking models on GLM and DeepSeek groups return their reasoning in a reasoning_content field. The gateway passes it through, but ChatOpenAI does not surface it: you get the final answer only. If you need the reasoning text, call the model with the OpenAI SDK and read reasoning_content there. See Reasoning & thinking.
#Verify the setup
msg = llm.invoke("ping")
print(msg.content, msg.usage_metadata)const msg = await llm.invoke("ping");
console.log(msg.content, msg.usage_metadata);A reply with token counts means the base URL and key work. The request should also appear in Usage.
#Troubleshooting
404 on every request with ChatOpenAI. The base URL is missing /v1. In JS it must be inside configuration, not a top-level baseURL.
404 on every request with ChatAnthropic. The URL ends in /v1. Use https://bigmodel.io.
404 ... only supports /v1/chat/completions. Either ChatAnthropic is used with a Chat-Completions-only group, or ChatOpenAI was switched to the Responses API by reasoning / use_responses_api. Use plain ChatOpenAI.
No token usage on streamed responses. Set stream_usage=True (Python) or streamUsage: true (JS).
401 INVALID_API_KEY. The key is not reaching the client; an OPENAI_API_KEY or ANTHROPIC_API_KEY in your environment may be used instead. Pass api_key explicitly.
503 No available accounts: no available accounts supporting model: .... The model ID is wrong or not in the key's group. Copy it from the model catalog.
403 INSUFFICIENT_BALANCE or 429. Top up in Billing; for 429 see Limits & rate limits.
#Related
- OpenAI SDK and Anthropic SDK
- LlamaIndex and Vercel AI SDK
- Tool calling and Streaming
Updated · View as Markdown