1. Add llama.cpp to your project
- iOS / macOS (XCFramework)
- Android (Gradle + NDK)
Every llama.cpp release publishes a prebuilt The upstream
llama.xcframework with slices for iOS (device and simulator), macOS, visionOS, and tvOS. It is built with Metal enabled and includes the mtmd multimodal library.- Download
llama-<build>-xcframework.zipfrom llama.cpp releases and unzip it. - In Xcode, drag
llama.xcframeworkinto your targetβs Frameworks, Libraries, and Embedded Content. import llamain Swift. The C API is exposed directly; no bridging header is needed.
llama.swiftui example is a complete SwiftUI chat app built this way.2. Get a model onto the device
Download a GGUF from Hugging Face at first launch and keep it in app-private storage. llama.cpp memory-maps the file, so it must be a real file on disk β not a compressed asset.- Use
URLSessionConfiguration.background(withIdentifier:)on iOS andWorkManager(orDownloadManager) on Android so downloads survive backgrounding. Q4_0is the best default on phones: it is the smallest quantization and llama.cpp repacks it into Arm-optimized kernels at load time. UseQ4_K_Mwhen you want slightly better quality on capable devices. See Model Library for every GGUF repository.- For vision models also download the matching
mmproj-*.gguffrom the same repository (see Vision & Audio).
3. Load the model and stream a response
The generation loop is the same on every platform: load the model, create a context, build a sampler chain with the modelβs sampling parameters, format the prompt with the chat template, then decode and sample one token at a time.- Swift (iOS / macOS)
- Kotlin (Android)
4. Tune for mobile
- Memory. Weights are memory-mapped by default (
use_mmap = true), so they count as file-backed pages rather than app RSS β iOS jetsam and Android LMK treat them far more leniently. Keepn_ctxas small as the use case allows; the KV cache scales linearly with it. - Threads. Use the performance cores only:
n_threads = activeProcessorCount - 2is a good starting point. More threads than physical big cores usually slows decoding. - GPU. On Apple devices set
n_gpu_layers = 99to run on Metal. On Android, CPU is the safe default; Vulkan and OpenCL (Adreno) backends exist but need device-specific testing. - Quantization.
Q4_0for the smallest footprint and fastest Arm kernels;Q4_K_Mwhen quality matters more than a few hundred MB. - Vision and audio. The XCFramework includes
mtmd; on Android enableLLAMA_BUILD_MTMD. See Vision & Audio. - Benchmark on hardware.
llama-bench -m model.gguf -p 512 -n 128from the prebuilt Android or macOS binaries gives prefill/decode tokens-per-second before you write any app code. See Hardware Evaluation.
Next steps
Chat & Streaming
Multi-turn conversations, sampling parameters, prompt caching.
Structured Output
Constrain generation to a JSON schema or GBNF grammar.
Function Calling & Agents
Tool use with LFM2.5βs native tool-call parser.
Vision & Audio
Run LFM2.5-VL and LFM2.5-Audio on llama.cpp.