Chat

Build conversations that stay on device.

Create an on-device AI chat, or with your data that your app stores.

A Primordial chat keeps completed turns in memory. Model-only chat uses them for follow-ups. Source-backed chat uses recent turns to understand follow-up questions about content.

You can create a model-only chat with the configured AI generation model, or a source-backed chat with imported files or your collections. It searches for questions about source content and cites the passages it uses.

Create a Primordial Client

Import Primordial and create one client for the account, workspace, or data boundary your chat belongs to. The client owns the configured AI models and coordinates their use across chat, search, and other AI features.

import Primordial

let primordial = PrimordialClient(
    identifier: "account-42" // Optional
)

Use a stable identifier when your app separates data by account or workspace. If it does not, PrimordialClient() uses the stable default identifier.
You can also use Apple Foundation Model or different AI model . See AI Model Setup for details.

Choose the Chat Capability

Check the capability for the kind of conversation you want to offer. This tells your app whether the required AI resources are installed and whether the current device can use them.

Chat mode Capability What it needs
Model-only chat .generation The configured generation model.
Chat with your data .answering The configured generation and embedding models.

let requirement = await primordial.ai.status(
    for: .generation
)

print(requirement.isInstalled)
print(requirement.isAvailable)
print(requirement.downloadBytes)
print(requirement.requiredAvailableBytes)

isInstalled reports whether downloadable resources are already on the device. isAvailable checks whether the current device can use it. Before starting a download, use downloadBytes and requiredAvailableBytes to explain what the app needs and ask the user for permission.

For grounded chat, make the same check with .answering.

Activate Primordial

Activate the SDK before installing models or starting a conversation.

try await primordial.activate(
    .evaluationKey("pk_eval_your_key_here")
)

You can check primordial.isActivated when your UI needs to reflect activation state. Keep the evaluation key out of logs and user-visible error messages.

Prepare Chat on the Device

After the user agrees to any required download, call makeAvailable with the capability you checked. Primordial checks storage, downloads missing resources, verifies them, and reports progress.

for try await progress in primordial.ai.makeAvailable(
    .generation
) {
    print(progress.fractionCompleted)
    print(progress.downloadedBytes)
    print(progress.totalBytes)
    print(progress.phase)
}

Use .answering instead when the chat will use collections. Calling makeAvailable again reuses valid downloads rather than downloading an installed model again.

Do not start a model download automatically when a chat button is tapped. Check status first, show the size and storage requirement in your UI, and let the user decide when to continue.

Start a Model-Only Chat

Create a chat once, then keep that chat instance for the conversation. Calling send adds the user message, generates the next assistant message, and commits both messages to the history when the response succeeds.

let chat = try primordial.chat()

let reply = try await chat.send(
    "Why are Swift actors useful?"
)

print(reply.text)

reply.metrics reports primary-response duration and, when available, exact generated-token count and token rate. See Generation metrics for platform availability.

send returns the completed assistant message. The chat automatically includes earlier completed turns when the next message is sent, so a follow-up can refer to the conversation naturally.

Local thinking is off for this default chat.

Enable Thinking in Chat

Local thinking is off by default. Enable it for every turn when creating the chat:

let chat = try primordial.chat(
    generationOptions: .init(thinking: .enabled)
)

let reply = try await chat.send("Compare the two designs.")

Or enable it for only one turn while leaving the chat default unchanged:

let chat = try primordial.chat()

let consideredReply = try await chat.send(
    "Explore the tradeoffs carefully.",
    generationOptions: .init(thinking: .enabled)
)

.automatic inherits the current local default of off. Use .disabled to state that policy explicitly. By default, Primordial removes raw thinking from the assistant message and stream fragments. For a compatible MLX model, set reasoning: .verbatim with thinking: .enabled to include generated thinking in both. You can bound thinking with maximumThinkingTokens and validate answer room with minimumAnswerTokens; see Thinking Mode, Budgets, and Rationale.

reasoning: .summarized is separate, it requests a brief rationale in reply.reasoningSummary but does not turn thinking on.


Apple Foundation Models does not expose thinking control. Keep thinking: .automatic for an Apple-backed chat.

Understand Chat Messages

Every item in the conversation is a PrimordialChatMessage.

Value Meaning
role .user or .assistant.
text The message text committed to the conversation.
citations Sources used for answers about file or collection content. Model-only, conversational, and clarification replies have an empty array.

print(reply.role)
print(reply.text)
print(reply.citations)

Chat with Files

Open a named file workspace, attach local .txt or .md files, then pass it directly to chat. Primordial handles reading, indexing, and retrieval.

let files = try await primordial.openFiles(named: "project-notes")
try await files.attach(Set(fileURLs), onProgress: { progress in
    print(progress.fileName, progress.fractionCompleted)
})
let chat = try primordial.chat(using: files)
let reply = try await chat.send("Summarize these documents.")
print(reply.text)

onProgress: reports each file's import progress. See Named File Workspaces for persistence and file management. Chat history stays in memory, so create a new chat after relaunch.

To chat over files and app collections together, combine them as sources:

let notes = try primordial.collection("notes")
let sources = try primordial.sources(
    files: files,
    collections: [notes]
)
let mixedChat = try primordial.chat(using: sources)

Chat searches the imported files and any collections included in sources. It can answer greetings or questions about file names and counts without searching. Ambiguous questions may prompt a request for clarification.

Show Chat Activity

Pass onStage to send to update your UI as a turn progresses. The callback runs on the main actor. Keep it quick and clear the displayed stage when the call returns or throws.

@MainActor
func ask(_ question: String, in chat: PrimordialChat) async throws -> PrimordialChatMessage {
    defer { showActivity(nil) }
    return try await chat.send(question) { stage in showActivity(stage) }
}

Here, showActivity(_:) accepts an optional PrimordialChatStage and updates your status label; nil hides it. Display .analyzingMessage as “Analyzing your message…”, .searchingSources as “Searching files…”, and .answering as “Answering…”. These are activity hints, so a turn may skip stages. Model-only chat can report just .answering; a source-backed reply may finish without generation. There is no .finished stage; clear the indicator when the turn ends.

Chat with Your App’s Data

For questions about stored content, chat searches one or more Primordial collections and uses the retrieved passages to answer.

Following examples demonstrate grounded chat using a research notes collection. You can learn more about collections in Store and Search.

let notes = try primordial.collection("research notes")

Once the collection is ready, create the chat with that collection and send a question.

let chat = try primordial.chat(
    using: [notes]
)

let reply = try await chat.send(
    "Why Primordial makes life so easy?"
)

print(reply.text)
print(reply.citations)

For follow-up questions about your data’s content, chat uses recent messages to understand what you mean and searches the same collections. To use different collections or settings, create a new chat.

Control What Grounds the Conversation

Use PrimordialAnswerOptions to choose which text fields are searched, influence ranking, filter eligible records, and bound how much retrieved text is supplied to the generation model. Learn more about it in Answers and Citations.

If a question about source content finds no matches, chat returns emptySourcesResponse without generation and saves the turn normally. This also applies to files, mixed sources, and streaming. Conversational and clarification replies do not use this option. See Empty sources to customize the response.

let options = PrimordialAnswerOptions(
    fields: ["title", "body"],
    weights: ["title": 1.5, "body": 1.0],
    filter: .field(
        "kind",
        .equals(.keyword("guide"))
    ),
    resultLimit: 6,
    contextCharacterLimit: 6_000,
    searchStrategy: .hybrid
)

let chat = try primordial.chat(
    using: [notes],
    options: options
)

let reply = try await chat.send(
"Why Primordial makes life so easy?"
)

Show Grounded Sources

A completed grounded assistant message contains the citations that supported its generated context. Connect them to your source UI so people can inspect the underlying data.

for citation in reply.citations {
    print(citation.collection)
    print(citation.recordID)
    print(citation.field)
    print(citation.excerpt)
    print(citation.fields)
    print(citation.sourceRange)
}

A citation identifies a source supplied to the generation model. It does not prove that every sentence in the reply is correct. Keep important decisions under normal application review.

Stream a Chat Turn

Use stream when the UI should begin showing text before the complete assistant message is ready. Append each fragment as it arrives, then finalize the UI with the completed message.

@MainActor
func streamTurn(in chat: PrimordialChat) async throws {
    defer { showActivity(nil) }
    var visibleText = ""

    for try await event in chat.stream("How does that isolation work?") {
        switch event {
        case .stage(let stage):
            showActivity(stage)

        case .fragment(let text):
            visibleText += text
            print(visibleText)

        case .reasoningSummary(let summary):
            showRationale(summary)

        case .completed(let message):
            visibleText = message.text
            print(message.citations)

        @unknown default:
            break
        }
    }
}

Use showActivity(_:) to update your status label with the stage, and pass nil to hide it when streaming finishes, fails, or is cancelled.

Create a chat with default generationOptions, or pass an optional override to send or stream for one turn. A completed message exposes an explicitly requested bounded rationale through reasoningSummary.

Fragments are provisional. Citations for answers about file or collection content arrive with the single .completed event, after Primordial has mapped and revalidated the supporting records.

If streaming fails or is cancelled, the partial text is not added to chat history. Commit durable UI state only after receiving .completed.

Inspect and Restore Conversation History

Read messages to inspect every completed user and assistant message in order. Because PrimordialChat is an actor, access its state with await.

let messages = await chat.messages

for message in messages {
    print(message.role)
    print(message.text)
}

You can also begin a chat with existing messages. This is useful after your app restores a conversation from its own storage.

let restoredMessages = [
    PrimordialChatMessage(
        role: .user,
        text: "What is actor isolation?"
    ),
    PrimordialChatMessage(
        role: .assistant,
        text: "Actor isolation protects actor-owned mutable state."
    )
]

let restoredChat = try primordial.chat(
    messages: restoredMessages
)

To restore a grounded conversation, pass the same collection boundary and answer options together with the saved messages.

let restoredGroundedChat = try primordial.chat(
    using: [notes],
    options: options,
    messages: restoredMessages
)

Primordial keeps chat history in memory only. Your app decides whether to persist it, how long to retain it, and how to translate messages into its own durable data model.

Clear or Reset a Chat

clear() removes the current in-memory history. reset() restores the messages supplied when the chat was created.

try await chat.clear()
print(await chat.messages) // []

try await restoredChat.clear()
try await restoredChat.reset()
print(await restoredChat.messages) // restoredMessages

Neither operation changes your collections or deletes conversation data that your app persisted elsewhere.

Keep One Turn Active per Chat

One chat accepts one active send or stream turn at a time. Starting another turn on that same chat before the first finishes returns a resource-conflict error. Separate chat instances keep separate histories and may operate independently.

A user and assistant message enter history together only after the response finishes successfully. Failure or cancellation commits neither message, and a cancelled Swift task remains CancellationError.

Stop a Chat Reply

Store the Swift task running chat.send so you can stop the reply:

let replyTask = Task {
    try await chat.send("How should I protect shared state?")
}

To stop the reply, cancel that task. Wait for it to finish before sending another message on the same chat:

replyTask.cancel()
_ = await replyTask.result

A cancelled turn adds neither the user message nor the partial reply to chat history. Messages from completed turns stay in history. Use try await replyTask.value to read a successful reply, and handle CancellationError without showing an error message.

For chat.stream, cancel the task containing the stream loop and discard any unfinished text in your UI. The stream may need a short time to stop. See Stop a Response for the view model and navigation example.

Handle Chat Errors

Empty messages, unavailable AI resources, incompatible grounded collections, invalid answer options, overlapping turns, generation failures, and cancellation all fail explicitly.

do {
    let reply = try await chat.send(
        "How should I protect shared state?"
    )
    print(reply.text)
} catch is CancellationError {
    print("Chat cancelled")
} catch let error as PrimordialCollectionError {
    print("Collection error: \(error.localizedDescription)")
} catch let error as PrimordialAIError {
    switch error {
    case .capabilityUnavailable(let requirement):
        print("Download required: \(requirement.downloadBytes) bytes")

    case .invalidQuery(let message),
         .invalidConfiguration(let message),
         .invalidRecord(let message),
         .resourceConflict(let message):
        print(message)

    default:
        print(error.localizedDescription)
    }
}

When .capabilityUnavailable is returned, use its requirement to explain what is missing and ask the user before calling makeAvailable. Do not begin a model download automatically from an error handler.