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

# LangChain

> How to connect LangChain to RuAPI via ChatOpenAI with a custom base_url — models and embeddings in Python and JS

[LangChain](https://www.langchain.com) is a framework for LLM apps — chains, agents, RAG. Since RuAPI is OpenAI-compatible, connecting comes down to the **`ChatOpenAI`** class with a changed `base_url` — no separate integration needed.

## Prerequisites

<Steps>
  <Step title="RuAPI account and API key">
    Console → **Tokens** → create an `sk-...` key. The balance tops up in USDT — see [Top-up](/en/topup).
  </Step>
</Steps>

## Python

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

```python theme={null}
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="claude-opus-4-8",                  # exact name on the pricing page
    api_key="sk-YOUR_KEY",
    base_url="https://www.ruapi.ai/v1",  # with /v1
)

print(llm.invoke("Hi! Answer in one sentence.").content)
```

From here `llm` drops into any chain, agent or LCEL construct like a regular model.

## JavaScript / TypeScript

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

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

const llm = new ChatOpenAI({
  model: "claude-opus-4-8",
  apiKey: "sk-YOUR_KEY",
  configuration: { baseURL: "https://www.ruapi.ai/v1" },
});

const res = await llm.invoke("Hi! Answer in one sentence.");
console.log(res.content);
```

<Tip>
  To switch models, change the `model` field. The same key reaches Claude, GPT, Gemini, DeepSeek and more.
</Tip>

## Embeddings (RAG)

```python theme={null}
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(
    model="text-embedding-3-small",       # or another embedding model from the catalog
    api_key="sk-YOUR_KEY",
    base_url="https://www.ruapi.ai/v1",
)
```

## Troubleshooting

<AccordionGroup>
  <Accordion title="401 / Unauthorized">
    `api_key` must be a RuAPI token (`sk-...`). Check for stray spaces.
  </Accordion>

  <Accordion title="404 / model not found">
    The name in `model` must match RuAPI verbatim — see the [pricing page](https://www.ruapi.ai/pricing).
  </Accordion>

  <Accordion title="Do I need ChatAnthropic for Claude?">
    No. Claude is called through `ChatOpenAI` on the same `base_url` — just put the Claude model ID in the `model` field.
  </Accordion>
</AccordionGroup>
