Generative AI & LLM Apps
Topic 5 · Building LLM apps · ⏱ about 2 h

Run LLMs on Your Own Computer

Why and when to run an open-weight model locally, how much memory it needs and why, what quantisation and GGUF files are, the main tools (Ollama, LM Studio, llama.cpp), and calling a local model from Python.

Show:
In this topic
Beginner

Why run a model locally?

An open-weight model is one whose trained weights you can download, such as Llama, Qwen, Gemma or Mistral. Running one on your own computer has real advantages:

  • Privacy: nothing you type leaves your machine, which matters for health, legal or company data.
  • Cost: no charge per token, so it suits big batch jobs such as labelling 50,000 documents.
  • Offline and under your control: it works without internet, and the model never changes under you.

The trade-offs: local models that fit on ordinary hardware are smaller and weaker than the best hosted models at hard reasoning and long tasks, and you are responsible for the hardware and setup. A common pattern is a small local model for simple, private or high-volume steps, and a hosted model for the hard ones.

Beginner

How much memory does a model need?

Almost everything comes down to memory, usually the VRAM on a graphics card, or the unified memory on an Apple Silicon Mac. A model needs room for three things:

  1. Weights: parameters × bits per weight ÷ 8. An 8-billion-parameter model at 16 bits is 8 × 16 ÷ 8 = 16 GB.
  2. KV cache: the keys and values for every token in the conversation (see transformers): 2 × layers × KV heads × head size × tokens × bytes. For Llama 3.1 8B at 8,192 tokens it is about 1 GB, and it grows in a straight line with the context length.
  3. Runtime: the program and its working buffers, roughly 0.5–1 GB.

Speed when writing is limited mainly by memory bandwidth: to produce each token, the whole model has to be read from memory once. So tokens per second ≈ bandwidth ÷ model size. A graphics card with 900 GB/s writes far faster than a laptop CPU with 60 GB/s, even when both can hold the model.

Try your own hardware in the LLM VRAM calculator.

Try it

Try it: what fits in memory?

Change the model size, the bits per weight and the context length, and compare with common memory sizes.

Interactive: this part runs in your browser.

Beginner

Quantisation and GGUF files

Quantisation stores each weight with fewer bits. Weights are trained in 16 bits, but rounding them to 8 or about 4 bits loses surprisingly little quality, while cutting memory by two to four times and making the model faster.

LevelBits per weight (about)8B modelQuality
FP16 / BF161616 GBthe original
Q8_08.58.5 GBpractically identical
Q5_K_M5.75.7 GBvery close
Q4_K_M4.854.9 GBthe usual sweet spot: a small drop
Q3_K_M, Q2_K3–43–4 GBnoticeably worse; a last resort

These names come from GGUF, the file format used by llama.cpp and the tools built on it. "K" quantisations store weights in small blocks, each with its own scale, and give some sensitive layers more bits, which is why the bits per weight are a little above the number in the name.

A rule of thumb: a bigger model at Q4 usually beats a smaller model at Q8 of the same file size. Below Q4, quality falls off faster.

Beginner

The main tools

ToolBest forGetting started
Ollamathe quickest start from the terminal, and a local API for your codeinstall from ollama.com, then ollama run qwen3:8b
LM Studioa desktop app: search, download and chat, with a hint about what fits your machineinstall, search for a model, click download
llama.cppfull control: the engine many other tools use underneathllama-server -m model.gguf -c 8192
vLLMserving many users at once on a server GPUa Python package for Linux servers

Ollama and LM Studio also start a local server with an OpenAI-compatible API, so code written for a hosted API can use a local model by changing the address.

Beginner

Choosing a model

  • Start small: try a 3B–8B model first. For summarising, extracting fields, classifying and simple chat, it is often enough.
  • Match the task: use an instruction-tuned ("instruct" or chat) version, a coder model for code, and an embedding model for search (see embeddings).
  • Check the licence: open-weight does not always mean free for any use. Read the model card for commercial limits.
  • Test on your own examples: public leaderboards are a starting point, but a small test set of your real inputs (see evaluation) is what tells you if it is good enough.
Beginner

Code: call a local model from Python

With Ollama running and a model pulled (ollama pull qwen3:8b), the ollama package talks to it. The second example uses the openai package against Ollama's OpenAI-compatible endpoint, so the same code could later point at a hosted model.

Python (ollama)
# pip install ollama      (Ollama must be running: it listens on http://localhost:11434)
import ollama

reply = ollama.chat(
    model="qwen3:8b",
    messages=[
        {"role": "system", "content": "You summarise clinic notes in one plain-English sentence."},
        {"role": "user", "content": "Pt c/o 3/7 productive cough, afebrile, chest clear. Advised fluids, review if worse."},
    ],
    options={"temperature": 0.2, "num_ctx": 8192},   # low temperature for steady output; context length in tokens
)
print(reply["message"]["content"])
This code calls a model running on your own computer with Ollama ↗. Install Ollama, run ollama pull llama3.2, then pip install ollama and run the code. Press Copy to take it with you.
Python (OpenAI-compatible)
# pip install openai      the same client works with Ollama, LM Studio and llama-server
from openai import OpenAI

client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")   # local servers ignore the key
resp = client.chat.completions.create(
    model="qwen3:8b",
    messages=[{"role": "user", "content": "Give three tips for a good night's sleep, one line each."}],
    temperature=0.3,
)
print(resp.choices[0].message.content)
This code calls a hosted model, so it needs the internet and an API key. On your computer or in Colab: pip install openai, set your key as the environment variable OPENAI_API_KEY (never paste keys into code), then run it. Press Copy to take it with you.
Check yourself

Quiz

1. Roughly how much memory do the weights of a 14-billion-parameter model need at 8 bits per weight?

2. You double the context length. What happens to the KV cache?

3. What mostly limits how fast a local model writes each token?

4. Your files are the same size: a 14B model at Q4_K_M or a 7B model at Q8_0. Which is usually better?

Go deeper

📚 Further reading

Trusted, mostly free sources: official documentation, well-known textbooks and courses. When you want to confirm or go deeper on anything here, start with these.

  • Ollama ↗ run open-weight LLMs on your own computer
  • llama.cpp ↗ the engine behind GGUF files and many local LLM tools
  • LM Studio ↗ a desktop app for downloading and running local models
Ready