Ollama wraps llama.cpp inside a Go service daemon with a model registry, Modelfile packaging, and automatic idle unloading, whereas llama.cpp exposes llama-server as a direct C++ binary over ggml with explicit control over slots, KV cache types, speculative decoding, and Prometheus metrics. Both run GGUF weights and serve OpenAI-compatible HTTP endpoints, so the choice comes down to whether you want zero-friction model management or predictable memory and latency tuning.
Go daemon and registry vs direct C++ binary
Ollama behaves like a container runtime for weights. Running ollama serve starts a background Go daemon that manages a content-addressed blob store under OLLAMA_MODELS, pulls tagged manifests from its library or Hugging Face, and builds derived configurations through a Modelfile. You declare a base model with FROM, attach defaults with PARAMETER, bake in a SYSTEM prompt or TEMPLATE, and run ollama create without touching command-line flags at launch time.
Model lifecycle in Ollama is dynamic by default. When a request arrives for an unloaded model, the daemon reads the weights into memory, serves the request, and keeps the model warm for five minutes before unloading it automatically, as documented in the Ollama FAQ. You can override that timer globally with OLLAMA_KEEP_ALIVE or per request using the keep_alive field, setting -1 to pin weights permanently or 0 to unload immediately after generation.
Direct llama.cpp skips the registry layer and executes as a single native binary built on ggml, with backends for CUDA, Metal, HIP, Vulkan, SYCL, and tuned CPU SIMD paths. Launching llama-server loads the specified GGUF file (local or fetched via -hf) once at startup and keeps it resident until the process exits. There is no background garbage collector swapping weights behind your back, which eliminates surprise cold-start pauses in shared services. For a broader look at how both fit alongside vLLM on larger hardware, see the local LLM inference servers comparison.
# Ollama Modelfile example
FROM qwen3:8b
PARAMETER num_ctx 16384
PARAMETER temperature 0.2
SYSTEM "Return concise, verifiable technical answers."
VRAM and RAM layer offloading
When a model fits completely in GPU memory, both engines deliver similar token throughput because both execute ggml kernels underneath. Differences show up when weights and context approach or exceed VRAM capacity.
In llama-server, GPU placement is controlled explicitly via --n-gpu-layers (short flag -ngl), which accepts auto, all, or an exact layer count. When a 32-layer model overflows a 12 GiB GPU, passing -ngl 24 keeps the first 24 transformer blocks on the GPU and leaves the remaining 8 blocks plus output layers in system RAM. The split is deterministic across restarts, and you can inspect memory placement immediately in startup logs.
Ollama decides layer placement automatically based on its device memory probe at load time. You can see the resulting split with ollama ps, whose PROCESSOR column reports percentages such as 75%/25% CPU/GPU. Partial offloading keeps a model runnable, though generation speed drops sharply once every token has to stream CPU-resident weights across system RAM bandwidth. Budgeting weights, quantization headroom, and KV cache together before choosing a card or model size is covered in the local LLM hardware guide.
# llama-server: pin exactly 28 layers to GPU and verify context size
llama-server -m /srv/models/qwen3-14b-q4_k_m.gguf \
--host 127.0.0.1 --port 8080 \
--n-gpu-layers 28 -c 16384
# Ollama: inspect loaded model placement and context allocation
ollama ps
Parallel slots, continuous batching, and Flash Attention
Serving multiple concurrent requests requires dividing KV cache memory without stalling active generation steps. Both tools support continuous batching and Flash Attention, yet they expose very different controls.
Ollama regulates concurrency through environment variables. OLLAMA_NUM_PARALLEL sets the maximum parallel requests per loaded model (defaulting to 1 on standard setups, or a memory-based default on Apple MLX), OLLAMA_MAX_LOADED_MODELS caps simultaneous loaded models (default 3 times GPU count, or 3 on CPU), and OLLAMA_MAX_QUEUE rejects bursts with HTTP 503 after 512 queued requests. Enabling OLLAMA_FLASH_ATTENTION=1 activates Flash Attention across supported backends. Because required VRAM scales as OLLAMA_NUM_PARALLEL * OLLAMA_CONTEXT_LENGTH, raising concurrency from 1 to 4 multiplies KV cache reservation by four.
llama-server uses an explicit slot model backed by continuous batching by default. Passing -np (--parallel) sets the slot count: leaving -np -1 lets the server share one unified KV buffer dynamically, whereas setting -np 4 -c 32768 turns off the unified buffer by default and assigns 8192 tokens per slot. Pairing -np with -fa on (--flash-attn on) reduces attention scratchpad memory and unlocks KV quantization. You can query GET /slots at runtime to verify n_ctx and prompt-cache state per slot, as detailed in the llama-server documentation.
# Ollama configured for 4 concurrent requests with Flash Attention
OLLAMA_HOST=127.0.0.1:11434 \
OLLAMA_NUM_PARALLEL=4 \
OLLAMA_CONTEXT_LENGTH=16384 \
OLLAMA_FLASH_ATTENTION=1 \
OLLAMA_KEEP_ALIVE=30m \
ollama serve
# llama-server with 4 fixed slots, Flash Attention, and metrics
llama-server -m /srv/models/qwen3-8b-q4_k_m.gguf \
--host 127.0.0.1 --port 8080 \
--parallel 4 -c 32768 -fa on -ngl all --metrics
KV cache quantization
Long contexts quickly make the KV cache larger than a 4-bit model file itself. Quantizing attention keys and values from f16 down to 8-bit or 4-bit halves or quarters that memory footprint, letting you double concurrency or extend context length on the same GPU.
In llama-server, you control keys and values independently using -ctk (--cache-type-k) and -ctv (--cache-type-v). A common production baseline is -ctk q8_0 -ctv q8_0 with -fa on, which cuts KV cache VRAM nearly in half with minimal accuracy drift. When VRAM is extremely tight, some teams keep keys at q8_0 and drop values to q4_0 or use iq4_nl, because attention keys are typically more sensitive to quantization noise than values.
Ollama exposes OLLAMA_KV_CACHE_TYPE alongside OLLAMA_FLASH_ATTENTION=1. According to the Ollama FAQ, OLLAMA_KV_CACHE_TYPE defaults to f16 and is currently a global setting applied to every model the daemon loads. You cannot configure q8_0 for a 32B coding model while keeping f16 for a small extraction model inside the same Ollama instance. Whenever you quantize KV cache below f16, run regression checks against your prompts and tool schemas using AI agent evals so silent retrieval or JSON regressions do not slip into production.
# llama-server: 8-bit KV cache quantization with Flash Attention
llama-server -m /srv/models/qwen3-14b-q4_k_m.gguf \
--host 127.0.0.1 --port 8080 \
-ngl all -fa on -ctk q8_0 -ctv q8_0 -c 32768 -np 4
Speculative decoding and grammar constraints
Latency-sensitive workloads such as IDE autocomplete and structured extraction benefit from features that llama-server exposes directly while Ollama keeps abstracted.
llama-server supports speculative decoding natively. You attach a small draft model from the same tokenizer family using --model-draft (-md) or --spec-draft-hf alongside --spec-draft-n-max and --spec-draft-n-min (or ngram-based --spec-type ngram-mod when no draft weights exist). The target model verifies batches of drafted tokens in a single forward pass, raising single-user decode speed on repetitive code or boilerplate text without changing the output distribution. Ollama does not expose draft-model speculative decoding flags.
For structured generation, both servers support JSON Schema via OpenAI response_format, and Ollama documents OpenAI endpoints on its OpenAI compatibility page alongside its native format field. However, llama-server compiles JSON Schemas down to GBNF grammars and also accepts arbitrary --grammar or --grammar-file definitions directly, plus per-request grammar fields. If you need custom DSL output, SQL dialects, or strict non-JSON syntax without post-processing retries, llama-server gives you token-mask enforcement out of the box.
# llama-server with draft-model speculative decoding and GBNF grammar
llama-server \
-m /srv/models/qwen2.5-coder-14b-q4_k_m.gguf \
--model-draft /srv/models/qwen2.5-coder-0.5b-q8_0.gguf \
--spec-draft-n-max 8 --spec-draft-n-min 2 \
--n-gpu-layers all --n-gpu-layers-draft all \
--grammar-file /etc/llama/sql-only.gbnf \
--host 127.0.0.1 --port 8080
Systemd service hardening and API security
Neither server should listen unauthenticated on a public interface. Ollama binds 127.0.0.1:11434 by default and has no built-in API key verification, so exposing it to teammates or applications requires placing a TLS reverse proxy with bearer-token checks in front of it. llama-server binds 127.0.0.1:8080 by default and supports --api-key or --api-key-file directly on API endpoints (while leaving /health public for readiness checks).
Running either server under systemd with least privilege limits damage if a parser or template bug is exploited. Create a dedicated system user, mount the model directory read-only, bind only loopback, and lock down filesystem and kernel capabilities as covered in the Linux VPS hardening guide.
[Unit]
Description=llama.cpp inference server
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=llm
Group=llm
EnvironmentFile=/etc/llama/server.env
ExecStart=/usr/local/bin/llama-server \
-m /srv/models/qwen3-8b-q4_k_m.gguf \
--host 127.0.0.1 --port 8080 \
-ngl all -fa on -ctk q8_0 -ctv q8_0 -c 32768 -np 4 \
--api-key-file /etc/llama/api-keys.txt \
--metrics
Restart=on-failure
RestartSec=5
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=yes
ReadOnlyPaths=/srv/models /etc/llama
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
LockPersonality=yes
[Install]
WantedBy=multi-user.target
Comparison and decision matrix
| Dimension | Ollama | llama.cpp llama-server |
|---|---|---|
| Runtime model | Go daemon managing ggml runners |
Single C++ binary (llama-server) |
| Weight packaging | Registry pulls, Modelfile, GGUF import |
Direct local .gguf or -hf cache |
| Memory lifecycle | Auto-loads on request, unloads after OLLAMA_KEEP_ALIVE (5m default) |
Loads at startup, stays pinned until exit (or router LRU) |
| GPU offloading | Automatic probe, inspected via ollama ps |
Explicit -ngl (auto, all, or exact layer count) |
| Concurrency | OLLAMA_NUM_PARALLEL, OLLAMA_MAX_QUEUE (512 default) |
Continuous batching with -np slots and /slots API |
| KV cache quantization | Global OLLAMA_KV_CACHE_TYPE + OLLAMA_FLASH_ATTENTION |
Per-server -ctk and -ctv with -fa on |
| Speculative decoding | Not user-configurable | Draft model (-md) and ngram speculative modes |
| Grammar constraints | JSON / JSON Schema (format, response_format) |
JSON Schema plus raw GBNF (--grammar-file) |
| Authentication & metrics | No API key, status via /api/ps |
--api-key-file, Prometheus /metrics with --metrics |
Choose Ollama when one developer or small workstation needs fast model switching by tag, automatic VRAM release during idle periods, and zero-flag setup via Modelfile. Choose llama.cpp llama-server when you run a persistent API service, want exact layer offloading and asymmetric KV cache quantization (-ctk/-ctv), depend on speculative decoding or GBNF grammars, or require built-in API keys and Prometheus /metrics without an extra wrapper.