Disclosure: I run APIVAI, the gateway used in the examples below. The same config works with any endpoint that supports the OpenAI Responses API.
Codex CLI talks to OpenAI by default, but it lets you define your own model provider in its config file. That means you can point it at another gateway with a few lines of TOML. Here is the setup, where the key goes, and the mistakes that cause most of the errors.
Codex uses the Responses API
Unlike most tools, Codex CLI does not call /v1/chat/completions. It uses OpenAI's Responses API (/v1/responses). Whatever endpoint you point it at has to support that API, or tool calls and streaming will break. The wire_api = "responses" line in the config tells Codex which API to use.
Step 1: the config file
Location:
- macOS / Linux:
~/.codex/config.toml - Windows:
%USERPROFILE%\.codex\config.toml
model = "gpt-5.5"
model_provider = "apivai"
[model_providers.apivai]
name = "APIVAI"
base_url = "https://api.apivai.com/v1"
env_key = "OPENAI_API_KEY"
wire_api = "responses"
| Field | What it does |
|---|---|
model |
Default model ID |
model_provider |
Which provider to use; must match the [model_providers.xxx] name |
base_url |
Endpoint, with /v1; Codex appends /responses
|
env_key |
Environment variable that holds the key |
wire_api |
responses |
Step 2: the key
The key does not go in the config file. Codex reads it from the variable named in env_key.
export OPENAI_API_KEY="your-key"
codex "explain what this repo does in two sentences"
Windows PowerShell:
$env:OPENAI_API_KEY = "your-key"
codex "explain what this repo does in two sentences"
Start with a small question like this before you let it edit code.
Step 3: check the model ID
curl https://api.apivai.com/v1/models \
-H "Authorization: Bearer your-key"
To switch models for one run, pass -m:
codex -m gpt-5.5 "add unit tests for utils.py"
Common errors
-
401 Unauthorized: Codex can't see the key. Check that the variable name in
env_keymatches the one you set, and that it is set in the terminal you're using (echo $OPENAI_API_KEY). -
404 Not Found:
base_urlmust behttps://api.apivai.com/v1. Don't drop/v1, and don't add/responsesyourself. -
"provider not found", or requests not reaching the gateway: the
model_providervalue must match the[model_providers.xxx]name exactly, and you must be editing the rightconfig.toml. -
Tool calls fail or output hangs:
wire_apiis missing or set tochat, or the endpoint doesn't support the Responses API. -
Model not found: use an exact ID from
GET /v1/models. - 429: the default limit is 60 requests per minute per key.
What it costs
At the time of writing, GPT models on APIVAI are up to 86% below OpenAI's list prices and Claude models 56–62% below Anthropic's. It's prepaid and billed per token, with no subscription. The full table is at https://apivai.com/pricing, and setup guides for other tools are at https://apivai.com/blog.
Questions are welcome in the comments.
Top comments (0)