How Do I Run Codex Through the Gateway?
Codex supports custom model providers. A small configuration block gives it the Gateway endpoint and the name of the environment variable that holds your key.
Outcome
Run one read-only Codex task through a custom AI Gateway provider without storing the key in config.toml.
Fast Track
- Put the Gateway key in
AI_GATEWAY_API_KEY - Add the provider block below to the Codex configuration
- Start Codex in a throwaway repository and run a small task
Hands-on exercise
Add a Vercel provider to ~/.codex/config.toml for a personal default, or to .codex/config.toml in a trusted repository for a project-specific setting. Check the current Gateway catalog before choosing a model because model IDs change over time.
model_provider = "vercel"
model = "openai/gpt-5.6-sol"
[model_providers.vercel]
name = "Vercel AI Gateway"
base_url = "https://ai-gateway.vercel.sh/v1"
env_key = "AI_GATEWAY_API_KEY"The file contains the environment variable's name, not its value. Set the real key in the shell or secret manager that starts Codex.
Try It
Open Codex in a throwaway repository containing demo.ts, then ask:
Explain demo.ts in three bullets. Do not modify anything.Codex should complete the task with the configured provider. If startup fails, verify the TOML structure, confirm that AI_GATEWAY_API_KEY exists in the launching environment, and check that the selected model is still in the Gateway catalog.
Done-When
- Codex starts with the Vercel provider selected
- The selected model exists in the current Gateway catalog
- Codex completes the read-only task
- The key value does not appear in
config.toml, source control, or terminal output
Solution
The provider block above is the complete Codex configuration. The file identifies the Gateway endpoint and credential variable while the launching environment supplies the secret.
Related Questions
Was this helpful?