The default ChatOpenAI client is hardwired to one base URL, and every chain, agent, and notebook you build inherits it. Using LangChain with an OpenAI-compatible API such as AtmoRouter is usually a three-line change — but LangChain has three config paths that decide routing silently, and two of them will cost you real money if you get them wrong.
This guide wires AtmoRouter into ChatOpenAI: install, base URL precedence, the two flags that break streaming and routing, catalogue model IDs, and token math. It is AtmoRouter-authored, and every price below is quoted from our public model list at writing time. Scope is narrow on purpose — chat models only. Embeddings, LangGraph checkpointing, and Responses-API-only features are out of scope.
What an OpenAI-compatible endpoint actually buys you
LangChain's ChatOpenAI targets the official OpenAI API shape only. Anything a provider bolts on top — reasoning_content, reasoning_details, vendor-specific tool payloads — is not extracted or preserved by the wrapper, which is why the integration docs send you to provider-specific packages when a service extends the format.
A gateway that mirrors Chat Completions and passes model IDs straight through is the other case, and it is exactly what base_url exists for.
Worth checking before you write any config: one curl to the endpoint should return standard choices, usage, and tool_calls fields, with prompt and completion tokens both reported. If those are there, the wrapper has what it needs for chains, tool calling, structured output, and cost accounting. If a service hides or renames token counts, you lose cost visibility, and the fix is a different package rather than more config.
Install and point ChatOpenAI at the gateway
bashpip install -U langchain-openai
export ATMOROUTER_API_KEY="ar-xxxxxxxxxxxx"pythonimport os
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="zai/glm-5.3",
base_url="https://atmorouter.dev/v1",
api_key=os.environ["ATMOROUTER_API_KEY"],
use_responses_api=False,
stream_usage=True,
max_retries=2,
timeout=60,
)
reply = llm.invoke("Summarise this diff in one sentence: ...")
print(reply.text, reply.usage_metadata)Two details in that block matter. base_url includes the /v1 segment and the SDK appends /chat/completions itself, so trimming or doubling the suffix produces a 404 that reads like an auth failure. And the key comes from the environment, so the same file runs locally and in CI.
The two flags are deliberate. Neither is a default; each one exists because of a failure mode described below.
base_url precedence: three places routing can hide
LangChain resolves the endpoint in a fixed order, first match wins: the explicit base_url (or openai_api_base) kwarg, then OPENAI_API_BASE read by LangChain at init, then OPENAI_BASE_URL read by the underlying SDK client.
The third one is the trap, because it is read for more than URL resolution. LangChain inspects OPENAI_BASE_URL to decide whether to enable stream_usage by default, and when it is set the default is left off — the docs are explicit that many non-OpenAI endpoints do not support streaming token usage. A config that works fine for chat can therefore stop reporting usage, and you find out when the monthly report shows zero spend against a real invoice.
api_key has the same shape of problem in reverse. If you do not pass it, the class falls back to OPENAI_API_KEY, so a stray key from another tool in your environment gets sent to whatever base_url points at. Passing both explicitly is unglamorous and it eliminates a whole class of works-on-my-machine reports.
The two flags that bite in production
Chat Completions does not stream token usage unless asked. With stream_usage=True, LangChain sets stream_options.include_usage and fills usage_metadata on the final chunk. Without it, .stream() hands you text and nothing else: your per-request logging records zero while the balance keeps moving. Set it at init unless an endpoint rejects stream_options, and if one does, run metered paths non-streaming.
use_responses_api=False is the second. The Chat Completions versus Responses decision is partly inferred from the model name, independently of base_url, so a model ID that reads like a reasoning model can push the client at /v1/responses — a path a compatible gateway may simply not implement. Making it explicit keeps the request target a property of your code instead of a string that arrives from the catalogue.
Model IDs, prices, and what a run costs
Model IDs are catalogue IDs with the family prefix attached: zai/glm-5.3, not glm-5.3. The prefix is part of the string you send. Drop it and you get model_not_found, not a helpful fallback.
Rates below are from the public model list on 2026-11-12. Models have multiple price tiers, so read these as starting figures and check the model page before you budget.
| Model ID | From $/M input | From $/M output | |---|---|---| | zai/glm-5.3 | $0.0035 | $0.011 | | zai/glm-5.3-flash | $0.000375 | $0.00125 | | mm/MiniMax-M3 | $0.00075 | $0.003 | | ali/qwen3.8-max | $0.025 | $0.075 | | oai/gpt-5.6-luna | $0.164 | $0.984 |
Illustrative math, not a benchmark: a nightly job of 2,000 calls averaging 4,000 input and 700 output tokens moves 8M input and 1.4M output tokens. At zai/glm-5.3, that is about $0.028 of input and $0.015 of output, roughly $0.04 per run. The same job on oai/gpt-5.6-luna is about $1.31 of input and $1.38 of output, near $2.69 — a 60x gap created entirely by one string in model.
Repeated prompts change that picture faster than model choice does. Cached input is billed at 10% of the input rate where the model supports it, so a long system prompt or a fixed tool schema resent on every call collapses in cost the moment caching kicks in.
When it does not work
- 404 on
/chat/completions—base_urlis missing/v1or has it twice. model_not_found— prefix stripped, typo, or the ID is not in the live list.- 401 — key came from
OPENAI_API_KEYinstead of the variable you meant. usage_metadatais None — streaming withoutstream_usage=True.- Empty
contentwith tool calls — expected; readtool_calls, notcontent. - Output cut short —
max_tokensis still accepted and converted tomax_completion_tokensinternally, but the model's output cap still applies.
FAQ
Can I just set OPENAI_API_KEY and OPENAI_BASE_URL and change nothing?
You can, and everything in the process inherits the routing. For a single-purpose script that is fine; for an app that also calls a direct provider, pass api_key and base_url at the call site so one client cannot hijack another's billing.
Do I need a provider-specific LangChain package?
Only if you depend on response fields outside the official shape. For standard choices and usage objects, ChatOpenAI with base_url is the supported path, not a workaround.
Does changing models mean rewriting chains?
No — the ID is a parameter. What does change is capability: image input, tool calling, and context limits vary by model, so pin a model per route and test one path before swapping the default everywhere.
Next step
The base URL is the same one the Cline and OpenCode guides use, so you can point an editor and a LangChain service at the same prepaid balance. Create a key on atmorouter.dev, run the snippet above against one chain, and check live rates on the model page before you commit a nightly job to a price.