onexai — Python SDK for the 1xAi gateway
1xAi is an OpenAI-compatible AI gateway for Iran: one key and one base URL for OpenAI, Anthropic Claude, Google Gemini and DeepSeek — no VPN, no foreign card, billed in Toman.
pip install 1xai-sdk
from onexai import OneXAI, cost_of_response
client = OneXAI() # reads ONEXAI_API_KEY, falls back to OPENAI_API_KEY
reply = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "سلام! یک جوک فارسی بگو"}],
)
print(reply.choices[0].message.content)
print(cost_of_response(reply)) # -> 102 Toman (gpt-4o-mini, 14in/23out)
Why this package is thin (and why that is the point)
1xAi's whole claim is that it is the OpenAI API at a different base URL. So this
package does not re-implement an SDK — it depends on the official openai package and
pre-configures it. Writing our own HTTP, streaming, retry and tool-call layer would
mean shipping a worse copy of a client OpenAI already maintains, and it would drift
every time the API gains a field.
You do not strictly need this package at all:
from openai import OpenAI
client = OpenAI(api_key="1xai-...", base_url="https://1xai.ir/v1")
That is the entire integration. What onexai adds is the small set of things the
generic SDK cannot know about a gateway in Iran:
| Toman cost | estimate_cost() / cost_of_response() priced from 1xAi's public catalogue |
| Real errors | the gateway answers in Persian; explain() turns that into one actionable English sentence — including 402, which on this gateway means your wallet is empty, not your code is broken |
| Key validation | pasting an sk-... key fails loudly at construction instead of as a confusing 401 later |
| Native passthroughs | anthropic_client() and gemini_client() for prompt caching, thinking budgets and cachedContents |
onexai doctor |
one command that tells you which of key / network / credit / upstream is actually broken |
CLI
onexai doctor # diagnose key, connectivity, credit, upstream health
onexai price gpt-4o-mini 1000 500 # Toman cost of a call
onexai models --provider anthropic # what you can call, with prices
onexai status # live upstream health, as seen from Iran
Cost estimation
Prices come from https://1xai.ir/api/models — public, no key required. A snapshot
ships with the package so the helper works offline; pass live=True to fetch current
prices.
from onexai import estimate_cost
e = estimate_cost("claude-opus-4-5", input_tokens=50_000, output_tokens=2_000)
print(e.total_toman) # 66,067.0
print(e.provider) # anthropic
Audio and image endpoints are billed per second and per image, so they are not in
the token catalogue. estimate_cost("whisper-1", ...) raises UnknownModelError
explaining that, rather than quietly returning zero.
Native passthroughs
The OpenAI-compatible path silently drops provider-specific features. When you need them, use the native endpoints:
from onexai import anthropic_client # pip install '1xai-sdk[anthropic]
claude = anthropic_client() # -> https://1xai.ir/anthropic
msg = claude.messages.create(
model="claude-opus-4-5",
max_tokens=1024,
system=[{"type": "text", "text": BIG_DOC,
"cache_control": {"type": "ephemeral"}}], # dropped on /v1
messages=[{"role": "user", "content": "خلاصه کن"}],
)
print(msg.usage.cache_read_input_tokens)
Full cookbook
Runnable recipes for Cursor, Continue, Cline, Open WebUI, n8n, streaming, Persian Whisper transcription, image generation and cost control live in the repository README.
License
MIT. Not an official 1xAi product unless the 1xAi team adopts it; see the repository.
Release files for 1xai-sdk 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| 1xai_sdk-0.1.0.tar.gz | 20.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 1xai_sdk-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 41.3 kB
Release files / 1xai_sdk-0.1.0.tar.gz
| Download URL | 1xai_sdk-0.1.0.tar.gz |
|---|---|
| Size | 20.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eec223d37fadf95c0b6ef801aecb2f83386951589ca5a74a7fc73a6f30414c60
|
|
BLAKE2b-256 checksum How to use checksums |
a8dc2d7e235c03274e49585b012f82417825966455399c5bf85dfb6fb94bf037
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Release files / 1xai_sdk-0.1.0-py3-none-any.whl
| Download URL | 1xai_sdk-0.1.0-py3-none-any.whl |
|---|---|
| Size | 20.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6fa351a0c1ee00de9c527c9308a95de01701a4be3dcf0d5f8c1dbea0ba4d19bc
|
|
BLAKE2b-256 checksum How to use checksums |
f04cc7242bd6d2369ca54c12a6a82bff36f01d86e54c46ee5f899664fa6d112e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|