# LangChain

> Connecting LangChain in Python and JS through ChatOpenAI with the gateway base URL.

LangChain connects through the standard `ChatOpenAI` class — set the gateway
base URL, your key and a model ID.

## Quick start

```bash [Python]
pip install langchain-openai
```

```bash [JS]
npm install @langchain/openai @langchain/core
```

```python [Python]
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    base_url="https://llmbox.cc/v1",
    api_key="$LLMBOX_API_KEY",
    model="openai/gpt-5.6-sol",
)
print(llm.invoke("Hello!").content)
```

```ts [JS]
import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
  model: "openai/gpt-5.6-sol",
  apiKey: "$LLMBOX_API_KEY",
  configuration: { baseURL: "https://llmbox.cc/v1" },
});
console.log((await llm.invoke("Hello!")).text);
```

Take the model ID from the [model catalog](https://llmbox.cc/en/models); switching models means
changing the `model` string, the rest of your chains and agents stays the same.

## Streaming and tools

`llm.stream(...)` returns the answer in chunks, and `llm.bind_tools([...])`
(`bindTools` in JS) enables function calling — the gateway passes your tools to
the model as is, and `tool_calls` come back in the usual LangChain shape.

## How it works

- `ChatOpenAI` sends requests to `/v1/chat/completions`; recent versions of
  `@langchain/openai` switch some models to
  [`/v1/responses`](https://llmbox.cc/en/docs/responses) on their own — the gateway supports
  both.
- Usage and tokens of every request show up in the cabinet.
- `OpenAIEmbeddings` calls [`/v1/embeddings`](https://llmbox.cc/en/docs/embeddings) with an
  embedding model from the catalog. For models outside the OpenAI family, pass
  `check_embedding_ctx_length=False` so LangChain sends text rather than
  OpenAI vocabulary tokens.

## Troubleshooting

| Symptom | Cause and fix |
| --- | --- |
| `404 model_not_found` | The model ID has a typo — check `GET /v1/models`. |
| `401 invalid_api_key` | Wrong or revoked key. |
| `404 model_not_found` on embeddings | `OpenAIEmbeddings` is set to a text model — use an embedding model from the catalog. |
