Ollama server: run it, expose it safely, connect agents
Run Ollama as a server for coding agents: ollama serve and the systemd service, OLLAMA_HOST, a 64K context, reaching it over SSH or Tailscale, the /v1 endpoints.
ollama serve runs Ollama’s server, and the Linux installer sets it up as the ollama systemd service, so on most machines it is already running. It listens on 127.0.0.1:11434, which only that machine can reach; set OLLAMA_HOST to change the address. The API has no authentication, so anyone who can reach the port can use it: reach a GPU machine from another computer through an SSH tunnel or a private network such as Tailscale, never the open internet. For coding agents, also raise OLLAMA_CONTEXT_LENGTH to at least 64000. The current release is v0.40.19 (November 26, 2026).
Starting the server
On Linux, the install script creates an ollama user and a service at /etc/systemd/system/ollama.service whose ExecStart is /usr/bin/ollama serve, so the server starts at boot. Running the same script again updates it:
curl -fsSL https://ollama.com/install.sh | shCopiedIf you installed by hand, or want to watch the server’s output while you try settings, run it in the foreground; its log prints in that terminal. Stop the service first with sudo systemctl stop ollama, or the second copy cannot take port 11434:
ollama serveCopiedOn a Mac or Windows, the Ollama app runs the server in the background while it is open. In Docker, Ollama’s own command publishes the port with -p 11434:11434, which opens it on every interface of the host, and Docker’s documentation says traffic to a published port “gets diverted before it goes through the ufw firewall settings.” On a machine with a public address, anyone could then use the server. Publish it on the host’s loopback address instead:
docker run -d -v ollama:/root/.ollama -p 127.0.0.1:11434:11434 --name ollama ollama/ollamaCopiedAdd --gpus=all after -d for an NVIDIA card. How to see what a server actually exposes is in checking open ports on Linux.
Checking it is up
Two documented endpoints answer without loading a model. The first returns the server’s version, the second the models it has pulled:
curl http://127.0.0.1:11434/api/versionCopiedcurl http://127.0.0.1:11434/api/tagsCopiedRun them from the machine your agent runs on, with the address the agent uses for Ollama in place of 127.0.0.1: an answer there is the one that matters. ollama ps lists the models loaded now, with the context each was given. When something fails, the service’s log is the first place to look:
journalctl -u ollama --no-pager --follow --pager-endCopiedOn a Mac it is ~/.ollama/logs/server.log; on Windows, server.log in %LOCALAPPDATA%\Ollama.
Changing the server’s settings
Every server setting is an environment variable read when the server starts. Setting one in your shell changes nothing for the systemd service, which has its own environment. Ollama’s FAQ gives a systemd override for this, kept in a file of its own beside the service. Open it with:
sudo systemctl edit ollama.serviceCopiedand add the lines you need under [Service]. These keep the server on this machine only, give agents a 64K context and hold a model in memory for an hour between requests:
[Service]
Environment="OLLAMA_HOST=127.0.0.1:11434"
Environment="OLLAMA_CONTEXT_LENGTH=64000"
Environment="OLLAMA_KEEP_ALIVE=1h"CopiedThen reload and restart:
sudo systemctl daemon-reload
sudo systemctl restart ollamaCopiedOn a Mac, set each one with launchctl setenv OLLAMA_CONTEXT_LENGTH 64000 and restart the app. On Windows, quit Ollama from the taskbar, add the variables to your user account’s environment variables, and start it again from the Start menu.
The variables that matter for an agent
| Variable | Default | What it does |
|---|---|---|
OLLAMA_HOST | 127.0.0.1:11434 | The address and port the server listens on |
OLLAMA_CONTEXT_LENGTH | Depends on the GPU (below) | The context a model is loaded with unless a request or model says otherwise |
OLLAMA_KEEP_ALIVE | 5m | How long a model stays in memory after its last request; a request’s own keep_alive overrides it, and -1 keeps it loaded |
OLLAMA_NUM_PARALLEL | 1; on a Mac running MLX, chosen by memory | Requests each model handles at once; more needs more memory |
OLLAMA_MAX_LOADED_MODELS | 3 per GPU, or 3 on CPU | Models held in memory at once, if they fit |
OLLAMA_ORIGINS | 127.0.0.1, 0.0.0.0 | Extra origins allowed to call the server from a browser page or extension |
OLLAMA_MODELS | /usr/share/ollama/.ollama/models on Linux | Where pulled models are stored; the ollama user needs read and write access |
OLLAMA_NO_CLOUD | Unset | Turns off Ollama’s cloud models and web search |
OLLAMA_ORIGINS is about browsers only: a coding agent in a terminal sends no origin, so it never needs it. An agent that pauses for a minute and then waits a long time on its next request is often reloading a model that the five-minute default let go.
Context length is the setting agents trip on
Ollama’s context length page sets the default by the GPU’s memory: 4k under 24 GiB, 32k from 24 to 48 GiB, and 256k at 48 GiB or more. Its FAQ still says “By default, Ollama uses a context window size of 4096 tokens,” which is the first of those cases. Either way, a GPU with less than 24 GiB serves 4,096 tokens, and a coding agent’s instructions and tool definitions alone can exceed that: the model then answers without parts of what it was told. The same page says:
Tasks which require large context like web search, agents, and coding tools should be set to at least 64000 tokens.CopiedSet it on the server as above, then check the CONTEXT column of ollama ps after the agent’s first request. More context takes more memory, and a model that no longer fits spills onto the CPU, which ollama ps shows in its PROCESSOR column and the agent feels as a slowdown. The OpenAI-compatible API has no way to ask for a larger context per request, so for agents that talk to /v1, the server’s setting is the one that counts.
Reaching it from another machine
A common setup puts Ollama on a machine with a GPU and the agent on a laptop or a small server. Ollama’s authentication page is plain about the local server: “The local API at http://localhost:11434 does not require authentication.” Whoever can reach the port can use the whole API with your models and your GPU. So the question is only who can reach it.
An SSH tunnel needs no change on the GPU machine at all. Ollama stays on 127.0.0.1, and this, run on the agent’s machine, makes the remote server appear at 127.0.0.1:11434 there for as long as it runs:
ssh -N -L 11434:127.0.0.1:11434 you@gpu-boxCopiedIf the agent’s machine runs its own Ollama on 11434, use another local port, such as -L 11435:127.0.0.1:11434, and point the agent there. More on forwards in opening a localhost app on a remote server.
A private network such as Tailscale or your LAN lets every device on it reach the server without a tunnel. The bind address that does it, 0.0.0.0, is every interface, a public one included, so on a machine with a public address, first keep 11434 closed to the internet in the provider’s or the host’s firewall. Then replace the OLLAMA_HOST line in the override:
[Service]
Environment="OLLAMA_HOST=0.0.0.0:11434"CopiedReload and restart as before, check from outside the private network that the port does not answer, and use the machine’s private name or address in the agent, for example http://gpu-box:11434. How a tailnet name and Tailscale’s own sharing work is in Tailscale Serve and Funnel.
Ollama’s FAQ also shows an ngrok and a Cloudflare Tunnel command. Both give the server a public address with nothing in front of it, so anyone who finds the URL can use your GPU. If it has to be on the internet, put a proxy that checks a password or a token in front of it. Ollama’s own proxy examples rewrite the Host header to localhost:11434, which any proxy in front of it should copy.
The addresses agents use
One server answers three APIs on the same port. Which one an agent wants decides whether its base URL ends in /v1:
| API | Base URL | Who uses it |
|---|---|---|
Ollama’s own | http://127.0.0.1:11434 (paths under /api) | The ollama CLI; OpenClaw’s Ollama provider |
OpenAI-compatible | http://127.0.0.1:11434/v1 | OpenCode, Hermes Agent, Codex |
Anthropic-compatible | http://127.0.0.1:11434 (Messages at /v1/messages) | Claude Code |
The OpenAI-compatible side serves chat completions, completions, models, embeddings and /v1/responses, the API Codex uses: its Ollama profile sets wire_api = "responses" and asks for at least 64k of context. Both compatible APIs need a key in the client, and Ollama ignores it, so any placeholder such as ollama works. The Anthropic side does not support token counting, tool_choice, prompt caching or batches. For most of these agents, ollama launch <agent> writes the settings for you; the linked guides cover each agent’s settings, its context needs and the models that work with it.
LM Studio’s server, for comparison
LM Studio runs the same kind of server: the Start server switch in its Developer tab, or lms server start from a terminal, with OpenAI- and Anthropic-compatible endpoints. Its bind address is also 127.0.0.1 by default, changed with --bind or LMS_SERVER_HOST, and --port falls back to the last port used; its docs’ examples use 1234. Unlike Ollama, it can require a key: authentication is off by default and turned on under Developers Page, Server Settings, after which clients send Authorization: Bearer with a token. LM Studio recommends that for any bind other than 127.0.0.1.
Version
The latest release is v0.40.19, published November 26, 2026, with a v0.40.20 release candidate out since November 28. v0.40.16 and v0.40.17, earlier in November, changed what the compatible APIs do: /v1/responses now honours truncation instead of cutting silently, /v1/models returns an empty list rather than null when nothing is installed, and tool definitions keep their JSON schema keywords. Your server’s own version is what /api/version returns.