Codex supports a wide range of AI models, from OpenAI's latest models to local open-source models running on your own machine. This guide explains how to select models and configure custom model providers.
Note: This documentation applies to Codex CLI. If you're using Codex in an IDE (VS Code, Cursor, Windsurf), the configuration is shared through
~/.codex/config.toml, so the examples below will work for both CLI and IDE usage. For IDE-specific features, see the IDE documentation.
By default, Codex uses OpenAI's models. Simply run:
codexAnd sign in with your ChatGPT account or API key.
If you have LMStudio running with models loaded:
codex --ossThis will automatically connect to your local LMStudio instance (default port: 1234).
If you prefer Ollama:
codex --oss --local-provider ollamaThis connects to your local Ollama instance (default port: 11434).
You can specify which model to use with the -m or --model flag:
codex -m gpt-5.1
codex --oss -m llama3.3:70bCodex comes with three built-in model providers:
- OpenAI (
openai) - Default provider using OpenAI's API - LMStudio (
lmstudio) - Local models via LMStudio - Ollama (
ollama) - Local models via Ollama
You can add your own model providers by editing ~/.codex/config.toml. This allows you to use any OpenAI-compatible API endpoint.
Add a [model_providers.<name>] section to your config:
[model_providers.my-provider]
name = "My Custom Provider"
base_url = "https://api.example.com/v1"
env_key = "MY_API_KEY"Then use it:
export MY_API_KEY="your-api-key-here"
codex -c model_provider=my-providerEach model provider can be configured with these options:
name- Friendly display name for the providerbase_url- Base URL for the OpenAI-compatible API endpoint
-
env_key- Environment variable containing the API keyenv_key = "MY_API_KEY"
-
env_key_instructions- Help text shown to users if the API key is missingenv_key_instructions = "Get your API key from https://example.com/api-keys"
-
experimental_bearer_token- Hardcoded API key (not recommended for security)experimental_bearer_token = "sk-..."
-
requires_openai_auth- Set totrueif this provider requires OpenAI authenticationrequires_openai_auth = false
-
wire_api- API protocol to use (default:"responses")"responses"- Modern OpenAI Responses API (used by OpenAI, LMStudio)
The legacy
"chat"(Chat Completions) wire API has been removed; providers must expose the Responses API.wire_api = "responses"
-
query_params- Additional query parameters for requests[model_providers.azure.query_params] api-version = "2025-04-01-preview"
-
http_headers- Static HTTP headers to include[model_providers.my-provider.http_headers] X-Custom-Header = "custom-value"
-
env_http_headers- HTTP headers from environment variables[model_providers.my-provider.env_http_headers] X-Custom-Header = "MY_HEADER_ENV_VAR"
request_max_retries- Max HTTP request retries (default: 4)stream_max_retries- Max SSE stream reconnection attempts (default: 5)stream_idle_timeout_ms- Stream idle timeout in milliseconds (default: 300000 = 5 minutes)
request_max_retries = 4
stream_max_retries = 10
stream_idle_timeout_ms = 300000[model_providers.azure]
name = "Azure OpenAI"
base_url = "https://your-resource.openai.azure.com/openai"
env_key = "AZURE_OPENAI_API_KEY"
wire_api = "chat"
[model_providers.azure.query_params]
api-version = "2025-04-01-preview"Usage:
export AZURE_OPENAI_API_KEY="your-key"
codex -c model_provider=azure -m gpt-4If your LMStudio runs on a different port:
[model_providers.lmstudio]
name = "LMStudio"
base_url = "http://localhost:8080/v1"
wire_api = "responses"
requires_openai_auth = falseOr use environment variables:
export CODEX_OSS_PORT=8080
codex --oss[model_providers.ollama]
name = "Ollama"
base_url = "http://localhost:11434/v1"
wire_api = "chat"
requires_openai_auth = false[model_providers.openrouter]
name = "OpenRouter"
base_url = "https://openrouter.ai/api/v1"
env_key = "OPENROUTER_API_KEY"
wire_api = "chat"
[model_providers.openrouter.http_headers]
HTTP-Referer = "https://codex.local"
X-Title = "Codex CLI"Usage:
export OPENROUTER_API_KEY="sk-or-..."
codex -c model_provider=openrouter -m anthropic/claude-3.5-sonnet[model_providers.together]
name = "Together AI"
base_url = "https://api.together.xyz/v1"
env_key = "TOGETHER_API_KEY"
wire_api = "chat"Usage:
export TOGETHER_API_KEY="your-key"
codex -c model_provider=together -m meta-llama/Llama-3.3-70B-Instruct[model_providers.groq]
name = "Groq"
base_url = "https://api.groq.com/openai/v1"
env_key = "GROQ_API_KEY"
wire_api = "chat"Usage:
export GROQ_API_KEY="gsk_..."
codex -c model_provider=groq -m llama-3.3-70b-versatileInstead of passing -c model_provider=<name> every time, you can create configuration profiles:
[profiles.local]
model_provider = "lmstudio"
model = "llama-3.3-70b-instruct"
[profiles.groq]
model_provider = "groq"
model = "llama-3.3-70b-versatile"
approval_policy = "never" # Auto-approve for faster iterationThen use:
codex -p local # Use LMStudio
codex -p groq # Use Groq- Install LMStudio from lmstudio.ai
- Download your preferred models (e.g., Llama 3.3 70B, DeepSeek R1)
- Start the local server:
lms server start - Run Codex:
codex --oss
Codex will automatically:
- Verify LMStudio is running
- Check if the model is downloaded
- Download the model if needed
- Load the model in the background
When using --oss with LMStudio (the default local provider), Codex defaults to openai/gpt-oss-20b. You can override this:
codex --oss -m deepseek/r1-distill-qwen-32b
codex --oss -m meta-llama/llama-3.3-70b-instructWhen using Ollama, the default model is gpt-oss:20b. You can override this:
codex --oss --local-provider ollama -m llama3.3:70b
codex --oss --local-provider ollama -m deepseek-r1:32b- Install Ollama from ollama.ai
- Pull models:
ollama pull llama3.3:70b - Run Codex:
codex --oss --local-provider ollama -m llama3.3:70b
The default model for Ollama is gpt-oss:20b.
You can customize provider behavior with environment variables:
OPENAI_BASE_URL- Override OpenAI API endpointOPENAI_ORGANIZATION- OpenAI organization IDOPENAI_PROJECT- OpenAI project ID
CODEX_OSS_BASE_URL- Override local provider URLCODEX_OSS_PORT- Override local provider port (default: 1234 for LMStudio, 11434 for Ollama)
Examples:
# Use OpenAI proxy
export OPENAI_BASE_URL="https://my-proxy.example.com/v1"
codex
# Use LMStudio on custom port
export CODEX_OSS_PORT=8080
codex --oss
# Use completely custom local endpoint
export CODEX_OSS_BASE_URL="http://my-server:9000/v1"
codex --ossMake sure LMStudio is running:
lms server startOr check if it's accessible:
curl http://localhost:1234/v1/modelsCheck your config:
cat ~/.codex/config.tomlMake sure the provider name matches what you specified with -c model_provider=<name>.
If you get authentication errors, verify your environment variable is set:
echo $MY_API_KEYAnd that the env_key in your config matches the environment variable name.
If you get errors about request format, try changing the wire_api:
- Most third-party providers use
wire_api = "chat" - OpenAI and LMStudio support
wire_api = "responses"