Skip to content
AI GatewayAI Gateway Manual

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).

  • Finish Get a Gateway Key.
  • A model you want to use must be allowed in Models of the same project.
  • Install Codex CLI.

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.

Model setup dialog. 1 is Model ID and 2 is 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.

In the terminal where you run Codex, set the key as an environment variable. A new terminal needs it set again.

Terminal window
export AI_GATEWAY_API_KEY='<your Gateway Key>'

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 = false

Keep web_search = "disabled" because the Gateway connection needs it. env_key takes the name of the environment variable, not the key value.

If Codex was running, quit it. In the terminal where you set the key, start Codex with the model and send a short request.

Terminal window
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.