Codex CLI has a built-in local mode. One flag runs it against LM Studio or Ollama; a profile in config.toml makes the choice permanent. Setup, models, context length, and the Responses-API caveat.
Codex, OpenAI's terminal coding agent, ships with a local mode: Codex talks to Ollama also offers Then The 20B models handle scoped tasks — a new function with tests, a refactor within a file, a script — and get lost in large multi-file changes. Give Codex a small, verifiable task, let it run the tests, and read the diff. If a model produces plans but no edits, it lacks tool calling; switch models. Related: Claude Code with LM Studio on Apple Silicon and Claude Code with Ollama. Sources: LM Studio, Codex integration; Ollama, Codex CLI integration; Ollama library, gpt-oss and qwen3-coder.codex --oss talks to a local server instead of OpenAI's API. Both LM Studio and Ollama support it out of the box, so the setup is even shorter than for Claude Code. What differs from the Claude Code guides is the protocol: Codex uses OpenAI's Responses API, not the older Chat Completions API, and it is hungrier for context.Prerequisites
npm install -g @openai/codex (codex --version to confirm).gpt-oss:20b needs about 16 GB, qwen3-coder about 19 GB; gpt-oss:120b is a 65 GB download meant for an 80 GB GPU or a large-memory Mac.With LM Studio
lms load openai/gpt-oss-20b --context-length 65536
lms server start --port 1234
codex --oss # defaults to openai/gpt-oss-20b
codex --oss -m qwen/qwen3-coder-30b # any other loaded model
http://localhost:1234/v1/responses. If the model is not loaded, LM Studio loads it on first use with the server's default context, which is usually too small; load it yourself with --context-length as above.With Ollama
OLLAMA_CONTEXT_LENGTH=65536 ollama serve # in one terminal (or the app setting)
ollama pull gpt-oss:20b
codex --oss -m gpt-oss:20b # in another
ollama launch codex, which writes a profile for you and starts Codex. The context-length note from the Ollama guide for Claude Code applies unchanged: on a laptop Ollama's default context is 4K, and Codex needs far more.Make it permanent with a profile
~/.codex/config.toml can hold a provider and a profile so that codex --profile
local does the right thing without flags:[model_providers.lmstudio]
name = "LM Studio"
base_url = "http://localhost:1234/v1"
wire_api = "responses"
[model_providers.ollama]
name = "Ollama"
base_url = "http://localhost:11434/v1"
wire_api = "responses"
[profiles.local]
model_provider = "lmstudio"
model = "openai/gpt-oss-20b"
[profiles.local-ollama]
model_provider = "ollama"
model = "gpt-oss:20b"
codex --profile local or codex --profile local-ollama. wire_api =
"responses" is the important line: a provider entry without it, or an old server that only implements Chat Completions, answers Codex's requests with 404.What to expect