Connect LlamaIndex to an AI API from Iran
LlamaIndex is a Python framework for RAG: it splits documents, embeds them, indexes them and answers questions about them with a language model. You put the chat model on 1xAi with OpenAILike and the embedding model with OpenAIEmbedding, both with one 1xai-* key and a Toman bill.
Setting up LlamaIndex
- [01]
Create an account and a 1xAi key. Register at 1xai.ir (the app is in Persian) and confirm your email or mobile number. You spin the sign-up gift wheel (from 100,000 to 1,000,000 Toman of gift credit) once, which is enough to test the connection. Then create a 1xai-* key in the dashboard and copy it at once: a key is shown only once.
- [02]
Install the packages. Run pip install -U llama-index llama-index-llms-openai-like llama-index-embeddings-openai.
- [03]
Put the key in an environment variable. Export ONEXAI_API_KEY with your 1xai-* key, so that the key is not written into the code.
- [04]
Set the chat model with OpenAILike. Create OpenAILike with model set to the model name, api_base set to https://1xai.ir/v1 and api_key from the environment variable, and set is_chat_model=True; give context_window the size of the model's context window as well.
- [05]
Set the embedding model. Create OpenAIEmbedding with model set to text-embedding-3-small and the same api_base and api_key, and put both in Settings so that every index uses them.
- [06]
Run the example. Run python rag.py: the document is indexed and the question answered. The embedding and the answer are separate calls, and the Toman cost of each is recorded in the 1xAi dashboard.
Ready to copy
# pip install -U llama-index llama-index-llms-openai-like llama-index-embeddings-openai
# https://developers.llamaindex.ai/python/framework-api-reference/llms/openai_like/
import os
from llama_index.core import Document, Settings, VectorStoreIndex
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.llms.openai_like import OpenAILike
KEY = os.environ["ONEXAI_API_KEY"]
BASE = "https://1xai.ir/v1"
Settings.llm = OpenAILike(
model="claude-sonnet-5",
api_base=BASE,
api_key=KEY,
is_chat_model=True,
is_function_calling_model=True,
context_window=200000,
)
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small", api_base=BASE, api_key=KEY)
docs = [Document(text="1xAi bills each API call in Toman from a prepaid wallet.")]
index = VectorStoreIndex.from_documents(docs)
print(index.as_query_engine().query("How is an API call billed?"))from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
# Settings.llm and Settings.embed_model as in rag.py
docs = SimpleDirectoryReader("./docs").load_data()
index = VectorStoreIndex.from_documents(docs)
index.storage_context.persist("./storage") # embed once, reuse later
chat = index.as_chat_engine()
print(chat.chat("What do these documents say about pricing?"))Recommended model
Recommended: claude-sonnet-5. A cheaper alternative: gpt-5-mini. For precise answers over documents the recommended model works well; for simple, numerous questions the alternative is cheaper. For embeddings, text-embedding-3-small is both inexpensive and enough for most documents. Every model and its Toman price is on the models page.
What it costs
Each call is billed at the model's Toman price: the vendor's list price plus a markup of 15–30% depending on the model, at the day's dollar rate (refreshed every day at 9 in the morning, Tehran time). RAG costs in two parts: embedding the documents once, when they are indexed, and each question, which is the question's own embedding plus a chat-model call with the pieces found. Save the index with persist, so that the documents are not embedded again every time. Estimate with the calculator (in Persian).
Common errors
- Error: Unknown model ... Please provide a valid OpenAI model name
- That is the error of LlamaIndex's own OpenAI class, which checks the model name against its list of OpenAI models. For Claude, Gemini and the other models use OpenAILike, which has no such check.
- An answer is cut short, or the prompt is shortened
- Without context_window, OpenAILike assumes a small context window and shortens the prompt. Give context_window the model's real size from the models page, and do not forget is_chat_model=True; without it the request goes to the old completions route.
- 401: invalid key
- The key is wrong, has expired, or you revoked it. Copy it again from the dashboard or create a new one, with no space before or after it.
- 402: insufficient balance
- The account's Toman credit is used up. A wallet top-up by the Zarinpal gateway or the Digipay gateway or card-to-card transfer with a receipt; calls work again as soon as a payment succeeds.
- Model name or routing error
- 1xAi routes by model name: claude-* to Anthropic, gemini-* to Google, deepseek-* to DeepSeek; grok-*, kimi-*, glm-*, qwen*, MiniMax-* and hy* reach the Grok, Kimi, GLM, Qwen, MiniMax and Hunyuan models; every other name goes to OpenAI. Write a model id exactly as the model list shows it.
Every error code is in the error reference.
Why OpenAILike and not the OpenAI class?
LlamaIndex's OpenAI class knows only OpenAI's model names and fails on the others. OpenAILike calls the same protocol without that check, so claude-*, gemini-* and deepseek-* models work with the same key and address.
Is the text of my documents stored anywhere?
The index and the vectors stay on your own machine or database. To build embeddings and answers, pieces of the text are sent to 1xAi and from there to the vendor of the model. Next to each usage record, the start of the request and of the response (up to 64 KB each) and, when it is longer, its last 16 KB are kept so that you can read them in your own usage report. That text is erased after 30 days. The numbers (model, tokens, cost, status and time) stay for accounting.
Other guides: Cursor, Claude Code, Codex CLI, n8n, Dify, Cline, Open WebUI, LibreChat, Continue, LangChain, Vercel AI SDK, LiteLLM.
1xAi is an independent gateway. It is not affiliated with, endorsed by, or a reseller for OpenAI, Anthropic, Google, DeepSeek, xAI, Moonshot AI, Z.ai, Alibaba Qwen, MiniMax or Tencent Hunyuan. Requests are relayed upstream rather than answered by models of our own, so each provider's own usage policies apply to what their models will and will not do, and their model availability is theirs to change. Current reachability is published on the status page.