foundation-models
On-device LLM integration using Apple's Foundation Models framework. Use when implementing AI text generation, structured output, or tool calling.
What this skill does
# Foundation Models
Integrate Apple's on-device LLM into your apps for privacy-preserving AI features.
## When This Skill Activates
- User wants AI text generation features
- User needs structured data from natural language
- User asks about prompting or LLM integration
- User wants to implement AI assistants
- User needs content summarization or extraction
## Quick Start
### 1. Check Availability
```swift
import FoundationModels
struct IntelligentView: View {
private var model = SystemLanguageModel.default
var body: some View {
switch model.availability {
case .available:
ContentView()
case .unavailable(.deviceNotEligible):
UnsupportedDeviceView()
case .unavailable(.appleIntelligenceNotEnabled):
EnableIntelligenceView()
case .unavailable(.modelNotReady):
ModelDownloadingView()
case .unavailable(let reason):
ErrorView(reason: reason)
}
}
}
```
### 2. Create a Session
```swift
// Simple session
let session = LanguageModelSession()
// Session with instructions
let session = LanguageModelSession(instructions: """
You are a helpful cooking assistant.
Provide concise, practical advice for home cooks.
""")
```
### 3. Generate Response
```swift
let response = try await session.respond(to: "What's a quick dinner idea?")
print(response.content)
```
## Prompt Engineering Best Practices
### The Instruction Formula
Instructions set the model's persona and constraints. They're prioritized over prompts.
```
[Role] + [Task] + [Style] + [Safety]
```
**Example:**
```swift
let instructions = """
You are a fitness coach specializing in home workouts.
Help users create exercise routines based on their equipment and goals.
Keep responses under 100 words and use bullet points for exercises.
Decline requests for medical advice and suggest consulting a doctor.
"""
```
### Instruction Components
| Component | Purpose | Example |
|-----------|---------|---------|
| **Role** | Define persona | "You are a travel expert" |
| **Task** | What to do | "Help plan itineraries" |
| **Style** | Output format | "Use bullet points, be concise" |
| **Safety** | Boundaries | "Don't provide medical advice" |
### Effective Prompts
Prompts are user inputs. Make them:
| Principle | Bad | Good |
|-----------|-----|------|
| **Specific** | "Help with cooking" | "Suggest a 30-minute vegetarian dinner" |
| **Constrained** | "Tell me about dogs" | "Describe Golden Retrievers in 3 sentences" |
| **Focused** | "I need help with many things" | "What ingredients substitute for eggs in baking?" |
### Prompt Patterns
**Question Pattern:**
```swift
let prompt = "What are three ways to reduce food waste at home?"
```
**Command Pattern:**
```swift
let prompt = "Create a weekly meal plan for a family of four, budget-friendly."
```
**Extraction Pattern:**
```swift
let prompt = """
Extract the following from this email:
- Sender name
- Meeting date
- Action items
Email: \(emailContent)
"""
```
**Transformation Pattern:**
```swift
let prompt = "Rewrite this text to be more formal: \(casualText)"
```
## Structured Output with @Generable
Get typed Swift data instead of raw strings.
### Define Generable Types
```swift
@Generable(description: "A recipe suggestion")
struct Recipe {
var name: String
@Guide(description: "Cooking time in minutes", .range(5...180))
var cookingTime: Int
@Guide(description: "Difficulty level", .options(["Easy", "Medium", "Hard"]))
var difficulty: String
@Guide(description: "List of ingredients", .count(3...15))
var ingredients: [String]
@Guide(description: "Step-by-step instructions")
var instructions: [String]
}
```
### @Guide Constraints
| Constraint | Use Case | Example |
|------------|----------|---------|
| `.range(min...max)` | Numeric bounds | `.range(1...100)` |
| `.options([...])` | Enum-like choices | `.options(["Low", "Medium", "High"])` |
| `.count(n)` | Exact array length | `.count(5)` |
| `.count(min...max)` | Array length range | `.count(3...10)` |
### Generate Structured Data
```swift
let session = LanguageModelSession(instructions: """
You are a recipe assistant. Generate practical, home-cook friendly recipes.
""")
let recipe = try await session.respond(
to: "Suggest a quick pasta dish",
generating: Recipe.self
)
print("Recipe: \(recipe.content.name)")
print("Time: \(recipe.content.cookingTime) minutes")
print("Ingredients: \(recipe.content.ingredients.joined(separator: ", "))")
```
### Complex Nested Structures
```swift
@Generable(description: "A travel itinerary")
struct Itinerary {
var destination: String
@Guide(description: "Daily activities for the trip")
var days: [DayPlan]
}
@Generable(description: "Activities for one day")
struct DayPlan {
var dayNumber: Int
@Guide(description: "Morning activity")
var morning: String
@Guide(description: "Afternoon activity")
var afternoon: String
@Guide(description: "Evening activity")
var evening: String
}
```
## Tool Calling
Let the model call your code to access data or perform actions.
### Define a Tool
```swift
struct WeatherTool: Tool {
let name = "getWeather"
let description = "Get current weather for a location"
struct Arguments: Codable {
var location: String
}
func call(arguments: Arguments) async throws -> ToolOutput {
let weather = await WeatherService.shared.fetch(for: arguments.location)
return .string("Temperature: \(weather.temp)°F, Conditions: \(weather.conditions)")
}
}
```
### Use Tools in Session
```swift
let weatherTool = WeatherTool()
let session = LanguageModelSession(
instructions: "You help users plan outdoor activities based on weather.",
tools: [weatherTool]
)
// Model automatically calls tool when needed
let response = try await session.respond(
to: "Should I go hiking in San Francisco today?"
)
```
### Tool Error Handling
```swift
do {
let response = try await session.respond(to: prompt)
} catch let error as LanguageModelSession.ToolCallError {
print("Tool '\(error.tool.name)' failed: \(error.underlyingError)")
} catch {
print("Generation error: \(error)")
}
```
## Snapshot Streaming
Show responses as they generate for better UX.
### Stream to SwiftUI
```swift
@Generable
struct StoryIdea {
var title: String
@Guide(description: "A brief plot summary")
var plot: String
@Guide(description: "Main characters", .count(2...4))
var characters: [String]
}
struct StreamingView: View {
@State private var partial: StoryIdea.PartiallyGenerated?
@State private var isGenerating = false
var body: some View {
VStack(alignment: .leading) {
if let partial {
if let title = partial.title {
Text(title).font(.headline)
}
if let plot = partial.plot {
Text(plot)
}
if let characters = partial.characters {
ForEach(characters, id: \.self) { char in
Text("• \(char)")
}
}
}
Button("Generate Story Idea") {
Task { await generateStory() }
}
.disabled(isGenerating)
}
}
func generateStory() async {
isGenerating = true
defer { isGenerating = false }
let session = LanguageModelSession()
let stream = session.streamResponse(
to: "Create a sci-fi story idea",
generating: StoryIdea.self
)
for try await snapshot in stream {
partial = snapshot
}
}
}
```
## Multi-Turn Conversations
Reuse sessions to maintain context.
```swift
@Observable
final class ChatViewModel {
private var session: LanguageModelSessRelated in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.