LearnThatStack Ace your next interview

Open-Source & Local LLMs

Commands for vLLM V1, Ollama and llama.cpp Checked Sep 2026 30 code snippets

The model

  • An open-weight model ships its weights as a download. Its license, not the word open, says what you may do.
  • Memory is weights plus KV cache plus overhead. The KV cache grows with context and with every user.
  • Decode reads every weight for each token, so memory bandwidth sets its speed and batching is nearly free.
  • Pick the runtime by concurrency. Ollama and llama.cpp serve one user. vLLM and SGLang batch many users.
  • Every runtime speaks the OpenAI-compatible API, so switching is mostly a base URL change.
Your appOpenAI SDKServerOllama, vLLMWeightsin VRAM, fixedKV cachein VRAM, growsPREFILLsend promptprefill whole promptDECODEread all weightsread the cacheappend 1 tokenstream token
Prefill handles the whole prompt in one pass. Each decode step makes one token and reads all the weights and the cache, so memory bandwidth sets its speed.

Open models & licenses

what you can download, and what you may do with it

Open-weight, open source, proprietary

Open-weight, like Llama or Qwen.Get the weights, but not data or code
Open source, like Ai2's Olmo.Get the weights, data and code
Proprietary. The weights never ship.Reach the model only through an API
releasedkept private

TrapMost models called open source are only open-weight. Llama's weights are free to download, but its license adds use limits.

Read a model license

Find the families under Apache 2.0 or MITApache 2.0 for Qwen, gpt-oss, Olmo and Gemma 4. MIT for DeepSeek, GLM and Phi.
Tell Apache 2.0 from MITApache adds a patent grant, and you must mark your changes.
List what the Llama license addsAn acceptable-use policy, a Built with Llama notice, and a deal above 700M monthly users.
List what the Gemma terms addUse limits you must pass to every recipient, and Google may tell you to stop. Gemma 4 is Apache 2.0.

TrapCustom licenses reach your fine-tunes. A Llama fine-tune's name must start with Llama.

Self-host or hosted API

when owning the GPU pays off

Self-host or use a provider

Keep prompts inside your networkSelf-host. Air-gapped sites have no other option.
Avoid surprise model changesPin the weights, so no deprecation or silent update reaches you.
Know when to stay on a hosted APISpiky or low traffic, and the hardest tasks, where frontier models still lead.
Run an open model without buying GPUsUse Together or Fireworks. They sell open models per token.
Get the fastest token generationGroq. It runs custom chips and sells speed, not catalog breadth.

TipStart hosted. The weights are open, so you can move the same model in-house later without rewriting prompts.

TrapOwned GPUs only save money while they stay busy. An idle GPU bills the same.

Cost per million tokens

Self-host dollars per million tokensgpu_hr / (tok_s * 3600) * 1e6 / util$2/hr GPU at 2,500 tok/s = $0.22full load0.22 / 0.40 = $0.5640% busy
Add what the invoice never showsSpare GPUs for failover, reserved commitments, and engineers on call.
Decide when to self-hostLarge, steady volume, with the model pinned for months.

TrapMeasure tokens per second yourself, at your p95 latency target. A vendor's peak number is higher than you'll get.

Run a model locally

one user, one machine

Ollama, llama.cpp and LM Studio

Pull a model and chat with itollama run llama3.2
Point an OpenAI SDK at Ollamaexport OPENAI_BASE_URL=http://localhost:11434/v1
Serve a GGUF file with some layers on the GPUllama-server -m model.gguf -ngl 32 --port 8080
Name the hardware llama.cpp runs onCPUs, plus Metal, CUDA, Vulkan or ROCm GPUs. A laptop runs a small model.
Say what LM Studio isA closed-source desktop app over llama.cpp and MLX. Its server listens on port 1234.

TipOllama ships each model with its chat template and defaults, so you don't format prompts by hand.

TrapAll three are built for one user. llama-server batches requests, but its throughput stays flat as load grows.

GGUF files

Say what one GGUF file holdsThe tensors plus the tokenizer, architecture and chat template.
Read the quant level in a GGUF file nameQ4_K_M means a 4-bit k-quant, medium size.

TrapGGUF is for inference only. You convert to it from Hugging Face weights, and you never train in it.

Fix slow local responses

Find which phase is slowRead load_duration, prompt_eval_duration and eval_duration in Ollama's response.
Check the model fits in VRAMollama ps100% GPU is healthy. A CPU/GPU split is slow.
Halve the KV cache's memory in OllamaOLLAMA_FLASH_ATTENTION=1 OLLAMA_KV_CACHE_TYPE=q8_0 ollama serve

Trap100% CPU on a GPU machine means the GPU lacked free VRAM, or the runtime never found it.

VRAM & KV cache math

will it fit, and for how many users

VRAM to load a model

GB = billions of params x bytes per weight8B at FP16 = 8 x 2 = 16 GB16 GB + 20% overhead = about 19 GB
Find the bytes per weightFP16 takes 2, FP8 or INT8 takes 1, and 4-bit Q4_K_M about 0.6.
Check whether a 70B fits one 80 GB cardNo at FP16, which needs 140 GB. Yes at 4-bit, about 50 GB with overhead.

Trap4-bit isn't exactly 0.5 bytes. Files also store scales and keep some tensors at higher precision.

The KV cache

KV bytes per token2 x layers x kv_heads x head_dim x bytes2 x 32 x 8 x 128 x 2 = 128 KiBLlama 3.1 8B, FP16128 KiB x 128k tokens = 16 GiBone long request
Say what the KV cache storesKeys and values of every past token, per layer, so they aren't recomputed.
Work out how many requests fitDivide the VRAM left after weights and overhead by KV per token. An 8B on an H100 fits about 50 at 8k.

TrapQuantizing the weights doesn't shrink the KV cache. It stays FP16 by default.

Shrink the KV cache

Halve it for a small accuracy hitvllm serve <model> --kv-cache-dtype fp8
Cap the context window to fit more requestsvllm serve <model> --max-model-len 32768
Know what GQA and MLA saveGQA cuts it 4 to 8x, and DeepSeek's MLA about 93%. Both ship with the model.

TrapKV compression fails at long context first. Test quality on your longest prompts.

Dense vs mixture-of-experts

Size an MoE's memoryBy total parameters, since the router can call any expert.
Estimate an MoE's speedBy active parameters. A 30B-A3B computes about 3B per token.
Serve an MoE across GPUsvllm serve <moe-model> --enable-expert-parallel --data-parallel-size 8

TipOn one GPU, llama.cpp can keep attention on the GPU and the experts in system RAM.

Pick a model size

Classify, extract or routeOne to eight billion parameters.
Answer over RAG, summarize or draftAbout eight to thirty billion.
Run agents, multi-step reasoning or hard codeThirty billion and up, or an MoE.

TipStart one size above your guess, then step down until quality breaks. The last size that passed wins.

Quantization

fewer bits per weight, for memory and speed

What quantization does, and how far to go

Say what quantization doesIt stores weights in fewer bits. 4-bit is about a quarter of the 16-bit size.
Pick a default level4-bit, as AWQ or Q4_K_M, unless your eval set shows a real drop.
Spend a fixed VRAM budgetA larger model at 4-bit usually beats a smaller one at 8-bit.
Push a quantized model's accuracy furtherCalibrate on your own traffic. Keep embeddings and lm_head at higher precision.

TrapQuality holds to about 4 bits, then drops fast. Code and math show the damage before chat does.

GGUF, AWQ, GPTQ or FP8

Run on a laptop, a Mac or with CPU offloadGGUF k-quants in llama.cpp, Ollama or LM Studio.
Fit a big model in tight GPU memoryAWQ, or GPTQ if there's no AWQ checkpoint. Both are 4-bit and close in quality.
Keep near-lossless quality on a newer GPUvllm serve <model> --quantization fp8FP8. It needs Hopper, Ada or newer.
Tell AWQ from GPTQGPTQ pushes rounding error into later weights. AWQ protects salient channels first.

TrapFP8 only halves memory. A 70B model still needs two large cards.

Prompts, sampling & JSON

get the output you asked for

Instruct models and chat templates

Pick a base or an instruct modelInstruct, unless you'll fine-tune. Look for -instruct or -it in the name.
Define a chat templateA Jinja template that turns messages into the exact string the model trained on.
Build the prompt string yourselfCall apply_chat_template with add_generation_prompt=True.

TipChat endpoints apply the template for you. Raw completion endpoints don't.

TrapWrong turn markers fail quietly. The model ignores the system prompt, runs past its stop, or breaks tool calls.

Sampling settings

Set temperature0 for extraction, 0.7 for chat. Set it first.
Trim unlikely tokensUse one control, top_p at 0.9 to 0.95 or top_k at 40.
Stop the model repeating itselfSet a repetition penalty of 1.0 to 1.1. Above 1.15 it breaks code.

TrapTemperature 0 is greedy, not reproducible. Batch size and kernel order can still change the output.

Reliable JSON and tool calls

Force output to match a JSON SchemaConstrained decoding, through response_format in vLLM or a GBNF grammar in llama.cpp.
Turn on tool calls in vLLMvllm serve <model> --enable-auto-tool-choice --tool-call-parser hermes
Guarantee a tool call parsesSet tool_choice to required or a named tool.

TrapA valid shape isn't correct content. Still validate the values, and retry on failure.

Serve many users

batch requests on a GPU server

Pick a runtime

Serve one user on a laptopOllama to start fast. llama.cpp to embed the engine or reach flags Ollama hides.
Serve many users on a GPUvLLM, with the widest model and hardware support.
Serve long shared prefixes or chained callsSGLang, whose RadixAttention caches shared prefixes.
Know where TGI standsIn maintenance mode. Hugging Face now points to vLLM or SGLang.

TrapDon't judge by one request. llama.cpp looks fine alone, then time to first token climbs as users queue.

Stand up an OpenAI-compatible server

Name the routes an OpenAI-compatible server copies/v1/chat/completions and /v1/models, with OpenAI's request and response shapes.
Serve under your app's model name, with a keyvllm serve <model> --served-model-name gpt-4o-mini --api-key local-key
Point the app at itexport OPENAI_BASE_URL=http://localhost:8000/v1export OPENAI_API_KEY=local-key
Check it answers before changing app codecurl -H "Authorization: Bearer local-key" localhost:8000/v1/models

TipCompatible means the same shape, not every feature. Tool calls, structured output and logprobs vary by runtime.

Trap/v1/chat/completions needs a chat template. Pass --chat-template if the model ships none.

Continuous batching and PagedAttention

staticAidleCstaticBDcontinuousACEcontinuousBDtime

Each letter is a request in one of two batch slots. Static batching leaves a slot idle until the longest request ends. Continuous batching refills it at once.

Explain continuous batchingThe batch changes every decode step. A finished request leaves and a waiting one joins.
Explain PagedAttentionIt stores the KV cache in 16-token blocks, so waste is at most one partial block.
Say what vLLM does when the cache fillsIt preempts a request and recomputes it later.

TrapKV-cache memory caps the batch, not compute.

Latency or throughput

Define latency and throughputLatency is one request's wait. Throughput is tokens per second across all users.
Tune for latencyvllm serve <model> --tensor-parallel-size 2 --max-num-seqs 16
Tune for throughputvllm serve <model> --max-num-seqs 256 --gpu-memory-utilization 0.95
Stop a long prompt stalling other requestsvllm serve <model> --enable-chunked-prefill

TrapFrequent preemptions mean you pushed the batch too far. Each evicted request gets recomputed.

Speculative decoding

Explain speculative decodingA small draft model guesses a few tokens. The big model checks them in one pass.
Know when it helpsBatch size one, on predictable output like code or JSON.
Know when it backfiresBig batches, where the GPU is already busy, and high-temperature prose.

TipIt's lossless. Verification keeps the output distribution the same, so only speed changes.

Fine-tunes & adapters

train on your data, then serve it

Fine-tune with LoRA or QLoRA

Pick LoRA or QLoRALoRA if the base fits on your GPU. QLoRA loads it in 4-bit if not.
Know what fine-tuning teachesFormat, style or behavior. Use retrieval for new facts.
Pick starting settingsThe instruct model, hundreds to thousands of examples, rank 16, 1 to 3 epochs.

TrapFormat the examples in the model's chat template, and stop when a held-out eval stops improving.

Adapters or merged weights

Serve a base model with an adaptervllm serve <base> --enable-lora --lora-modules sql=/adapters/sql
Choose adaptersSeveral fine-tunes on one GPU, or frequent new versions.
Choose merged weightsOne variant takes all traffic. Merge in fp16, then quantize.

TrapAn adapter only works on its exact base checkpoint, so pin that version.

Hot-swap LoRA adapters

Allow adapters to load at runtimeVLLM_ALLOW_RUNTIME_LORA_UPDATING=True vllm serve <base> --enable-lora
Load an adapter without a restartPOST its name and path to /v1/load_lora_adapter. Clients then send that name as model.
Cap adapters per batchvllm serve <base> --enable-lora --max-loras 4 --max-lora-rank 32

TrapGuard the load endpoint. It reads any path you hand it.

Scale across GPUs

split a model across GPUs and plan capacity

Split work across GPUs

Split every layer across GPUs in one nodevllm serve <model> --tensor-parallel-size 8
Add stages across nodesvllm serve <model> --tensor-parallel-size 8 --pipeline-parallel-size 2
Pick the orderTP first, up to 8 GPUs on NVLink. Add PP only if it still doesn't fit.
Pick hardware for each phasePrefill wants FLOPs. Decode wants memory bandwidth and room for KV cache.
Run prefill and decode on separate GPU poolsDisaggregated serving. It needs NVLink or InfiniBand, and pays off only at scale.

TrapTP size must divide the attention head count. PP needs enough requests to keep every stage busy.

Plan capacity and benchmark

Little's Lawin flight = 40 req/s x 6 s = 240replicas = 240 / 40 per GPU = 6 + headroom
Find one GPU's capacityRaise the batch at real prompt lengths until p95 time per token breaks the budget.
Leave headroomSize for peak and keep replicas near 70% load. Queueing delay explodes above that.
Load-test a vLLM servervllm bench serve --model <model> --dataset-name random --request-rate 8
Report the right numbersTTFT, inter-token and end-to-end latency at p50, p95 and p99.

TrapReplayed prompts hit the prefix cache and inflate results. Turn it off unless production shares prefixes.

Evaluate, ship & monitor

quality tests, rollouts and platform design

Monitor a self-hosted model

Scrape vLLM's Prometheus metricscurl -s localhost:8000/metrics
Spot saturationWatch queue wait and preemptions. GPU utilization stays high even as throughput drops.
Watch GPU healthUse DCGM, NVIDIA's metrics agent, for power, throttling and ECC errors.

TrapAlert on latency percentiles per phase, never on averages.

Pin and roll out model versions

Pin a model behind a stable namevllm serve <model> --revision <hash> --served-model-name assistant
List what one version pinsWeights, tokenizer, chat template, quantization and engine version.
Roll out a new versionBlue-green. Gate on the golden set, then shift a small share. Roll back with a router flip.

TrapHub tags can move. A changed chat template alters output with no weight change.

Air-gapped deployment

Stop runtime downloads and telemetryexport HF_HUB_OFFLINE=1export TRANSFORMERS_OFFLINE=1export DO_NOT_TRACK=1
Bring the artifacts inWeights, images and wheels cross once, hash-verified and signed.

TrapBlock egress at each node. A runtime that retries a download passes staging, then hangs inside the gap.

Found this useful? Pass it on.
Pro · $10/mo

This sheet stays free. With Pro, you can:

  • Every answer on this sheet. Read the full answer to all 55 questions it links, not just the 7 free ones.
  • My sheet. The cards you star and the ones you miss in Quiz me, from every sheet, on one page to quiz and print.
  • PDFs. Download this sheet and My sheet, laid out for A4, with every question a link to its answer.
  • The rest of Pro. Every answer in the question library, and 1,000 AI credits a month.