/xcode-skills:foundation-models-specialistfrom xcode-skills
Guides using Apple's FoundationModels framework in Swift: LanguageModelSession, prompting, @Generable structured output, tool calling, and on-device vs server model selection.
Ships in xcode-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
This is official Apple guidance written by the FoundationModels framework engineers, so consider this guide ground-truth and fully up-to-date. The guide's focus is idioms, patterns, performance, and strategy, so it augments the FoundationModels docs. Still refer to the docs for essentials and API reference.
Read Essential Tips first, then dig into topics as needed with References.
FoundationModels is the recommended way to run an LLM within a Swift app in the Apple ecosystem. Apple Intelligence refers to the user experience on Apple platforms, while Apple Foundation Models (AFM) refer to specific LLMs used by Apple Intelligence. FoundationModels is app developers' access to AFM models ("Apple models"), but the framework is compatible with non-Apple custom LLM integrations too in 27.0+ targets.
Always verify model availability before starting a session, otherwise the session will throw. See references/apple-models.md for SystemLanguageModel and PrivateCloudComputeLanguageModel availability checks.
LanguageModelSession: can handle 1 request at a time. You can create and run multiple in different Swift Tasks at a time, but for on-device models they'll end up running sequentially so it might not be a savings.
SystemLanguageModel no need to pass it for the model session argument; it's the default.respond or streamResponse call, cancel the Swift Task enclosing the try await part. If the user no longer needs the result, always cancel the Task right away to reduce memory load and latency so other sessions/prompts in your experience aren't queued waiting on the old session to finish.respond or streamResponse. Passing model: SystemLanguageModel.default together with a trailing instructions closure is ambiguous and won't compile, so omit model: for the system model:let session = LanguageModelSession(tools: [FindHotels()]) {
"You are a friendly travel assistant."
}
let response = try await session.respond(to: "Find a hotel in Kyoto for 2 nights", generating: HotelPick.self)
⚠️ Security: never put untrusted content in Instructions AFM models like SystemLanguageModel are trained to trust Instructions as higher authority than anything arriving in a Prompt.
Instructions: corresponds to the "system prompt" in other LLM frameworks: add Instructions to tell the model how it should handle an incoming prompt. SystemLanguageModel works best when you give the model a role with clearly stated abilities at the start of Instructions:
let instructions = Instructions {
"""
You are a friendly travel assistant with tools to access and manage a user's bookings.\
Help the user plan out their trip or answer questions about their bookings.
"""
}
Instructions are given to a LanguageModelSession it will fall back on a model's default instructions.Prompt: corresponds to the "user prompt" in other LLM frameworks, and is your primary input to the model.
Prefer streamResponse for content that should animate in for UI, and prefer respond for non-UI tasks and background Swift tasks. Content streams in as snapshots: partial text for String generation and optional properties for Generable generation:
let session = LanguageModelSession()
for try await update in session.streamResponse(
to: "Generate a list of suggested search terms for a trip planner.",
generating: SearchSuggestions.self
) {
withAnimation(.bouncy) {
// Overwrite the UI state to update with the latest snapshot
searchTerms = update.content.searchTerms ?? []
}
}
Each snapshot holds everything generated so far, so replace UI state on every snapshot instead of appending to it. Elements of a streamed array are PartiallyGenerated too, so their properties are optional:
for try await snapshot in session.streamResponse(to: notes, generating: CarePlan.self) {
// Replace, don't append: the snapshot already includes earlier elements
rows = (snapshot.content.actions ?? []).compactMap { action -> Row? in
guard let title = action.title else { return nil }
return Row(title: title, detail: action.detail ?? "")
}
}
⚠️ GenerationError is deprecated: Use LanguageModelError instead.
Why? GenerationError has been replaced with LanguageModelError as of 27.0+ targets. The error cases are very similar between LanguageModelSession.GenerationError and LanguageModelError but the base error type was changed in 27.0 to change error ownership from the session to the model. This was needed because 27.0 introduced the ability to run many LLMs (not just SystemLanguageModel), and different models have different error conditions.
The new error type LanguageModelError has a few base error cases and more model-specific error cases. Know which model(s) your session will use.
LanguageModelError is nonexhaustive, so a switch over it needs a default. Don't invent cases; these are the ones that exist:
do {
// ... call try await `respond` or `streamResponse`
} catch let error as LanguageModelError {
switch error {
case .contextSizeExceeded: message = "That's too long. Try a shorter request."
case .guardrailViolation, .refusal: message = "This request can't be handled."
case .rateLimited: message = "Too many requests. Try again shortly."
case .unsupportedLanguageOrLocale: message = "This language isn't supported."
case .timeout: message = "The request timed out."
default: message = error.localizedDescription
}
}
If the app target still includes 26.0+ targets, you'll need to handle both error types and ignore the deprecation warning to keep legacy support:
do {
// ... call try await `respond` or `streamResponse`
} catch {
if #available(iOS 27.0, macOS 27.0, visionOS 27.0, *) {
if let error = error as? LanguageModelError {
// New error handling logic
} else {
// Rethrow
}
} else {
// Legacy support: this will produce a deprecation warning
if let error = error as? LanguageModelSession.GenerationError {
// Pre-existing error handling logic
} else {
// Rethrow
}
}
}
Fundamental topics:
references/apple-models.md: Use when checking availability, choosing SystemLanguageModel vs PCC, entitlement/quotaUse, and performance best-practices.references/generable.md: Use for generating structured output. Covers performance, @Generable and @Guide best-practices, GeneratedContent, GenerationSchema, and DynamicGenerationSchemaOnly load a reference below when its topic is actually in play.
references/tool-calling.md: Use for defining a Tool for a session to call. Covers static tools vs. runtime-supplied dynamic tools.(GeneratedContent arguments plus a hand-built GenerationSchema), hiding a tool's schema from instructions with includesSchemaInInstructions, and how tool calls and outputs land in the transcript. Read references/generable.md first.references/agentic-experiences.md: (27.0 min target) Use when building a session whose instructions, tools, or model change with app state. Covers the DynamicProfile / DynamicInstructions / Profile layering, state and session lifecycle hooks, and when to reach for dynamic profiles versus fixed instructions. Read references/tool-calling.md first.references/custom-models.md: (27.0 min target) Use whenever the model isn't one of Apple's — a server-side or third-party LLM, a vendor's Swift SDK, or your own weights running on device — including when an app currently calls an LLM's REST API by hand with URLSession. Covers the out-of-the-box paths as well as fully custom paths.What SKILL.md sets; muted values are Claude Code's defaults
15 skills in this plugin
/adopt-c-bounds-safetyAdopt and debug the C -fbounds-safety extension: pointer annotations like __counted_by, build settings, bounds-violation traps. C code only./app-intents-specialistWrite or review App Intents code: intents and perform(), AppEntity and EntityQuery, AppEnum, parameters, Shortcuts and App Shortcuts. For iOS 26/27 additions use app-intents-whats-new-27./app-intents-whats-new-27New App Intents APIs and deprecations in iOS 26/27: supportedModes (replaces openAppWhenRun), interactive snippets, undo and cancel, Spotlight indexing, Visual Intelligence. Use for 'what's new' or migrating older intents code./app-resizabilityMake an app's layout adapt to resizable windows and multiple windows: replace mainScreen, interfaceOrientation and idiom checks; support split screen, Stage Manager, and iPhone Duo./audit-xcode-security-settingsAudit and enable security-related Xcode build settings: compiler warnings, static analyzer checkers, Enhanced Security hardening for C, C++, Objective-C and Swift./building-document-based-swiftui-applicationsBuild or migrate document-based SwiftUI apps: DocumentGroup, the Document protocol, open/save/autosave/export, FileWrapper, custom document types, replacing FileDocument.npx claudepluginhub mariusfanu/xcode-skills --plugin xcode-skillsOther plugins' skills for the same job