April 28, 2026Ā·7 minĀ·By

Run LLMs Locally with Ollama: Complete Setup and API Guide

Ollamalocal LLMLlamaprivacyAIPython

Cloud LLMs are convenient but expensive, rate-limited, and send your data to third parties. Ollama makes local LLMs as easy as running any other dev tool.

Installation

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

# Or via Homebrew
brew install ollama

# Windows: download from ollama.com

Running Models

# Pull and run a model (interactive chat)
ollama run llama3.2

# Available models
ollama run mistral
ollama run codellama
ollama run phi4
ollama run gemma3

# List downloaded models
ollama list

# Remove a model
ollama rm llama3.2

REST API

Ollama runs a local server at http://localhost:11434:

# Chat completion
curl http://localhost:11434/api/chat -d '{
    "model": "llama3.2",
    "messages": [{"role": "user", "content": "Explain async/await in JavaScript"}],
    "stream": false
}'

# Generate (completion mode)
curl http://localhost:11434/api/generate -d '{
    "model": "llama3.2",
    "prompt": "Write a Python function to reverse a string",
    "stream": false
}'

Python Integration

import requests

def chat(model: str, message: str, system: str = None) -> str:
    messages = []
    if system:
        messages.append({"role": "system", "content": system})
    messages.append({"role": "user", "content": message})

    response = requests.post(
        "http://localhost:11434/api/chat",
        json={"model": model, "messages": messages, "stream": False}
    )
    return response.json()["message"]["content"]

result = chat("llama3.2", "What is pgvector?")
print(result)

Or use the OpenAI-compatible API:

from openai import OpenAI

# Ollama is OpenAI-compatible
client = OpenAI(
    base_url="http://localhost:11434/v1",
    api_key="ollama"  # Any non-empty string
)

response = client.chat.completions.create(
    model="llama3.2",
    messages=[{"role": "user", "content": "Explain reentrancy attacks"}]
)
print(response.choices[0].message.content)

This means you can swap Ollama for OpenAI or vice versa with a single line change.

Streaming Responses

import json

def stream_chat(model: str, message: str):
    response = requests.post(
        "http://localhost:11434/api/chat",
        json={"model": model, "messages": [{"role": "user", "content": message}], "stream": True},
        stream=True
    )

    for line in response.iter_lines():
        if line:
            chunk = json.loads(line)
            if not chunk.get("done"):
                print(chunk["message"]["content"], end="", flush=True)
    print()

stream_chat("llama3.2", "Write a Python web scraper")

Model Selection Guide

Model Size Best For
llama3.2:1b 1.3GB Fast, low-memory testing
llama3.2:3b 2.0GB Good balance of quality/speed
llama3.2 (8b) 4.7GB Recommended for most tasks
mistral 4.1GB Strong reasoning, multilingual
codellama 3.8GB Code generation
phi4 9.1GB Strong reasoning, Microsoft
llama3.1:70b 40GB Near-GPT-4 quality locally

Models below 8B need at least 8GB RAM. 70B models need 64GB+ RAM or a powerful GPU.

Custom Modelfile

Customize a model's system prompt and parameters:

# Modelfile
FROM llama3.2

SYSTEM """
You are an expert Solidity developer. You write gas-efficient, secure smart contracts.
When reviewing code, always check for reentrancy, integer overflow, and access control issues.
"""

PARAMETER temperature 0.3
PARAMETER num_ctx 8192
ollama create solidity-expert -f Modelfile
ollama run solidity-expert

When to Use Local vs Cloud

Use Ollama locally when:

  • Processing sensitive/private data (medical records, legal documents, PII)
  • High volume with predictable traffic (avoid rate limits)
  • Offline or air-gapped environments
  • Prototyping (no API costs)

Use cloud LLMs (Claude, GPT-4) when:

  • Need best-in-class quality (Llama 8B is good, not GPT-4 level)
  • Unpredictable traffic spikes
  • Need multimodal capabilities (vision, audio)
  • Team doesn't have the hardware

Ollama is excellent for internal tools, privacy-sensitive apps, and cost optimization at scale.

K
Founder & Technical Lead, Innovibe

Building software for 15+ years. Passionate about AI, system design, and shipping things that work.

Frequently asked questions

Do I need a GPU to run this in production?+

For hosted APIs (OpenAI, Anthropic, Google) — no. You call an HTTPS endpoint. GPUs only matter if you're self-hosting models, which is overkill for most production use cases.

How do I keep LLM costs under control?+

Cache identical prompts aggressively, use the smallest model that meets your quality bar, and set hard token limits per request. A response cache alone can cut costs 40–60% on typical workloads.

What's the difference between fine-tuning and RAG?+

Fine-tuning bakes knowledge into model weights — expensive, slow to update. RAG retrieves context at query time — cheap to update, easier to debug. Use RAG for most production use cases and fine-tune only when you need a very specific tone or format.

How do I prevent an AI agent from doing something destructive?+

Design for reversibility first — log before you act, prefer soft-deletes, require human confirmation for irreversible actions. Add explicit guardrails in your system prompt and test with adversarial inputs before going live.

Does Innovibe build this kind of thing for clients?+

Yes — this is exactly what we do day-to-day for clients across BC and Canada. If you'd rather have us build and maintain it than implement it yourself, reach out.

Building something with AI?

We scope and ship AI features quickly. Let's talk.

Start a Conversation