joaquincampo/my-plugins/foundation-models-integration
Claude Code and agent skill by joaquincampo/my-plugins
- community
Checking that the SKILL.md file is still in the repository.
Add Apple's on-device Foundation Models (Apple Intelligence) to a SwiftUI app: streaming, `@Generable` structured output, `Tool` protocol, availability checks. iOS / iPadOS / macOS / visionOS 26+. Triggers on `SystemLanguageModel`, `LanguageModelSession`, `@Generable`.
Install
npx vskill@latest install joaquincampo/my-plugins/foundation-models-integrationSKILL.md
The same SKILL.md was published earlier as joaquincampo/my-plugins/foundation-models-integration.
Foundation Models Integration
When this applies
On-device LLM features in a SwiftUI app: streaming chat, structured output extraction from text, tool calling against system data (Contacts, Calendar, Location) or custom logic. Runs locally with no network, no API key, no per-request cost. Use this skill instead of a cloud LLM when privacy, offline operation, or predictable cost matter more than frontier reasoning quality.
Prerequisites
- Deployment target iOS 26, iPadOS 26, macOS 26, or visionOS 26.
- Device that supports Apple Intelligence and has it enabled in Settings.
import FoundationModelsat the top of any file that touches the framework.- Simulator runs the model but performance is misleading. Profile on device.
The 5-step playbook
- Read
SystemLanguageModel.default.availabilityand switch on it. Render a fallback view for each.unavailablereason (.deviceNotEligible,.appleIntelligenceNotEnabled,.modelNotReady). Re-read onscenePhasebecoming active because download or enable state can change while backgrounded. - Create one
LanguageModelSessionper long-lived conversation, owned by an@Observableservice (for exampleChatService). Configure tools and instructions in the initializer. Instructions are trusted, prompts are not, so never interpolate user input into the instructions string. - Pick the output mode. Free-form text streams via
session.streamResponse(to:)and yields a growingString. Structured output requires a@Generabletype (for exampleSummaryGenerable) and streams viasession.streamResponse(generating:), yieldingPartiallyGenerated<T>whose fields are optional and fill in over time. Each iteration replaces; do not append. - For tool calling, conform a struct to
Toolwithname,description, a nested@Generable struct Arguments, andfunc call(arguments:) async throws -> ToolOutput. Register on the session at init viaLanguageModelSession(tools: [WeatherTool()]). Mention each tool by name in the instructions so the model knows when to invoke it. - Catch the three named errors around every
respondandstreamResponsecall:LanguageModelSession.GenerationError.guardrailViolation,.exceededContextWindowSize,.unsupportedLanguageOrLocale. Map each to a user-visible message. On context overflow, rebuild the session with a condensed transcript or a rolling summary.
Deep dives in references/
references/session-streaming-generable.md: availability handling, session lifecycle, free-form streaming wired to SwiftUI state,@Generabletypes with@Guide, structured streaming withPartiallyGenerated<T>.references/tools-and-errors.md:Toolprotocol conformance, registering and triggering tools, the three error cases with try/catch examples and recommended UI copy, context-window recovery.
Gotchas
- Symptom: streaming UI flashes between full text and partial text. Cause: concatenating chunk values from
streamResponse(to:)instead of replacing. Each iteration yields the cumulative string. Fix: assign, do not append. - Symptom: model "forgets" earlier turns within a single conversation. Cause: a new
LanguageModelSessionwas created per turn. Fix: store the session on a long-lived@Observableservice for the conversation's lifetime. - Symptom: first response is multi-second slow, later responses are fast. Cause: no prewarm; weights load lazily on first request. Fix: call
session.prewarm()as soon as user intent is clear (field focused, chat opened). - Symptom:
exceededContextWindowSizethrown after a long session. Cause: full-transcript replay grew past the limit. Fix: rebuild the session with a condensed transcript or a rolling summary; seereferences/tools-and-errors.md. - Symptom:
PartiallyGenerated<T>fields arenileven after the stream ends. Cause: structured-output fields are optional by design and only fill as the model emits them; the final value can still be missing fields if generation was cut. Fix: render each field as optional in the view; do not gate on the whole struct.
Anti-patterns
- Putting user-controlled text in the
instructionsargument. That is the prompt-injection vector. - Force-unwrapping tool outputs (network bodies, parsed HTML, decoded JSON). Tools must fail soft and return a
ToolOutputstring explaining the failure. - Estimating tokens by word count divided by a constant. Use a proper tokenizer when you need a tight budget.
- Persisting an in-progress placeholder string like
"..."while streaming. Use an empty string plus astreaming: Boolflag, or keep the placeholder outside your durable store until the stream finishes. - Creating a new
LanguageModelSessionper turn. You lose conversation context, pay the prewarm cost again, and re-send the schema on every structured call.
Performance levers
- Call
session.prewarm()as soon as user intent is clear (input field focused, chat view appeared, conversation tapped). It loads weights and primes the KV cache so first-token latency drops. - On context overflow, prefer a rolling summary over full-transcript replay. Generate the summary with the same session after each turn, store it on your model, and prompt with summary plus last user message when token count crosses a safe threshold.
- Profile with the Foundation Models Instrument in Xcode. Track first-token latency, tokens per second, and thermal state on long sessions.
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingapple-dev/skills/foundation-models-integration/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/joaquincampo/my-plugins/foundation-models-integration)