Repository work can use a model served on the same machine as the coding assistant. Codex connects to Ollama's local API so a downloaded model can answer prompts without a hosted model account.

You need an installed Codex CLI and Ollama running locally with a downloaded model that supports tools. An existing disposable Git repository provides a working directory for the first task.

Codex sends its own instructions and tool definitions alongside the prompt, so a local model needs enough context and memory for more than the user's question. Ollama recommends at least 64,000 tokens of context for Codex; model loading and CPU inference can take minutes even for a short answer.

Steps to use Ollama models with Codex:

  1. List the models available in Ollama.
    $ ollama list
    NAME                             ID              SIZE      MODIFIED     
    qwen3:4b-instruct-2507-q4_K_M    6214d088d384    2.5 GB    11 hours ago

    The complete tag in the NAME column is the model name passed to Codex. A missing model needs downloading before use; a connection error means the local server is unavailable.
    Related: How to pull an Ollama model
    Related: How to start the Ollama server

    Ollama's local API has no authentication boundary. A loopback listener prevents other machines from submitting requests to it.

  2. Ask Codex a Git question through the installed Ollama model.

    Codex tools can read accessible files and execute commands under the active approval and sandbox policy. Read-only mode prevents file writes; it does not hide private files from tools.

    $ codex exec -c 'model_provider="ollama"' \
      --oss --local-provider ollama \
      -m qwen3:4b-instruct-2507-q4_K_M \
      --sandbox read-only -C ~/repo \
      "What is a Git branch? Answer in one sentence."
    OpenAI Codex v0.166.0
    ##### snipped #####
    model: qwen3:4b-instruct-2507-q4_K_M
    provider: ollama
    ##### snipped #####
    codex
    A Git branch is a separate line of development within a repository, allowing multiple features or changes to be worked on independently without affecting the main codebase.
    ##### snipped #####

    Here, ~/repo is the existing disposable Git repository. The --oss flag selects local-provider mode, --local-provider ollama selects Ollama, and -m names the exact inventory tag. The -c 'model_provider="ollama"' override keeps the configured provider on Ollama for this run, avoiding a provider-mismatch error in Codex CLI 0.166.0. The --sandbox read-only option prevents tool writes, and this question requires no file access.
    Related: How to fix the Codex trusted-directory error

  3. Confirm that the Codex output names Ollama and the selected model before showing an answer to the question.

    The header should show provider: ollama and the same model: tag listed by Ollama. An answer explaining a Git branch shows that the local model completed the task through Codex. A provider error or a header without an assistant answer does not establish that the task finished.

Share this guide