---
title: "llama.cpp"
description: "Run GGUF models locally with maximum performance. A lightweight, high-performance inference engine for running quantized models on any hardware."
sidebar:
  label: "Llama.cpp"
seo:
  title: "llama.cpp Integration"
  description: "Run GGUF models locally with llama.cpp for maximum performance and efficiency. Complete setup guide for all platforms."
---

## What is the Autohand llama.cpp integration? [#quick-answer]

The Autohand llama.cpp integration connects the coding agent to a local or self-managed llama.cpp server. It runs compatible GGUF models behind an OpenAI-style endpoint, keeping model execution on hardware you control.

|  |  |
| --- | --- |
| Status | Available |
| Requires | A running llama.cpp server, a compatible GGUF model, and enough local compute and memory for that model. |
| Configure with | Start the llama.cpp server, set its base URL in the `llamacpp` provider configuration, and select the loaded model. |
| Best for | Portable local inference across macOS, Linux, and Windows hardware. |

:::warning
**Know before you start:** Quality, context length, speed, and tool support depend on the chosen model, quantization, server build, and available hardware.
:::

## Overview

llama.cpp is a high-performance C++ inference engine optimized for running large language models locally. When integrated with Autohand, you get:

- Maximum inference speed with optimized C++ code
- Support for quantized GGUF models (2-bit to 8-bit)
- Efficient memory usage with memory-mapped files
- Cross-platform support (macOS, Linux, Windows)
- Hardware acceleration (Metal, CUDA, OpenCL, Vulkan)
- Direct control over model loading and inference parameters

:::tip
**When to use llama.cpp:** Choose llama.cpp over Ollama when you need maximum performance, fine-grained control over inference parameters, or want to run specific GGUF model variants.
:::

## Installation

Build llama.cpp from source or use pre-built binaries.

### macOS

Build with Metal support for Apple Silicon:

```bash
# Clone the repository
git clone https://github.com/ggml-org/llama.cpp
cd llama.cpp

# Build with Metal acceleration (Apple Silicon)
make LLAMA_METAL=1

# Or use CMake for more options
mkdir build && cd build
cmake .. -DLLAMA_METAL=ON
cmake --build . --config Release

# Verify installation
./llama-server --version
```

Using Homebrew:

```bash
# Install via Homebrew
brew install llama.cpp

# Verify installation
llama-server --version
```

### Linux

Build with CUDA support for NVIDIA GPUs:

```bash
# Install dependencies
sudo apt install build-essential cmake

# Clone and build
git clone https://github.com/ggml-org/llama.cpp
cd llama.cpp

# Build with CUDA (NVIDIA GPU)
make LLAMA_CUDA=1

# Or build with OpenBLAS (CPU optimized)
make LLAMA_OPENBLAS=1

# Or build with Vulkan (AMD/Intel GPU)
make LLAMA_VULKAN=1

# Verify
./llama-server --version
```

### Windows

Build using Visual Studio or MinGW:

```bash
# Clone repository
git clone https://github.com/ggml-org/llama.cpp
cd llama.cpp

# Build with CMake (Visual Studio)
mkdir build
cd build
cmake .. -DLLAMA_CUDA=ON
cmake --build . --config Release

# Or download pre-built binaries from:
# https://github.com/ggml-org/llama.cpp/releases
```

### Start the server

Run the OpenAI-compatible API server:

```bash
# Start server with a model
./llama-server \
  --model /path/to/model.gguf \
  --host 127.0.0.1 \
  --port 8080 \
  --ctx-size 4096 \
  --n-gpu-layers 35

# Server is now running at http://127.0.0.1:8080
```

**Tool calling depends on the model and server.** An OpenAI-compatible endpoint alone does not guarantee usable tool calls. Confirm that the selected model, its chat template and your server support the tools your task needs. Memory use also grows with context size; these examples are starting points, not performance guarantees.

## GGUF models [#models]

Download quantized GGUF models from Hugging Face or convert your own.

### Download models

Popular models are available on Hugging Face:

```bash
# Let llama.cpp select and download the Q4_K_M files
./llama-server -hf Qwen/Qwen2.5-Coder-7B-Instruct-GGUF:Q4_K_M \
  --ctx-size 4096 --port 8080

# Or try 14B when your machine has more memory available
./llama-server -hf Qwen/Qwen2.5-Coder-14B-Instruct-GGUF:Q4_K_M \
  --ctx-size 4096 --port 8080
```

### Recommended models

| Model | Format | Starting point |
| --- | --- | --- |
| Qwen2.5-Coder 7B Instruct | GGUF, Q4\_K\_M | Start here for coding |
| Qwen2.5-Coder 14B Instruct | GGUF, Q4\_K\_M | Try when more memory is available |

### Quantization levels

| Quantization | Bits | Quality | Speed |
| --- | --- | --- | --- |
| Q2\_K | 2-bit | Lowest | Fastest |
| Q3\_K\_M | 3-bit | Low | Fast |
| Q4\_K\_M | 4-bit | Good | Balanced |
| Q5\_K\_M | 5-bit | Better | Moderate |
| Q6\_K | 6-bit | High | Slower |
| Q8\_0 | 8-bit | Highest | Slowest |

:::tip
**Recommendation:** Q4\_K\_M offers the best balance of quality and speed for most use cases. Use Q5\_K\_M or Q6\_K for tasks requiring higher accuracy.
:::

## CLI configuration

Configure Autohand to use llama.cpp in `~/.autohand/config.json`:

```json
{
  "provider": "llamacpp",
  "llamacpp": {
    "baseUrl": "http://127.0.0.1:8080",
    "model": "qwen2.5-coder-7b-instruct",
    "contextSize": 4096,
    "temperature": 0.7,
    "topP": 0.9,
    "topK": 40,
    "repeatPenalty": 1.1,
    "seed": -1
  }
}
```

### Configuration options

| Option | Description | Default |
| --- | --- | --- |
| `baseUrl` | llama.cpp server URL | `http://127.0.0.1:8080` |
| `model` | Model name (for display) | \- |
| `contextSize` | Context window size | `4096` |
| `temperature` | Sampling temperature (0-2) | `0.7` |
| `topP` | Nucleus sampling threshold | `0.9` |
| `topK` | Top-K sampling | `40` |
| `repeatPenalty` | Repetition penalty | `1.1` |
| `seed` | Random seed (-1 for random) | `-1` |

## Server options

Fine-tune the llama.cpp server for optimal performance:

### Basic options

```bash
./llama-server \
  -hf Qwen/Qwen2.5-Coder-7B-Instruct-GGUF:Q4_K_M \
  --host 127.0.0.1 \
  --port 8080 \
  --ctx-size 8192 \
  --batch-size 512 \
  --threads 8
```

### GPU acceleration

```bash
# Offload layers to GPU (more layers = more VRAM, faster)
./llama-server \
  -hf Qwen/Qwen2.5-Coder-7B-Instruct-GGUF:Q4_K_M \
  --n-gpu-layers 35 \
  --main-gpu 0

# Use multiple GPUs
./llama-server \
  --model ./models/llama-70b.Q4_K_M.gguf \
  --n-gpu-layers 80 \
  --tensor-split 0.5,0.5
```

### Memory optimization

```bash
# Use memory-mapped files (recommended for large models)
./llama-server \
  --model ./models/large-model.gguf \
  --mmap \
  --mlock

# Reduce memory with smaller batch size
./llama-server \
  --model ./models/model.gguf \
  --batch-size 128 \
  --ctx-size 2048
```

### Server options reference

| Option | Description |
| --- | --- |
| `--model` | Path to GGUF model file |
| `--ctx-size` | Context size (tokens) |
| `--batch-size` | Batch size for prompt processing |
| `--threads` | Number of CPU threads |
| `--n-gpu-layers` | Layers to offload to GPU |
| `--main-gpu` | Primary GPU index |
| `--tensor-split` | Split ratio for multi-GPU |
| `--mmap` | Use memory-mapped files |
| `--mlock` | Lock model in memory |

## Performance tuning [#performance]

### Optimal GPU layers

Calculate layers to offload based on your VRAM:

```bash
# Check model layers
./llama-cli --model model.gguf --verbose 2>&1 | grep "n_layer"

# General formula: VRAM_GB * 2 = approximate layers
# 8GB VRAM ~ 16-20 layers
# 12GB VRAM ~ 24-30 layers
# 24GB VRAM ~ 48-60 layers
```

### Batch size tuning

| VRAM | Batch Size | Context |
| --- | --- | --- |
| 4GB | 128 | 2048 |
| 8GB | 256 | 4096 |
| 12GB | 512 | 8192 |
| 24GB+ | 1024 | 16384+ |

### CPU-only optimization

```bash
# Optimize for CPU inference
./llama-server \
  --model model.gguf \
  --threads $(nproc) \
  --batch-size 512 \
  --ctx-size 4096 \
  --mmap
```

## Convert models [#convert]

Convert Hugging Face models to GGUF format:

### Install dependencies

```bash
# Install Python dependencies
pip install torch transformers sentencepiece
```

### Convert to GGUF

```bash
# Convert from Hugging Face format
python convert_hf_to_gguf.py \
  /path/to/hf-model \
  --outfile model-f16.gguf \
  --outtype f16

# Quantize the model
./llama-quantize model-f16.gguf model-q4_k_m.gguf Q4_K_M
```

### Quantization options

```bash
# Available quantization types
./llama-quantize --help

# Common quantizations:
# Q4_K_M - Best balance (recommended)
# Q5_K_M - Higher quality
# Q8_0   - Highest quality, largest size
# Q2_K   - Smallest, lowest quality
```

## Best practices

- **Match quantization to hardware**: Use Q4\_K\_M for most systems, Q5\_K\_M+ for high-end GPUs.
- **Maximize GPU offload**: Offload as many layers as VRAM allows.
- **Use memory mapping**: Enable `--mmap` for faster model loading.
- **Tune batch size**: Larger batches improve throughput but use more memory.
- **Monitor temperature**: GPU inference can cause thermal throttling.

### Recommended configurations

#### Start with Qwen2.5-Coder 7B

```bash
./llama-server \
  -hf Qwen/Qwen2.5-Coder-7B-Instruct-GGUF:Q4_K_M \
  --n-gpu-layers 20 \
  --ctx-size 4096 \
  --batch-size 256
```

#### Try Qwen2.5-Coder 14B with more headroom

```bash
./llama-server \
  -hf Qwen/Qwen2.5-Coder-14B-Instruct-GGUF:Q4_K_M \
  --n-gpu-layers 40 \
  --ctx-size 8192 \
  --batch-size 512
```

#### Apple Silicon (M1/M2/M3)

```bash
./llama-server \
  -hf Qwen/Qwen2.5-Coder-7B-Instruct-GGUF:Q4_K_M \
  --n-gpu-layers 99 \
  --ctx-size 8192 \
  --batch-size 512
```

## Troubleshooting

### Common issues

| Issue | Solution |
| --- | --- |
| Out of memory | Reduce `--n-gpu-layers` or `--ctx-size` |
| Slow inference | Increase GPU layers or use smaller quantization |
| Model not loading | Verify GGUF format and file integrity |
| CUDA error | Update GPU drivers, rebuild with correct CUDA version |
| Metal not working | Rebuild with `LLAMA_METAL=1` |

### Debug mode

```bash
# Run with verbose output
./llama-server \
  --model model.gguf \
  --verbose

# Check model info
./llama-cli --model model.gguf --verbose 2>&1 | head -50
```

### Test the server

```bash
# Test API endpoint
curl http://127.0.0.1:8080/v1/models

# Test completion
curl http://127.0.0.1:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [{"role": "user", "content": "Hello!"}],
    "max_tokens": 100
  }'
```

**How do I configure the Autohand llama.cpp integration?**

Start the llama.cpp server, set its base URL in the `llamacpp` provider configuration, and select the loaded model.

**What does the Autohand llama.cpp integration require?**

A running llama.cpp server, a compatible GGUF model, and enough local compute and memory for that model.

**What limitations should I know about?**

Quality, context length, speed, and tool support depend on the chosen model, quantization, server build, and available hardware.
