Overview

Ollama runs open-weight models on your own hardware and serves them over a local HTTP API, including an OpenAI-compatible one. This guide installs it, runs a model, customizes it with a Modelfile, and calls the API. Which model and quantization to choose for your memory budget is covered in ollama; the Modelfile reference is ollama-modelfile.

Prerequisites

  • macOS, Windows, or Linux with enough memory for the model you pick; larger models need a GPU or substantial unified memory. Check the model’s page in the Ollama library for its size before pulling.
  • curl for API tests, and Python 3.10 or newer for the SDK example.

Steps

1. Install

Download the installer for macOS or Windows from ollama.com/download. On Linux:

curl -fsSL https://ollama.com/install.sh | sh

Confirm the server is up:

ollama --version
curl -s http://localhost:11434/api/tags      # JSON list of installed models

If the server is not running, start it with ollama serve.

2. Pull and run a model

ollama pull llama3.2:3b
ollama list
ollama run llama3.2:3b          # interactive; /bye to exit

ollama ps shows which models are loaded.

3. Bake in a system prompt and context size

cat > Modelfile <<'EOF'
FROM llama3.2:3b
PARAMETER num_ctx 8192
SYSTEM """
You are a concise technical writer. Reply in plain text. Lead with the rule, then the rationale.
"""
EOF
ollama create writer -f Modelfile
ollama run writer

Set num_ctx explicitly; a larger context uses more memory, so size it to the task. Prompt-writing rules: system-prompts.

4. Call the REST API

curl http://localhost:11434/api/generate -d '{"model":"llama3.2:3b","prompt":"Explain HNSW in two sentences.","stream":false}'

5. Use the OpenAI-compatible endpoint

Ollama serves /v1/chat/completions, /v1/embeddings, and /v1/responses. Point an OpenAI client at it; the key is required by the client but ignored.

from openai import OpenAI
 
client = OpenAI(base_url="http://localhost:11434/v1/", api_key="ollama")
r = client.chat.completions.create(model="llama3.2:3b",
                                   messages=[{"role": "user", "content": "What is RAG?"}])
print(r.choices[0].message.content)

The compatibility layer covers a subset of the OpenAI API; for example, logprobs and tool_choice are not supported. For embeddings, pull an embedding model such as nomic-embed-text and call /v1/embeddings; see embeddings.

Verify it worked

ollama list | grep llama3.2
curl -s http://localhost:11434/v1/chat/completions -H "Content-Type: application/json" \
  -d '{"model":"llama3.2:3b","messages":[{"role":"user","content":"Say OK"}]}' | python3 -c "import sys,json; print(json.load(sys.stdin)['choices'][0]['message']['content'])"

Common errors

  • Connection refused on port 11434. The server is not running; start the app or run ollama serve.
  • A pull fails with a manifest error. The model name or tag is wrong; check the Ollama library.
  • Out-of-memory or very slow output. The model does not fit in GPU or unified memory; choose a smaller or more quantized variant (ollama).
  • A system prompt seems ignored. Confirm you ran the model created from the Modelfile, not the base model.

FAQ

What port does Ollama use?

Ollama serves its API on http://localhost:11434; a connection refused there means the server is not running, so start the app or run ollama serve.

Can I use the OpenAI SDK with Ollama?

Yes; point the client at http://localhost:11434/v1/ with any API key, which the client requires and Ollama ignores. The layer covers a subset of the API, and logprobs and tool_choice are not supported.

How do I set a system prompt and context size in Ollama?

Write a Modelfile with FROM, PARAMETER num_ctx, and SYSTEM, then run ollama create <name> -f Modelfile. Set num_ctx explicitly, because a larger context uses more memory.

Why is Ollama slow or running out of memory?

The model does not fit in GPU or unified memory; choose a smaller or more quantized variant.