Use xAI in Codex

Prices and modelsEndpoint verified 2026-11-09

Generate a Codex config from the Responses-compatible endpoint in xAI's docs. The API key in the config is a placeholder; replace it with a key you create at xAI.

Endpoints

  • Pay as you gohttps://api.x.ai/v1xAI documents a Responses API but has no dedicated Codex guide.Docs · verified 2026-11-09

Generate the config

Pick from the 4 models this provider sells, or type another model name.

Save to ~/.codex/config.toml; merge it in if the file already exists
model = "grok-4.7"
model_provider = "xai"

[model_providers.xai]
name = "xAI"
base_url = "https://api.x.ai/v1"
env_key = "XAI_API_KEY"
wire_api = "responses"

Models and prices

ModelInputOutputRatio
Grok 4.7grok-4.7$2.00$6.001.00×
Grok 4.6grok-4.6$2.00$6.001.00×
Grok 4.5grok-4.5$2.00$6.001.00×
Grok 4.3grok-4.3$1.25$2.501.00×
$ per 1M tokens. Each model uses the provider's cheapest group; the ratio uses the output price

How to check it works

Restart Codex and send a message. If it replies and the request shows up in xAI's console, it is using this endpoint.

FAQ

Why is the key in an environment variable?

config.toml only holds the variable's name, so the key never goes into the config file and does not leak when you share or sync it. To keep it, add the export line to ~/.zshrc or ~/.bashrc.

How do I switch back to OpenAI?

Remove the model_provider line from config.toml, then restart Codex.

Does my key pass through this site?

No. The page only generates config text and never asks for your key; the key only goes into the terminal or environment variables on your own machine.

Codex configs for other providers