LlamaIndex with an OpenAI-Compatible API (OpenAILike)
LlamaIndex has more than one way to reach an OpenAI-style API. This guide helps you pick the right class, set the flags that change behaviour, and avoid the embeddings default that catches most people.
LlamaIndex is a Python framework for building applications on top of your own data: indexes, retrieval, query engines and agents. For the language model part it can use any OpenAI-compatible API, including FreeModel. The question is which LlamaIndex class to use, because there is more than one and they behave differently.
Decision: which class?
| Class | Package | Use it when |
|---|---|---|
OpenAILike | llama-index-llms-openai-like | You call a non-OpenAI service in the OpenAI format. This is the one for FreeModel. |
OpenAI | llama-index-llms-openai | You call OpenAI itself with OpenAI's model names. |
| Anthropic class | llama-index-llms-anthropic | You want the Anthropic message format. Not needed for FreeModel unless you prefer it. |
Why not the plain OpenAI class with a different api_base? It is built around OpenAI's own model catalogue and uses the model name to look up things like context size. An id it does not recognise, such as gpt-6-luna or glm-5.3-flash, can be rejected or treated with the wrong assumptions. OpenAILike accepts any id and lets you state the model's properties yourself.
Setup
pip install -U llama-index-core llama-index-llms-openai-like
export FREEMODEL_API_KEY=fm_your_key_here
import os
from llama_index.llms.openai_like import OpenAILike
llm = OpenAILike(
model="gpt-6-luna",
api_base="https://freemodel.in/v1",
api_key=os.environ["FREEMODEL_API_KEY"],
is_chat_model=True,
is_function_calling_model=True,
context_window=1000000,
)
print(llm.complete("Name three uses of a hash map."))
Decision: which flags?
OpenAILike cannot ask the API what a model can do, so you tell it. Each flag changes LlamaIndex behaviour:
| Flag | Set it to | What it changes |
|---|---|---|
is_chat_model | True | Uses /v1/chat/completions. FreeModel has no legacy /v1/completions, so this must be True. |
is_function_calling_model | True if the model has Tools | Lets agents and structured tools use native tool calls |
context_window | the model's published context | How much text LlamaIndex packs into a prompt |
max_tokens | optional | Cap on output length |
Context sizes are on each model's page: GPT-6 Luna lists 1M. If a model does not publish one, choose a conservative value; the library default is small, which is safe but wastes the model's real capacity. Capabilities for every model are on the models page.
Chat and streaming
from llama_index.core.llms import ChatMessage
messages = [
ChatMessage(role="system", content="You answer in one short paragraph."),
ChatMessage(role="user", content="What is retrieval-augmented generation?"),
]
print(llm.chat(messages).message.content)
for part in llm.stream_complete("Count from one to five."):
print(part.delta, end="", flush=True)
To make this the default model for every LlamaIndex component:
from llama_index.core import Settings
Settings.llm = llm
An agent with a tool
FunctionAgent uses native tool calling, which is why is_function_calling_model=True matters. Plain Python functions with type hints and a docstring become tools:
import asyncio
from llama_index.core.agent.workflow import FunctionAgent
def multiply(a: float, b: float) -> float:
"""Multiply two numbers and return the result."""
return a * b
agent = FunctionAgent(
tools=[multiply],
llm=llm,
system_prompt="You are a careful assistant. Use tools for arithmetic.",
)
async def main():
result = await agent.run("What is 1234 times 5678?")
print(str(result))
asyncio.run(main())
Each tool step is a separate request, and every request resends the conversation so far. Input and output tokens both count toward your plan.
The embeddings trap
This is the most common surprise. Settings.llm only sets the language model. Indexes also need an embedding model, set separately with Settings.embed_model. If you leave it unset, LlamaIndex may fall back to its default embedding provider, which is not FreeModel, and fail with an authentication error that seems to come from nowhere.
So decide explicitly. This guide uses FreeModel for the language model side only. Pick an embedding model you run locally or already use elsewhere, install its LlamaIndex integration, and assign it in code:
from llama_index.core import Settings
Settings.llm = llm
Settings.embed_model = my_embedding_model # the embedding integration you chose
Setting both in one place means nothing depends on a hidden default, and a missing embedding setup fails loudly at start-up instead of halfway through indexing.
Keeping retrieval prompts small
A query engine sends the retrieved chunks to the language model on every question, so the number of chunks drives your token use more than anything else. Start with a small similarity_top_k and raise it only if answers miss context. Input tokens count just like output tokens.
Claude and other paid models
Swap the model id and keep everything else: model="claude-sonnet-5". Claude Haiku 5.5 and GPT-6.2 Astra are on the free plan; the other Claude models, GPT-6 Astra, Gemini and the other premium models need a paid plan; a free key gets 403 upgrade_required. Plans are prepaid for 30 days, from $1.20, and every paid plan reaches every model. Each model uses plan tokens at its own rate (Claude Sonnet 5 at 3×); see pricing.
Troubleshooting
- 401 — the key did not reach the request. Check
api_keyand the environment variable. - 404
model_not_found—modelis not an exact FreeModel id. - 404 on every call —
api_basemust end at/v1, oris_chat_modelisFalse. - Agent error about function calling — set
is_function_calling_model=Trueand use a Tools model. - 402 — tokens used up (
tokens_exhausted); nothing is charged for the rejected request. - 429 — per-key rate limit; lower concurrency.
FAQ
Why not use the regular LlamaIndex OpenAI class?
It is built around OpenAI's own model names and may reject or misjudge ids it does not know. OpenAILike accepts any model id and lets you state its capabilities yourself.
Why does my FunctionAgent refuse to start?
FunctionAgent needs an LLM that reports function calling support. Set is_function_calling_model=True on OpenAILike and use a model with the Tools capability.
Does setting Settings.llm also change embeddings?
No. Settings.embed_model is separate. If you do not set it, LlamaIndex may try its default embedding provider, which is not FreeModel.
Can I use Claude models this way?
Yes: set model to a Claude id such as claude-sonnet-5 on a paid plan. Free accounts can use the free models, for example claude-haiku-5.5 or gpt-6-luna.
Grab a free API key — 1M tokens on 8 free models after a one-time Telegram verification, no card. Every model from $1.20.