Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 131/ 160
07. Deployment

Ollama for Local Models

Run open models locally for privacy, demos, and offline development.

BeginnerLocal Serve · Model Pull3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Ollama lets you run an AI model on your own computer, so you can build and test without paying per request or sending data to anyone.

Think of it like…

A practice flight simulator on your desk: free to run, safe and available offline, ideal for rehearsing the procedure, but you still check the real aircraft's behaviour before carrying passengers.

Where you’ve seen it

A developer pulls an 8B instruction model in 4-bit quantisation (about 4.9 GB on disk): the weights need roughly 8B × 4.85 bits ÷ 8 ≈ 4.85 GB of memory, plus the KV cache, which for 8,192 tokens of context at 128 KiB per token is about 1 GiB, so around 6 GB in total: comfortable on a 16 GB laptop and tight on 8 GB. Decoding speed is limited by memory bandwidth: a machine with about 100 GB/s of bandwidth has a ceiling of roughly 100 / 4.85 ≈ 20 tokens per second, and one with 400 GB/s about 82 (real speeds are typically 60 to 80% of that). The app's LLM client uses base_url="http://localhost:11434/v1" and a model name, so switching between local and cloud is a configuration change. A Modelfile sets a system prompt and temperature 0.2 and a larger num_ctx (the default context length is small, so long prompts get silently truncated without it). On the team's 300-case golden set the local 8B model passes 71% versus 90% for the production model, so it's used for development and smoke tests (free), while quality decisions and release gates run against the production model. The Ollama port stays bound to localhost; nobody exposes it publicly because it has no authentication.

Words you’ll hear

GGUF / quantised model
A compact file format with low-bit weights for local inference.
Modelfile
A recipe that defines a model's base, system prompt and parameters.
num_ctx
The context window Ollama allocates; too small a default truncates prompts.
OpenAI-compatible endpoint
A local API that accepts the same request shape as hosted chat APIs.

Visual diagram

Loading interactive diagram

Theory & deep dive

How it works and what it needs

Ollama wraps a llama.cpp-style inference engine: models are distributed as GGUF files with weights quantised to 4 to 8 bits (names like q4_K_M), which cut memory and bandwidth needs at a modest quality cost. Memory required ≈ parameters × bits per weight ÷ 8 (an 8B model at about 4.85 effective bits is 4.85 GB) plus the KV cache, which grows with context length (128 KiB per token for an 8B GQA model: 8,192 tokens ≈ 1 GiB) and runtime overhead. Models load into GPU memory if available (partially offloading layers if not) or run on CPU, and unload after a keep-alive period (default a few minutes), so the first request after idle is slow. Decode speed is memory-bandwidth-bound: tokens per second ≈ bandwidth ÷ model size, so a 100 GB/s system has a ceiling near 20 tokens per second for this model and a 400 GB/s Apple-silicon or GPU system near 80.

Commands and API: ollama pull <model>, ollama run <model> (interactive), ollama list, ollama ps (loaded models), ollama create <name> -f Modelfile; the server exposes /api/generate, /api/chat, /api/embeddings and OpenAI-compatible /v1/chat/completions, /v1/embeddings. A Modelfile sets FROM <base>, SYSTEM "...", and PARAMETER temperature 0.2, PARAMETER num_ctx 8192. Environment variables tune behaviour (OLLAMA_HOST, OLLAMA_NUM_PARALLEL for concurrent requests, OLLAMA_MAX_LOADED_MODELS, OLLAMA_KEEP_ALIVE). Embedding models (for local RAG experiments) and vision models are also available.

Memory and speed estimate

8B Q4: weights ≈ 8e9 × 4.85 / 8 = 4.85 GB; KV at 8,192 tokens = 8,192 × 131,072 B = 1.0 GiB; total ≈ 6 GB. Bandwidth 400 GB/s ÷ 4.85 GB ≈ 82 tokens/s ceiling; expect about 50 to 65 in practice.

Key takeaway: Local inference is limited by memory capacity and bandwidth; estimate weights plus KV and tokens-per-second ceilings from bandwidth.

Using local models well, and where they stop

Good uses: privacy-sensitive experiments (data never leaves the machine), offline work, cheap inner loops (iterate on prompts, parsers, tool schemas and UI without spending tokens), CI smoke tests that confirm the pipeline runs end to end with a tiny model, demos, and learning how models behave (temperature, context, quantisation). Keep the client code provider-agnostic (OpenAI-compatible client with a configurable base URL and model name) so the same app runs against Ollama in dev and a production model in staging. Set num_ctx explicitly (a small default context silently truncates long prompts and RAG context), pin the model tag (llama3.1:8b-instruct-q4_K_M) rather than relying on a moving alias and check licences of the models you use.

Limits: quality of small quantised models is lower than frontier APIs (71% versus 90% pass rate on the example golden set), so don't make release or quality decisions from local results; tool-calling and structured-output reliability vary by model; throughput under concurrency is low (a single process on a laptop); there's no authentication or multi-tenant isolation, so never expose the port publicly (keep the default 127.0.0.1 binding and put an authenticated reverse proxy in front if you must share a server); and production serving needs engines built for it (vLLM, TGI) with batching and observability. A practical pattern: a tiered test strategy with local models for fast, free smoke runs, a mid-size hosted model for nightly regression and the production model for release gates and canaries.

Provider-agnostic client

client = OpenAI(base_url=settings.llm_base_url, api_key=settings.llm_key) with llm_base_url = "http://localhost:11434/v1" in dev and the provider URL in staging and production; model = settings.model_id. No code changes between environments.

Key takeaway: Use Ollama for dev, demos and smoke tests with a provider-agnostic client, and verify quality on the production model.

How it works, step by step

Step 1 of 4
Developer

1. Check hardware fit

Estimate weights, KV cache and bandwidth; choose model size and quantisation for the machine.

8B Q4 ≈ 6 GB at 8k context
Key numbers at this step
Speed ceiling
bandwidth ÷ size

When to use it & trade-offs

What it is

Ollama is a tool for running open-weight language models locally with one command. It downloads quantised model files (GGUF), manages them like images (ollama pull, ollama list, ollama run), starts a local server on localhost:11434 with its own REST API and an OpenAI-compatible endpoint (/v1/chat/completions), and lets you customise models with a Modelfile (base model, system prompt, parameters such as temperature and context length). It uses CPU, Apple silicon unified memory or GPUs depending on the machine. Typical uses are privacy-sensitive prototyping, offline development, demos, CI smoke tests and a cheap inner loop that avoids API costs, not production serving at scale.

Why it matters: Local models remove per-token cost, network latency and data-sharing concerns from the development loop, and let you test an application against a real LLM on a plane or in a locked-down environment. They also teach the constraints of open models (memory, quantisation, speed, context length). But local 8B-class models are weaker than frontier APIs and a laptop is not a serving platform, so the main skill is knowing what to trust local results for and what to verify against the production model.

When to reach for it

  • Prompt and tool iteration without API cost
  • CI smoke tests and offline demos
  • Privacy-sensitive prototypes

When to avoid or be careful

  • Using local results to judge production quality
  • Exposing the Ollama port to the network
  • Leaving the default context window for long prompts

Running models

Ollama (local)
Best for: Development, demos, privacy and offline work

Strengths: Free per token; simple; private.

Watch out: Weaker models; limited throughput; no auth.

vLLM or TGI (self-hosted servers)
Best for: Production serving of open models

Strengths: Batching; high throughput; observability.

Watch out: GPU infrastructure and operations.

Hosted provider API
Best for: Best quality and zero infrastructure

Strengths: Frontier models; elastic.

Watch out: Cost and data sharing.

Interview signals & numbers to know

When this topic comes up

  • “Can we run it locally?”
  • GGUF and quantisation
  • Modelfile
  • OpenAI-compatible local endpoint
  • num_ctx truncation

Benchmarks & rules of thumb

~4.9 GB + ~1 GiB -
8B Q4 weights plus KV at 8k context
≈ 20 / 82 tok/s -
bandwidth ceilings at 100 and 400 GB/s
71% vs 90% -
local versus production model pass rate in the example

Common mistakes & follow-ups

Common interview mistakes

  • Assuming a prompt fits when the default context silently truncates it.
  • Exposing 0.0.0.0:11434 on a shared network.
  • Benchmarking quality on a heavily quantised small model.

Likely interviewer follow-ups

  • How would you use a local model in a development workflow?
  • What limits local inference speed?
  • Why is Ollama unsuitable for production at scale?
Interview tip: Estimate tokens per second from bandwidth ÷ model size. It shows you understand why local speed varies by machine.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Ollama for Local Models with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
GPU Instances
Next topic
Autoscaling