Local LLM on any device (llama.cpp)
Device-agnostic guide to run, recommend, and tune local GGUF models on whatever hardware is present. Detects the machine, picks a model that fits its memory, tunes inference-time settings, and optionally wires it into an agentic client.
When to use
- Deploy / swap / stop a local model on this machine (llama.cpp
llama-serveror allmctl-style manager). - Pick a model + quantization that fits the device's VRAM/RAM and context needs.
- Set context size, threads, KV-cache type, samplers, reasoning off/on.
- Debug llama.cpp crashes (
DeviceLost, reasoning-content swallowed, tool-call garbage, KV corruption). - Build a personal model manager script, or connect any OpenAI-compatible client to the local endpoint.
Workflow
- Bootstrap (fresh machine, no llama.cpp yet) → ensure a
llama-serverbinary and a model file exist: build/install llama.cpp with the backend matchingdetect-device.sh, verifyllama-server --list-devices, download a.ggufintoMODEL_DIR. Seereferences/bootstrap.md. - Detect the device →
scripts/detect-device.sh(OS, GPU vendor/model/memory, shared-vs-dedicated, RAM, CPU cores incl. efficiency topology, suggested backend). Seereferences/device-detection.md. - Pick a model →
scripts/recommend-models.sh "<name>" --ram <gib>(live HuggingFace search, GGUF quants + sizes, fits hints). Capacity math + KV-cache budget inreferences/model-selection.md. - Tune inference → quantization,
-ccontext,-tthreads, KV-cache type, samplers inreferences/tuning.md. - Run →
llama-server -m model.gguf <flags>(or thellmctl-template.shmanager). Verify withcurl /healththen one/v1/chat/completions. - Wire agentic (optional) → any OpenAI-compatible client against the local endpoint in
references/agentic.md. - Fix crashes / bad output → common failure modes and fixes in
references/stability.md.
Key invariants (apply regardless of hardware)
- Capacity:
weights_GiB + KV(ctx) + ~2 GiB buffer ≤ available memory. Weights go in VRAM if a GPU backend offloads them; on shared-memory GPUs that heap is carved from system RAM — use available RAM, not nominal total. - Never assume quant-lower = faster: on some backends the dequant shader for IQ quants is heavier than K-quants; decode is bounded by
weights_GiB / effective_bandwidth. Always measure, never extrapolate from one run. - KV cache:
fp16is the safe default;q8_0can corrupt multi-turn state on some backends (greedy garbage / repetition);q4_0saves ~75% KV memory and is safe on most backends. KV memory grows linearly with-c. - Reasoning-preserving templates (Qwen3, gemma-4, MiniCPM5): the chat template can swallow the whole output into
reasoning_content, leavingcontent:''. Set--reasoning off --reasoning-format deepseekand assertcontentis non-empty. - Context limits are native: YaRN / rope-scale only interpolate positions — they do not reduce KV memory or add memory past the model's native
context_length. They only matter beyond it. - One slot per client:
-npsplits the total context across slots; a single interactive/agent client should use-np 1so the whole-cgoes to one slot. - Agentic needs a real model: tool-call reliability collapses below ~12B. Small models are fast at chat but useless for coding agents — verify tool calls with
curlbefore believing them.
Scripts
| script | purpose |
|---|---|
| scripts/detect-device.sh | one-shot device capability report → JSON + suggested backend |
| scripts/recommend-models.sh | live HuggingFace GGUF search + fits hints |
| scripts/llmctl-template.sh | parameterized personal model manager (list/start/stop/status/profile/client) |
References (read on demand)
references/bootstrap.md— from-zero setup: build/install llama.cpp with the right backend, verify --list-devices, download a model.references/tuning.md— quantization / context / threads / samplers methodology, decode ceiling, decay behavior.references/stability.md— common failure modes and fixes, plus a regression recipe.references/agentic.md— local agentic via OpenAI-compatible endpoint, tool-call verification, bench methodology, pi wiring.references/build-your-own-manager.md— design + adapter steps for a personal model manager.
Requirements
- bash (3.2+ compatible; 4+ recommended),
curl,python3 jqoptional —detect-device.shfalls back to python3 for JSON output- Linux reads
lspci/lscpu//proc/meminfo; macOS usessystem_profiler/sysctl - No GPU required — everything degrades gracefully to CPU-only with a note.
微信扫一扫