# LangChain and LlamaIndex with open-weight models

> Use open-weight LLMs in LangChain and LlamaIndex: ChatOpenAI with a base_url, tool binding, LangChain.js and OpenAILike. Pay per token in crypto.

- URL: https://llmwithcrypto.com/docs/langchain/
- Last updated: 2026-10-23
- Publisher: LLMwithCrypto (https://llmwithcrypto.com)

LangChain and LlamaIndex both ship an OpenAI chat model that accepts a custom base URL. Point it at our API and every chain, agent and retriever you built keeps working, now running on DeepSeek, Qwen, gpt-oss and the other open-weight models.

**Quick answer — How do I use LLMwithCrypto in LangChain?**

Install `langchain-openai` and create `ChatOpenAI(model="deepseek-v4-pro", base_url="https://llmwithcrypto.com/api/v1", api_key=…)`. **LLMwithCrypto** speaks the OpenAI chat format, so `invoke`, streaming and `bind_tools` work as with OpenAI, on the models that support tools. LlamaIndex uses `OpenAILike` with the same base URL. Documented on 23 October 2026.

## Which class to use

| Attribute | Value |
| --- | --- |
| LangChain (Python) | `ChatOpenAI` from `langchain-openai`, with `base_url` |
| LangChain.js | `ChatOpenAI` from `@langchain/openai`, with `configuration.baseURL` |
| LlamaIndex | `OpenAILike` from `llama-index-llms-openai-like`, with `api_base` and `is_chat_model=True` |
| Model ids | On the [models page](https://llmwithcrypto.com/models/) |

## How do I set up ChatOpenAI?

Pass the base URL and the key when you create the model. Setting `max_tokens` keeps every step of a chain or an agent from writing more than you want to pay for; the reply’s `usage_metadata` shows the tokens of each call.

*LangChain: ChatOpenAI on DeepSeek V4 Pro*

```python
import os

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="deepseek-v4-pro",
    base_url="https://llmwithcrypto.com/api/v1",
    api_key=os.environ["LLMWITHCRYPTO_API_KEY"],
    max_tokens=300,
    temperature=0.3,
)
reply = llm.invoke("Explain what a vector database is in two sentences.")
print(reply.content)
print(reply.usage_metadata)

```

## Do tools and agents work?

Yes, on the models that support tool calling. Streaming works on every model; tool calling and JSON output follow what each model itself supports. `bind_tools` sends your tools in the OpenAI format; test your schema on the model you pick before you depend on it. LangGraph agents built on `ChatOpenAI` work the same way.

*LangChain: a tool bound to gpt-oss-120b*

```python
import os

from langchain_core.tools import tool
from langchain_openai import ChatOpenAI


@tool
def convert_currency(amount: float, rate: float) -> float:
    """Convert an amount with an exchange rate."""
    return round(amount * rate, 2)


llm = ChatOpenAI(
    model="gpt-oss-120b",
    base_url="https://llmwithcrypto.com/api/v1",
    api_key=os.environ["LLMWITHCRYPTO_API_KEY"],
)
llm_with_tools = llm.bind_tools([convert_currency])
msg = llm_with_tools.invoke("How much is 250 at a rate of 0.92?")
for call in msg.tool_calls:
    print(call["name"], call["args"], "->", convert_currency.invoke(call["args"]))

```

## And from JavaScript?

LangChain.js takes the base URL inside `configuration`, which it passes to the OpenAI client underneath.

*LangChain.js: ChatOpenAI with a baseURL*

```javascript
import { ChatOpenAI } from '@langchain/openai';

const llm = new ChatOpenAI({
  model: 'qwen3-235b-a22b-instruct-2507',
  apiKey: process.env.LLMWITHCRYPTO_API_KEY,
  configuration: { baseURL: 'https://llmwithcrypto.com/api/v1' },
  maxTokens: 300,
});
const reply = await llm.invoke('Give me a three-step plan to learn SQL.');
console.log(reply.content);

```

## How do I use it in LlamaIndex?

Use `OpenAILike`, which does not check model names against OpenAI’s list. Set `is_chat_model=True` so it sends chat messages, and give the model’s context window so LlamaIndex can size its prompts: DeepSeek V4.1 Flash reads up to 1M tokens as of 23 October 2026, the example sets a smaller window on purpose to keep prompts short.

*LlamaIndex: OpenAILike on DeepSeek V4.1 Flash*

```python
import os

from llama_index.llms.openai_like import OpenAILike

llm = OpenAILike(
    model="deepseek-v4.1-flash",
    api_base="https://llmwithcrypto.com/api/v1",
    api_key=os.environ["LLMWITHCRYPTO_API_KEY"],
    is_chat_model=True,
    context_window=131072,
    max_tokens=300,
)
print(llm.complete("Name three uses of a knowledge graph."))

```

## How do I keep agent costs under control?

An agent calls the model many times per task: once to plan, once per tool result, once to answer. Each call resends the whole conversation, so the input grows at every step.

- **Cap each call** with `max_tokens`, and the whole run with a maximum number of steps.
- **Use a small model where it is enough.** As of 23 October 2026, gpt-oss-20b costs $0.033 per million input tokens against $1.254 for DeepSeek V4 Pro: route simple steps to the small one and keep the large one for the final answer.
- **Trim the history** in long conversations, or summarize old turns.
- **Watch the credit** with `GET https://llmwithcrypto.com/api/v1/credits` before large runs.

The dashboard lists every call with its tokens and its cost, which shows quickly which step of a chain is the expensive one.

## Frameworks: common questions

### Can I mix models in one chain?

Yes: create one `ChatOpenAI` per model, for example a cheap one for routing and a large one for the final answer. All of them spend the same credit.

### Do embeddings work?

No. The API serves chat completions only; use your existing embedding model alongside it.

### What do I pay for an agent run?

Every model call of the run, per token. You pay per token from your balance: input and output tokens are counted by the model’s own tokenizer and charged at that model’s price per 1M tokens. A call that ends in an error costs nothing. Prices per model are on the [pricing page](https://llmwithcrypto.com/pricing/).

---

LLMwithCrypto: LLMwithCrypto is an OpenAI-compatible LLM API for open-weight models — DeepSeek V4 Pro, DeepSeek V4.1 Flash, DeepSeek R1, Qwen3.5 397B A17B, Qwen3 235B A22B Instruct, gpt-oss-120b, gpt-oss-20b, GLM-4.7, Llama 3.3 70B Instruct, Gemma 4 31B, Mistral Large 3, Mistral Small 4 — paid per token from a prepaid crypto balance, at 5% below the median price of the public hosts. No KYC: no ID, no selfie, no documents. Prompts and outputs are not stored.
