Overview

Ollama serves HTTP on 127.0.0.1:11434 with no authentication. Every endpoint family below shares that server, so exposing the port exposes inference, model loading, and model deletion. Configure the server with environment variables, and put an authenticating proxy in front before binding to a network interface.

Use the native API for full control

POST /api/chat and POST /api/generate accept Ollama-specific fields: options, format, think, and keep_alive. Responses stream by default; set "stream": false for batch pipelines.

curl http://localhost:11434/api/chat -d '{
  "model": "qwen3.8:27b",
  "messages": [{"role": "user", "content": "Summarize the GDPR in three bullets."}],
  "stream": false,
  "options": {"temperature": 0.3, "num_ctx": 8192, "num_predict": 512}
}'
  • num_predict caps output tokens; always set it in batch jobs to stop runaway generation.
  • num_ctx here overrides the server default for that request; larger values cost more memory.
  • Use /api/embed for embeddings. It accepts an input array plus truncate (default true), dimensions, options, and keep_alive, and returns L2-normalized vectors. The older /api/embeddings is single-prompt and superseded.
  • Streaming returns one JSON object per line; the last has "done": true and the token counts and timings.

Use the OpenAI-compatible endpoints to reuse existing clients

Ollama implements /v1/chat/completions, /v1/completions, /v1/embeddings, /v1/models, and /v1/responses (stateless only).

from openai import OpenAI
 
client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")  # key is required by the SDK, ignored by Ollama
r = client.chat.completions.create(
    model="qwen3.8:27b",
    messages=[{"role": "user", "content": "Explain HNSW in one paragraph."}],
)
  • The model name is the exact Ollama tag, not an OpenAI model name.
  • Unsupported: logprobs, tool_choice, n, logit_bias, image URLs (send base64 images). Tool calling and vision depend on the model.
  • The OpenAI API has no way to set context size. Create a Modelfile variant with PARAMETER num_ctx (see ollama-modelfile) or raise OLLAMA_CONTEXT_LENGTH.

Point Anthropic-format clients at /v1/messages

/v1/messages accepts the Anthropic Messages format, which lets Claude Code run against a local model.

export ANTHROPIC_BASE_URL=http://localhost:11434
export ANTHROPIC_AUTH_TOKEN=ollama   # required by the client, ignored by Ollama
claude --model qwen3-coder

ollama launch claude does the same setup interactively.

  • Unsupported or partial: forced tool_choice, prompt caching, the Batches API, PDF input, and URL images. budget_tokens is accepted but not enforced, and token counts are approximate.
  • Give the model at least 64K context (OLLAMA_CONTEXT_LENGTH=65536) or agent loops truncate. See claude-code.

Constrain output with the format field

Set format to "json" or to a JSON Schema object. Put the schema in the prompt as well, and use a low temperature.

import ollama
from pydantic import BaseModel
 
class Contact(BaseModel):
    name: str
    email: str
 
r = ollama.chat(model="qwen3.8:27b", format=Contact.model_json_schema(),
                messages=[{"role": "user", "content": "Alice Smith, alice@acme.com"}])
print(Contact.model_validate_json(r.message.content))

The OpenAI-compatible endpoints take response_format instead. Validate the result anyway; see structured-output.

Set concurrency and keep-alive deliberately

VariableDefaultEffect
OLLAMA_NUM_PARALLEL1Parallel requests per loaded model. Memory scales with NUM_PARALLEL x CONTEXT_LENGTH
OLLAMA_MAX_LOADED_MODELS3 per GPU (3 on CPU)Models resident at once, if they fit
OLLAMA_MAX_QUEUE512Queue depth before the server returns 503
OLLAMA_KEEP_ALIVE5mHow long an idle model stays loaded
  • Lower OLLAMA_MAX_QUEUE in batch pipelines so overload fails fast.
  • The per-request keep_alive field overrides OLLAMA_KEEP_ALIVE. Use -1 to pin a model, 0 to unload it.
  • Raise OLLAMA_NUM_PARALLEL only after checking ollama ps for headroom; benchmark with your real request mix.

Run under systemd or Docker

On Linux the install script (curl -fsSL https://ollama.com/install.sh | sh) creates the ollama user and service. Change settings with a drop-in instead of rewriting the unit:

sudo systemctl edit ollama   # add under [Service]: Environment="OLLAMA_HOST=0.0.0.0:11434" etc.
sudo systemctl restart ollama
journalctl -e -u ollama

For containers, mount a volume so models survive restarts:

docker run -d --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
# AMD: add --device /dev/kfd --device /dev/dri and use the ollama/ollama:rocm image
  • NVIDIA containers need the NVIDIA Container Toolkit configured for Docker.
  • On macOS, run Ollama natively so it can use Metal, and point containers at host.docker.internal:11434.
  • Move model storage to a fast, large disk with OLLAMA_MODELS=/data/ollama/models.

Authenticate before exposing the port

OLLAMA_HOST=0.0.0.0:11434 binds every interface. Keep the default bind and publish through a reverse proxy that enforces auth; use OAuth2 proxy or Cloudflare Access for teams.

location / {
    auth_basic "Ollama";
    auth_basic_user_file /etc/nginx/.htpasswd;
    proxy_pass http://127.0.0.1:11434;
    proxy_set_header Host localhost:11434;
    proxy_buffering off;        # keeps streaming responses streaming
    proxy_read_timeout 600s;    # long generations
}

Add browser origins with OLLAMA_ORIGINS only when a web app calls Ollama directly.