$npx -y skills add rshankras/claude-code-apple-skills --skill foundation-modelsOn-device LLM integration using Apple's Foundation Models framework. Use when implementing AI text generation, structured output, or tool calling.
| 1 | # Foundation Models |
| 2 | |
| 3 | Integrate Apple's on-device LLM into your apps for privacy-preserving AI features. Companion references: **safety-and-guardrails.md** (model limits, prompt design, the four-layer safety stack), **models-and-agents.md** (Private Cloud Compute, `LanguageModel` protocol, vision input, `DynamicProfile` agentic sessions — the iOS 27 wave), and **utilities-package.md** (Apple's open-source utilities package: OpenAI-compatible endpoints, just-in-time Skills, history compression). |
| 4 | |
| 5 | ## When This Skill Activates |
| 6 | |
| 7 | - User wants AI text generation features |
| 8 | - User needs structured data from natural language |
| 9 | - User asks about prompting or LLM integration |
| 10 | - User wants to implement AI assistants or agentic features (tool loops, multi-profile sessions) |
| 11 | - User needs content summarization or extraction |
| 12 | - User asks about Private Cloud Compute, guardrails, or model safety |
| 13 | |
| 14 | ## Model Fit — Check Before Building |
| 15 | |
| 16 | The on-device model is ~3B parameters (2-bit quantized): built for **summarization, extraction, classification, tagging, revision, short chat** — not math, code generation, facts, or world knowledge (WWDC25 248). For capability boundaries, prompt-design rules, and the safety stack, read `safety-and-guardrails.md` first. For anything bigger, `PrivateCloudComputeLanguageModel` (32k context, reasoning) and third-party backends are in `models-and-agents.md`. |
| 17 | |
| 18 | ## Quick Start |
| 19 | |
| 20 | ### 1. Check Availability |
| 21 | |
| 22 | ```swift |
| 23 | import FoundationModels |
| 24 | |
| 25 | struct IntelligentView: View { |
| 26 | private var model = SystemLanguageModel.default |
| 27 | |
| 28 | var body: some View { |
| 29 | switch model.availability { |
| 30 | case .available: |
| 31 | ContentView() |
| 32 | case .unavailable(.deviceNotEligible): |
| 33 | UnsupportedDeviceView() |
| 34 | case .unavailable(.appleIntelligenceNotEnabled): |
| 35 | EnableIntelligenceView() |
| 36 | case .unavailable(.modelNotReady): |
| 37 | ModelDownloadingView() |
| 38 | case .unavailable(let reason): |
| 39 | ErrorView(reason: reason) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | ``` |
| 44 | |
| 45 | ### 2. Create a Session |
| 46 | |
| 47 | ```swift |
| 48 | // Simple session |
| 49 | let session = LanguageModelSession() |
| 50 | |
| 51 | // Session with instructions |
| 52 | let session = LanguageModelSession(instructions: """ |
| 53 | You are a helpful cooking assistant. |
| 54 | Provide concise, practical advice for home cooks. |
| 55 | """) |
| 56 | ``` |
| 57 | |
| 58 | ### 3. Generate Response |
| 59 | |
| 60 | ```swift |
| 61 | let response = try await session.respond(to: "What's a quick dinner idea?") |
| 62 | print(response.content) |
| 63 | ``` |
| 64 | |
| 65 | ## Prompt Engineering Best Practices |
| 66 | |
| 67 | ### The Instruction Formula |
| 68 | |
| 69 | Instructions set the model's persona and constraints. They're prioritized over prompts. |
| 70 | |
| 71 | ``` |
| 72 | [Role] + [Task] + [Style] + [Safety] |
| 73 | ``` |
| 74 | |
| 75 | **Example:** |
| 76 | ```swift |
| 77 | let instructions = """ |
| 78 | You are a fitness coach specializing in home workouts. |
| 79 | Help users create exercise routines based on their equipment and goals. |
| 80 | Keep responses under 100 words and use bullet points for exercises. |
| 81 | Decline requests for medical advice and suggest consulting a doctor. |
| 82 | """ |
| 83 | ``` |
| 84 | |
| 85 | ### Instruction Components |
| 86 | |
| 87 | | Component | Purpose | Example | |
| 88 | |-----------|---------|---------| |
| 89 | | **Role** | Define persona | "You are a travel expert" | |
| 90 | | **Task** | What to do | "Help plan itineraries" | |
| 91 | | **Style** | Output format | "Use bullet points, be concise" | |
| 92 | | **Safety** | Boundaries | "Don't provide medical advice" | |
| 93 | |
| 94 | ### Effective Prompts |
| 95 | |
| 96 | Prompts are user inputs. Make them: |
| 97 | |
| 98 | | Principle | Bad | Good | |
| 99 | |-----------|-----|------| |
| 100 | | **Specific** | "Help with cooking" | "Suggest a 30-minute vegetarian dinner" | |
| 101 | | **Constrained** | "Tell me about dogs" | "Describe Golden Retrievers in 3 sentences" | |
| 102 | | **Focused** | "I need help with many things" | "What ingredients substitute for eggs in baking?" | |
| 103 | |
| 104 | ### Prompt Patterns |
| 105 | |
| 106 | **Question Pattern:** |
| 107 | ```swift |
| 108 | let prompt = "What are three ways to reduce food waste at home?" |
| 109 | ``` |
| 110 | |
| 111 | **Command Pattern:** |
| 112 | ```swift |
| 113 | let prompt = "Create a weekly meal plan for a family of four, budget-friendly." |
| 114 | ``` |
| 115 | |
| 116 | **Extraction Pattern:** |
| 117 | ```swift |
| 118 | let prompt = """ |
| 119 | Extract the following from this email: |
| 120 | - Sender name |
| 121 | - Meeting date |
| 122 | - Action items |
| 123 | |
| 124 | Email: \(emailContent) |
| 125 | """ |
| 126 | ``` |
| 127 | |
| 128 | **Transformation Pattern:** |
| 129 | ```swift |
| 130 | let prompt = "Rewrite this text to be more formal: \(casualText)" |
| 131 | ``` |
| 132 | |
| 133 | ## Structured Output with @Generable |
| 134 | |
| 135 | Get typed Swift data instead of raw strings. |
| 136 | |
| 137 | ### Define Generable Types |
| 138 | |
| 139 | ```swift |
| 140 | @Generable(description: "A recipe suggestion") |
| 141 | struct Recipe { |
| 142 | var name: String |
| 143 | |
| 144 | @Guide(description: "Cooking time in minutes", .range(5...180)) |
| 145 | var cookingTime: Int |
| 146 | |
| 147 | @Guide(description: "Difficulty level", .options(["Easy", "Medium", "Hard"])) |
| 148 | var difficulty: String |
| 149 | |
| 150 | @Guide(description: |