Codex is OpenAI's terminal AI coding assistant. Connect it to Alibaba Cloud Model Studio via Token Plan (Team Edition), Coding Plan, or pay-as-you-go.
Install Codex
-
Install or update Node.js (v18.0 or later).
-
Install Codex:
npm install -g @openai/codexVerify the installation:
codex --version
Configure Access Credentials
Edit ~/.codex/config.toml and set the OPENAI_API_KEY environment variable for your billing plan:
Token Plan (Team Edition)
For model, select a supported model. Set the OPENAI_API_KEY environment variable to the Token Plan (Team Edition) dedicated API Key.
Responses API (qwen3.7-max, qwen3.7-plus, qwen3.6-plus, qwen3.6-flash)
qwen3.7-max, qwen3.7-plus, qwen3.6-plus, and qwen3.6-flash support the Responses API, compatible with the latest Codex version.
model_provider = "Model_Studio_Token_Plan"
model = "qwen3.7-max"
[model_providers.Model_Studio_Token_Plan]
name = "Model_Studio_Token_Plan"
base_url = "https://token-plan.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
env_key = "OPENAI_API_KEY"
wire_api = "responses"
Chat/Completions API (other models)
Other models require the Chat/Completions API. Install an older version of Codex, such as 0.80.0:
model_provider = "Model_Studio_Token_Plan"
model = "glm-5"
[model_providers.Model_Studio_Token_Plan]
name = "Model_Studio_Token_Plan"
base_url = "https://token-plan.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
env_key = "OPENAI_API_KEY"
wire_api = "chat"
Configure Environment Variables
Set the OPENAI_API_KEY environment variable to the Token Plan (Team Edition) dedicated API Key.
macOS
-
Check your default shell:
echo $SHELL -
Set the environment variable based on your shell type:
Zsh
# Replace YOUR_API_KEY with the Token Plan (Team Edition) API Key echo 'export OPENAI_API_KEY="YOUR_API_KEY"' >> ~/.zshrcBash
# Replace YOUR_API_KEY with the Token Plan (Team Edition) API Key echo 'export OPENAI_API_KEY="YOUR_API_KEY"' >> ~/.bash_profile -
Apply the change:
Zsh
source ~/.zshrcBash
source ~/.bash_profile
Windows
CMD
-
Set the environment variable:
REM Replace YOUR_API_KEY with the Token Plan (Team Edition) API Key setx OPENAI_API_KEY "YOUR_API_KEY" -
Open a new CMD window to verify:
echo %OPENAI_API_KEY%
PowerShell
-
Set the environment variable:
# Replace YOUR_API_KEY with the Token Plan (Team Edition) API Key [Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "YOUR_API_KEY", [EnvironmentVariableTarget]::User) -
Open a new PowerShell window to verify:
echo $env:OPENAI_API_KEY
Coding Plan
For model, select a supported model. Set the OPENAI_API_KEY environment variable to the Coding Plan dedicated API Key.
Chat/Completions API
Coding Plan only supports the Chat/Completions API. Install an older version of Codex, such as 0.80.0:
model_provider = "Model_Studio_Coding_Plan"
model = "qwen3.7-plus"
[model_providers.Model_Studio_Coding_Plan]
name = "Model_Studio_Coding_Plan"
base_url = "https://coding-intl.dashscope.aliyuncs.com/v1"
env_key = "OPENAI_API_KEY"
wire_api = "chat"
Configure Environment Variables
Set the OPENAI_API_KEY environment variable to the Coding Plan dedicated API Key.
macOS
-
Check your default shell:
echo $SHELL -
Set the environment variable based on your shell type:
Zsh
# Replace YOUR_API_KEY with the Coding Plan API Key echo 'export OPENAI_API_KEY="YOUR_API_KEY"' >> ~/.zshrcBash
# Replace YOUR_API_KEY with the Coding Plan API Key echo 'export OPENAI_API_KEY="YOUR_API_KEY"' >> ~/.bash_profile -
Apply the change:
Zsh
source ~/.zshrcBash
source ~/.bash_profile
Windows
CMD
-
Set the environment variable:
REM Replace YOUR_API_KEY with the Coding Plan API Key setx OPENAI_API_KEY "YOUR_API_KEY" -
Open a new CMD window to verify:
echo %OPENAI_API_KEY%
PowerShell
-
Set the environment variable:
# Replace YOUR_API_KEY with the Coding Plan API Key [Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "YOUR_API_KEY", [EnvironmentVariableTarget]::User) -
Open a new PowerShell window to verify:
echo $env:OPENAI_API_KEY
Pay-as-you-go
Set OPENAI_API_KEY to your Model Studio API Key and choose from the supported models.
Set base_url for your region. The API Key must match the selected region:
-
China North 2 (Beijing):
https://dashscope.aliyuncs.com/compatible-mode/v1 -
Singapore:
https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1. ReplaceWorkspaceIdwith your actual Get the Workspace ID
Pay-as-you-go supports both the Responses API and Chat/Completions API. Choose based on your model:
Responses API
For models supporting the OpenAI Responses API (such as qwen3.7-max), compatible with the latest Codex version.
model_provider = "Model_Studio"
model = "qwen3.7-max"
[model_providers.Model_Studio]
name = "Model_Studio"
base_url = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
env_key = "OPENAI_API_KEY"
wire_api = "responses"
Chat/Completions API
For models that only support the Chat/Completions API. Install Codex 0.80.0:
model_provider = "Model_Studio"
model = "qwen3.6-plus"
[model_providers.Model_Studio]
name = "Model_Studio"
base_url = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
env_key = "OPENAI_API_KEY"
wire_api = "chat"
Configure Environment Variables
Set the OPENAI_API_KEY environment variable to the Model Studio API Key.
macOS
-
Check your default shell:
echo $SHELL -
Set the environment variable based on your shell type:
Zsh
# Replace YOUR_API_KEY with the Model Studio API Key echo 'export OPENAI_API_KEY="YOUR_API_KEY"' >> ~/.zshrcBash
# Replace YOUR_API_KEY with the Model Studio API Key echo 'export OPENAI_API_KEY="YOUR_API_KEY"' >> ~/.bash_profile -
Apply the change:
Zsh
source ~/.zshrcBash
source ~/.bash_profile
Windows
CMD
-
Set the environment variable:
REM Replace YOUR_API_KEY with the Model Studio API Key setx OPENAI_API_KEY "YOUR_API_KEY" -
Open a new CMD window to verify:
echo %OPENAI_API_KEY%
PowerShell
-
Set the environment variable:
# Replace YOUR_API_KEY with the Model Studio API Key [Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "YOUR_API_KEY", [EnvironmentVariableTarget]::User) -
Open a new PowerShell window to verify:
echo $env:OPENAI_API_KEY
Verify Configuration
Open a new terminal and start Codex:
codex
If the chat interface launches, the configuration is correct.
FAQ
What should I do if a third-party tool reports "domestic models not supported" or "check rejected / Bad request (400)"?
Cause: Some third-party management tools (such as CC-Switch) send a "health check / connection test" probe request when switching providers. The format of this probe differs from the request format Codex actually uses, so the Model Studio gateway may reject it with 400 Bad request, and the tool then reports "domestic models not supported". This message only indicates that the health check probe failed; it does not mean Model Studio lacks support for domestic models, nor does it affect actual Codex usage.
Note: Model Studio supports using domestic models such as qwen3.7-max, qwen3.7-plus, qwen3.6-plus, qwen3.6-flash, and glm-5 through Codex. For configuration details, see Configure Access Credentials above.
Solution: Configure Codex directly in ~/.codex/config.toml as described in Configure Access Credentials, without relying on the third-party tool's health check result. After configuration, start Codex as described in Verify Configuration; if the chat interface launches normally, domestic models are working.
What should I do if I get a wire_api configuration error?
Cause: Newer versions of Codex no longer support wire_api = "chat". Depending on the version, you may see one of the following errors:
-
wire_api = "chat" is no longer supported -
unknown configuration field wire_api
Solution:
-
Error
wire_api = "chat" is no longer supported: Changewire_apitoresponsesand verify thatbase_urlis correct. See Configure Access Credentials for configuration examples. -
Error
unknown configuration field wire_api: Remove thewire_apiline from the corresponding provider section in~/.codex/config.toml.
What should I do if I get the error unexpected status 401 Unauthorized?
Cause:
-
API Key mismatch (Token Plan, Coding Plan, and pay-as-you-go keys are not interchangeable)
-
Subscription expired
-
API Key copied incorrectly (incomplete, contains spaces, or has a typo)
Solution:
-
Confirm you are using the correct API Key for your plan.
-
Check your plan's management page for subscription expiration.
-
Re-copy the API Key without extra spaces.
-
If the error persists, reset the API Key on your plan's management page and reconfigure with the new key.
What should I do if I get the error unexpected status 404 Not Found?
Cause: The base_url or wire_api in the configuration file is incorrect.
Solution: Ensure base_url and wire_api match your plan's configuration in Configure Access Credentials above.
What should I do if I get the error "stream disconnected before completion: stream closed before response.completed"?
Cause: The streaming connection between Codex and the server was interrupted before the response completed. This commonly occurs in the following scenarios:
-
The conversation thread is too long, causing a context compaction request to fail
-
Unstable network causing the SSE or WebSocket connection to drop mid-stream
-
Server overload or rate limiting that terminates the connection early
Solution:
-
Start a new conversation thread to avoid excessive context accumulation in a single thread.
-
Check your network connection. Try disabling VPN or proxy and retry.
-
Wait and retry. Codex has a built-in retry mechanism that resolves most transient failures automatically.