Set up an AI agent
Connect Codex CLI to an AI Gateway project model and check the first response.
This page connects one Codex CLI to get your first model response. For common values in other clients, see the AI agent setup guide; for desktop apps and the model list, see Connect Codex (ChatGPT).
Before you start
Section titled “Before you start”- Finish Get a Gateway Key.
- A model you want to use must be allowed in Models of the same project.
- Install Codex CLI.
1. Check the model details
Section titled “1. Check the model details”In the project’s Models, select the gear icon in the Installation column of the model row you want. Check the Model ID and Gateway base URL.

Use 1 Model ID for model and 2 Gateway base URL for base_url in the Codex configuration. The copy buttons next to each value copy them as-is.
2. Set the key
Section titled “2. Set the key”In the terminal where you run Codex, set the key as an environment variable. A new terminal needs it set again.
export AI_GATEWAY_API_KEY='<your Gateway Key>'3. Configure Codex
Section titled “3. Configure Codex”If ~/.codex does not exist, create it, then put the content below in ~/.codex/config.toml. If the file already exists, merge so that entries with the same name are not duplicated. Replace <Gateway base URL> and <provider-name>/<model-id> with the values you checked in step 1.
model = "<provider-name>/<model-id>"model_provider = "ai_gateway"web_search = "disabled"
[model_providers.ai_gateway]name = "AI Gateway"base_url = "<Gateway base URL>"env_key = "AI_GATEWAY_API_KEY"wire_api = "responses"supports_websockets = falseKeep web_search = "disabled" because the Gateway connection needs it. env_key takes the name of the environment variable, not the key value.
4. Send the first request
Section titled “4. Send the first request”If Codex was running, quit it. In the terminal where you set the key, start Codex with the model and send a short request.
codex -m '<provider-name>/<model-id>'When a response comes back, the connection is done.
To add MCP tools, continue to Set up MCP. To choose a model from the model list, see Connect Codex (ChatGPT); for other clients, see the AI agent setup guide.