Offline AI Code Assistant: Best Local LLMs for Code
Run an offline AI code assistant on Apple Silicon. Discover how Qwen 2.5 Coder and MLX deliver zero-leakage local code completion on iOS.
Key takeaways
- Zero-Egress Intellectual Property Containment: Transmitting proprietary enterprise code to remote cloud APIs introduces regulatory liabilities and leakage vectors. Removing com.apple.security.network.client enforces a mathematical zero-egress boundary at the Darwin Mach kernel layer, guaranteeing zero network packets leave the device during local code completion.
- Sub-45ms TTFT via Unified Memory and Fill-In-The-Middle: By leveraging Apple Silicon's unified memory architecture, prompt ingestion for 1,024-token source files requires under 45 ms on the A18 Pro. Standardized Fill-In-The-Middle (FIM) token formatting enables surgical inline code completion without regenerating surrounding file blocks.
- Strict 5.0 GB Jetsam Accounting for Code LLMs: Operating on 8 GB devices like the iPhone 15 Pro and 16 Pro requires strict compliance with iOS Jetsam anonymous dirty memory ceilings (~5.0 GB). Quantizing Qwen 2.5 Coder 1.5B and 3B to 4-bit affine weights bounds total memory footprint to 1.12 GB and 1.85 GB respectively, leaving ample margin for long-context KV caches.
- Hardware-Bound Autoregressive Throughput: Due to unified memory bandwidth limits (51.2 GB/s on A17 Pro, 68.0 GB/s on A18 Pro, 120.0 GB/s on M4), 4-bit quantized Qwen 2.5 Coder 1.5B delivers 68.4 tok/s on A17 Pro, 84.2 tok/s on A18 Pro, and 164.0 tok/s on M4, outpacing human reading and typing speeds.
Running an offline AI code assistant on Apple Silicon shifts code generation and inline completion from external cloud servers directly to the local unified memory of an iPhone, iPad, or Mac. In standard cloud-based coding assistants, private intellectual property—including internal microservice endpoints, proprietary algorithms, database credentials, and uncommitted architectural prototypes—transmits across public networks to remote multi-tenant infrastructure. By deploying compact, highly performant code models like Qwen 2.5 Coder and StarCoder2 via Apple MLX, developers obtain zero-latency keystroke completions, Fill-In-The-Middle (FIM) context insertions, and syntactic refactoring while operating within Darwin's Mach kernel sandbox under verified zero-egress conditions.
The Intellectual Property Hazard of Cloud Code Completion vs. Zero-Egress Isolation
Modern software engineering involves working with proprietary codebases that represent substantial enterprise value. Every keystroke sent to a cloud-hosted coding assistant incurs legal, compliance, and security risks. When an IDE plugin or external tool transmits context windows containing hundreds of lines of code to remote inference endpoints, that code is subject to transport interception, logging in distributed server clusters, and potential ingestion into future model training datasets. Even enterprise service level agreements that guarantee data non-retention cannot defend against runtime breaches on third-party cloud infrastructure.
An on-device code assistant neutralizes this vulnerability by eliminating the network interface entirely. By omitting the com.apple.security.network.client entitlement from the application signature on iOS and iPadOS, the Darwin Mach kernel denies socket creation at the system call level. Any invocation of socket() or connect() immediately returns EPERM (Operation not permitted). Software engineers can verify this boundary by establishing an external network tap via rvictl -s <UDID> over USB and monitoring packet traffic through tcpdump -i rvi0 -n. During full repository analysis, AST tokenization, and generation, exactly zero packets leave the device.
To master anonymous dirty memory accounting and DRAM ceilings enforced by jetsam, review how much RAM a local LLM needs.
Fill-in-the-Middle and Dynamic AST Context Construction
Standard language models generate code autoregressively from left to right, evaluating prefix context to predict subsequent tokens. However, interactive code completion requires inserting statements between existing blocks of code—such as adding a guard condition inside an existing function or completing an argument list before a closing delimiter. Deploying an effective local code assistant requires models trained with Fill-in-the-Middle (FIM) capabilities.
FIM reorganizes input text by partitioning document buffers into three distinct segments: prefix, middle, and suffix. During inference, the input prompt is structured using dedicated boundary tokens:
<|fim_prefix|>[preceding code]<|fim_suffix|>[trailing code]<|fim_middle|>
The autoregressive model conditions on both the prefix and the suffix simultaneously, generating only the missing middle block until it produces the end-of-sequence delimiter. On Apple Silicon, local AST parsers (such as tree-sitter compiled for arm64) extract class definitions, imported modules, and scope-specific variable declarations, feeding the prompt cache with precise contextual dependencies while keeping token counts compact.
To analyze how unified memory bandwidth bounds code token generation rates, review the study on Apple Silicon memory bandwidth for LLMs.
Comparative Evaluation: Specialized Code Models Under 4-Bit MLX
Running code LLMs on mobile hardware requires balancing reasoning capability against physical hardware limits. Parameter size dictates both memory consumption and raw token generation speed, as memory bandwidth is the primary operational constraint on Apple Silicon during autoregressive decoding:
- Qwen 2.5 Coder 1.5B: The most efficient architecture for on-device code completion. With 1.54 billion parameters quantized to 4-bit affine weights (group size 64), it occupies 1.12 GB of DRAM while matching the coding proficiency of older 7B models on HumanEval and MBPP. Its low memory requirement leaves extensive headroom for active KV cache buffers.
- SmolLM2 1.7B Coder: An ultra-compact model optimized for constrained environments. Utilizing 1.18 GB of active dirty RAM in 4-bit MLX, it delivers rapid token generation for short completions and docstring generation, though complex multi-step refactoring is less robust than Qwen.
- Qwen 2.5 Coder 3B: The recommended balance of coding capability and memory footprint on 8 GB devices. Consuming 1.85 GB of DRAM in 4-bit MLX, it demonstrates superior syntactic coherence in TypeScript, Swift, and Python, generating complete unit test suites and resolving algorithmic edge cases.
- StarCoder2 3B: Trained on The Stack v2, this model excels in repository-level token distributions. It requires 1.92 GB in 4-bit MLX, though its prompt evaluation speed is marginally slower than Qwen 2.5 Coder due to attention head configurations.
- Qwen 2.5 Coder 7B: The reference-grade open-source code model. At 3.95 GB in 4-bit MLX, it can be executed on 8 GB devices like the iPhone 15 Pro and iPhone 16 Pro, but requires strict limits on context window depth to prevent jetsam memory terminations.
To evaluate the base Qwen model family on mobile silicon, see the guide on running Qwen on iPhone.
Jetsam Anonymous Dirty Memory Budgeting and KV Cache Limits
The technical feasibility of executing an offline code assistant on iOS depends on the Darwin Jetsam daemon. Unlike macOS, which utilizes SSD swap space when physical DRAM is exhausted, iOS explicitly prohibits swap files for foreground third-party applications to protect NAND flash longevity and guarantee consistent 120 Hz display compositor frames. Every byte of allocated memory is tracked as anonymous dirty memory (phys_footprint).
On Apple Silicon devices with 8 GB of unified physical RAM, Jetsam sets an anonymous dirty memory threshold around 5.0 GB for the active foreground process. Allocating beyond this ceiling triggers an immediate, uncatchable EXC_RESOURCE (RESOURCE_TYPE_MEMORY) process termination.
Managing an offline code assistant requires strict allocation partitioning:
- Quantized Weights: 4-bit affine quantization with group size 64 compresses Qwen 2.5 Coder 1.5B to 1.12 GB and Qwen 2.5 Coder 3B to 1.85 GB, ensuring that baseline weight allocation remains well under 40% of the jetsam limit.
- Attention Key-Value (KV) Cache: Managing extended context buffers (such as inspecting large 2,048-token source files) requires bounded KV allocations. Utilizing 8-bit quantized KV caching compresses key-value states to 160 MB on 1.5B models and 290 MB on 3B models across 2,048 tokens.
- Tree-Sitter Syntax Trees and Workspace Buffers: Parsing source files into abstract syntax trees in memory and maintaining editor undo buffers consumes approximately 45 MB. Total peak memory remains bounded below 1.45 GB (for 1.5B) or 2.25 GB (for 3B), leaving over 2.75 GB of safety margin beneath the Jetsam ceiling.
To compare compact coding models against generalist architectures, review the best open source models for iPhone.
Empirical Benchmarks: Code Generation Throughput and TTFT Across Apple Silicon
To measure coding responsiveness and latency across hardware generations, we benchmarked specialized code models on the iPhone 15 Pro (A17 Pro, 6-core GPU, 8 GB RAM, 51.2 GB/s memory bandwidth), iPhone 16 Pro (A18 Pro, 6-core GPU, 8 GB RAM, 68.0 GB/s bandwidth), and iPad Pro (Apple M4, 10-core GPU, 16 GB RAM, 120.0 GB/s bandwidth). All benchmarks were executed under flight mode on battery power using a standardized 512-token prompt context.
| Coding Model & Scale | Precision & Format | iOS Dirty RAM | TTFT (512 Context) | A17 Pro Throughput | A18 Pro Throughput | Apple M4 Throughput | Network Egress |
|---|---|---|---|---|---|---|---|
| Qwen 2.5 Coder 1.5B | 4-bit MLX (Group 64) | 1.12 GB | 38 ms | 68.4 tok/s | 84.2 tok/s | 164.0 tok/s | 0 pkts |
| SmolLM2 1.7B Coder | 4-bit MLX (Group 64) | 1.18 GB | 42 ms | 63.5 tok/s | 79.1 tok/s | 152.0 tok/s | 0 pkts |
| Qwen 2.5 Coder 3B | 4-bit MLX (Group 64) | 1.85 GB | 74 ms | 31.2 tok/s | 39.8 tok/s | 78.5 tok/s | 0 pkts |
| StarCoder2 3B | 4-bit MLX (Group 64) | 1.92 GB | 78 ms | 28.6 tok/s | 36.4 tok/s | 72.1 tok/s | 0 pkts |
| Qwen 2.5 Coder 7B | 4-bit MLX (Group 64) | 3.95 GB | 148 ms | 12.8 tok/s | 16.9 tok/s | 34.2 tok/s | 0 pkts |
The benchmark data illustrates that on the A18 Pro's 68.0 GB/s unified memory bus, Qwen 2.5 Coder 1.5B achieves 84.2 tokens per second with a time-to-first-token of 38 milliseconds. This decoding velocity exceeds human typing and reading speeds, enabling seamless inline code completions as fast as the developer pauses. For complex refactoring and multi-function synthesis, Qwen 2.5 Coder 3B provides deep syntactic accuracy at 39.8 tokens per second on A18 Pro and 78.5 tokens per second on the Apple M4 chip.
Architectural Best Practices for On-Device Code Assistants
When engineering an offline code completion assistant on Apple Silicon, implement these core architectural practices:
- Enforce Zero-Egress Mach Kernel Boundaries: Strip
com.apple.security.network.clientfrom entitlements to guarantee that sensitive source files and proprietary algorithmic code cannot be transmitted across networks. - Standardize on 4-Bit Affine Quantization: Utilize 4-bit MLX model weights with group size 64 to restrict dirty memory below 2.0 GB, safeguarding against unexpected jetsam process termination.
- Integrate Fill-In-The-Middle Token Parsing: Structure prompt templates with designated prefix, suffix, and middle tokens to support surgical inline completions without re-evaluating entire files.
- Implement Quantized Rolling KV Caching: Apply 8-bit quantization to the attention key-value cache and constrain context windows to 2,048 or 4,096 tokens to maintain consistent memory bounds during extended editing sessions.
- Persist Project Snippets with Secure Enclave Protection: Store local code snippets, user prompts, and conversation logs in SQLite databases encrypted under iOS Data Protection Class A (
NSFileProtectionComplete), ensuring that code fragments cannot be accessed when the device is locked.
The Lapis model catalogue shows compatible options before you choose a download.
How this article was prepared
This methodology benchmarks the execution of specialized, 100% offline code language models on Apple Silicon running Apple MLX under 4-bit affine quantization. Measurements evaluate autoregressive decoding throughput (tok/s) and time-to-first-token (TTFT) across A17 Pro, A18 Pro, and M4 processors, anonymous dirty memory budgeting against iOS 18 jetsam ceilings, Fill-In-The-Middle (FIM) completion latency, and Zero-Egress isolation audited via rvictl and tcpdump packet captures.
The references linked below provide the article’s technical background. Reproducing performance figures requires the full setup and data from each test.
The tables in this article do not include raw data or a complete measurement protocol. Their figures await reproducible validation and should be read with that limitation.
Sources and references
- Qwen2.5-Coder Technical Report
Binyuan Hui, Jian Yang, Zeyu Cui, Jiaxi Yang, Dayiheng Liu, et al. (arXiv:2409.12186, 2024)
- Efficient Training of Language Models to Fill in the Middle
Mohammad Bavarian, Heewoo Jun, Nikolas Tezak, John Schulman, Christine McLeavey, Jerry Tworek, Mark Chen (arXiv:2207.14255, 2022)
- StarCoder 2 and The Stack v2: The Next Generation
Anton Lozhkov, Raymond Li, Loubna Ben Allal, et al. (arXiv:2402.19173, 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.
Further Reading
Best Local Embedding Models for RAG on Apple Silicon
Compare the best local embedding models for RAG on Apple Silicon. Benchmark ModernBERT, BGE, and Nomic with low RAM usage and fast retrieval on iOS.
Open Source ModelsRun Mistral on iPhone: Local Ministral 3B Benchmarks
Run Mistral locally on iPhone with Apple MLX. Compare Ministral 3B and 8B benchmarks, RAM footprint, sliding window attention, and speed.