Reasoning Models

Function Calling Local LLMs on Apple Silicon and iOS

Lapis EngineeringPublished Updated 6 min read

A useful tool call needs the right name, valid arguments and a reason to act.

Macro view of green computer code, system logic, and data structures streaming across a dark screen
Photo: Markus Spiske · Unsplash
What to try
  • Score schema validity, tool choice and arguments separately.
  • Include requests that should receive an ordinary reply, ask for clarification or abstain.

A useful tool call needs the right name, valid arguments and a reason to act. Constraining the JSON solves one part; the app must still decide whether the action is appropriate.

The Mechanics of On-Device Tool Calling: Schema Prompting vs. Constrained Grammars

In cloud-hosted inference pipelines, function calling typically relies on prompting large foundation models with JSON Schemas injected into the system prompt. While massive models generally follow formatting instructions reliably, deploying edge models with 1 to 3 billion parameters—such as Llama 3.2 3B or Qwen 2.5 3B—exposes high rates of structural degradation under unconstrained autoregressive decoding:

  • Schema Drift and Key Hallucination: Small models frequently substitute requested parameter names with plausible synonyms (for instance, emitting "city_name" instead of "location"), breaking downstream deserialization pipelines.
  • Truncation and Syntax Corruption: Under tight sequence limits, edge models often omit closing braces (}) or brackets, leaving JSON strings syntactically incomplete.
  • Type Coercion Failures: Models frequently output numeric or boolean values as unparsed strings (e.g., "true" instead of boolean true, or "42" instead of integer 42), causing type mismatch exceptions in strongly-typed Swift decoders.
  • Conversational Bleed: Unconstrained models often prepend or append conversational chatter (such as "Here is the function call:") around raw JSON payloads, requiring brittle regex parsers.

Grammar-constrained decoding eliminates these failure modes at the hardware sampling layer. Rather than hoping the model adheres to instructions, the target JSON Schema is compiled into a Deterministic Finite Automaton (DFA) or Context-Free Grammar (CFG). At each generation step $t$, the DFA evaluates the current parse state $S_t$. Before the GPU executes sampling over the vocabulary $V$, a custom Metal compute kernel applies a logit mask across the unnormalized logits $z \in \mathbb{R}^{|V|}$:

$z_i' = \begin{cases} z_i & \text{if token } i \in \text{ValidTokens}(S_t) \\ -\infty & \text{otherwise} \end{cases}$

Because invalid tokens receive a logit value of $-\infty$, their probability under softmax evaluates to exactly zero. The GPU cannot select a token that violates the JSON Schema. This guarantees 100% syntactic validity, eliminates post-processing retry loops, and accelerates generation by pruning ungrammatical branches from the sampling tree.

Token overhead from tool schemas is drastically reduced via prefix KV caching. The guide to prompt caching for local LLMs on Apple Silicon details that speedup.

Memory Architecture: Managing Prompt Prefill, Prefix Caching, and Jetsam Limits

Integrating function calling into an edge LLM introduces significant token volume before generation begins. Specifying 4 to 8 native tools—including parameter descriptions, enumerated values, and requirement arrays—adds between 800 and 1,600 prompt tokens to the conversational context. Managing this overhead on mobile hardware requires navigating two distinct bottlenecks: prefill latency and operating system memory limits.

During the prompt prefill phase, the processor must ingest the entire system prompt and tool schema in parallel ($B=1$, matrix-matrix multiplication). On the Apple A18 Pro SoC with 170 GB/s unified memory bandwidth, prefilling a 1,200-token tool definition requires approximately 48.6 ms. On the A17 Pro (150 GB/s), prefill takes roughly 62.4 ms. In a multi-turn tool interaction—where a user asks a question, the model calls a tool, the application executes the tool locally, and the model synthesizes the result—re-encoding the identical tool definitions on every turn introduces perceptible UI lag.

To eliminate redundant computation, Apple MLX supports persistent prefix caching within unified memory (MTLResourceStorageModeShared). Once the static tool schema is prefilled during application launch or session initialization, its Key and Value projection tensors remain resident in memory. On subsequent conversational turns, the prefill engine skips the 1,200 static schema tokens and computes attention only for new user inputs and tool returns. This reduces Time-To-First-Token (TTFT) by up to 78%, dropping invocation latency down to 12.4 ms on A18 Pro.

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.

Growing context also affects tool use. The guide to KV cache quantization on Apple Silicon explains that memory cost.

How to test it on your device

Score schema validity, tool choice and arguments separately. Include requests that should receive an ordinary reply, ask for clarification or abstain.

To see how these tool invocations are orchestrated into autonomous on-device loops, review the guide to local AI agents for iPhone on Apple MLX.

Model Evaluation: Best Open-Weights Models for Mobile Tool Calling

Selecting the optimal model checkpoint for on-device tool calling requires evaluating parameter efficiency, tokenizer design, and fine-tuning lineage:

  • Qwen 2.5 3B Instruct: The strongest edge model for function calling currently available. Qwen 2.5 incorporates dedicated special tokens (<tool_call> and </tool_call>) and underwent extensive multi-turn agent training. It excels at extracting structured arguments, handling nested dictionaries, and accurately resolving multiple tools within a single conversational turn.
  • Llama 3.2 3B Instruct: Exceptionally fast during prompt prefill and autoregressive decoding. While Llama 3.2 possesses strong general reasoning capabilities, its unconstrained tool calling accuracy lags behind Qwen on complex schemas. When paired with Metal grammar-constrained decoding, its accuracy jumps to 89.4%, making it an excellent choice for lightweight, latency-critical tools with flat argument structures.
  • Hermes 3 3B (Llama-3.2 based): Nous Research's fine-tune specializes in ChatML tool schemas and agentic self-reflection. It demonstrates high compliance with function calling protocols, though its slightly larger KV activations require diligent memory monitoring on 8 GB devices.

For a closer look at one of these families, read the analysis of Qwen on iPhone and its memory requirements.

Zero-Cloud Architecture: Sandboxed Native iOS Tool Execution

Executing function calling directly on-device alters the privacy and security characteristics of autonomous agents. In a cloud-dependent architecture, every tool call requires transmitting full conversational context and system state over the public internet to third-party model servers. On iOS, native tool execution operates within a secure local loop:

  1. Deterministic Extraction: The local model emits a grammar-verified JSON payload specifying the selected tool and typed arguments.
  2. Sandboxed Swift Dispatch: An internal tool dispatcher parses the payload directly into strongly-typed Swift Decodable structures. The call is dispatched through isolated Swift actors to system frameworks:
    • EventKit: Querying calendar schedules and creating reminders without network traffic.
    • Contacts Framework: Resolving contact details and phone numbers offline.
    • Local SQLite / GRDB Databases: Running parameterized queries against on-device document stores or local RAG vector embeddings.
    • AppIntents & Shortcuts: Triggering user-configured automated actions directly within the operating system.
  3. Zero Telemetry Leakage: Sensitive data—such as calendar attendee names, private address book entries, and internal database records—never leaves physical DRAM. The entire agentic workflow remains fully operational in airplane mode, immune to external API downtime and third-party data collection.

Engineering Best Practices for Deploying Function Calling on iOS

When implementing production-grade function calling using Apple MLX on iOS, follow these architectural principles developed during the engineering of Lapis:

  1. Pre-Compile Schemas into Compact DFAs: Never parse raw JSON Schemas dynamically during interactive generation. Compile all registered application tool definitions into compact Deterministic Finite Automata (DFAs) ahead of time. Store pre-computed transition tables in contiguous Metal buffers to minimize thread divergence during logit masking kernels.
  2. Enforce Persistent Prefix Caching: Configure MLX Swift to pin the Key and Value cache tensors of the static tool definition prefix in shared memory (MTLResourceStorageModeShared). Ensuring that system prompts are evaluated only once reduces interactive TTFT from 48 ms to under 13 ms across multi-step agent interactions.
  3. Clamp Argument Sequence Lengths: Impose an absolute token ceiling (typically 256 to 512 tokens) on argument generation. Because function calling arguments represent structured data rather than open-ended prose, strict length clamping bounds KV cache memory growth and guards against rare circular generation loops.
  4. Monitor Memory Pressure via DispatchSource: Integrate a proactive memory observer using DispatchSource.makeMemoryPressureSource(eventMask: [.warning, .critical], queue: .main). When iOS signals system-wide memory constraints, immediately flush intermediate tool execution caches or downsample conversation history to prevent Darwin's jetsam daemon from terminating the process.
  5. Isolate Tool Execution Within Swift Actors: Decouple inference compute kernels from tool execution pipelines. Execute local tool side-effects inside background Swift actors to ensure the main thread remains fluid and responsive to user input while the local model parses and executes system commands.

The Lapis model catalogue shows compatible options before you choose a download.

Sources and references

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.

App Store