· rangkumin.tech

LangChain With an OpenAI-Compatible API: Base URL, Streaming Flags and Token Math for ChatOpenAI

A new AtmoRouter guide shows how to point LangChain's ChatOpenAI client at an OpenAI-compatible gateway, which config paths silently decide routing, and which two flags keep streaming usage and request routing predictable.

Why it matters

LangChain resolves the endpoint in a fixed order: the explicit base_url argument, then OPENAI_API_BASE, then OPENAI_BASE_URL read by the OpenAI SDK. The last one also changes behaviour: when it is set, stream_usage stays off by default, so streaming calls can report zero tokens while the balance still moves. Passing base_url and api_key explicitly avoids that and stops a stray OPENAI_API_KEY from being sent to the wrong endpoint.

The guide recommends stream_usage=True so token counts arrive in the final streamed chunk, and use_responses_api=False because LangChain partly infers Responses API routing from the model name, a path a compatible gateway may not implement. Model IDs need their family prefix, such as zai/glm-5.3.

An illustrative nightly job of 8 million input and 1.4 million output tokens costs about $0.04 on zai/glm-5.3 versus about $2.69 on oai/gpt-5.6-luna at listed starting rates on 12 November. Prices change, so the live model page stays authoritative; the post is AtmoRouter-authored.

Sources

Choose English or Indonesian. Up to two editions a day. Unsubscribe anytime.