Connect Codex (ChatGPT) to AI Gateway
How to connect Codex CLI and Codex in the macOS ChatGPT desktop app to AI Gateway and choose a model.
This guide explains how to connect Codex CLI and Codex in the macOS ChatGPT desktop app to AI Gateway.
Before you start
Section titled “Before you start”- Install Codex CLI or the ChatGPT desktop app.
- Issue a Gateway Key from the Gateway Keys menu of the project you want to use. See Issue a Gateway Key for the steps.
- Check that the model you want appears in the Models menu of the same project. That list is the models allowed in the project.
1. Set the Gateway Key
Section titled “1. Set the Gateway Key”Set the Gateway Key as an environment variable in the terminal where you run Codex. Example for bash/zsh on macOS or Linux:
export AI_GATEWAY_API_KEY='<your Gateway Key>'A new terminal may not have this setting. To keep it, set AI_GATEWAY_API_KEY in your shell configuration or secret manager.
2. Configure Codex
Section titled “2. Configure Codex”In AI Gateway, open the project you want to use, then open Models in the top menu. In the row of the model you want, select the gear icon in the Installation column and check the Gateway base URL and model ID.
The CLI and the macOS app read the same ~/.codex/config.toml. If the ~/.codex directory does not exist, create it first, then add the content below to ~/.codex/config.toml. If the file already exists, keep the existing settings and merge so that no key or [features] section is duplicated.
The configuration looks like this. Replace <gateway-host> and <provider-name>/<model-id> to match the Gateway base URL and model ID you checked above. The model ID has the form provider-name/model-id.
model = "<provider-name>/<model-id>"model_provider = "ai_gateway"web_search = "disabled"
[model_providers.ai_gateway]name = "AI Gateway"base_url = "https://<gateway-host>/v1/"model_catalog_url = "https://<gateway-host>/v1/codex/models"env_key = "AI_GATEWAY_API_KEY"wire_api = "responses"supports_websockets = false
[features]api_key_model_discovery = truebase_url is the address model requests go to, and model_catalog_url is the address the list of available models comes from. Both must point to the same AI Gateway. env_key takes the name of the environment variable you set in the previous step, not the key value.
web_search = "disabled" is required for the Gateway connection. Native web search can break sessions with non-OpenAI models, so keep it after you confirm the connection. It must be a top-level setting before the first TOML table, including [features]. If a top-level web_search already exists, change its value instead of adding another entry.
3. Check the connection in Codex CLI
Section titled “3. Check the connection in Codex CLI”If Codex is already running, quit it. If you use the background Codex daemon, restart it as well so it picks up the change. Run this after any Codex work in progress has finished.
codex app-server daemon restartIf you do not use the daemon, you do not need this command.
In the terminal where the key is set, go to the directory you want to work in and run Codex.
codexRun /model inside Codex to choose from the models available with that Gateway Key. Names in the list have the form provider-name/model-id. After choosing a model, send a short request and check that a response comes back.
You can also start Codex with a model ID given directly by the -m (--model) option. The model you give overrides model in config.toml for that run only.
codex -m '<provider-name>/<model-id>'4. Check the connection in the ChatGPT desktop app (macOS)
Section titled “4. Check the connection in the ChatGPT desktop app (macOS)”Apps launched from the desktop do not receive environment variables set in a terminal. In the terminal where you set the Gateway Key in step 1, run:
launchctl setenv AI_GATEWAY_API_KEY "$AI_GATEWAY_API_KEY"Quit the ChatGPT app completely (⌘Q) and start it again. Open the model list in the app’s Codex view, choose the model you want, send a short request, and check that a response comes back.
This setting also reaches apps launched later in the current macOS user session. You must set it again after logging out. When you are done with AI Gateway, clear it with launchctl unsetenv AI_GATEWAY_API_KEY and restart the app. For details, see OpenAI’s guide to connecting the macOS app to a gateway.
If models do not appear
Section titled “If models do not appear”If /model in the CLI shows No additional models are available right now., or the app is missing a model you expected, check the following.
- Run the CLI again in the same terminal where
AI_GATEWAY_API_KEYis set. For the app, set it withlaunchctlas in step 4, then quit the app completely and start it again. - Check
model_catalog_url,api_key_model_discovery = true, andmodel_providerinconfig.toml. - Check in the AI Gateway Models menu that the model is allowed in the project. A Gateway Key belongs to the project it was issued in, so another project’s key gives a different list.
- If the model is in the Models menu but missing only from the Codex list, it may belong to a custom provider an administrator registered. The Codex list includes custom-provider models only when the provider allows Responses streaming or OpenAI-format Chat Completions streaming. Ask the administrator to check the provider settings.
- If it still does not appear, ask a project administrator to check your Gateway Key and model permissions. Do not share the key value itself.
For the location of Codex configuration files and setting precedence, see OpenAI’s configuration guide.