How to Run OpenHands with Ollama: Local AI Coding Agent

What you are building

OpenHands is an open-source coding agent that reads a repository, edits files, runs shell commands, and tests its own work. It used to need a frontier API key to be useful. Paired with Ollama, the whole loop runs on your own machine: no code leaves your network, and there is no per-task bill. This guide wires the two together in about 30 minutes.

One note on names: the project now ships as Agent Canvas, a self-hosted control center for coding agents, and the OpenHands agent runs inside it out of the box. That is the setup used here.



Source: OpenHands Docs — the Advanced LLM settings where you point Agent Canvas at Ollama.

What you need

  • A GPU with at least 24 GB of VRAM for the recommended model, or a Mac with Apple Silicon and 64 GB of unified memory.
  • Docker installed and running.
  • Ollama installed, with free disk space for a model download in the tens of gigabytes.
  • A project folder you are happy to let an agent edit.

Step 1: Pull a coding model with Ollama

OpenHands' own docs recommend Qwen3.6-35B-A3B as the first local model to try: an open-weight mixture-of-experts model built for agentic coding, with a large context window. Pull it with:

ollama pull qwen3.6:35b-a3b

If your hardware is lighter, start with Qwen3.5 9B instead. An independent guide verified that exact setup on a 16 GB M3 Mac, with Ollama running natively and OpenHands in Docker. Smaller models fumble tool calls more often, so treat the 9B as a way to learn the workflow rather than the final setup.



Source: All Things How — Qwen3.6-35B-A3B running locally.

ollama pull qwen3.5:9b

Step 2: Start Ollama with a large enough context window

Ollama's default context window is 4,096 tokens. OpenHands' docs are blunt about this: at that size, not even the agent's system prompt fits. The minimum is about 22,000 tokens, and 32,768 is the recommended value. Start Ollama with the larger window, listening on all interfaces so the Docker container can reach it:

OLLAMA_CONTEXT_LENGTH=32768 OLLAMA_HOST=0.0.0.0 OLLAMA_KEEP_ALIVE=-1 ollama serve

Then check that the endpoint answers:

curl -s localhost:11434/v1/models

You should see your model listed. If this call fails, fix Ollama before touching OpenHands. Every later error will look like an OpenHands problem when it is not.

Step 3: Start Agent Canvas with Docker

Create a workspace folder for the projects the agent may touch. The agent only sees what you mount here.

export PROJECTS_PATH="$HOME/projects"
mkdir -p "$PROJECTS_PATH"

Then start Canvas:

docker run -it --rm \
  -p 127.0.0.1:8000:8000 \
  -e AGENT_CANVAS_ALLOW_LAN_SESSION_KEY=true \
  -v openhands-state:/home/openhands/.openhands \
  -v "${PROJECTS_PATH}:/projects" \
  --add-host host.docker.internal:host-gateway \
  ghcr.io/openhands/agent-canvas:1.40.0

The --add-host flag matters on Linux, where host.docker.internal does not resolve on its own. Docker Desktop on macOS and Windows resolves it automatically, so the flag is harmless there. Open http://localhost:8000/canvas once the container is up.

Step 4: Point Agent Canvas at Ollama

On first launch, Canvas asks you to set up an LLM. Open Settings > LLM, switch to the Advanced tab, and enter these values:

  • Custom Model: openai/qwen3.6:35b-a3b (or openai/qwen3.5:9b for the lighter model)
  • Base URL: http://host.docker.internal:11434/v1
  • API Key: local-llm

The openai/ prefix is not a mistake. It tells Canvas to speak the OpenAI-compatible protocol, which is exactly what Ollama's /v1 endpoint provides. The part after the slash must match the model name Ollama lists, so check it with the curl command from step 2 if you are unsure. Ollama never checks the API key, but the field cannot be empty, so any placeholder works. Save the profile, start a new conversation, and send a short message to confirm the model answers.

Use host.docker.internal, never localhost. Inside the container, localhost means the container itself, where no Ollama exists.

Step 5: Give the agent its first task

Pick a small, verifiable job in a project under ~/projects, something with a clear pass condition, like "add a unit test for the login function and run the test suite" or "find why this script fails on empty input and fix it." Watch the first run instead of handing over a big refactor: the event log shows each command the agent runs. A good first task finishes in a few minutes and leaves a diff you can read. If it does, scale up from there.

What to expect from a local model

A local model is not a frontier model. Expect slower reasoning, the occasional loop, and tool calls that need a nudge. The docs warn that local models vary widely in tool-use reliability. Qwen3.6-35B-A3B is the recommended starting point precisely because smaller models stall on long agentic runs. Keep tasks small, keep the repo clean, and review every diff before you merge it. The agent works inside the mounted workspace, so it cannot reach files outside ~/projects unless you mount them.

Troubleshooting

  • Connection refused on the first model call: the Base URL is wrong, or Ollama is not listening on all interfaces. Check that the URL says host.docker.internal, confirm OLLAMA_HOST=0.0.0.0, and on Linux confirm the --add-host flag.
  • The agent answers like a chatbot and never touches files: the model is too weak, or the context window is too small. Raise OLLAMA_CONTEXT_LENGTH to 32768, or move to the 35B model.
  • The agent loops on the same failing command: stop the run, shrink the task, and paste the error back in plain words. Long, vague tasks are where local models fall apart first.
  • Canvas will not start: Docker is not running, or port 8000 is taken. Check docker ps and pick a free port.

Bottom line

That is the whole setup: Ollama serving a coding model, Agent Canvas pointing at it, and a mounted folder the agent is allowed to touch. It costs nothing per task and keeps your code on your own disk. If you want a lighter local coding helper instead of a full autonomous agent, our OpenCode with Ollama guide and Aider with Ollama guide cover the terminal-based options, and the Pydantic AI agent tutorial shows how to build a small agent of your own.

Comments

Popular posts from this blog

Open-Source AI Weekly: Bonsai, GigaChat 4 Lead

Claude Claims Proof of Riemann Hypothesis After 167 Years

How to Build an AI Agent with LangGraph in 30 Minutes