The goal: a terminal coding agent with no API bill
OpenAI’s Codex CLI is a terminal coding agent, and unlike some tools it has local model support built in. Running Codex CLI with Ollama needs no proxy or translation layer: a single flag switches it to a local provider. If you have followed my guide to Claude Code with local LLM models, this is the same idea with less plumbing.
Step 1: install Codex and pull a model
npm install -g @openai/codex
# the default model for --oss mode
ollama pull gpt-oss:20b
gpt-oss:20b is about a 14GB download and is what Codex uses by default in local mode. With serious memory (64GB+ unified or equivalent) you can try gpt-oss:120b, or another tool-capable coder such as qwen3-coder:30b.
Step 2: raise the context window first
This matters more than model choice. Ollama’s own Codex guide recommends a context window of at least 64k tokens. Ollama’s default depends on your VRAM and can be as low as 4k, which leaves an agent unable to hold its own instructions, let alone your code.
OLLAMA_CONTEXT_LENGTH=64000 ollama serve
# confirm: check the CONTEXT column
ollama ps
On macOS you can set the same thing with the context slider in the Ollama app’s settings.
Step 3: run in –oss mode
# use the default local model
codex --oss
# pick a specific model
codex --oss -m qwen3-coder:30b
Codex supports both Ollama and LM Studio as local providers. If you have not set a default, the interactive CLI asks which to use; non-interactive codex exec runs exit with an error instead. Set the default in ~/.codex/config.toml:
# default local provider used with --oss
oss_provider = "ollama" # or "lmstudio"
You can also choose per run with --local-provider.
Option: let Ollama write the config
Recent Ollama versions can set Codex up for you:
ollama launch codex # configure and start
ollama launch codex --config # configure only
ollama launch codex --restore # undo
Option: a custom provider in config.toml
If your server is elsewhere or speaks the OpenAI API (llama.cpp, vLLM, a box on your LAN), define a provider. The IDs openai, ollama and lmstudio are reserved, so give yours a different name:
model = "qwen3-coder:30b"
model_provider = "local_ollama"
[model_providers.local_ollama]
name = "Ollama"
base_url = "http://localhost:11434/v1"
Provider settings belong in your user-level ~/.codex/config.toml; a project-level .codex/config.toml ignores them. Codex also supports profile files you load with codex --profile <name>, which is a tidy way to keep a local setup alongside a cloud one. Ollama’s guide shows a profile using wire_api = "responses"; check the Codex config docs for what your version expects.
Troubleshooting
- Agent loops, forgets the task or ignores files: almost always context. Check
ollama ps. - Tool calls fail or come back as plain text: the model’s tool calling is not good enough. Try gpt-oss or a dedicated agentic coder.
- Codex complains about the Ollama version: update Ollama; newer Codex builds expect a recent server.
- Web search errors: with an Ollama-launched profile, web search relies on Ollama sign-in. Disable it if you want to stay fully local.
- Painfully slow: part of the model is on the CPU. Use a smaller model or quant, or reduce context slightly.
Verdict
Codex CLI with Ollama is the easiest way to get a proper terminal agent running locally: one flag, a sensible default model, and native LM Studio support. Give it enough context and a tool-capable model and it is useful for real refactors. For picking that model, see choosing a local coding model.
This post is part of a series on running AI coding tools against local models. The others cover Zed with Ollama, VS Code and Continue with Ollama, JetBrains AI Assistant with local models, Codex CLI with Ollama, Xcode with Ollama, and a capstone on choosing a local coding model for your hardware.

Leave a Reply
You must be logged in to post a comment.