LangChain Integration

LangChain connects to KOPRouter through the OpenAI-compatible ChatOpenAI — swap the Base URL and API key.

**Prerequisites**
  • LangChain installed
  • A KOPRouter API key (prefixed with sk-)

Python

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    base_url="https://koprouter.kopglobal.com/v1",
    api_key="sk-...",
    model="MODEL_ID",
)

resp = llm.invoke("Hello!")
print(resp.content)

Streaming

for chunk in llm.stream("Introduce yourself"):
    print(chunk.content, end="", flush=True)
The Base URL must be `/v1`; `MODEL_ID` is the `id` returned by `/v1/models`. **Can't connect (network restricted)?**

If your network cannot reach KOPRouter directly (e.g. mainland China), configure a VPN or proxy first. LangChain (Python) reads the HTTPS_PROXY / HTTP_PROXY environment variables — see Network & Proxy for how to set them.