> ## Documentation Index
> Fetch the complete documentation index at: https://docs.modellix.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Use Modellix LLM with LangChain

> Point LangChain ChatOpenAI at the Modellix LLM gateway with base_url, a Modellix API key, and provider/name model IDs.

Use [LangChain](https://www.langchain.com/) against the Modellix LLM gateway through the OpenAI-compatible Chat Completions API. Configure [`ChatOpenAI`](https://docs.langchain.com/oss/python/integrations/chat/openai) from `langchain-openai` with `base_url` set to `https://llm.modellix.ai/v1` — the same pattern as the [OpenAI SDK](/llm/sdk/openai-sdk).

<Note>
  This page covers **LLM chat** only (`https://llm.modellix.ai`). Modellix model IDs are `provider/name` (for example `openai/gpt-5.5`). Pass that full string as `model`. See [Models & Pricing](/llm/overview#models-and-pricing).

  The LLM gateway does **not** document an embeddings API. For RAG, use Modellix for generation and a separate embeddings provider, or keep embeddings on another OpenAI-compatible host.
</Note>

## Set Up LangChain

<Steps>
  <Step title="Install Packages">
    Python (recommended imports from `langchain-openai`):

    ```bash theme={null}
    pip install langchain langchain-openai
    ```

    TypeScript:

    ```bash theme={null}
    npm install @langchain/openai @langchain/core
    ```
  </Step>

  <Step title="Set Credentials">
    Create a Modellix API key in the [console](https://modellix.ai/console/api-key), then export:

    ```bash theme={null}
    export OPENAI_API_KEY="mdlx-xxxxxxxx"
    export OPENAI_BASE_URL="https://llm.modellix.ai/v1"
    ```

    | Setting           | Value                                         |
    | ----------------- | --------------------------------------------- |
    | `OPENAI_API_KEY`  | Modellix API Key (not an OpenAI platform key) |
    | `OPENAI_BASE_URL` | `https://llm.modellix.ai/v1` (include `/v1`)  |
    | Model             | Exact Modellix Model ID (`provider/name`)     |

    <Warning>
      Missing `/v1` on the base URL commonly causes 404s. Do not point LangChain at the media host (`https://api.modellix.ai`).
    </Warning>
  </Step>

  <Step title="Create ChatOpenAI">
    Prefer constructor args so the base URL is explicit in code. Environment variables work as a fallback when you omit `api_key` / `base_url`.

    <CodeGroup>
      ```python Python theme={null}
      from langchain_openai import ChatOpenAI

      llm = ChatOpenAI(
          model="openai/gpt-5.5",
          api_key="mdlx-xxxxxxxx",  # or rely on OPENAI_API_KEY
          base_url="https://llm.modellix.ai/v1",
          max_tokens=256,
      )

      response = llm.invoke("Introduce yourself in one sentence")
      print(response.content)
      ```

      ```typescript TypeScript theme={null}
      import { ChatOpenAI } from "@langchain/openai";

      const llm = new ChatOpenAI({
        model: "openai/gpt-5.5",
        apiKey: "mdlx-xxxxxxxx", // or rely on OPENAI_API_KEY
        configuration: {
          baseURL: "https://llm.modellix.ai/v1",
        },
        maxTokens: 256,
      });

      const response = await llm.invoke("Introduce yourself in one sentence");
      console.log(response.content);
      ```
    </CodeGroup>

    You can use the same `base_url` with other Modellix catalog IDs (for example `anthropic/claude-sonnet-5` or `google/gemini-3.6-flash`); traffic still goes through Chat Completions on `https://llm.modellix.ai/v1`.
  </Step>

  <Step title="Optional: Streaming and Fallbacks">
    Streaming:

    ```python theme={null}
    for chunk in llm.stream("Write a one-line haiku about APIs"):
        print(chunk.content, end="", flush=True)
    ```

    Multi-model fallback on the same gateway:

    ```python theme={null}
    primary = ChatOpenAI(
        model="openai/gpt-5.5",
        base_url="https://llm.modellix.ai/v1",
        api_key="mdlx-xxxxxxxx",
    )
    fallback = ChatOpenAI(
        model="google/gemini-3.6-flash",
        base_url="https://llm.modellix.ai/v1",
        api_key="mdlx-xxxxxxxx",
    )

    llm = primary.with_fallbacks([fallback])
    ```
  </Step>

  <Step title="Optional: ChatAnthropic (Messages)">
    For native Anthropic Messages instead of Chat Completions, use `langchain-anthropic` with the same host shape as the [Anthropic SDK](/llm/sdk/anthropic-sdk) (**no** `/v1` on the base URL):

    ```bash theme={null}
    pip install langchain-anthropic
    ```

    ```python theme={null}
    import os
    from langchain_anthropic import ChatAnthropic

    os.environ["ANTHROPIC_API_KEY"] = "mdlx-xxxxxxxx"
    os.environ["ANTHROPIC_BASE_URL"] = "https://llm.modellix.ai"

    llm = ChatAnthropic(
        model="anthropic/claude-sonnet-5",
        max_tokens=256,
    )
    ```

    Most LangChain apps should stay on `ChatOpenAI` + `/v1` unless you specifically need the Messages protocol.
  </Step>
</Steps>

## Troubleshooting

| Symptom                    | Check                                                                            |
| -------------------------- | -------------------------------------------------------------------------------- |
| 404 Not Found              | `base_url` / `OPENAI_BASE_URL` is `https://llm.modellix.ai/v1` (include `/v1`)   |
| 401 Unauthorized           | Key is a valid Modellix API key                                                  |
| Model not found            | Model is a full ID like `openai/gpt-5.5`, not bare `gpt-5.5`                     |
| Deprecated import warnings | Use `from langchain_openai import ChatOpenAI`, not `langchain.chat_models`       |
| Embeddings / RAG errors    | LLM gateway has no documented embeddings route — use another embeddings provider |

## Related

* [LangChain OpenAI-compatible providers](https://docs.langchain.com/oss/python/concepts/providers-and-models) — `ChatOpenAI` + `base_url`
* [OpenAI SDK](/llm/sdk/openai-sdk) — same `/v1` gateway without LangChain
* [Anthropic SDK](/llm/sdk/anthropic-sdk) — Messages protocol used by `ChatAnthropic`
* [Models & Pricing](/llm/overview#models-and-pricing) — Model IDs and rates
* [LLM API guide](/llm/api/api) — protocols and curl examples
* [Chat Completions](/llm/chat-completions) — OpenAPI reference
