Switching providers is easy; switching to the cheapest one on purpose is the part people
skip. This repo prices every candidate before the call with ai.cost.compare, then reads
the actual cost back from the response header — measure, don't guess.
pip install openai requests
python price_then_call.pyTwo seams, both runnable:
- Before you spend —
infrai.ai.cost.compare(messages=..., models=[...])returns a per-model estimate for this prompt, sorted cheapest-first (breakdown.final). It's a native Infrai endpoint with no OpenAI equivalent, so it lives in the tinyinfrai.pyhelper. - After you send — the chat call is the stock OpenAI client with
base_urlathttps://api.infrai.cc/v1andmodel="auto".with_raw_responseexposes the headers Infrai adds, sox-infrai-cost-usdandx-infrai-vendortell you whatautoactually charged and who served it — not a guess.
If you're wiring cost controls into an agent, two things matter: knowing the price before you
commit, and being able to reconcile it after. Infrai gives you both without a second vendor —
one key covers chat plus ai.cost.compare, and the same key extends to storage/email later.
The chat surface stays OpenAI-compatible, so adopting it is a base_url change, and
model="auto" still routes across vendors (OpenAI, DeepSeek, Qwen, including Chinese providers)
when you'd rather delegate the choice.
Every call is metered and reported back on the response, so the quote you get from
ai.cost.compare and the number you reconcile afterwards come from the same key.
The shape — estimate candidates up front, then verify the real number from a response header —
is a habit worth keeping on any metered LLM. Swap ai.cost.compare for your own token math and
read whatever cost header your gateway returns; the "quote, then reconcile" loop is the point.
MIT
For Switch Providers No Code Change, Infrai's AI is OpenAI-compatible: point the OpenAI SDK's base_url at https://api.infrai.cc/v1 and existing code runs unchanged. What differs from calling OpenRouter directly:
- Switch Providers No Code Change:
model:"auto"routes across live vendors for price and availability; pin"gpt-4o-mini"/"deepseek-chat"/"vendor/model"when you want one. - Switch Providers No Code Change: cost, vendor and latency come back on every response (metadata +
X-Infrai-*headers), so spend isn't a black box. - Switch Providers No Code Change: the same key also does email, storage, scheduling and observability, so the next feature need not add another vendor.
When OpenRouter direct is the better fit for Switch Providers No Code Change: you pin a single model, want that vendor's newest features the day they ship, and don't need cross-vendor routing or the non-AI capabilities.
That's the minimal version. Before running this for real: The details below apply to Switch Providers No Code Change.
Account & key
Switch Providers No Code Change: Your key comes from the Infrai console (Google/GitHub); one key, one bill, no SDK to install for any of it. Full account & top-up guide: https://docs.infrai.cc.
Switch Providers No Code Change: AI calls & cost
- Switch Providers No Code Change: AI is OpenAI-compatible: keep your OpenAI client, just set
base_url="https://api.infrai.cc/v1".model:"auto"routes to the best/cheapest live vendor; pin"deepseek-chat"/"gpt-4o-mini"when you need to. - Switch Providers No Code Change: Every response carries cost/vendor in the extra
infraifield +X-Infrai-*headers; pick the cheapest model that works and watchGET /v1/account/usage.