Use Codex CLI with Any AI Model (Not Just OpenAI)
Most people think Codex CLI only works with OpenAI. It's actually an OpenAI-compatible client — point it at any provider. GLM, Claude via OpenRouter, even a local model. Same Codex, different brain.
Here's how to set it up in one config file.
What a "model provider" actually is
A model provider is just three things:
base_url— where the request goes.wire_api— chat or responses format.env_key— the environment variable holding your API key.
That's it. Nothing mystical. You switch providers for one of four reasons: cost, privacy, running fully local, or a model OpenAI doesn't sell.
The one file: ~/.codex/config.toml
Everything lives in one file. Pick your provider at the top, then define it under [model_providers.<name>]. Here's the verified GLM config:
# ~/.codex/config.toml
model_provider = "glm"
model = "glm-4.6"
[model_providers.glm]
name = "Z.ai GLM"
base_url = "https://api.z.ai/api/coding/paas/v4"
wire_api = "chat"
env_key = "ZAI_API_KEY"
Then export the key and launch:
export ZAI_API_KEY="sk-..."
codex
# ▸ codex 0.142.5 · provider: glm · model: glm-4.6
# ✓ ready
You're now using GLM, not OpenAI.
⚠ The Z.ai dual-endpoint gotcha
Z.ai has two base URLs. Using the wrong one gives you a 401:
/api/coding/paas/v4← Coding Plan (for tools like Codex). Use this one./api/paas/v4← standard pay-as-you-go API.
This is the single most common reason a "it just won't connect" happens.
Switching providers is one line
Change model_provider to whatever you defined:
| Provider | base_url |
Note |
|---|---|---|
glm |
api.z.ai/api/coding/paas/v4 |
Z.ai — cheap, fast |
openrouter |
openrouter.ai/api/v1 |
Claude · Gemini · Llama through one endpoint |
ollama |
localhost:11434/v1 |
local · free · private — no API key, no bill |
OpenRouter gives you Claude, Gemini, and Llama through a single endpoint. Ollama runs models fully on your machine — no API key, no bill.
FAQ
Does Codex CLI only work with OpenAI models?
No — it is an OpenAI-compatible client, not an OpenAI-only one. One ~/.codex/config.toml file points it at GLM, Claude via OpenRouter, or a fully local model. Same Codex, different brain.
Why does my GLM setup return a 401?
Z.ai has two base URLs. /api/coding/paas/v4 is the Coding Plan endpoint meant for tools like Codex; /api/paas/v4 is the standard pay-as-you-go API. Using the wrong one is the single most common cause of "it just won't connect."
Can Codex CLI run fully local?
Yes — point it at Ollama (localhost:11434/v1). No API key, no bill, and your code never leaves your machine.
What actually defines a model provider?
Three settings: base_url (where requests go), wire_api (chat or responses format), and env_key (the environment variable holding your API key).
That's it
One config file, any model. Copy the snippet above, swap in your provider, run codex. Same Codex, different brain.
Subscribe to devkingov for more AI dev workflows.
Keep reading
What AI Coding Actually Costs in 2026
Cursor at $0.07/task vs Claude Code at ~$4 — the real numbers, the token-efficiency axis, the 65% output cut, and the free local floor. A cost playbook for AI-assisted development.
The Composable AI Stack — My 2026 Playbook
Cursor for edits, Claude Code as host, Codex for greenfield, Antigravity for free orchestration. The build order, the cost rule, the local escape, and the safety rails — one playbook.
AI Coding Tools, Ranked — July 2026
Cursor, Claude Code, Codex, Antigravity 2.0 — an honest ranking with verified benchmarks, costs, and best-use cases. Updated for July 2026.
Want help applying this? Book a 1-on-1 with a consultant.
Find a Consultant