How to use local models with Codex

Local model runs in Codex are for coding prompts that should stay on a workstation or lab machine instead of going to a hosted model. The Codex CLI can hand the prompt to Ollama or LM Studio while still working against the repository path selected for the task.

The local provider must expose an OpenAI-compatible API with a model identifier that Codex can request. Ollama normally listens on http://localhost:11434/v1, LM Studio normally listens on http://localhost:1234/v1, and --local-provider selects which local backend answers the run.

The model must already be loaded or pullable by the provider before the prompt starts. Keep the first run short, point Codex at the repository with -C when the current shell is elsewhere, and keep the provider listener on localhost unless another machine intentionally needs access.

Steps to use local models with Codex:

  1. List the model IDs exposed by the local provider.
    $ curl --silent http://localhost:11434/v1/models
    {"object":"list","data":[{"id":"gpt-oss:20b","object":"model","created":1765244842,"owned_by":"library"}]}

    For LM Studio, replace port 11434 with 1234 and use the returned id value.
    Related: How to use Ollama models with Codex
    Related: How to use LM Studio models with Codex

  2. Run Codex with the matching provider, model ID, and repository path.
    $ codex exec --oss --local-provider ollama -m gpt-oss:20b -C ~/repo "Reply with exactly: OK"
    OpenAI Codex v0.139.0
    --------
    workdir: /home/user/repo
    model: gpt-oss:20b
    provider: ollama
    --------
    codex
    OK

    Use lmstudio with an LM Studio model ID when that server owns the run. The provider line should match the backend selected with --local-provider.
    Related: How to fix the Codex trusted-directory error

  3. Save the final local reply when another script or reviewer needs the assistant message in a file.
    $ codex exec --oss --local-provider ollama -m gpt-oss:20b -C ~/repo --output-last-message /tmp/codex-local.txt "Reply with exactly: OK"
    OK

    --output-last-message overwrites the destination file when it already exists.

  4. Read the saved file and confirm it contains only the local-model reply.
    $ cat /tmp/codex-local.txt
    OK
  5. Remove the sample output file if it was only used for the smoke test.
    $ rm /tmp/codex-local.txt