Setup guide - Documented by Codex CLI
Use Grok in Codex CLI
Codex CLI has no built-in xAI provider, but its documented custom model_providers config (Responses wire API only) can point at xAI's documented Responses endpoint at https://api.x.ai/v1 with XAI_API_KEY.
Last verified 3 steps4 official sources
Steps
- Step 1.
Create an xAI API key and export it (from xAI quickstart):
Terminalexport XAI_API_KEY="your_api_key" - Step 2.
Add a custom provider to ~/.codex/config.toml using Codex's documented model_providers keys (name, base_url, env_key, wire_api) with xAI's documented base URL and model id:
TOMLmodel = "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"
Caveats
- Combined config: neither OpenAI nor xAI documents this pairing. The keys come from Codex's config reference; the base URL, env var and model id come from xAI docs.
- Codex now supports only wire_api = "responses" ('responses is the only supported value'). xAI serves POST /v1/responses and lists Responses as grok-4.7's API, so the protocol lines up, but Codex-specific Responses features (hosted tools, compaction, WebSocket transport) may not be supported by xAI.
- Custom providers cannot reuse the reserved ids openai, ollama or lmstudio.
More setup guides
Same tool, other providers
Related reading
- Grok Build Developer Guide: xAI's Terminal Coding Agent (June 2026)Grok Build, xAI's terminal coding agent, runs up to 8 parallel subagents with a plan-first workflow. Install, pricing, commands and a Claude Code comparison.
- GPT-6 Sol vs Claude Opus 5.5 vs Grok 4.7: The September 2026 Price WarGPT-6 Sol vs Claude Opus 5.5 vs Grok 4.7 pricing: verified per-token rates, cost-per-task math and a decision guide for the September 2026 price war.
- Grok Bot's Core Primitive: Every Bot Gets a ComputerGrok Bot gives every Bot a persistent cloud computer with a browser and terminal. Why that one primitive explains routines, Bot teams, and always-on work.
- Grok Bot's Meta Controls: Oversight as a Product PrimitiveGrok Bot's meta controls make agent oversight a product primitive: approval gates, a chief-of-staff structure and escalation learning, not a settings page.
- Grok Bot Has the Right Shape: Four Primitives, Nothing ElseGrok Bot ships four primitives that compose - a text thread, its own cloud computer, a chief of staff over Bots, and show-it-once routines - and nothing else.
- Grok Bot Routines: Automations Without Automation-BuildingGrok Bot routines let you demonstrate a job once, correct it in plain language and hand the Bot the schedule. How it works and how approval gates keep it safe.
FAQ
How do I use Grok in Codex CLI?
Codex CLI has no built-in xAI provider, but its documented custom model_providers config (Responses wire API only) can point at xAI's documented Responses endpoint at https://api.x.ai/v1 with XAI_API_KEY.
Which Grok model ids work in Codex CLI?
The official docs read for this guide list grok-4.7, grok-4.3 and grok-build-0.1. Requests go to https://api.x.ai/v1.
Is this an official Grok and Codex CLI setup?
Codex CLI documents this setup in its own docs. It was last checked against 4 official pages on October 17, 2026.
Sources
Last verified against 4 official pages.
- developers.openai.comhttps://developers.openai.com/codex/config-advanced
- developers.openai.comhttps://developers.openai.com/codex/config-reference
- docs.x.aihttps://docs.x.ai/developers/quickstart.md
- docs.x.aihttps://docs.x.ai/developers/grok-4-7.md
Newsletter
New models, set up the day they ship
One email per week. New model releases, the coding tools that support them and how to wire them up.