Codex config.toml Reference: model_providers, env_key, wire_api

Blog / Developer tools

Codex config.toml Reference: model_providers, env_key, wire_api

Codex CLI reads its configuration from ~/.codex/config.toml (or from the directory set in CODEX_HOME, if you have set it). To point it at a third-party or self-hosted endpoint you need at least three things: a top-level model_provider, a [model_providers.<id>] table (containing base_url, env_key and wire_api), and an environment variable that actually exists. This Codex config.toml reference goes through the fields one by one and covers two common traps: putting the API key itself into env_key, and giving a custom provider a built-in ID.

Field names and allowed values are defined by the official OpenAI Codex configuration docs and by docs/config.md in the openai/codex repository. Codex changes often, so wherever this article talks about "which values are allowed", check the docs for the version that codex --version reports on your machine.

What does the overall structure of config.toml look like?

Here is a skeleton; each part is explained below:

# ---- Top-level keys: must come before any [table] ----
model = "<model name>"
model_provider = "mygateway"

# ---- Custom provider ----
[model_providers.mygateway]
name = "My Gateway"
base_url = "https://api.example.com/v1"
env_key = "MYGATEWAY_API_KEY"
wire_api = "chat"

# ---- profiles ----
[profiles.main]
model = "<model name>"
model_provider = "mygateway"

There is one TOML rule that people often overlook: once a [table] header appears, every following key = value belongs to that table until the next table starts. If you put model = "..." under [model_providers.mygateway], it does not necessarily raise an error. It simply becomes a field of that provider, and the top-level model stays unset. Many "my config change has no effect" problems come from top-level keys placed in the wrong spot.

What do the top-level model and model_provider do?

FieldPurposeCommon mistake
modelThe model name sent to the endpoint, placed into the request body as-isUsing a display name from a web page; the endpoint does not recognize it and returns 400 or 404
model_providerWhich provider to use; the value is the <id> in [model_providers.<id>]Case or spelling that does not match the table name
profileThe profile enabled by defaultNaming a profile that does not exist

The value of model must match the model list of the endpoint you use. You can request the endpoint's /models path to confirm it (see the verification steps below) instead of going from memory.

How do you write the fields under [model_providers.<id>]?

base_url: how far should the path go?

base_url is the root URL of the endpoint. For OpenAI-compatible endpoints it usually ends at /v1, and Codex appends /chat/completions or /responses itself. To check it, append /models to base_url and request it with curl. If you get a model list, the path level is right.

Two typical mistakes:

  • Leaving out /v1: the request goes to https://api.example.com/chat/completions, which usually returns 404.
  • Adding too much: writing https://api.example.com/v1/chat/completions makes the path repeat, which is also a 404.

env_key: it holds the variable name, not the key

The value of env_key is the name of an environment variable. When Codex starts, it reads that variable and sends its value in the Authorization: Bearer header.

# Correct
env_key = "MYGATEWAY_API_KEY"

# Wrong: the key itself is written here
env_key = "sk-xxxxxxxxxxxxxxxx"

The wrong form makes Codex look for an environment variable literally named sk-xxxx..., which of course does not exist. Behavior differs between versions. Some report a missing environment variable straight away. Others may show no obvious error and appear to keep using your earlier ChatGPT login, which makes it look as if the third-party endpoint "already works" when the request never reached base_url. That is harder to spot than an outright error, so when troubleshooting, confirm which provider the request actually goes to (step 4 of the verification steps).

Also, writing the key in plain text in config.toml is a bad idea in itself: the file is often copied, synced, or pasted into issues. Keeping the key in an environment variable, injected by your shell profile or a secrets manager, is safer.

wire_api: chat or responses?

wire_api decides which protocol and endpoint Codex uses to talk to the server:

  • "chat": uses /chat/completions, which most OpenAI-compatible gateways support;
  • "responses": uses /responses, which is OpenAI's Responses API.

Which one you pick depends on what the other side actually implements. To find out, send one minimal request to each path and see which returns 200. The default value and the supported range have changed between versions. If you see a hint or warning about wire_api at startup, follow the documentation for your current version.

Other optional fields

FieldPurpose
nameDisplay name; only affects the UI
query_paramsURL parameters added to every request, for example the api-version that some cloud-hosted services require
http_headersFixed extra request headers
env_http_headersExtra request headers whose values are read from environment variables
request_max_retries / stream_max_retriesNumber of retries for requests and streaming connections
stream_idle_timeout_msHow long a streaming response may go without data before it is treated as disconnected

On an unstable network, raising stream_idle_timeout_ms a bit can reduce mid-task stream drops in long jobs. If it triggers often, the problem is most likely at the network layer; working through Network requirements for Claude Code and Codex item by item is more effective than tuning the timeout.

Why can't a custom provider be called openai?

Codex ships with several built-in providers, and openai is one of them (the built-in IDs related to local models change between versions, so check the built-in providers list in the docs). If your custom provider is also named openai, it does not override the built-in definition. Your base_url and env_key may be ignored, and requests still go to the official address.

The fix is to give your custom provider an ID that cannot collide, such as mygateway or localllm, and reference that ID in model_provider.

How do profiles work, and does -c override them?

[profiles.<name>] bundles a set of settings, and you switch to it at startup with codex --profile <name> (or -p <name>). A profile can hold fields such as model, model_provider, model_reasoning_effort and approval_policy; which fields are allowed inside a profile depends on the docs for your version.

-c temporarily overrides a single field. The value is parsed as TOML, so strings need quotes:

codex -c model='"<model name>"' -c model_provider='"localllm"'

Precedence, from highest to lowest:

  1. Dedicated command-line flags (such as --model) and -c key=value;
  2. The profile selected by --profile (or, if none is given, the one named by the top-level profile field);
  3. Top-level fields in config.toml;
  4. Built-in defaults.

When you need to work out "which config is actually in effect", go through them in this order from the top.

Example: two profiles, "main endpoint + local model"

In the example below, main uses a remote OpenAI-compatible endpoint and local uses an inference server on your own machine. The addresses, port, timeout value and model names are examples; replace them with your own.

profile = "main"

[model_providers.mygateway]
name = "Remote Gateway"
base_url = "https://api.example.com/v1"
env_key = "MYGATEWAY_API_KEY"
wire_api = "chat"
stream_idle_timeout_ms = 300000

[model_providers.localllm]
name = "Local LLM"
base_url = "http://127.0.0.1:11434/v1"
wire_api = "chat"

[profiles.main]
model_provider = "mygateway"
model = "<remote model name>"

[profiles.local]
model_provider = "localllm"
model = "<local model name>"

profile = "main" goes on the first line for the TOML table reason described earlier. Local servers usually do not check the key, so env_key can be left out; add it if your local server requires authentication.

Day-to-day use:

export MYGATEWAY_API_KEY="your key"   # put this in ~/.zshrc or ~/.bashrc
codex                 # defaults to main
codex -p local        # switch to the local model

How do you verify the config after changing it?

Check in the order below; each step gives a conclusion on its own:

  1. Does the variable exist? echo ${MYGATEWAY_API_KEY:0:6} shows only the first few characters, enough to confirm it is non-empty. A terminal launched from a GUI, or an IDE's built-in terminal, may not have loaded your shell profile.
  2. Is base_url right?
    BASE_URL="https://api.example.com/v1"   # replace with the base_url from config.toml
    curl -s "$BASE_URL/models" -H "Authorization: Bearer $MYGATEWAY_API_KEY" | head -c 300
    
    A model list means both the address and the key are right; 401 is usually a key problem, and 404 is usually a path-level problem. For what each status code means, see the LLM API error code cheat sheet.
  3. Is wire_api right? Send one minimal request each to /chat/completions and /responses and see which returns 200.
  4. Which provider does Codex actually use? Temporarily run unset MYGATEWAY_API_KEY and start Codex again. If Codex still answers, the request is not going through your custom provider; go back and check the position of the top-level keys and the provider ID.
  5. A minimal task: run codex exec "reply ok", and start real work once it returns normally.

If the config is correct and failures are only occasional, rule out the service itself first. In one snapshot on the cdnping API status page taken on October 17, 2026 (probed from the Cloudflare Denver node, DEN), api.openai.com had a median round trip of 96 ms and a P95 of 194 ms over the previous 24 hours, with 100% availability. Among the incidents synced from the OpenAI status page, there is a minor incident on 2026-10-14 titled "Degraded performance in Codex and ChatGPT Work" that lasted about 167 minutes. Errors during windows like that have nothing to do with your configuration. When something looks off, check Is Codex down? first, then use the online speed test to see your own connection time to the endpoint. cdnping only began collecting data on 2026-10-17, so the figures above are a single snapshot and do not represent long-term performance.

How do you find out when fields change?

Codex configuration options are added and removed between versions, and any tutorial online, including this one, can go out of date. Two ways to stay current:

How does this differ from other tools' configuration?

Claude Code uses a JSON settings.json, injects the endpoint address and key through the env field, and has three levels: user, project and local. See the Claude Code settings.json guide for details. Codex uses TOML, declares a provider explicitly as a table, and references the key indirectly through an environment variable named by env_key. For a side-by-side comparison of how several tools change the endpoint address, see Custom API endpoints for Claude Code, Codex and OpenCode.

If you use an aggregating gateway, the configuration looks the same as the mygateway example above. Take AGI Plan as an example (AGI Plan is a commercial partner of cdnping): it is compatible with the OpenAI API format and has a step-by-step Codex tutorial that does not require a ChatGPT account. The exact base_url and wire_api are whatever the tutorial on agiplan.dev says. Some models are marked "outside mainland China only"; read the notes before use, and follow each service's terms and local regulations.

If you are on an official account, usage limits are a separate issue from configuration; see the Codex usage limit reset timeline.

Related articles

Keep reading