Learn
Local & edge AI

How to Run Open-Weight LLMs Offline Without Internet

The short answer

Running a model offline means staging its entire dependency closure: weights, tokenizer, chat template, runtime binary and GPU libraries. Download once on a connected machine, verify hashes, carry the bundle across, then serve with llama.cpp, Ollama or vLLM bound to localhost. The hard parts are memory budgeting and chat-template correctness, not the download itself.

On this page

Offline is a property of the bundle, not the model

A .gguf file on a USB stick is not an offline deployment. At minimum a working bundle contains the weights, the tokenizer (vocabulary plus merges, or a tokenizer.json), the chat template, the generation configuration, an inference binary, and every shared library that binary links against: CUDA runtime, cuBLAS, cuDNN, Metal, Vulkan or ROCm depending on the target. It also contains the absence of things. Ollama reaches for its registry when a model is missing, Hugging Face libraries write telemetry and check for updates, some runtimes verify licences over the network, and anything that resolves a hostname needs DNS that will not exist.

It helps to separate three levels of offline. Disconnected inference means the model path is local but the machine may still have a network; this is what most people actually need. Air-gapped means no route off the box at all, so the dependency closure must be complete and every lazy import must already be satisfied. Edge deployment adds power, thermal and physical-access constraints on top. The failure mode is the same at all three levels: a path typo turns into from_pretrained("org/model"), the library quietly falls back to the Hub, and the error you see is a connection timeout forty seconds into startup rather than the real problem.

Choose the runtime before the model

The runtime fixes the weight format, and the weight format fixes which checkpoints you can use. GGUF is the only format that runs on CPU, Apple Metal, CUDA, ROCm and Vulkan from a single file, and it is the only one that supports partial GPU offload layer by layer. vLLM and TensorRT-LLM expect safetensors or AWQ/GPTQ checkpoints and a real GPU with a working driver. MLX needs its own converted weights and only runs on Apple silicon. Standardising on one runtime narrows the catalogue; needing portability points at GGUF.

  • llama.cpp and llama-server: a C++ binary, GGUF input, CPU and GPU hybrid execution, an OpenAI-compatible /v1 endpoint, and no Python dependency tree. The easiest thing to sneakernet.
  • Ollama: a wrapper around llama.cpp with a model store and a clean API, but its default flow pulls from a registry. Import a local GGUF with a Modelfile for offline use.
  • vLLM: Python and CUDA, safetensors/AWQ/GPTQ/FP8, paged attention, continuous batching, tensor parallelism. Fast for many users, but the Python wheelhouse has to be mirrored too.
  • MLX and mlx-lm: unified-memory inference on Apple silicon with its own quantised format. Excellent on a Mac, useless on anything else.
  • Transformers with bitsandbytes: maximum flexibility and the heaviest dependency tree. Best kept for evaluation and conversion, not for serving.

Staging the bundle on a connected machine

Do the download on a machine with the same architecture and ideally the same OS and Python version as the target. The current command is hf download <repo> --local-dir ./bundle/<model>; older installs use huggingface-cli download. For a GGUF repository, fetch the single quantisation file plus tokenizer.json and tokenizer_config.json if the chat template is not already baked into the metadata. For a safetensors repository, fetch the whole snapshot: config.json, generation_config.json, every shard, the index file that maps tensor names to shards, and the tokenizer files.

Then verify. Every file stored through Git LFS carries a sha256 object id that the repository listing exposes, so sha256sum on the target should match it exactly. A truncated GGUF usually fails loudly at load with a tensor offset error; a truncated safetensors shard can load and produce quiet garbage. Archive with tar --sort=name and a fixed mtime if you want a reproducible hash for the bundle itself, copy it across, and verify again on the far side. Keep the manifest next to the weights, not in your head.

  • bundle/models/<model>-q4_k_m.gguf — weights, tokenizer and template in one file.
  • bundle/runtime/llama-server plus its shared objects, or a wheelhouse directory for Python runtimes.
  • bundle/manifest.json — repository, revision, filename, sha256, quantisation type, runtime version and the exact launch flags.
  • bundle/eval/ — a fixed prompt set, expected-property notes and a held-out text file for perplexity.

Quantisation follows from the hardware

Pick the quantisation after you know the memory ceiling, not before. On a 24 GB GPU a BF16 8B model fits and is the quality reference; on 8 GB the same model has to be Q4_K_M; on a 16 GB laptop Q5_K_M or Q6_K is a reasonable compromise. On CPU, Q4_K_M is usually the throughput sweet spot: below four bits the dequantisation arithmetic and codebook lookups cost more than the bandwidth you save, which is why an IQ3 file can be smaller and slower than Q4_K_M on the same machine. Q8_0 is close to lossless and almost never worth it on a card where it barely fits.

The file-size arithmetic is parameters times bytes per parameter, with about half a bit per weight of overhead for scales. A 7B model is roughly 4.4 GB at Q4_K_M, 5.1 GB at Q5_K_M, 5.9 GB at Q6_K, 7.6 GB at Q8_0 and 14 GB at BF16. A 7B coder checkpoint such as AstraGPTCoder-7B, to take one example, lands at about 6 GB of VRAM in a four-bit build with a few thousand tokens of context: comfortable on an 8 GB card, impossible at BF16. That is memory arithmetic, not a speed claim, and it is the calculation that decides whether a deployment is possible at all.

The memory budget, line by line

Three costs sit on top of the weights. The KV cache stores keys and values for every token in context and is the one people forget: bytes per token equals two, times layers, times key-value heads, times head dimension, times bytes per element. Llama 3.1 8B has 32 layers, 8 KV heads and a head dimension of 128, so at FP16 it costs 2 × 32 × 8 × 128 × 2 = 131,072 bytes, or 128 KiB per token. An 8K context is 1 GiB; a 32K context is 4 GiB, which is nearly the size of the weights again. Qwen2.5-7B uses 4 KV heads over 28 layers, so it costs 56 KiB per token and a 32K context is only 1.75 GiB.

The second cost is the compute buffer: activations, attention workspace and the graph itself. With Flash Attention on a 7B model at 4K to 8K context this is typically a few hundred megabytes; without it, or at very large batch sizes, it grows quickly. The third is runtime overhead: CUDA context, allocator fragmentation, compiled graphs, and on a laptop the desktop compositor, which can hold half a gigabyte or more. Sum all four before you choose a context length, and leave ten percent headroom.

  • 8 GB card, 7B Q4_K_M, 8K context: 4.4 GB weights + 1.0 GB cache + 0.5 GB buffer + 0.3 GB overhead ≈ 6.2 GB. Fits.
  • 8 GB card, 7B Q4_K_M, 32K context: 4.4 + 4.0 + 0.6 + 0.3 ≈ 9.3 GB. Does not fit without KV quantisation.
  • 16 GB card, 8B Q6_K, 16K context: 6.6 + 2.0 + 0.7 + 0.4 ≈ 9.7 GB. Comfortable.
  • 64 GB unified memory, 70B Q4_K_M, 8K context: 42 + 2.5 + 1.0 ≈ 45.5 GB. Fits, at unified-memory bandwidth.

Serving it locally

With llama.cpp the whole server is one command: llama-server -m bundle/models/model-q4_k_m.gguf --ctx-size 8192 --n-gpu-layers 99 --flash-attn --jinja --host 127.0.0.1 --port 8080. --n-gpu-layers 99 offloads every layer; lower it until memory fits if the model spills. --ctx-size is the maximum context the server will reserve cache for, so setting it to the model's architectural maximum wastes memory on every request. --jinja makes the server apply the chat template embedded in the GGUF metadata. --host 127.0.0.1 keeps it off the network, which is the point. Add --threads equal to physical cores, and use --mlock only when the machine has memory to spare, because locking pages that the OS needs can wedge the box.

Ollama is easier but hides two settings. Write a Modelfile with FROM ./model-q4_k_m.gguf, PARAMETER num_ctx 8192 and PARAMETER temperature 0.2, run ollama create local-model -f Modelfile, then ollama run local-model. Set OLLAMA_MODELS to a directory on the big disk and OLLAMA_HOST=127.0.0.1:11434 so it does not listen publicly. The default context window is small, so num_ctx is not optional if you care about long prompts. For vLLM, export HF_HUB_OFFLINE=1 and TRANSFORMERS_OFFLINE=1 before launch, then vllm serve ./bundle/model --served-model-name local --max-model-len 8192 --gpu-memory-utilization 0.90. The Python dependencies must already be installed from a wheelhouse built on a connected machine with the same platform tags: pip download -d wheelhouse -r requirements.txt, then pip install --no-index --find-links wheelhouse.

Chat templates are where offline setups break

The template decides where system, user and assistant turns go, what text is appended to start the reply, and which tokens stop generation. Get it wrong and the model is not obviously broken: it produces fluent text that continues the user's turn, invents its own role prefix, or never emits an end-of-sequence token and runs to the context limit. In GGUF the template lives in the tokenizer.chat_template metadata key. In a safetensors directory it lives in tokenizer_config.json. Ollama will use a template from the Modelfile if one is supplied, otherwise the one in the GGUF.

Test the template directly rather than through a chat UI. Render it with apply_chat_template(messages, tokenize=False) and read the exact string, including whether a BOS token is added there or by the tokenizer; adding it twice degrades output in ways that look like a bad quantisation. Then send a raw completion ending in the assistant header and confirm the model stops at the right token. If a model emits its own name as a role prefix, the template is missing a turn separator, not misbehaving.

Verifying a model you cannot look up

Once the box is offline you cannot check a leaderboard or a model card, so build the evidence before you leave. Keep a fixed set of twenty to fifty prompts with notes on what a correct answer looks like, and run it against the same bundle on the connected staging machine and on the target. Run llama-perplexity over a held-out text file and compare with the FP16 source, converted on the same machine; a jump of more than a few percent means the conversion or quantisation is wrong, not that the model is bad. Check determinism at temperature zero with a fixed seed, because nondeterminism across identical binaries usually means a different backend or a different number of offloaded layers.

  • Tokenizer round-trip: decode(encode(text)) returns the original for ASCII, Devanagari and emoji.
  • Stop behaviour: the model emits EOS rather than running to the context limit.
  • Context recall: a fact placed at the start of a 2K and an 8K prompt is still retrieved.
  • Structured output: a JSON-only prompt returns parseable JSON ten times out of ten.
  • Throughput: prompt-processing and generation tokens per second recorded with the exact flags used in production.

Laptops, ARM boards and edge boxes

Unified memory changes the calculus without repealing physics. A 64 GB Mac can hold a 70B model at four bits, roughly 40 GB, but its 546 GB/s of memory bandwidth caps decode well below a 24 GB RTX 4090 at 1008 GB/s running a model a tenth the size. Capacity decides what runs; bandwidth decides how fast. The same split appears in small boxes: a Raspberry Pi 5 with 8 GB runs a 3B model at Q4 in about 2 GB and produces a few tokens per second, which is fine for classification or a fixed-response assistant and painful for conversation. Snapdragon X Elite laptops pair 135 GB/s of LPDDR5X with 32 to 64 GB of capacity.

Thermals are part of the benchmark. A 45 W laptop GPU boosts for the first thirty seconds and then settles, so measure sustained tokens per second over five minutes rather than the first burst. Keep the display driven by the integrated GPU so the discrete card keeps its full VRAM, disable sleep and hibernation, and expect the first request after an idle period to be slow while pages fault in from NVMe. On a fanless box, place the model on fast storage and accept that a long prompt will heat-soak the SoC.

Updating and maintaining an air-gapped install

Treat the deployment like a software release. Build the new bundle beside the old one, verify it, then switch a symlink and restart the service, so rollback is one command and thirty seconds. Keep the previous two bundles on disk. Pin the runtime version: llama.cpp changes flags and GGUF support between releases, and a model that loaded last quarter can fail after an upgrade for reasons that have nothing to do with the weights. Never overwrite a model file in place, because a half-copied 40 GB file looks like a corrupt model and costs a day to diagnose.

  • Record repository, revision, filename, sha256 and quantisation in the manifest, and diff it before every rollout.
  • Keep a known-good runtime binary alongside the new one; the old binary is your rollback.
  • Re-run the fixed eval set after any runtime, driver or flag change, not only after a model change.
  • Mirror the wheelhouse for Python runtimes, including transitive dependencies and the exact platform tags.

Model files are executable-adjacent

PyTorch .bin and .pt checkpoints are pickle archives and can execute arbitrary code when loaded, which makes an unknown checkpoint equivalent to an unknown binary. Safetensors and GGUF avoid pickle, but GGUF parsers are C++ and have had memory-safety bugs, so a file from an untrusted source is still a risk. Run the server as an unprivileged user with no access to anything but the model directory, bind it to localhost, and put an authenticating reverse proxy in front if other machines need to reach it. Offline removes remote attack surface and concentrates it in the files you carried in.

A working checklist

  • Decide the runtime first; let it dictate the weight format.
  • Download with a pinned revision and record every sha256.
  • Verify hashes on both sides of the transfer, not just the source.
  • Choose the quantisation from the memory ceiling, then check the KV cache at the context length you actually need.
  • Bake the chat template into the bundle and test the rendered string, not just the chat reply.
  • Set the context size explicitly in llama.cpp or Ollama; never accept the default.
  • Bind to 127.0.0.1 and run as an unprivileged user.
  • Measure sustained throughput and time to first token with the production flags.
  • Keep two previous bundles and a pinned runtime for rollback.
  • Re-run the eval set after every change, including driver updates.
Page feedback

Was this helpful?

A quick signal helps improve these notes.

Report a mistake Straight to the lab inbox

Put it to work

See the models, datasets and gateway this lab runs.