Use Codex CLI without paying full price
OpenAI's Codex CLI is a fast way to edit code, run commands, and pair with a model directly from your terminal. The friction is cost: pointed at official endpoints, a busy day of agentic coding adds up quickly. The good news is that Codex CLI talks to an OpenAI-compatible endpoint, so you can route it through a cheaper gateway with a one-line change and keep the exact same workflow.
This guide shows how to configure Codex CLI to use APIVAI, which resells GPT and Claude models at a steep discount off official pricing.
What you need
- Codex CLI installed and working.
- An APIVAI API key.
- The APIVAI base URL:
https://api.apivai.com/v1.
Step 1 — point Codex at the APIVAI endpoint
Codex CLI reads an OpenAI-style base URL and API key from environment variables. Set them in the shell that launches Codex.
export OPENAI_BASE_URL="https://api.apivai.com/v1"
export OPENAI_API_KEY="YOUR_APIVAI_API_KEY"
On Windows PowerShell:
$env:OPENAI_BASE_URL="https://api.apivai.com/v1"
$env:OPENAI_API_KEY="YOUR_APIVAI_API_KEY"
That is the whole change. Codex now sends its requests to APIVAI instead of the default endpoint, and your existing prompts, approvals, and file edits behave the same.
Step 2 — pick a model
Before hardcoding a model name, list what your account can use and choose one from the response.
curl https://api.apivai.com/v1/models \
-H "Authorization: Bearer $OPENAI_API_KEY"
Set the model Codex should use to one of the returned IDs, for example a GPT-class model for general coding. APIVAI exposes the OpenAI Responses API that Codex relies on, so tool calls and streaming work as expected.
Step 3 — run a small task first
Start with a tiny, low-risk request to confirm routing before turning Codex loose on a real change.
codex "explain what this repo does in two sentences"
If you get a normal response, the endpoint, key, and model are all wired correctly. From here, use Codex exactly as you would against the official API.
Why route Codex through APIVAI
- Lower effective cost on GPT and Claude models, with no subscription.
- One OpenAI-compatible key that also works with Cursor, Claude Code, and your own scripts.
- Pay-as-you-go top-ups, including crypto and Alipay.
- Model discovery via
/v1/modelsso you avoid stale, hardcoded model names.
Troubleshooting
- A
401usually means the key is missing or not exported in the shell that launched Codex. - A
404often means the base URL is missing/v1or has it twice. - A model error means the configured model is not in the current
/v1/modelsresponse — pick a returned ID. - If streaming looks stuck, confirm the client reads the response incrementally and retry a non-streaming request to isolate the issue.
Get started
Create an API key, export the two environment variables above, choose a model from /v1/models, and run a small Codex task to confirm everything works. Most existing OpenAI-compatible setups need only the base-URL and key change.
Top comments (0)