---
title: "Swift SDK"
description: "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."
sidebar:
  label: "Swift"
seo:
  title: "Swift SDK | Autohand Code Agent SDK"
  description: "Swift guide for the Autohand Code Agent SDK with native providers, async streaming, permission managers, and loop strategies."
---

## Choose a runtime

Use `AutohandSDK(configuration:)` on macOS when your host needs saved CLI sessions, CLI tool permissions, run budgets, or JSON-RPC notifications. Use the native provider and agent loop on macOS or iOS for in-process execution. See the [Swift API reference](/agent-sdk/swift-api).

## Add the package [#install]

```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 [#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 [#streaming]

`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 [#permissions]

```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

- With the native runner, pass provider configuration directly in Swift. The macOS CLI client can inherit the provider from the CLI configuration.
- 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](/agent-sdk/swift-api) for the reference surface.
- Open [Handle approvals and user input](/agent-sdk/io/approvals) for the review loop.
