SwiftyAISwiftyAI

Search documentation

Find a docs page by title or section

2

Providers

SwiftyAI ships ten providers. The SDK API stays the same across all of them — the provider decides how requests are encoded and which features are available.

Provider familyTypical use
OpenAICompatibleProviderOpenAI, Groq, OpenRouter, Mistral, Cohere, Cloudflare, Ollama, and private gateways
AnthropicProviderClaude text, streaming, and tool-loop workflows through AIToolCallingModel
GeminiProviderGemini text, streaming, tool-loop workflows through AIToolCallingModel, and Gemini media
AppleFoundationProviderOn-device Apple Intelligence when available
ProviderRegistryApp-local or test-local custom model strings

Built-In Prefixes

Register providers with AI.configure and reference them by string:

AI.configure { ai in
    ai.openAI(apiKey: openAIKey)
    ai.anthropic(apiKey: anthropicKey)
    ai.gemini(apiKey: geminiKey)
    ai.groq(apiKey: groqKey)
    ai.mistral(apiKey: mistralKey)
    ai.cohere(apiKey: cohereKey)
    ai.openRouter(apiKey: openRouterKey)
    ai.ollama(baseURL: "http://localhost:11434/v1")
}
 
let response = try await generateText(
    model: "groq/llama-3.3-70b-versatile",
    prompt: "Write a concise migration note."
)

Provider Quick Reference

OpenAI

// Via AI.configure
ai.openAI(apiKey: openAIKey)
// model string: "openai/gpt-4o"
 
// Direct
let provider = OpenAICompatibleProvider(
    baseURL: "https://api.openai.com/v1",
    apiKey: openAIKey,
    model: "gpt-4o"
)

Anthropic

// Via AI.configure
ai.anthropic(apiKey: anthropicKey)
// model string: "anthropic/claude-opus-4-5"
 
// Direct
let provider = AnthropicProvider(
    apiKey: anthropicKey,
    model: "claude-opus-4-5"
)

Gemini

// Via AI.configure
ai.gemini(apiKey: geminiKey)
// model string: "gemini/gemini-2.0-flash"
 
// Direct
let provider = GeminiProvider(
    apiKey: geminiKey,
    model: "gemini-2.0-flash"
)

Groq

// Via AI.configure
ai.groq(apiKey: groqKey)
// model string: "groq/llama-3.3-70b-versatile"
 
// Direct
let provider = OpenAICompatibleProvider(
    baseURL: "https://api.groq.com/openai/v1",
    apiKey: groqKey,
    model: "llama-3.3-70b-versatile"
)

Mistral

// Via AI.configure
ai.mistral(apiKey: mistralKey)
// model string: "mistral/mistral-large-latest"
 
// Direct
let provider = OpenAICompatibleProvider(
    baseURL: "https://api.mistral.ai/v1",
    apiKey: mistralKey,
    model: "mistral-large-latest"
)

Cohere

// Via AI.configure
ai.cohere(apiKey: cohereKey)
// model string: "cohere/command-r-plus"
 
// Chat / generation
let provider = OpenAICompatibleProvider(
    baseURL: "https://api.cohere.com/compatibility/v1",
    apiKey: cohereKey,
    model: "command-r-plus"
)

OpenRouter

// Via AI.configure
ai.openRouter(apiKey: openRouterKey)
// model string: "openrouter/meta-llama/llama-3.3-70b-instruct"
 
// Direct
let provider = OpenAICompatibleProvider(
    baseURL: "https://openrouter.ai/api/v1",
    apiKey: openRouterKey,
    model: "meta-llama/llama-3.3-70b-instruct"
)

Cloudflare Workers AI

// Via AI.configure
ai.cloudflare(accountID: accountID, apiKey: cfKey)
// model string: "cloudflare/@cf/meta/llama-3.3-70b-instruct-fp8-fast"
 
// Direct
let provider = OpenAICompatibleProvider(
    baseURL: "https://api.cloudflare.com/client/v4/accounts/\(accountID)/ai/v1",
    apiKey: cfKey,
    model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast"
)

Ollama (Local)

// Via AI.configure
ai.ollama(baseURL: "http://localhost:11434/v1")
// model string: "ollama/llama3.2"
 
// Direct
let provider = OpenAICompatibleProvider(
    baseURL: "http://localhost:11434/v1",
    apiKey: "ollama",
    model: "llama3.2"
)

Apple Foundation (On-Device)

// Direct only — no AI.configure prefix
// Requires iOS 26+ / macOS 26+, FoundationModels framework
if AppleFoundationProvider.isAvailable {
    let provider = AppleFoundationProvider()
    let response = try await provider.generate("Hello")
}

Capability Matrix

SwiftyAI has nine model-string provider prefixes plus the conditional AppleFoundationProvider. A check means the provider has public docs for the capability and SwiftyAI has a matching provider path. Model choice, account access, and regional limits can still reject a call.

ProviderTextStreamToolsMultimodal inputImage genTranscriptionSpeechVideo gen
OpenAI
Anthropic
Gemini
Groq
OpenRouter
Mistral
Cohere
Cloudflare
Ollama
Apple Foundation

Mistral image generation and Cohere transcription exist in provider-specific APIs, but SwiftyAI does not currently have dedicated provider adapters for those endpoint shapes. Cloudflare has media models in its catalog, but the OpenAI-compatible route used by SwiftyAI is documented for text generation.

Direct Provider Types

Use direct providers when you want explicit construction without the registry.

let openAI = OpenAICompatibleProvider(
    baseURL: "https://api.openai.com/v1",
    apiKey: openAIKey,
    model: "gpt-4o-mini"
)
 
let anthropic = AnthropicProvider(
    apiKey: anthropicKey,
    model: "claude-3-5-sonnet-latest"
)
 
let gemini = GeminiProvider(
    apiKey: geminiKey,
    model: "gemini-2.0-flash"
)

AppleFoundationProvider is available when the target can import FoundationModels.

Convenience typealiases exist for OpenAI-compatible endpoints:

TypealiasConcrete type
OpenAIProviderOpenAICompatibleProvider
GroqProviderOpenAICompatibleProvider
OpenRouterProviderOpenAICompatibleProvider
MistralProviderOpenAICompatibleProvider
CohereProviderOpenAICompatibleProvider
CloudflareProviderOpenAICompatibleProvider
OllamaProviderOpenAICompatibleProvider

They are naming conveniences only; endpoint behavior still comes from the configured base URL and model.

When you call media APIs (generateImage, transcribe, generateSpeech, generateVideo) using AI.configure model strings, only the openai/ and gemini/ prefixes route through the shared registry's media resolver. Other configured providers throw AIError.unsupportedFeature from the model-string overloads even if the underlying endpoint would work. For those providers, construct an OpenAICompatibleProvider directly and pass the instance into the function-form API:

let mistral = OpenAICompatibleProvider(
    baseURL: "https://api.mistral.ai/v1",
    apiKey: mistralKey,
    model: "pixtral-large-latest"
)
let image = try await generateImage(model: mistral, prompt: "A snowy mountain")

Or register the model via ProviderRegistry's imageModels / transcriptionModels / speechModels / videoModels maps.