Introduction
AI features for iOS and macOS apps, in plain Swift.
swift-ai-sdk is a toolkit for building AI features into iOS and macOS apps: streaming chat, agents that call your code, structured output, live voice, and Apple's on-device models, all behind one small API.
The whole thing in four lines:
import AI
let result = try await generateText(
model: AnthropicModel("claude-sonnet-5"),
prompt: "Why is the sky blue?"
)
print(result.text)Swap AnthropicModel for OpenAIModel, GoogleModel, XaiModel, an
Ollama model on your Mac, or Apple's on-device model. Nothing else
changes.
If you've built with the AI SDK on the web, everything here will feel
familiar, down to the streaming protocol: an app built with swift-ai-sdk can
talk to the /api/chat route you already have.
Coming from the web?
| You know | Here it's |
|---|---|
generateText / streamText | The same calls, with async/await and for try await |
| zod schemas | A Schema DSL that validates output before decoding |
useChat | ChatSession, an @Observable object your SwiftUI views watch |
| Server stream helpers | UIMessageStream builders for Swift servers |
| Realtime | RealtimeSession for voice, same providers |
And one thing the web can't do: on-device models answer without a network, a key, or your data leaving the phone.
Start here
Getting started
Add the package and stream a first response in about five minutes.
Guides
Step-by-step builds: a chat screen, an agent, a voice assistant.
Generating text
The loop that powers everything: steps, tools, streaming.
Chat UI
ChatSession and friends for SwiftUI.
Realtime voice
Live speech over WebSockets.
On-device models
Apple Intelligence through the same API as the cloud.
Building with a coding agent? Install the
swift-ai-sdk skill so it writes correct code. Every docs
page is also raw markdown — append .mdx to any URL, or start from
/llms.txt.