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 page |
| Model ID | model | deepseek-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.
pip install -U langchain-openaiimport 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.
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).baseURLbelongs insideconfiguration. Plain JavaScript ignores the top-level field without an error, and the request goes to OpenAI with your cheapaimodel key.requires the langchain-deepseek packageorUnable to infer model providerfrominit_chat_model. Addmodel_provider="openai".503withmodel_not_foundon/v1/embeddings.OPENAI_BASE_URLorOPENAI_API_BASEis exported with our base URL.ChatOpenAIreads it, but so doesOpenAIEmbeddings, which then asks us for an embeddings model. Passbase_urlin code instead of exporting it.<bound method BaseMessage.text ...>is printed. Python is older than 3.10, so pip installed the 0.3 line, wheretextis a method. Upgrade Python and reinstall.503withmodel_not_foundon/v1/chat/completions.modelis missing, so LangChain sends its OpenAI defaultgpt-3.5-turbo, or the ID isn't in the catalog. Setmodelto an ID from the catalog.
For base URL mistakes such as a missing /v1, see Base URL.