Error / Memory
llama.cpp: CUDA error: out of memory
llama.cpp crashes while loading or during the response because there isn’t enough graphics memory for the model and context.
The message
ggml_backend_cuda_buffer_type_alloc_buffer: allocating … MiB on device 0: cudaMalloc failed: out of memory … CUDA error: out of memory
Why is this happening?
You assigned more layers to the GPU with -ngl or requested a larger context with -c than fits in VRAM. The context memory (KV cache) grows with context length and can take up several gigabytes with long contexts. Other programs on the graphics card further reduce the free space.
How to fix it
- Update llama.cpp. Newer versions automatically adjust unset values to the available memory with
--fit(on by default). Leave out-ngland-cinstead of setting large fixed values. - Reduce the context length, for example
-c 8192. - Put fewer layers on the GPU, for example
-ngl 30instead of-ngl 99, and work your way up until the error comes back. - Enable Flash Attention with
-fa onand shrink the KV cache with-ctk q8_0 -ctv q8_0. - For MoE models: With
--n-cpu-moe N, you put the expert weights of the first N layers on the CPU, while the rest stays fast on the GPU. - Use a smaller quantization (for example Q4_K_M instead of Q6_K) and close other programs that use VRAM.
- KoboldCpp: Lower the context size (
--contextsize) and GPU layers, and also reduce--blasbatchsizeto 128 or 64.
Set up from scratch again
If it still doesn’t work after that, a fresh setup step by step often helps:
- LM Studio: Your First Local Chat
Install LM Studio on Windows, load a suitable language model, and chat offline, even with your own documents. - VRAM explained: why it decides everything
How much VRAM does local AI need? Rule of thumb, quantization (Q4, Q8, fp8), context, image and video models, offloading, and which card si…
Terms for this
Similar errors
- Very slow responses because the model does not fit in VRAM
- AMD graphics card not being used (ROCm or Vulkan)
- Confusing answers, endless loops, or tags like <|im_end|> (wrong chat template)
- GGUF: tensor '…' data is not within the file bounds, model is corrupted or incomplete (model file incomplete)
- GGUF: unknown model architecture (program too old for the model)
Sources
- https://raw.githubusercontent.com/ggml-org/llama.cpp/master/tools/server/README.md
- https://github.com/LostRuins/koboldcpp/wiki
- https://raw.githubusercontent.com/LostRuins/koboldcpp/concedo/README.md
Checked on Oct 21, 2026. Didn't help? Ask in the forum and name the graphics card, program, and the full message.