LlamaIndex
Use cheapaimodel models in LlamaIndex through the OpenAI-compatible API.
LlamaIndex reaches OpenAI-compatible services through OpenAILike from the llama-index-llms-openai-like package. It can't look up a model's details from an ID it doesn't know, so you set two of them yourself: that the model is a chat model, and its context window.
| LlamaIndex setting | Value | |
|---|---|---|
| Base URL | api_base | 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 |
| Chat model | is_chat_model | True |
| Context window | context_window | The model's context length in tokens, from its page in the catalog |
Set it up
The current LlamaIndex packages need Python 3.10 or later.
pip install llama-index-llms-openai-likeLook up the context length on the model's page, for example deepseek-v4-1-flash, and put it in CONTEXT_WINDOW:
import os
from llama_index.core.llms import ChatMessage
from llama_index.llms.openai_like import OpenAILike
CONTEXT_WINDOW = ... # the context length from the model's page, in tokens
llm = OpenAILike(
model="deepseek-v4-1-flash",
api_base="https://api.cheapaimodel.ai/v1",
api_key=os.environ["CHEAPAIMODEL_API_KEY"],
is_chat_model=True,
context_window=CONTEXT_WINDOW,
)
response = llm.complete("Explain recursion in one sentence.")
print(response.text)
# Streaming
messages = [ChatMessage(role="user", content="Write a haiku about the sea.")]
for chunk in llm.stream_chat(messages):
print(chunk.delta, end="", flush=True)With is_chat_model=True, every call, complete included, goes to /v1/chat/completions.
Use it across LlamaIndex
Set it on Settings to make it the default for indexes, query engines and chat engines:
from llama_index.core import Settings
Settings.llm = llmVector indexes also need an embedding model, and Settings.embed_model defaults to OpenAI's. cheapaimodel has no embeddings models yet, so set Settings.embed_model to another provider's embedding model or a local one. Don't point it at our base URL.
Common mistakes
- Requests go to
/v1/completions.is_chat_modeldefaults toFalse, which sendscompleteand evenchatto the legacy completions endpoint as a single prompt. Our models list only/v1/chat/completionsand/v1/responses. Setis_chat_model=True. ValueError: Unknown model 'deepseek-v4-1-flash'. Please provide a valid OpenAI model name. The LLM isOpenAIfromllama_index.llms.openai, which accepts only OpenAI's own model names. UseOpenAILike.Could not load OpenAI embedding modelwhen building an index.Settings.embed_modelis still the OpenAI default. Set it to another provider's or a local embedding model.503withmodel_not_foundon/v1/embeddings. Same cause, withOPENAI_API_KEYandOPENAI_API_BASEset to your cheapaimodel key and our base URL: the default embedding model reads them and asks us for an embeddings model. Passapi_keyandapi_basetoOpenAILikeinstead, and setSettings.embed_model.400fromkimi-k3.OpenAILikealways sendstemperature,0.1by default, and kimi-k3 accepts only1or0.6. Passtemperature=1.0. See Sampling limits.- One query makes several requests.
context_windowdefaults to 3900 tokens, and LlamaIndex splits retrieved text to fit, so a query engine sends more requests than it needs to. Set it to the model's context length.
For base URL mistakes such as a missing /v1, see Base URL.