llama.cpp Cheatsheet
llama.cpp is a high-performance C/C++ local LLM inference engine supporting GGUF format models. It offers `llama-cli` (command-line inference) and `llama-server` (HTTP server with OpenAI-compatible API), supporting CPU/GPU/Metal inference and model quantization.
Quick Start
``bash
# Download a GGUF model
huggingface-cli download bartowski/Llama-3.2-3B-Instruct-GGUF --include 'Q4_K_M/*' --local-dir ./models
# Chat inference llama-cli -m ./models/Llama-3.2-3B-Instruct-Q4_K_M.gguf -p "Hello, how are you?"
# HTTP Server (OpenAI-compatible API) llama-server -m ./models/Llama-3.2-3B-Instruct-Q4_K_M.gguf --port 8080
# Query the server (from another terminal)
curl -s http://localhost:8080/v1/chat/completions \
-d '{"model":"model","messages":[{"role":"user","content":"Hello"}]}'
`
CLI Inference (llama-cli)
Use llama-cli for direct terminal inference. Key flags: -m (model file), -p (prompt), -n (max tokens), --temp (temperature), -ngl (GPU layers), -t (threads). Add --no-display-prompt for clean output.
HTTP Server (llama-server)
llama-server exposes an OpenAI-compatible API. Supports chat completions (/v1/chat/completions), text completions (/v1/completions), embeddings (/v1/embeddings), tokenization, and model management endpoints.
Model Quantization
Use llama-quantize to convert models between GGUF quantization levels. Typical command: llama-quantize input.gguf output.gguf Q4_K_M. Common types: Q4_0 (fastest), Q4_K_M (best balance), Q5_K_M (higher quality), Q8_0 (near lossless).
Embeddings
llama.cpp supports text embeddings for RAG pipelines. Enable the embeddings endpoint in server mode with --embeddings, or use the standalone llama-embedding CLI tool.
GPU Acceleration
Control GPU offload with -ngl N. -ngl 99` offloads all layers. Apple Silicon uses Metal automatically. NVIDIA GPUs use CUDA. AMD GPUs use Vulkan or HIP.
Startup & Modes(11)
| Command | Level | ||
|---|---|---|---|
llama-cli --helpShow llama.cpp CLI help | Basic | llama-cli --help | |
llama-cli -mSpecify model file path (GGUF format) | Basic | llama-cli -m models/llama-3.2-3b-q4_k_m.gguf -p 'Hello' | |
llama-cli -pSpecify input prompt | Basic | llama-cli -m model.gguf -p 'What is the capital of France?' | |
llama-cli -fRead prompt from file | Basic | llama-cli -m model.gguf -f prompt.txt | |
llama-cli -nSet max generated tokens | Basic | llama-cli -m model.gguf -p 'Hello' -n 512 | |
llama-cli -tSet thread count (CPU inference) | Intermediate | llama-cli -m model.gguf -p 'Hello' -t 8 | |
llama-cli --tempSet temperature (0-2) | Intermediate | llama-cli -m model.gguf -p 'Poem' --temp 0.7 | |
llama-cli --top-pSet top-p sampling | Intermediate | llama-cli -m model.gguf -p 'Story' --top-p 0.9 | |
llama-cli --ctx-sizeSet context window size | Intermediate | llama-cli -m model.gguf --ctx-size 8192 -p 'Long context' | |
llama-cli -nglSpecify GPU offload layers (GPU acceleration) | Intermediate | llama-cli -m model.gguf -p 'Hello' -ngl 35 | |
llama-cli --no-display-promptOutput only generated text (no prompt echo) | Basic | llama-cli -m model.gguf -p 'Hello' --no-display-prompt |
ai-server(8)
| Command | Level | ||
|---|---|---|---|
llama-server --helpShow HTTP server help | Basic | llama-server --help | |
llama-server -mStart model in HTTP server mode | Intermediate | llama-server -m models/model.gguf --port 8080 | |
llama-server --portSpecify server listening port | Basic | llama-server -m model.gguf --port 8080 | |
llama-server --hostSpecify bind host address | Intermediate | llama-server -m model.gguf --host 0.0.0.0 | |
llama-server -nglGPU offload in server mode | Intermediate | llama-server -m model.gguf -ngl 99 --port 8080 | |
llama-server --embeddingsEnable embeddings endpoint | Expert | llama-server -m model.gguf --embeddings --port 8080 | |
llama-server --chat-templateSpecify chat template file | Expert | llama-server -m model.gguf --chat-template chatml.json | |
curl localhost:8080/v1/chat/completionsSend OpenAI-compatible request to llama-server | Intermediate | curl -s http://localhost:8080/v1/chat/completions -d '{"model":"model","messages":[{"role":"user","content":"Hi"}]}'
|
ai-model(3)
| Command | Level | ||
|---|---|---|---|
llama-cli --hf-tokenUse HuggingFace token to download models | Intermediate | llama-cli --hf-token hf_xxx -m hf:bartowski/Llama-3.2-3B-Instruct-GGUF | |
llama-cli --hfLoad model directly from HuggingFace | Intermediate | llama-cli --hf bartowski/Meta-Llama-3.1-8B-Instruct-GGUF -p 'Hello' | |
llama-quantizeQuantize model to specified precision | Expert | llama-quantize model.gguf model-q4_k_m.gguf Q4_K_M |
ai-eval(1)
| Command | Level | ||
|---|---|---|---|
llama-perplexity -mEvaluate model perplexity | Expert | llama-perplexity -m model.gguf -f test.txt |
ai-embedding(2)
| Command | Level | ||
|---|---|---|---|
llama-embeddingGenerate text embeddings | Intermediate | llama-embedding -m model.gguf -p 'Hello world' | |
curl -X POST http://localhost:8080/v1/embeddingsGet embeddings via API | Intermediate | curl -s http://localhost:8080/v1/embeddings -d '{"model":"model","input":"Hello"}'
|