Codex with Azure OpenAI in Microsoft Foundry Models
OpenAI’s Codex CLI is the same coding agent that powers ChatGPT’s Codex. You can run this coding agent entirely on Azure infrastructure, while keeping your data inside your compliance boundary with the added advantages of enterprise-grade security, private networking, role-based access control, and predictable cost management. Codex is more than a chat with your code agent – it’s an asynchronous coding agent that can be triggered from your terminal, VS Code, or from a GitHub Actions runner. Codex allows you to automatically open pull requests, refactor files, and write tests with the credentials of your Foundry project and Azure OpenAI deployments.Prerequisites
- An Azure subscription - Create one for free
- Contributor permissions in Microsoft Foundry.
homebrew(macOS) or Node.js withnpmfor installing the Codex CLI. See Downloading and installing Node.js and npm.- For Windows, install and configure WSL2. See Install WSL.
| Requirements | Details |
|---|---|
| Operating systems | macOS 12+, Ubuntu 20.04+/Debian 10+, or Windows 11 via WSL2 |
| Git (optional, recommended) | 2.23+ for built-in pull request helpers |
| RAM | 4-GB minimum (8-GB recommended) |
Deploy a model in Foundry
- Go to Foundry and create a new project.
- From the model catalog select a reasoning model such as
gpt-5.1-codex-max,gpt-5.1-codex,gpt-5.1-codex-mini,gpt-5-codex,gpt-5,gpt-5-mini, orgpt-5-nano. - To deploy the model from the model catalog select Use this model, or if using the Azure OpenAI Deployments pane select deploy model.
- Copy the endpoint URL and the API Key.
Install the Codex CLI
From the terminal, run the following commands to install Codex CLI- npm
- brew
Create and configure config.toml
-
In order to use Codex CLI with Azure, you need to create and set up a
config.tomlfile. The config.toml file needs to be stored in the~/.codexdirectory. Create aconfig.tomlfile inside this directory or edit the existing file if it already exists: -
Copy the text below to use the v1 Responses API. With the v1 API you no longer need to pass api-version, but you must include /v1 in the
base_urlpath. You can’t pass your API key as a string directly toenv_key.env_keymust point to an environment variable. Update yourbase_urlwith your resource name: -
Once you have saved the updates to your
config.tomlfile return to the terminal and create an instance of the environment variable that is referenced in your config file. -
Now run one of the following commands in the terminal to test if your Codex CLI configuration was successful:
Command Purpose codex Launch interactive Terminal User Interface (TUI) codex “Initial prompt” Launch TUI with an initial prompt codex exec “Initial prompt” Launch TUI in non-interactive “automation mode”
Use codex in Visual Studio Code
You can also use Codex directly inside Visual Studio Code when using the OpenAI Codex extension- If you don’t already have Visual Studio Code, you can install it for macOS and Linux.
-
Install the OpenAI Codex extension. The extension relies on your
config.tomlfile that was configured for Codex CLI. -
If you are in a new terminal session setup the environment variable for
AZURE_OPENAI_API_KEY:
If you use WSL, also set the same environment variable on the Windows host so the extension can read it when needed.
-
Launch VS Code from the same Terminal session. (Launching from an app launcher can result in your API key environment variable not being available to the Codex extension.)
- You’ll now be able to use Codex in Visual Studio Code to chat, edit, and preview changes while toggling between three approval modes.
Approval modes
Approval modes determine how much autonomy and interaction you want to have with Codex.| Approval mode | Description |
|---|---|
| Chat | To chat and plan with the model. |
| Agent | Codex can read files, make edits, and run commands in the working directory automatically. Codex will need approval for activities outside the working directory or to access the internet. |
| Agent (full access) | All the capabilities of Agent mode without the need for step-by-step approval. Full access mode shouldn’t be used without full understanding of the potential risks as well as implementing additional guardrails such as running in a controlled sandbox environment. |
We recommend reviewing OpenAI’s guidance on Codex security.
Persistent guidance with AGENTS.md
You can give Codex extra instructions and guidance usingAGENTS.md files. Codex looks for AGENTS.md files in the following places and merges them top-down, giving it context about your personal preferences, project-specific details, and the current task:
~/.codex/AGENTS.md– personal global guidance.AGENTS.mdat your repository’s root – shared project notes.AGENTS.mdin the current working directory – subfolder/feature specifics.
AGENTS.md in your project root with the following content, derived from the Azure AI Agents SDK documentation:
\’s can be removed.
Experiment with Codex CLI
Launch codex with the following initial prompt:Codex in GitHub Actions
Codex can execute as part of your continuous integration (CI) pipeline. Store your API key in the repository’s secret store asAZURE_OPENAI_KEY and add a job like this to automatically update your changelog before a release:
Troubleshooting
| Symptom | Solution |
|---|---|
401 Unauthorized or 403 Forbidden | Export your AZURE_OPENAI_API_KEY environment variable correctly. Confirm that your key has project/deployment access. Make sure you aren’t passing the API Key as a string directly to the env_key in the config.toml file. You must pass a valid environment variable. |
ENOTFOUND, DNS error, or 404 Not Found | Verify base_url in config.toml uses your resource name, correct domain, and contains /v1. For example, base_url = "https://<your-resource>.openai.azure.com/openai/v1". |
| CLI ignores Azure settings | Open ~/.codex/config.toml and ensure: - model_provider = "azure" is set. - The [model_providers.azure] section exists. - env_key = "AZURE_OPENAI_API_KEY" matches your environment variable name. |
| Entra ID support | Entra ID support is currently not available for Codex. |
401 Unauthorized only with the WSL + VS Code Codex extension | When running VS Code from inside WSL with the Codex extension the extension may check for the API key environment variable on the local windows host rather than within the terminal shell that launched VS Code. To mitigate this issue, set the environment variable on the local windows host as well, then launch a new terminal from WSL and launch VS Code with code .. |