What is LangChain?
LangChain is an application framework for building LLM-powered chat, retrieval, workflow, and agent experiences.
This LangChain guide configures the maintained langchain-openai integration with a TokenHub-compatible API base. Test the basic invoke path first, then validate streaming, structured output, or tools for the capabilities your app needs. A minimal ChatOpenAI invoke verifies basic connectivity but does not validate every feature used by a production application. Test streaming, structured output, tool binding, retries, and timeouts independently, then add retrieval, chains, or agents only after the selected TokenHub model passes the capabilities your workflow requires.
LangChain's official Python integration for OpenAI lives in the separate langchain-openai package. Connect TokenHub by passing a custom base_url to ChatOpenAI. This is a code integration; there is no App settings screen.
Step 1: install the official integration package
pip install -U langchain-openaiFor a project managed by uv, use uv add langchain-openai. Installing the base langchain package alone does not provide ChatOpenAI.
Step 2: instantiate ChatOpenAI explicitly for TokenHub
export TOKENHUB_API_KEY="sk-..."import os
from langchain_openai import ChatOpenAI
model = ChatOpenAI(
base_url="__API_BASE_URL__/v1",
api_key=os.environ["TOKENHUB_API_KEY"],
model="YOUR_TOKENHUB_MODEL_ID",
timeout=60,
max_retries=2,
)
response = model.invoke("Reply with one sentence to confirm the connection.")
print(response.content)Base URL resolution favors the constructor's base_url / openai_api_base, then OPENAI_API_BASE, followed by the underlying SDK's OPENAI_BASE_URL. Passing it explicitly prevents an old OpenAI URL in the project environment from overriding TokenHub.
Step 3: verify one capability at a time
- Run the minimum
invokecall to confirm normal Chat Completions first. - Add
streamonly when basic invocation works. WhenOPENAI_BASE_URLpoints to a third-party endpoint, LangChain does not automatically enablestream_usage; setstream_usage=Trueonly if the TokenHub model and endpoint support streaming usage. - For an agent, bind one minimal tool with
bind_toolsand inspecttool_callson the returned object. Do not treat text that merely describes a call as a tool call. - Test structured output, images, and reasoning only after those layers work.
ChatOpenAI explicitly targets fields in the OpenAI API specification. Provider-specific extensions such as reasoning_content, reasoning, or reasoning_details may not be extracted or preserved. A successful text response does not prove that those extensions work.
Troubleshoot at the LangChain layer that failed
- Import fails: confirm
langchain-openaiis installed. - 404: inspect the effective
base_url, including/v1, and check for environment-variable overrides. - Model error: use the complete TokenHub model ID in
model. - Streaming text works but usage is absent: inspect
stream_usageand whether the endpoint supportsstream_options.include_usage. - Agent tool fails: isolate it with
bind_tools; a successful basicinvokeis not a tool test. - Non-standard reasoning field is absent: use the provider-specific LangChain integration or process the raw response directly.
Official reference
Choose a compatible LangChain model
LangChain ChatOpenAI FAQ
How do I point ChatOpenAI at TokenHub?
Set base_url to the TokenHub API root ending in /v1, pass the TokenHub key as api_key, and set model to the exact TokenHub ID.
Can I use OPENAI_API_BASE instead of base_url?
LangChain can read documented OpenAI base URL environment variables, but an explicit base_url is clearest for a single integration.
Does a successful invoke call prove an agent will work?
No. It proves basic chat connectivity. Validate tool calling, streaming, and structured output before using those features.
Do I need the langchain-openai package?
Yes. ChatOpenAI is maintained in the langchain-openai integration package; install a version compatible with the rest of your LangChain project.
How do I troubleshoot a LangChain 404?
Ensure base_url ends in /v1, remove duplicated request paths, and verify the exact model ID before checking TokenHub request logs.
References
This setup is based on TokenHub and official documentation, last verified on 2026-09-08.