# LangChain (/docs/guides/langchain)

Use cheapaimodel models in LangChain through the OpenAI-compatible API.



LangChain reaches OpenAI-compatible services through its OpenAI integration: `ChatOpenAI` from `langchain-openai` in Python and from `@langchain/openai` in TypeScript. Point it at our base URL and pass your cheapaimodel key.

|          | LangChain setting | Value                                                                             |
| -------- | ----------------- | --------------------------------------------------------------------------------- |
| Base URL | `base_url`        | `https://api.cheapaimodel.ai/v1`                                                  |
| API key  | `api_key`         | Your key from the console's [API Keys](https://console.cheapaimodel.ai/keys) page |
| Model ID | `model`           | `deepseek-v4-1-flash`, or any ID from the [catalog](/models)                      |

## Set it up [#set-it-up]

The table uses the Python names. In TypeScript they are `configuration.baseURL`, `apiKey` and `model`: the base URL goes inside `configuration`.

<Tabs items="[&#x22;Python&#x22;, &#x22;TypeScript&#x22;]">
  <Tab value="Python">
    The current `langchain-openai` needs Python 3.10 or later.

    ```bash title="Terminal"
    pip install -U langchain-openai
    ```

    ```python title="main.py"
    import os
    from langchain_openai import ChatOpenAI

    llm = ChatOpenAI(
        model="deepseek-v4-1-flash",
        base_url="https://api.cheapaimodel.ai/v1",
        api_key=os.environ["CHEAPAIMODEL_API_KEY"],
    )

    response = llm.invoke("Explain recursion in one sentence.")
    print(response.text)

    # Streaming
    for chunk in llm.stream("Write a haiku about the sea."):
        print(chunk.text, end="", flush=True)
    ```
  </Tab>

  <Tab value="TypeScript">
    ```bash title="Terminal"
    npm install @langchain/openai @langchain/core
    ```

    ```ts title="main.ts"
    import { ChatOpenAI } from "@langchain/openai";

    const llm = new ChatOpenAI({
      model: "deepseek-v4-1-flash",
      apiKey: process.env.CHEAPAIMODEL_API_KEY,
      configuration: {
        baseURL: "https://api.cheapaimodel.ai/v1",
      },
    });

    const response = await llm.invoke("Explain recursion in one sentence.");
    console.log(response.text);

    // Streaming
    const stream = await llm.stream("Write a haiku about the sea.");
    for await (const chunk of stream) {
      process.stdout.write(chunk.text);
    }
    ```
  </Tab>
</Tabs>

`ChatOpenAI` sends requests to `/v1/chat/completions`. Any model in the [catalog](/models) that lists that endpoint works in place of `deepseek-v4-1-flash`.

## With init\_chat\_model [#with-init_chat_model]

In Python, `init_chat_model` from the `langchain` package takes the same settings. Pass `model_provider="openai"`: without it, LangChain guesses the provider from the model ID and picks the wrong integration, or none.

```python title="main.py"
import os
from langchain.chat_models import init_chat_model

llm = init_chat_model(
    "deepseek-v4-1-flash",
    model_provider="openai",
    base_url="https://api.cheapaimodel.ai/v1",
    api_key=os.environ["CHEAPAIMODEL_API_KEY"],
)
```

## Embeddings [#embeddings]

cheapaimodel has no embeddings models yet. Keep `OpenAIEmbeddings`, or whichever embeddings class your retriever uses, on its own provider, and don't point it at our base URL.

## Common mistakes [#common-mistakes]

* **`'baseURL' does not exist in type 'ChatOpenAIFields'`** (TypeScript). `baseURL` belongs inside `configuration`. Plain JavaScript ignores the top-level field without an error, and the request goes to OpenAI with your cheapaimodel key.
* **`requires the langchain-deepseek package` or `Unable to infer model provider`** from `init_chat_model`. Add `model_provider="openai"`.
* **`503` with `model_not_found` on `/v1/embeddings`.** `OPENAI_BASE_URL` or `OPENAI_API_BASE` is exported with our base URL. `ChatOpenAI` reads it, but so does `OpenAIEmbeddings`, which then asks us for an embeddings model. Pass `base_url` in code instead of exporting it.
* **`<bound method BaseMessage.text ...>` is printed.** Python is older than 3.10, so pip installed the 0.3 line, where `text` is a method. Upgrade Python and reinstall.
* **`503` with `model_not_found` on `/v1/chat/completions`.** `model` is missing, so LangChain sends its OpenAI default `gpt-3.5-turbo`, or the ID isn't in the catalog. Set `model` to an ID from the [catalog](/models).

For base URL mistakes such as a missing `/v1`, see [Base URL](/docs/api/base-url#common-mistakes).
