Skip to content

Running OpenCode Agentic AI workflows with local LLM models on NCShare with an Ollama Server

This is a guide on setting up OpenCode to run Agentic AI workflows with local LLM models on NCShare. The model is served through an Ollama instance hosted on a GPU node and OpenCode can be run on any other node as a client connecting to that server.

In certain scenarios you may want to move away from cloud-based LLMs to locally hosted ones for your agentic AI workflows as they offer benefits including data privacy, offline functionality, freedom from subscription fees, API rate limits, and avoid censorship.

In this guide, we will launch an Ollama server on a GPU node, and set up OpenCode to communicate with models hosted locally for agentic AI workflows.

Prerequisites

The first step is to set up OpenCode on NCShare as instructed in the tutorial, Setting up OpenCode on NCShare.

The next steps involve building the Ollama Apptainer container and starting up an Ollama server on a GPU node as described in the guide Running an LLM server on NCShare with Ollama. Essentially, launching OpenCode with a local LLM replaces the Running inference sessions section in that guide. Please follow these steps before proceeding to the next section.

Connecting OpenCode to the Ollama server

Once you have the Ollama server launched on the GPU node, export the environment variables on the client shell where you will run OpenCode,

export OLLAMA_HOST="http://compute-gpu-03:11434"
export OLLAMA_MODELS=/work/${USER}/.ollama/models

Replace compute-gpu-03 and the port with the host and port printed by ollama_server_apptainer.sh when you started the server.

We then set up OpenCode to connect to it to use local LLM models.
Copy the following opencode.json configuration file to ~/.config/opencode/opencode.json.

~/.config/opencode/opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "default_agent": "plan",
  "permission": {
    "edit": "ask"
  },
  "provider": {
    "ollama": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Ollama (local)",
      "options": {
        "baseURL": "{env:OLLAMA_HOST}/v1"
      },
      "models": {
        "llama4:scout": {},
        "llama3.2:latest": {}
      }
    }
  }
}

The ollama provider section here enables OpenCode to retrieve models from an Ollama server. Here, we have the models llama4:scout and llama3.2:latest, but you may download models from Ollama or Hugging Face. Models can be downloaded by running ollama pull <model_name> with OLLAMA_HOST exported as above, and will be stored in the directory specified by the OLLAMA_MODELS environment variable. The model names listed here must match the names on the server, which you can check with ollama list.

Important!

If you will be using opencode to perform computationally intensive tasks, first request an interactive session on NCShare and run it on that.

Next, start OpenCode with the command,

opencode

and run the /models command there to select a model. You will see the local LLM models served through Ollama under the category, "Ollama (local)".
Now you can run your agentic AI workflows without having to worry about data privacy or exhausting your cloud AI tokens. An example agentic workflow to perform an Equation of State (EOS) analysis can be found in the tutorial, Performing Equation of State Analysis using Agentic AI Skills.

Using other agentic AI tools

OpenCode is not the only option here. The Ollama server exposes an OpenAI-compatible API, so any coding agent that can be pointed at a custom OpenAI-style endpoint can share the same server.

Codex CLI is configured the same way through ~/.codex/config.toml,

~/.codex/config.toml
model = "llama4:scout"
model_provider = "ollama"

[model_providers.ollama]
name = "Ollama (local)"
base_url = "http://compute-gpu-03:11434/v1"
env_key = "OLLAMA_API_KEY"
wire_api = "chat"

The model entry must name a model you have pulled onto the server. Unlike opencode.json, Codex CLI does not expand environment variables inside base_url, so you will need to write out the address from your OLLAMA_HOST value in full, e.g. http://compute-gpu-03:11434/v1. Ollama does not require authentication, but Codex CLI still expects the variable named by env_key to be set, so export OLLAMA_API_KEY with any non-empty placeholder value. Setting wire_api = "chat" tells Codex CLI to use the Chat Completions API rather than the Responses API.

Once you are done with your session, stop the server we started on the GPU node with,

apptainer instance stop ollama-$USER

Comments