Swift SDK
The Swift SDK is the native option in this set: provider-first, async by default, and built for Apple platform apps that want direct control over tools and loop behavior.
Add the package
Package.swift
// swift-tools-version: 6.0
import PackageDescription
let package = Package(
name: "MyAgentApp",
platforms: [.macOS(.v14)],
dependencies: [
.package(path: "../AgentSDK")
],
targets: [
.executableTarget(
name: "MyAgentApp",
dependencies: ["AgentSDK"]
)
]
)Create a first agent
Swift
import AgentSDK
import Foundation
let provider = OpenAIProvider(apiKey: "sk-...")
let agent = Agent(
name: "Reviewer",
instructions: "Be concise and specific.",
tools: [.readFile, .bash],
maxTurns: 10,
model: ModelID("gpt-4o"),
provider: provider
)
let output = try await Runner.runSync(
agent: agent,
prompt: "Review Sources/App.swift for obvious risks."
)
print(output)Stream events
`Runner.runStream()` yields structured events as an async stream.
Swift
let stream = Runner.runStream(
agent: agent,
prompt: "What files are in the current directory?"
)
for try await event in stream {
switch event.type {
case .content:
if let data = event.data {
print(data, terminator: "")
}
case .toolCall:
print("\nTool: \(event.tool?.rawValue ?? "unknown")")
case .done:
print("\nDone")
default:
break
}
}Use a permission manager
Swift
let hookManager = HookManager()
let permissionManager = PermissionManager(
hookManager: hookManager,
mode: .ask
)
Runner.setPermissionManager(permissionManager)
let stream = Runner.runStream(
agent: agent,
prompt: "Create a new Swift file and run tests."
)
for try await event in stream {
if event.type == .content, let data = event.data {
print(data, terminator: "")
}
}Loop strategies
- `.react` for the default tool-using loop.
- `.planAndExecute` for multi-step task planning.
- `.parallel` for independent tasks that can fan out.
Notes
- The Swift SDK is not a CLI wrapper in the same way as TypeScript, Python, and Go. Provider configuration is passed directly in code.
- Built-in tools, hook managers, and permission managers are native Swift types, which makes the SDK a good fit for desktop app integration.
Next steps
- Open [Swift API](/docs/agent-sdk/swift-api.html) for the reference surface.
- Open [Handle approvals and user input](/docs/agent-sdk/io/approvals.html) for the review loop.