Chapters

Hide chapters

Apple Foundation Models

First Edition · iOS 26.5 · Swift 6.3 · Xcode 26.4

Section I: Apple Foundation Models

Section 1: 9 chapters
Show chapters Hide chapters

1. Introduction to Foundation Models
Written by Bill Morefield

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Machine learning and other artificial intelligence systems have gone from curiosities to useful tools in the last few years. While their use cases are often overhyped, the fact that they can provide clear value to your app in the right situations is clear. You can now complete tasks on a device that fits in your hand that were previously difficult or impossible, even on enterprise equipment. Only a few of these technologies have garnered the hype and controversy as Large Language Models (LLMs). A traditional LLM requires a massive amount of computational power, memory, and resources to run. Well-funded startups were the one ones able to train and run these models and deploy huge amounts of memory, storage, and computing power.

To address these high system requirements, many programmers have explored deploying local models. These models are optimized and simplified to run on the devices and equipment of everyday users. Starting with iOS 26, iPadOS 26, macOS 26, and other version 26 operating systems, Apple is providing its own local model, optimized for use in apps, called Apple Foundation Models. Apple Foundation Models are Apple’s on-device AI models, designed to protect privacy while helping with tasks like writing text, summarizing information, and organizing data on supported devices. Because all data remains on the device, you don’t need an internet connection, there is less latency, and you avoid the privacy risks that come when sending data to third-party services. This book will explore the use of Apple Foundation Models in your apps.

What is Apple Foundation Models?

It’s worth starting with the most basic question: What is Apple Foundation Models Framework? The short answer is that it is a large language model (LLM) that Apple has optimized to run locally on end-user devices, such as laptops, desktops, and mobile devices. Traditional LLMs operate in data centers equipped with high-powered GPUs, which need a lot of memory and power. Bringing that functionality to an end-user device requires significant changes to the model. In Apple’s case, the two most important changes to produce Foundation Models are reducing the number of parameters and quantizing the values that form the model. You’ll learn more about how they did that later.

In this chapter, you will develop a chat-style app that interacts with Foundation Models to explore the possibilities and limitations of this framework. A chat app isn’t a great use case for Foundation Models due to the small size of the on-device model, but it provides a well-understood app to explore integrating Apple Foundation Models into a SwiftUI app. The immediate feedback will also make it easier to explore the model’s use and limitations.

Using Foundation Models

Open the starter app in Xcode 26 or later. Run the app, and you will see the starter implements a simple chat-style interface. The textbox at the bottom of the view provides the user a place to enter text and “send” it. Right now, the chat will echo any text entered.

Starter App Echos text back to user.
Dcewlug Ovv Obqap fapr pizs je ulic.

import FoundationModels
private let model = SystemLanguageModel.default
switch model.availability {
case .available:
  ChatView()
case .unavailable(let reason):
  ModelUnavailableView(reason: reason)
}
import FoundationModels
var reason: SystemLanguageModel.Availability.UnavailableReason
Image(systemName: "apple.intelligence")
  .font(.largeTitle)
switch reason {
case .deviceNotEligible:
  Text("Apple Intelligence is not available on this device.")
case .appleIntelligenceNotEnabled:
  Text("Apple Intelligence is available, but not enabled on this device.")
case .modelNotReady:
  Text("The model isn't ready. This is usually because it is still downloading.")
@unknown default:
  Text("An unknown error prevents Apple Intelligence from working.")
}
ModelUnavailableView(reason: .appleIntelligenceNotEnabled)
View Shown When Foundation Models Not Available
Poeg Gjeqx Jqic Xeomyolueh Tewufx Goj Iluohosjo

Testing Apple Intelligence Failure States

XCode does not provide a direct option either in its own settings or in the Simulator to set specific failure conditions. You can accomplish this using schemes. In XCode, select Product ▸ Scheme ▸ Edit Scheme….

Edit Scheme
Anew Ryhula

Using Scheme to Test Failure Conditions
Ecaml Pcroru ze Wuzr Zauzodo Newmosoelg

Scheme Settings Showing Apple Intelligence Not Available
Lyzadu Portujmh Rhubesj Uwlyo Usdoclewewna Cij Ocuahetfa

Using Foundation Models

Now that you know how to verify that Foundation Models is available on the device for your app, it’s time to finally tie this chat app into Foundation Models. Open ChatView.swift. First, import Foundation Models by adding the following import after the existing one.

import FoundationModels
// 1
guard !promptText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return }

// 2
addMessage(promptText, type: .prompt)

// 3
let session = LanguageModelSession()

do {
  // 4
  let modelResponse = try await session.respond(to: promptText)
  promptText = ""
  // 5
  addMessage(modelResponse.content, type: .fullResponse)
} catch {
  // 6
  let errorResponse = "An error occurred while processing your message. \(error.localizedDescription)"
  addMessage(errorResponse, type: .error)
}
Response to a hello prompt.
Nezgucmu ca e vocke wpocgk.

What is an LLM?

Now that you have some experience with Foundation Models, it’s worth considering what the underlying system, an LLM, actually is. Just the detailed discussion of how LLMs work could fill an entire book. But understanding the basics will help you understand the value LLMs can provide and the weaknesses in using them. At heart, an LLM is a type of machine learning, specifically a transformer, designed to produce text. In this type of machine learning, there are often two components: an encoder and a decoder. Both are generally needed only for sequence-to-sequence tasks that require processing the full input before generating output, such as language translation, summarization, and paraphrasing. Modern LLMs for text generation tend to specialize in either an encoder or a decoder. Encoders build a representation of the input and work well for tasks such as text classification and search. Decoders produce better results to create open-ended text. Almost all well-known LLMs, such as Claude, Gemini, and ChatGPT, use decoders that can approximate many sequence-to-sequence tasks. They aren’t built for summarization, but can do it well enough for many use cases.

How Apple optimized Foundation Model

The introduction of this chapter stated that a traditional LLM requires a massive amount of computational power, memory, and resources to run. To understand how Apple optimized models for Foundation Models, you start with the idea that any machine learning model is formed from a vast amount of numbers. In LLMs, these numbers are called parameters. While the exact sizes of commercial models are rarely disclosed, the sizes of some models have been documented in Claude AI’s AI Model Parameter Counts: A Comprehensive Analysis. Recent models often contain hundreds of billions of parameters. Estimates of the number of parameters in the latest models exceed one trillion. As each parameter consists of a number, that number must be represented in a format that computers understand. The most common is a 32-bit floating point, abbreviated as fp32. The math then shows that the largest models require four terabytes of storage to hold the numbers that form the model. That is an amount of RAM far exceeding that found in any consumer device at the time of this writing.

Handling Model Delays

While this basic implementation shows how little code you need to work with Foundation Models, it has several weaknesses. The most glaring is that you create a new LanguageModelSession for each prompt. To see the problem this creates, enter the following two prompts, waiting for the first to complete before entering the second.

Give me five popular fruits.
Which of these are commonly available in the United States in the summer?
Lack of Session Memory in Chat.
Yoxk ob Lumzaus Hisosp eh Pseq.

@State private var session = LanguageModelSession()
Chat with Session using responses from previous prompts.
Xzew ceps Nekxeey isoyv febluqres mlih lmiroaaj cwevkqk.

Error sending prompt to model before previous response finishes.
Ojhux fugjuss bpenrv de fofes nugeda lcujuiij yawhidga yumekqiq.

.disabled(session.isResponding)
if session.isResponding {
  TypingIndicator()
    .transition(.scale)
}
Working Indicator.
Qusfufh Uhtidicit.

private func resetChatHistory() {
  messages = []
  session = LanguageModelSession()
}
@State private var confirmClear: Bool = false
@ToolbarContentBuilder private var appToolbar: some ToolbarContent {
  ToolbarSpacer(.flexible, placement: .bottomBar)
  ToolbarItem(placement: .bottomBar) {
    Button("Clear", systemImage: "xmark.circle.fill") {
      confirmClear = true
    }
    .tint(.red)
    .confirmationDialog(
      "Are you sure you want to delete the chat history?",
      isPresented: $confirmClear
    ) {
      Button("Delete Chat History", role: .destructive) {
        resetChatHistory()
      }
    }
  }
}
.toolbar {
  appToolbar
}

Conclusion

In this chapter, you learned about what Apple Foundations Models provides and built the basics of an app to allow the user to interact with Foundation Models using the chat interface familiar to anyone who has used an LLM. Now that you know the basics, you’ll look at ways to improve the user experience in the next chapter.

Key Points

  • A large language model (LLM) is a type of machine learning, specifically a transformer, designed to produce text. There are often two components: an encoder and a decoder.
  • A traditional LLM requires a massive amount of computational power, memory, and resources to run.
  • Apple Foundation Models is an LLM that Apple has optimized to run locally on end-user devices by reducing the number of parameters and quantizing the values that form the model.
  • SystemLanguageModel is the on-device text foundation model.
  • You can test different failure scenarios using schemes.
  • Interactions with LLMs consist of a prompt sent to the model and a response from the model.
  • Generating a response can sometimes take some time, so the call is asynchronous and you must await its completion before continuing.
  • Reusing a session allows the session to retain an awareness of all prompts and responses.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now