
apple-on-device-ai
Build private, on-device AI features on iPhone, iPad, and Mac with Foundation Models, Core ML, MLX Swift, or llama.cpp. Use when choosing an Apple-local model runtime, building an Apple Intelligence chatbot or tool-calling feature, running an LLM on Apple Silicon, converting or compressing a Python model for Core ML, or comparing on-device inference backends. For Swift Core ML loading and prediction code, use the coreml skill.
- Installs
- 4K
- Last 24h
- +16
- Trending rank
- #6694
- Security
- Pass
Install
Works with every agent the skills CLI supports.
npx skills add https://github.com/dpearson2699/swift-ios-skills --skill apple-on-device-ai- 01
Install
Run the command in your project. The CLI asks which agents to install for.
- 02
Describe the task
Ask your agent for something this skill covers. It loads apple-on-device-ai automatically when relevant.
- 03
Review the result
The agent follows the SKILL.md steps, scripts and references bundled with the skill.
Security audits
Independent scans of the skill's instructions and bundled files.
SKILL.md
The instructions your agent reads when the skill is triggered.
On-Device AI for Apple Platforms
Guide for selecting, deploying, and optimizing on-device ML models. Covers Apple Foundation Models, Core ML, MLX Swift, and llama.cpp.
Contents
- Framework Selection Router
- Apple Foundation Models Overview
- Core ML Overview
- MLX Swift Overview
- Multi-Backend Architecture
- Performance Best Practices
- Common Mistakes
- Review Checklist
- References
Framework Selection Router
Use this decision tree to pick the right framework for your use case.
Apple Foundation Models
When to use: Text generation, summarization, entity extraction, structured output, and short dialog on iOS 26+ / macOS 26+ devices with Apple Intelligence enabled. No app-managed API key, network round trip, or model hosting; still handle system model asset readiness.
Best for:
- Generating text or structured data with
@Generabletypes - Summarization, classification, content tagging
- Tool-augmented generation with the
Toolprotocol - Apps that need guaranteed on-device privacy
Not suited for: Complex math, code generation, factual accuracy tasks, or apps targeting pre-iOS 26 devices.
Core ML
When to use: Deploying custom trained models (vision, NLP, audio) across all Apple platforms. Converting models from PyTorch, TensorFlow, or scikit-learn with coremltools.
Best for:
- Image classification, object detection, segmentation
- Custom NLP classifiers, sentiment analysis models
- Audio/speech models via SoundAnalysis integration
- Any scenario needing Neural Engine optimization
- Models requiring quantization, palettization, or pruning
MLX Swift
When to use: Running specific open-source LLMs (Llama, Mistral, Qwen, Gemma) on Apple Silicon with maximum throughput. Research and prototyping.
Best for:
- Highest sustained token generation on Apple Silicon
- Running Hugging Face models from
mlx-community - Research requiring automatic differentiation
- Fine-tuning workflows on Mac
llama.cpp
When to use: Cross-platform LLM inference using GGUF model format. Production deployments needing broad device support.
Best for:
- GGUF quantized models (Q4_K_M, Q5_K_M, Q8_0)
- Cross-platform apps (iOS + Android + desktop)
- Maximum compatibility with open-source model ecosystem
Quick Reference
| Scenario | Framework |
|---|---|
| Text generation on Apple Intelligence devices (iOS 26+) | Foundation Models |
| Structured output from on-device LLM | Foundation Models (@Generable) |
| Image classification, object detection | Core ML |
| Custom model from PyTorch/TensorFlow | Core ML + coremltools |
| Running specific open-source LLMs | MLX Swift or llama.cpp |
| Maximum throughput on Apple Silicon | MLX Swift |
| Cross-platform LLM inference | llama.cpp |
| OCR and text recognition | Vision framework |
| Sentiment analysis, NER, tokenization | Natural Language framework |
| Training custom classifiers on device | Create ML |
Apple Foundation Models Overview
Use the system language model for short generation, summarization, tagging, structured output, and tool-augmented tasks on Apple Intelligence devices. Gate every entry point before creating a session:
import FoundationModels
switch SystemLanguageModel.default.availability {
case .available:
guard SystemLanguageModel.default.supportsLocale(Locale.current) else {
// Use locale fallback before generating
break
}
// Proceed with model usage
case .unavailable(.appleIntelligenceNotEnabled):
// Guide user to enable Apple Intelligence in Settings
case .unavailable(.modelNotReady):
// System model assets are not ready; show loading state
case .unavailable(.deviceNotEligible):
// Device cannot run Apple Intelligence; use fallback
case .unavailable(let reason):
// Unknown or future unavailable reason; use fallback and log reason
}
Then create a session and keep its shared context budget small:
let session = LanguageModelSession {
"You are a helpful cooking assistant."
}
session.prewarm()
let response = try await session.respond(to: "Suggest a quick pasta recipe")
Required guardrails:
- Sessions are stateful and accept one request at a time; serialize access and
check
isRespondingbefore issuing another response. - Instructions, tools, schemas, prompts, transcripts, and output share the context window. Register only necessary tools and keep schemas compact.
- Resolve the locale with
supportsLocale(_:); do not raw-match language lists. - Keep untrusted user content in prompts, never instructions. System guardrails remain active, so handle refusal and other generation errors with fallback UI.
Load the Foundation Models reference when the
task needs @Generable, @Guide, streaming, tool definitions, transcripts,
generation options, custom adapters, prompt design, or detailed error handling.
Core ML Overview
Apple's framework for deploying trained models. Automatically dispatches to the optimal compute unit (CPU, GPU, or Neural Engine).
Model Formats
| Format | Extension | When to Use |
|---|---|---|
.mlpackage | Directory (mlprogram) | All new models (iOS 15+) |
.mlmodel | Single file (neuralnetwork) | Legacy only (iOS 11-14) |
.mlmodelc | Compiled | Pre-compiled for faster loading |
Always use mlprogram (.mlpackage) for new work.
Conversion Pipeline (coremltools)
import coremltools as ct
# PyTorch conversion (torch.jit.trace)
model.eval() # CRITICAL: always call eval() before tracing
traced = torch.jit.trace(model, example_input)
mlmodel = ct.convert(
traced,
inputs=[ct.TensorType(shape=(1, 3, 224, 224), name="image")],
minimum_deployment_target=ct.target.iOS18,
convert_to='mlprogram',
)
mlmodel.save("Model.mlpackage")
Validate, Fix, and Reconvert
- Freeze representative source-model fixtures and acceptable output/task tolerances before conversion.
- Convert, then run the same fixtures through the source and Core ML models.
- If output parity or task metrics miss tolerance, inspect shapes, operators, precision, and preprocessing; fix the conversion and rerun the fixtures.
- Compress only after the uncompressed model passes. Revalidate accuracy after each compression change and undo or tune changes that miss the threshold.
- Profile the passing model on physical target devices, then repeat until correctness, latency, memory, and package-size targets all pass.
Boundary with coreml
This skill owns Python-side conversion, compression, profiling, and framework
selection. Use the sibling coreml skill for Swift app integration, prediction
APIs, runtime configuration, Vision request wiring, and detailed model loading.
See references/coreml-conversion.md for the full conversion pipeline and references/coreml-optimization.md for optimization techniques.
MLX Swift Overview
Apple's ML framework for Swift. Highest sustained generation throughput on Apple Silicon via unified memory architecture.
Loading and Running LLMs
import MLX
import MLXLLM
import MLXLMCommon
import MLXLMHFAPI
let container = try await LLMModelFactory.shared.loadContainer(
from: HubClient.default,
using: TokenizersLoader(),
configuration: .init(id: "mlx-community/Qwen3-4B-4bit")
)
let session = ChatSession(container)
print(try await session.respond(to: "Hello"))
Model Selection by Device
| Device | RAM | Recommended Model | RAM Usage |
|---|---|---|---|
| iPhone 12-14 | 4-6 GB | SmolLM2-135M or Qwen 2.5 0.5B | ~0.3 GB |
| iPhone 15 Pro+ | 8 GB | Gemma 3n E4B 4-bit | ~3.5 GB |
| Mac 8 GB | 8 GB | Llama 3.2 3B 4-bit | ~3 GB |
| Mac 16 GB+ | 16 GB+ | Mistral 7B 4-bit | ~6 GB |
Memory Management
- Never exceed 60% of total RAM on iOS
- Set MLX cache limits:
Memory.cacheLimit = 512 * 1024 * 1024 - Unload MLX and llama.cpp models on backgrounding or memory pressure; for MLX,
also call
Memory.clearCache()after generation-heavy phases - Use "Increased Memory Limit" entitlement for larger models
- Validate MLX Swift and llama.cpp on physical Apple Silicon; Simulator cannot exercise Metal-dependent inference, memory, or performance
See references/mlx-swift.md for full MLX Swift patterns and llama.cpp integration.
Multi-Backend Architecture
When an app needs multiple AI backends (e.g., Foundation Models + MLX fallback):
func respond(to prompt: String) async throws -> String {
if SystemLanguageModel.default.isAvailable {
return try await foundationModelsRespond(prompt)
} else if canLoadMLXModel() {
return try await mlxRespond(prompt)
} else {
throw AIError.noBackendAvailable
}
}
Serialize all model access through a coordinator actor to prevent contention:
actor ModelCoordinator {
func withExclusiveAccess<T>(_ work: () async throws -> T) async rethrows -> T {
try await work()
}
}
For custom Core ML models, name only the conversion/optimization handoff here:
send Swift app integration, model loading, Vision wiring, and prediction
lifecycle to coreml. Keep private user content, such as journals, on device
unless product explicitly opts into a nonlocal fallback.
Performance Best Practices
- Run outside debugger for accurate benchmarks (Xcode: Cmd-Opt-R, uncheck "Debug Executable")
- Call
session.prewarm()for Foundation Models before user interaction - Pre-compile Core ML models to
.mlmodelcfor faster loading - Use EnumeratedShapes over RangeDim for Neural Engine optimization
- Use 4-bit palettization for best Neural Engine memory/latency gains
- Hand off detailed Vision, Natural Language, and Swift Core ML runtime integration to the sibling framework skills
Common Mistakes
- No availability check. Starting generation without checking
SystemLanguageModel.default.availabilityleaves unsupported devices with failures instead of fallback UI. - No fallback UI. Users on pre-iOS 26 or devices without Apple Intelligence see nothing. Always provide a graceful degradation path.
- Exceeding the context window. The token budget covers input + output.
Monitor usage via
tokenCount(for:)and summarize when needed. - Concurrent requests on one session.
LanguageModelSessionsupports one request at a time. Checksession.isRespondingor serialize access. - Untrusted content in instructions. User input placed in the instructions parameter bypasses guardrail boundaries. Keep user content in the prompt.
- Skipping conversion parity checks. Compare the source and Core ML model on fixed fixtures, then fix and reconvert before compressing or shipping.
- Forgetting
model.eval()before Core ML tracing. PyTorch models must be in eval mode beforetorch.jit.trace. Training-mode artifacts corrupt output. - Using neuralnetwork format. Always use
mlprogram(.mlpackage) for new Core ML models. The legacy neuralnetwork format is deprecated. - Exceeding 60% RAM on iOS (MLX Swift). Large models cause OOM kills.
- Trusting MLX simulator results. Validate Metal-dependent behavior on physical devices; Simulator is only a UI/control-flow smoke test.
- Not clearing MLX caches. Pair model unload with
Memory.clearCache().
Review Checklist
- Framework selection matches use case and target OS version
- Foundation Models: availability checked before every API call
- Foundation Models: graceful fallback when model unavailable
- Foundation Models: session prewarm called before user interaction
- Foundation Models:
@Generableproperties in logical generation order - Foundation Models: token budget accounted for (check
contextSize) - Core ML: model format is mlprogram (.mlpackage) for iOS 15+
- Core ML: source/Core ML parity passes fixed fixtures and task tolerances
- Core ML: compressed model revalidated and profiled on physical targets
- MLX Swift: model size appropriate for target device RAM
- MLX Swift: cache limits set, caches cleared, models unloaded
- All model access serialized through coordinator actor
- Concurrency: model types and tool implementations are
Sendable-conformant or@MainActor-isolated - Physical device testing performed (not simulator)
References
- Foundation Models API -- LanguageModelSession,
@Generable, tool calling, prompt design - Core ML Conversion -- Model conversion from PyTorch, TensorFlow, other frameworks
- Core ML Optimization -- Quantization, palettization, pruning, performance tuning
- MLX Swift & llama.cpp -- MLX Swift patterns, llama.cpp integration, memory management
FAQ
What does the apple-on-device-ai skill do?
Build private, on-device AI features on iPhone, iPad, and Mac with Foundation Models, Core ML, MLX Swift, or llama.cpp. Use when choosing an Apple-local model runtime, building an Apple Intelligence chatbot or tool-calling feature, running an LLM on Apple Silicon, converting or compressing a Python model for Core ML, or comparing on-device inference backends. For Swift Core ML loading and prediction code, use the coreml skill.
How do I install apple-on-device-ai?
Run npx skills add https://github.com/dpearson2699/swift-ios-skills --skill apple-on-device-ai in your project. The skills CLI detects the agents you use and copies the skill into the right folder. For Claude Code you can also place the skill folder at ~/.claude/skills/apple-on-device-ai/ (personal) or .claude/skills/apple-on-device-ai/ (project).
Which AI agents can use apple-on-device-ai?
Any agent that supports the open Agent Skills format (a folder with a SKILL.md file), including Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, Windsurf, Cline, OpenCode and more.
Is apple-on-device-ai safe to install?
Security partners have reviewed it: Gen Agent Trust Hub passed; Socket passed; Snyk passed; ZeroLeaks passed. As with any skill, read the SKILL.md and bundled scripts before installing.
Who maintains apple-on-device-ai?
It is published by dpearson2699 in dpearson2699/swift-ios-skills. It has 4,038 recorded installs.
More from dpearson2699/swift-ios-skills
swiftui-animation
dpearson2699/swift-ios-skills
Implement, diagnose, or review SwiftUI motion using explicit and scoped implicit animations, springs, transitions, PhaseAnimator, KeyframeAnimator, matched geometry or navigation zoom, SF Symbol effects, and custom Animation types. Use when views should animate on state changes, insertion, removal, navigation, or multi-step choreography, or when motion must respect Reduce Motion and Swift concurrency.
ios-accessibility
dpearson2699/swift-ios-skills
Build and audit SwiftUI, UIKit, and AppKit accessibility for VoiceOver, Voice Control, Switch Control, Full Keyboard Access, Dynamic Type, focus restoration, labels/traits/actions, traversal, custom rotors, NSAccessibility, XCTest checks, adaptive system preferences, and App Store accessibility declarations. Use when implementing accessible UI, fixing an accessibility audit, testing assistive-technology behavior, or substantiating App Store Accessibility Nutrition Labels.
swiftui-performance
dpearson2699/swift-ios-skills
Profile, diagnose, and remediate SwiftUI runtime performance using code review, Instruments, and repeatable measurements. Use when a SwiftUI screen renders slowly, scrolling or animations hitch, view bodies update excessively, list identity churns, layout work spikes, or broad Observation dependencies raise CPU cost. Covers evidence-based triage, SwiftUI Instruments lanes, lazy-container guardrails, state lifetime, and before/after verification.
swiftui-patterns
dpearson2699/swift-ios-skills
Builds and reviews SwiftUI views with modern MV architecture, state, composition, isolated previews, and migration guidance. Covers @Observable ownership, @State/@Bindable/@Environment wiring, view decomposition, ViewModifiers, environment values, .task loading, iOS 26+ handoffs, Writing Tools, clipboard availability, and performance. Use when structuring SwiftUI state, managing @Observable, composing views, previewing meaningful UI states, or correcting SwiftUI patterns.
app-store-review
dpearson2699/swift-ios-skills
Audits App Store submission readiness and rejection risk across current review guidelines, PrivacyInfo.xcprivacy and required-reason APIs, privacy labels, ATT, StoreKit payments, metadata, entitlements, widgets, and Live Activities. Use when preparing a submission, responding to rejection, reconciling privacy evidence, or separating upload blockers from cleanup.
ios-networking
dpearson2699/swift-ios-skills
Build, review, or improve networking code in iOS/macOS apps using URLSession with async/await, structured concurrency, and modern Swift patterns. Use when working with REST APIs, downloading files, uploading data, WebSocket connections, pagination, retry logic, request middleware, caching, background transfers, or network reachability monitoring. Also use when handling HTTP requests, API clients, network error handling, or data fetching in Swift apps.
Popular mobile skills
design-mobile-apps
designed-by-ai/skills
Design mobile app screens with Sleek, edit Sleek projects, and implement their designs in React Native or HTML.
ios-design
uizze.sh
Design and review iOS UI/UX for iPhone apps in SwiftUI, UIKit, or React Native. Use for mobile app design, screen redesigns, navigation flows, typography, forms, keyboard and safe-area layout, and accessibility checks. Apply Apple Human Interface Guidelines and consult real product references through the optional Uizze connection.
vercel-react-native-skills
vercel-labs/agent-skills
React Native and Expo best practices for building performant mobile apps. Use when building React Native components, optimizing list performance, implementing animations, or working with native modules. Triggers on tasks involving React Native, Expo, mobile performance, or native platform APIs.
animate-expo
emilkowalski/skills
Build animations in React Native and Expo, making the decisions in the order that determines whether they feel right — should it animate, which thread it runs on, which properties, spring or timing, how the gesture hands off, how it degrades. Writes the implementation with Reanimated, Gesture Handler, Expo Router and expo-haptics. Use when animating anything in an Expo app, adding gestures, sheets, screen transitions, press feedback or haptics, or fixing motion that stutters on device. For web animation use animate.
xcode-project-setup
firebase/agent-skills
Safely modifies Xcode projects (.pbxproj) to add Swift Packages and link files. Use this skill whenever an iOS project needs dependencies installed (e.g. Firebase, Alamofire).
firebase-remote-config-basics
firebase/agent-skills
Manages Firebase Remote Config templates, feature flags, loading strategies, and SDKs (Android, iOS). Use when downloading/deploying remoteconfig JSON templates, managing version history/feature flags, setting in-app defaults, fetchAndActivate(), real-time listeners, or SDK setup. Don't use for Firebase Hosting, Auth, Firestore, Data Connect, Crashlytics, or App Hosting.