---
title: "LangChain ChatOpenAI with a custom base_url | BroadRouter"
description: "Connect LangChain's ChatOpenAI to BroadNet Inference with base_url and api_key, and learn what LangChain does and doesn't keep from reasoning models."
url: https://broadrouter.com/guides/langchain-openai-base-url/
lang: en
updated: 2026-11-16
publisher: BroadRouter (a BroadNet site); API by BroadNet Inference, https://inference.broadnet.ai
---

# Use LangChain with BroadNet Inference

Updated 2026-11-16

Install langchain-openai and create ChatOpenAI with base\_url set to https://inference.broadnet.ai/v1, api\_key set to your BroadNet key, and a BroadNet model ID such as broadnet/glm-5.3. Invoke, stream and tool-calling chains then run over chat completions. Avoid Responses-only features, and leave max\_tokens room for reasoning.

LangChain’s `ChatOpenAI` accepts any endpoint through `base_url`. This works with BroadNet Inference because it uses the standard chat-completions shape. If you call the API directly instead, see [the Python SDK guide](https://broadrouter.com/guides/openai-python-sdk/).

## Install

Current `langchain-openai` releases (1.x) need Python 3.11 or newer.

```bash
pip install -U langchain-openai
export BROADNET_API_KEY='paste-the-key-once'
```

## Connect

```python
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="broadnet/glm-5.3",
    base_url="https://inference.broadnet.ai/v1",
    api_key=os.environ["BROADNET_API_KEY"],
    max_tokens=2000,  # reasoning counts toward this limit
)

print(llm.invoke("Summarise the CAP theorem in two sentences.").content)
```

`openai_api_base` is an alias for `base_url`. Without either, LangChain reads `OPENAI_API_BASE`, and then the underlying `openai` client reads `OPENAI_BASE_URL`.

## Things to know

-   **Reasoning text isn’t kept.** LangChain’s docs say `ChatOpenAI` follows the official OpenAI specification and does not keep non-standard fields such as `reasoning_content`. You get the final answer, which is usually what a chain needs.
-   **Leave room for reasoning.** GLM 5.3, DeepSeek V4.1 Flash and Qwen3.8 27B reason first. A small `max_tokens` can return an empty answer. [Why answers come back empty](https://broadrouter.com/guides/reasoning-tokens-empty-response/).
-   **Streaming usage.** When you configure the endpoint with `OPENAI_BASE_URL`, LangChain turns off `stream_usage` by default. Pass `stream_usage=True` if you want token counts while streaming. BroadNet’s docs say usage arrives in the last chunk when it’s requested.
-   **Stay on chat completions.** BroadNet serves `/v1/chat/completions`, not the Responses API. LangChain’s docs say `ChatOpenAI` switches to the Responses API when you use OpenAI built-in tools (web search, file search, code interpreter and similar), conversation state, or the `reasoning` parameter. Leave those out, and don’t set `use_responses_api=True`.
-   **Token limit name.** LangChain’s docs say `max_tokens` is converted to `max_completion_tokens` internally. BroadNet’s docs describe `max_tokens`. If a limit seems ignored, check the request your app sends.

## Which model for which chain

| Chain | Model |
| --- | --- |
| Tool calling and structured output | `broadnet/glm-5.3` |
| Long documents and agents, unlimited plan | `broadnet/deepseek-v4.1-flash` |
| Arabic text or images | `broadnet/qwen3.8-27b` |

[See prices for every model](https://broadrouter.com/pricing/).

## Troubleshooting

-   **401.** The key is wrong, expired, or out of pay-as-you-go allowance. If you passed no `api_key`, LangChain used `OPENAI_API_KEY`, which may hold an OpenAI key.
-   **404 or “not found”.** Check that `base_url` ends in `/v1` and that nothing routes the call to the Responses API (see above). Then check the model ID matches BroadNet’s exactly, with the `broadnet/` prefix. Paid models need a top-up; the $5 credit covers `broadnet/gemma-4-26b-a4b` and the two Transcribe models.
-   **Empty `content`.** Reasoning used the budget. Raise `max_tokens`. [Why answers come back empty](https://broadrouter.com/guides/reasoning-tokens-empty-response/).
-   **Tool calls never happen.** Use `broadnet/glm-5.3` or `broadnet/qwen3.8-27b`. BroadNet’s docs list tools only for those two.
-   **429 in batch or parallel chains.** A pay-as-you-go key allows 30 requests a minute and 4 at once. Set `max_concurrency` in the runnable config for `batch()`. See [error codes and retries](https://broadrouter.com/guides/errors-and-retries/).

## Terms in this guide

[Pay-as-you-go](https://broadrouter.com/glossary/pay-as-you-go/)

Pay-as-you-go is a pricing model in which you pay only for what you use, metered per unit such as tokens, requests or audio minutes, with no subscription or minimum commitment. Most LLM APIs bill this way, from a prepaid balance or a periodic invoice.

[Tool calling](https://broadrouter.com/glossary/tool-calling/)

Tool calling, also called function calling, is an LLM API feature in which the model returns a structured request to run a function the developer has defined, with JSON arguments. The application runs the function and sends the result back; the model never runs code itself.

[Streaming](https://broadrouter.com/glossary/streaming/)

Streaming is a way of returning a language model response in small chunks while it is still being generated, instead of as one reply at the end. OpenAI-compatible APIs stream over server-sent events when the request sets stream to true.

[Token](https://broadrouter.com/glossary/token/)

A token is the unit of text a language model reads and writes: a word, part of a word, a punctuation mark or a byte sequence produced by the model's tokenizer. LLM APIs count, limit and usually bill usage in tokens.

[Chat Completions API](https://broadrouter.com/glossary/chat-completions-api/)

The Chat Completions API is OpenAI's HTTP endpoint, POST /v1/chat/completions, that takes a model name and a list of role-tagged messages and returns the model's next message. Its request and response shape became the common format that OpenAI-compatible providers and SDKs reuse.

[Concurrency](https://broadrouter.com/glossary/concurrency/)

Concurrency is the number of requests a client has in flight at the same time. LLM APIs often cap it per key or plan, separately from requests per minute, because each open request holds GPU capacity until its response has finished.

[Context window](https://broadrouter.com/glossary/context-window/)

A context window is the maximum number of tokens a language model can work with in a single request. It covers the prompt, conversation history, tool definitions, images and the generated output, including any reasoning tokens. Anything that does not fit must be cut, summarised or split across several requests.

[Prepaid credit](https://broadrouter.com/glossary/prepaid-credit/)

Prepaid credit is money added to an API account before use. Each request deducts its cost from the balance, and when the balance runs out, metered requests stop until you top up. It caps spend at what you have paid in.

## Questions

### Should I use base\_url or openai\_api\_base in ChatOpenAI?

Either. openai\_api\_base is an alias for base\_url. If neither is passed, LangChain checks OPENAI\_API\_BASE, and then the openai client reads OPENAI\_BASE\_URL.

### Can I set the BroadNet endpoint with environment variables only?

Yes. Set OPENAI\_BASE\_URL to https://inference.broadnet.ai/v1 and OPENAI\_API\_KEY to your BroadNet key, then create ChatOpenAI with only the model ID. Pass stream\_usage=True if you want token counts while streaming.

### Why does LangChain drop reasoning\_content from BroadNet responses?

LangChain's docs say ChatOpenAI follows the official OpenAI specification and does not extract non-standard fields such as reasoning\_content. You still get the final answer in content.

### Does ChatOpenAI with BroadNet support tool calling?

Yes, through bind\_tools, on models BroadNet lists with tools. Use broadnet/glm-5.3 or broadnet/qwen3.8-27b for tool calling and with\_structured\_output.

## Related guides

-   [Use the OpenAI Python SDK with BroadNet Inference](https://broadrouter.com/guides/openai-python-sdk/)
-   [Why a reasoning model returns an empty answer](https://broadrouter.com/guides/reasoning-tokens-empty-response/)
-   [BroadNet Inference error codes and when to retry](https://broadrouter.com/guides/errors-and-retries/)
-   [Run the OpenAI Agents SDK on BroadNet Inference](https://broadrouter.com/guides/openai-agents-sdk-custom-provider/)
-   [All tools that work with BroadNet Inference](https://broadrouter.com/integrations/): SDKs, coding agents, chat UIs and automation tools, with exact settings for each

Try it on your real workload. New accounts start with $5 of credit for Gemma and transcription. DeepSeek, GLM and Qwen3.8 need a top-up.

[Get started](https://inference.broadnet.ai/account/?utm_source=broadrouter&utm_medium=referral&utm_campaign=guides-langchain-openai-base-url&utm_content=page-cta) ·

**Sources**

-   [LangChain docs, ChatOpenAI integration (checked 2026-11-14)](https://docs.langchain.com/oss/python/integrations/chat/openai)
-   [PyPI, langchain-openai package (1.8, Python 3.11+, checked 2026-11-14)](https://pypi.org/project/langchain-openai/)
-   [langchain-openai source, ChatOpenAI base URL resolution (checked 2026-11-16)](https://github.com/langchain-ai/langchain/blob/master/libs/partners/openai/langchain_openai/chat_models/base.py)
-   [BroadNet Inference API reference](https://inference.broadnet.ai/docs?utm_source=broadrouter&utm_medium=referral&utm_campaign=guides-langchain-openai-base-url&utm_content=sources)
