cheapaimodel

LangChain

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

Copy

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 settingValue
Base URLbase_urlhttps://api.cheapaimodel.ai/v1
API keyapi_keyYour key from the console's API Keys page
Model IDmodeldeepseek-v4-1-flash, or any ID from the catalog

Set it up

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

The current langchain-openai needs Python 3.10 or later.

Terminal
pip install -U langchain-openai
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)

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

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.

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

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

  • '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.

For base URL mistakes such as a missing /v1, see Base URL.