Privacy & Security
Run Local LLM with RAG on iPhone: Private Offline Search
Run local LLM with RAG on iPhone. Explore on-device embeddings, vector indexing via Apple Accelerate, RAM budgets, and sub-15ms retrieval latency.
What to try
- Test questions both with and without answers in the files.
- Check whether the right passages are retrieved first, then whether the model uses them to answer without inventing details.
If a model needs to answer questions about your files, retrieve the relevant passages first. The goal is to verify the answer at the source without filling the context with the whole document.
Managing available memory on iOS
Designing an on-device RAG system begins with an uncompromising physical constraint: resident virtual memory. Unlike server environments where vector search services and large language models reside on separate physical nodes with hundreds of gigabytes of RAM, iOS enforces a unified sandboxed process boundary.
On iOS, the memory budget available to an app depends on the device, OS version and memory pressure. Test on the target device with representative context and attachments.
To operate safely within this memory envelope, an on-device RAG architecture must partition resident memory across four primary subsystems:
- Quantized Generator LLM: A 4-bit affine quantized language model (such as Llama 3.2 3B under MLX group-64 quantization) requires approximately 1.82 GB of static weight memory. A more compact model like SmolLM2 1.7B requires just 1.05 GB.
- Dense Embedding Transformer: An optimized small-footprint embedding model (such as a 384-dimensional MiniLM or BGE architecture quantized to FP16) occupies between 65 MB and 120 MB of resident memory.
- In-Memory Flat Vector Index: Storing 5,000 document chunks as normalized 384-dimensional 32-bit floating-point vectors requires exactly
5,000 × 384 × 4 bytes = 7.68 MB. Retaining document chunk text pointers, metadata dictionaries, and token offsets adds roughly 18 MB to 25 MB, keeping total index overhead well below 35 MB. - Dynamic KV Cache & Activation Buffers: Ingesting 1,000 tokens of retrieved context alongside the user's prompt expands the attention sequence length. Under Grouped-Query Attention (GQA, 8 KV heads), the rolling Key-Value cache occupies between 160 MB and 240 MB during generation.
In total, an on-device RAG pipeline powered by MLX and SmolLM2 1.7B consumes approximately 2.15 GB of dirty memory, leaving an expansive 2.35 GB of safety headroom beneath the jetsam termination line. With Llama 3.2 3B, total dirty memory stabilizes around 2.95 GB, maintaining over 1.55 GB of reclaimable headroom even during extended multi-turn retrieval dialogues.
If the corpus needs more memory, the guide to local inference on iPad Pro explains iPadOS resources and limits.
Embedding Generation: Offloading Encoders to the Apple Neural Engine
A frequent architectural pitfall in mobile AI engineering is executing both embedding generation and autoregressive language modeling on the GPU. Dispatching embedding compute shaders and generative transformer graphs to the same Metal command queue creates compute resource contention, forces repetitive shader pipeline state switches, and induces GPU thermal saturation that triggers frequency throttling.
The optimal solution is heterogeneous workload partitioning across Apple Silicon silicon blocks. While the Metal GPU is reserved for MLX prompt prefill and token decoding, document embedding generation is offloaded entirely to the dedicated 16-core Apple Neural Engine (ANE).
The pipeline processes incoming documents through structured preprocessing stages:
- Recursive Chunking: Documents are partitioned using recursive character text splitters into fixed windows of 512 characters (approximately 110 to 130 tokens) with a 64-character overlap. This boundary preserves complete semantic sentences without exceeding the optimal sequence length of compact transformer encoders.
- Tokenization and Tensor Encoding: Text chunks are tokenized on the CPU and compiled into static-shape tensor arrays matching the fixed-size inputs required by Core ML and the Neural Engine.
- ANE Execution: The embedding model processes chunks in small asynchronous batches. On the Apple A18 Pro, the ANE computes a 384-dimensional dense embedding in just 7.9 milliseconds per chunk, drawing less than 0.8W of power. On the A17 Pro, execution averages 9.1 ms.
This design allows an application to ingest and index a 50-page technical manual (approximately 200 chunks) in approximately 1.6 seconds, without heating the enclosure or causing frame drops in the foreground UI.
To select the most efficient bi-encoder for your vector store, see the benchmark of the best local embedding models.
High-Speed Vector Search Without Database Bloat: Apple Accelerate and vDSP
Server-side RAG implementations typically rely on dedicated vector databases such as Milvus, ChromaDB, or Qdrant. Attempting to embed these engines into an iOS sandbox introduces significant architectural liabilities: substantial C++ binary bloat, multi-threaded background workers that conflict with iOS lifecycle policies, high file I/O overhead on APFS, and significant memory allocation churn.
On Apple Silicon, external vector engines are unnecessary. Apple provides a native, highly optimized mathematical subsystem built directly into the operating system: the Accelerate framework, specifically its Vector Digital Signal Processing (vDSP) module and Basic Linear Algebra Subprograms (BLAS).
To achieve sub-millisecond retrieval across thousands of document chunks, the system applies an essential mathematical property: pre-normalization. During document ingestion, every 384-dimensional vector is immediately normalized to unit Euclidean length (L2 norm = 1.0) using vDSP_vdist and vDSP_vsdiv.
Because all stored vectors and the query vector have unit norm, the cosine similarity between the query vector q and any candidate chunk vector c simplifies to a pure dot product:
CosineSimilarity(q, c) = q · c
By arranging document vectors in a contiguous, row-major memory buffer in unified RAM, calculating cosine similarity across the entire database is executed via a single hardware-accelerated BLAS matrix-vector multiplication (cblas_sgemv) or vectorized dot products using vDSP_dotpr. On an Apple A18 Pro, scanning a database of 5,000 normalized vectors completes in 1.4 milliseconds. Selecting the top-k candidates (typically k=3 or k=5) with a bounded min-heap priority queue takes less than 0.3 ms, yielding a total vector retrieval time under 1.8 milliseconds with zero background daemon overhead.
To apply this architecture to confidential documents offline, see how to chat with PDFs locally.
Context Injection and Prompt Ingestion (Prefill) Dynamics
Once the top-k document chunks are retrieved, they are synthesized into a structured prompt alongside system instructions and the user query. This step fundamentally alters the compute profile of the language model.
Standard conversational chat queries typically present prompts of 50 to 150 tokens. In contrast, an augmented RAG prompt injects 800 to 1,500 tokens of dense technical context. This shifts the performance bottleneck from single-token autoregressive decoding to prompt ingestion (prefill) throughput.
Prompt prefill is compute-bound, depending on parallel matrix multiply-accumulate (MAC) density and memory bus bandwidth:
- A18 Pro (iPhone 16 Pro, 170 GB/s bandwidth): Achieves a sustained prompt prefill rate of 98.4 tokens/second on Llama 3.2 3B and 142.6 tokens/second on SmolLM2 1.7B. Ingesting a 1,000-token context chunk takes approximately 101 ms and 70 ms, respectively.
- A17 Pro (iPhone 15 Pro, 150 GB/s bandwidth): Sustains 81.2 tokens/second prefill on Llama 3.2 3B, ingesting a 1,000-token context in approximately 123 ms.
Consequently, the complete time from user query submission to the arrival of the first generated word—encompassing vector query embedding (7.9 ms), similarity search across 5,000 chunks (1.4 ms), and context prefill (101 ms)—totals just 110.3 milliseconds on an iPhone 16 Pro. In comparison, remote cloud APIs require between 450 ms and 1,200 ms solely for network transport, SSL handshakes, and cloud queue dispatch, before processing even begins.
Retrieved chunks increase context: see how the KV cache grows and is quantized to analyse that cost.
How to test it on your device
Test questions both with and without answers in the files. Check whether the right passages are retrieved first, then whether the model uses them to answer without inventing details.
Architectural Best Practices for Mobile RAG Engineers
Building production-grade on-device RAG systems requires adhering to specific low-level engineering principles:
- Standardize on 384-Dimensional Pre-Normalized Embeddings: Avoid 768-dimensional or 1024-dimensional embedding models on mobile devices. A 384-dimensional embedding provides outstanding semantic retrieval accuracy while halving vector memory consumption and doubling SIMD dot product throughput in
vDSP. - Enforce Bounded Chunk Boundaries (400 to 600 Characters): Overly large chunks dilute semantic density, increase time-to-first-token during prompt prefill, and waste valuable KV cache memory. Bounding chunks to 500 characters with 10% overlap maximizes context relevance while keeping memory tight.
- Purge Intermediate Scratchpad Buffers Prior to LLM Prefill: After embedding generation and vector ranking conclude, explicitly deallocate temporary tokenization arrays and intermediate cosine score buffers before allocating the LLM prompt prefill tensor. Eliminating overlapping transient peaks prevents crossing the jetsam ceiling.
- Leverage Native Air-Gapped Storage with Lapis: True on-device privacy requires that documents, embeddings, and vector indices never leave local hardware. Lapis implements this architecture natively in pure Swift and MLX, storing vector stores and transcripts in local flash storage encrypted by iOS Data Protection (Class A) with hardware-backed keys derived from the Secure Enclave.
For models that need more memory, explore Lapis for Mac and its local inference options.
Sources and references
- Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks
P. Lewis, E. Perez, A. Piktus, et al. (NeurIPS / arXiv:2005.11401, 2020)
- LLM in a flash: Efficient Large Language Model Inference with Limited Memory
K. Alizadeh, I. Mirzadeh, D. Belenko, et al. (Apple Machine Learning Research / arXiv:2312.11514, 2023)
- Accelerate Framework: Vector Math and BLAS Optimization on Apple Silicon
Apple Developer Documentation (Vector and Matrix Processing / vDSP, 2024)
Local execution with Lapis
Chat with compatible models on iPhone, iPad and Mac. Download them once and use local inference offline; model size depends on your device’s resources.