Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Apple FoundationModels framework for on-device LLM — text generation, guided generation with @Generable, tool calling, and snapshot streaming in iOS 26+.
.claude/skills/loulanyue-foundation-models-on-device/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | -3% | 0% |
| case-02 | ✗→✓ | ▲ Improved | 3% | 0% |
| case-03 | ✗→✓ | ▲ Improved | 33% | 0% |
| case-06 | ✗→✓ | ▲ Improved | 42% | 0% |
| case-08 | ✗→✓ | ▲ Improved | 77% | 0% |
Patterns for integrating Apple's on-device language model into apps using the FoundationModels framework. Covers text generation, structured output with @Generable, custom tool calling, and snapshot streaming — all running on-device for privacy and offline support.
Always check model availability before creating a session:
swiftstruct GenerativeView: View { private var model = SystemLanguageModel.default var body: some View { switch model.availability { case .available: ContentView() case .unavailable(.deviceNotEligible): Text("Device not eligible for Apple Intelligence") case .unavailable(.appleIntelligenceNotEnabled): Text("Please enable Apple Intelligence in Settings") case .unavailable(.modelNotReady): Text("Model is downloading or not ready") case .unavailable(let other): Text("Model unavailable: \(other)") } } }
swift// Single-turn: create a new session each time let session = LanguageModelSession() let response = try await session.respond(to: "What's a good month to visit Paris?") print(response.content) // Multi-turn: reuse session for conversation context let session = LanguageModelSession(instructions: """ You are a cooking assistant. Provide recipe suggestions based on ingredients. Keep suggestions brief and practical. """) let first = try await session.respond(to: "I have chicken and rice") let followUp = try await session.respond(to: "What about a vegetarian option?")
Key points for instructions:
Generate structured Swift types instead of raw strings:
swift@Generable(description: "Basic profile information about a cat") struct CatProfile { var name: String @Guide(description: "The age of the cat", .range(0...20)) var age: Int @Guide(description: "A one sentence profile about the cat's personality") var profile: String }
swiftlet response = try await session.respond( to: "Generate a cute rescue cat", generating: CatProfile.self ) // Access structured fields directly print("Name: \(response.content.name)") print("Age: \(response.content.age)") print("Profile: \(response.content.profile)")
.range(0...20) — numeric range.count(3) — array element countdescription: — semantic guidance for generationLet the model invoke custom code for domain-specific tasks:
swiftstruct RecipeSearchTool: Tool { let name = "recipe_search" let description = "Search for recipes matching a given term and return a list of results." @Generable struct Arguments { var searchTerm: String var numberOfResults: Int } func call(arguments: Arguments) async throws -> ToolOutput { let recipes = await searchRecipes( term: arguments.searchTerm, limit: arguments.numberOfResults ) return .string(recipes.map { "- \($0.name): \($0.description)" }.joined(separator: "\n")) } }
swiftlet session = LanguageModelSession(tools: [RecipeSearchTool()]) let response = try await session.respond(to: "Find me some pasta recipes")
swiftdo { let answer = try await session.respond(to: "Find a recipe for tomato soup.") } catch let error as LanguageModelSession.ToolCallError { print(error.tool.name) if case .databaseIsEmpty = error.underlyingError as? RecipeSearchToolError { // Handle specific tool error } }
Stream structured responses for real-time UI with PartiallyGenerated types:
swift@Generable struct TripIdeas { @Guide(description: "Ideas for upcoming trips") var ideas: [String] } let stream = session.streamResponse( to: "What are some exciting trip ideas?", generating: TripIdeas.self ) for try await partial in stream { // partial: TripIdeas.PartiallyGenerated (all properties Optional) print(partial) }
swift@State private var partialResult: TripIdeas.PartiallyGenerated? @State private var errorMessage: String? var body: some View { List { ForEach(partialResult?.ideas ?? [], id: \.self) { idea in Text(idea) } } .overlay { if let errorMessage { Text(errorMessage).foregroundStyle(.red) } } .task { do { let stream = session.streamResponse(to: prompt, generating: TripIdeas.self) for try await partial in stream { partialResult = partial } } catch { errorMessage = error.localizedDescription } } }
| Decision | Rationale | |----------|-----------| | On-device execution | Privacy — no data leaves the device; works offline | | 4,096 token limit | On-device model constraint; chunk large data across sessions | | Snapshot streaming (not deltas) | Structured output friendly; each snapshot is a complete partial state | | @Generable macro | Compile-time safety for structured generation; auto-generates PartiallyGenerated type | | Single request per session | isResponding prevents concurrent requests; create multiple sessions if needed | | response.content (not .output) | Correct API — always access results via .content property |
model.availability before creating a session — handle all unavailability casesinstructions to guide model behavior — they take priority over promptsisResponding before sending a new request — sessions handle one request at a timeresponse.content for results — not .output@Generable for structured output — stronger guarantees than parsing raw stringsGenerationOptions(temperature:) to tune creativity (higher = more creative)model.availability first.output instead of .content to access response data@Generable structured output would work| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 20,976 | 9,609 | -54% | 1 | 1 | 0% | 3,806 | 3,705 | -3% | 0 | 0 | — |
case-02 | fail→pass | 22,848 | 12,098 | -47% | 1 | 1 | 0% | 4,071 | 4,213 | +3% | 0 | 0 | — |
case-03 | fail→pass | 17,531 | 11,604 | -34% | 1 | 1 | 0% | 3,140 | 4,169 | +33% | 0 | 0 | — |
case-04 | pass→pass | 16,830 | 2,820 | -83% | 1 | 1 | 0% | 2,674 | 2,455 | -8% | 0 | 0 | — |
case-05 | pass→pass | 15,228 | 8,649 | -43% | 1 | 1 | 0% | 2,697 | 3,495 | +30% | 0 | 0 | — |
case-06 | fail→pass | 13,655 | 6,579 | -52% | 1 | 1 | 0% | 2,173 | 3,083 | +42% | 0 | 0 | — |
case-07 | pass→pass | 12,588 | 9,334 | -26% | 1 | 1 | 0% | 1,957 | 3,582 | +83% | 0 | 0 | — |
case-08 | fail→pass | 10,480 | 5,146 | -51% | 1 | 1 | 0% | 1,605 | 2,843 | +77% | 0 | 0 | — |
case-09 | fail→pass | 10,522 | 4,385 | -58% | 1 | 1 | 0% | 1,576 | 2,683 | +70% | 0 | 0 | — |
case-10 | fail→pass | 12,549 | 5,478 | -56% | 1 | 1 | 0% | 1,917 | 2,852 | +49% | 0 | 0 | — |
case-11 | fail→pass | 14,667 | 8,717 | -41% | 1 | 1 | 0% | 2,399 | 3,427 | +43% | 0 | 0 | — |
case-12 | fail→pass | 8,202 | 3,670 | -55% | 1 | 1 | 0% | 1,222 | 2,499 | +105% | 0 | 0 | — |
case-13 | fail→pass | 13,766 | 4,874 | -65% | 1 | 1 | 0% | 1,980 | 2,665 | +35% | 0 | 0 | — |
case-14 | fail→pass | 15,414 | 2,958 | -81% | 1 | 1 | 0% | 2,417 | 2,410 | -0% | 0 | 0 | — |
case-15 | pass→pass | 11,596 | 8,643 | -25% | 1 | 1 | 0% | 1,769 | 3,452 | +95% | 0 | 0 | — |
case-20 | pass→pass | 13,522 | 14,154 | +5% | 1 | 1 | 0% | 2,649 | 4,809 | +82% | 0 | 0 | — |
case-16 | fail→pass | 15,140 | 6,683 | -56% | 1 | 1 | 0% | 2,388 | 3,075 | +29% | 0 | 0 | — |
case-17 | pass→pass | 11,250 | 5,750 | -49% | 1 | 1 | 0% | 1,710 | 2,846 | +66% | 0 | 0 | — |
case-18 | pass→pass | 13,141 | 5,420 | -59% | 1 | 1 | 0% | 2,280 | 2,946 | +29% | 0 | 0 | — |
case-19 | fail→pass | 7,168 | 5,046 | -30% | 1 | 1 | 0% | 1,115 | 2,748 | +146% | 0 | 0 | — |
case-21 | pass→pass | 11,440 | 9,710 | -15% | 1 | 1 | 0% | 2,176 | 3,670 | +69% | 0 | 0 | — |
case-22 | pass→pass | 17,750 | 13,905 | -22% | 1 | 1 | 0% | 3,743 | 4,774 | +28% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +59 percentage points is the difference between those two pass rates over the 22 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.