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

3. Tuning Apple 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

In the first two chapters, you learned the basics of Apple Foundation Models. In these chapters, you allowed the model to use default values for most settings. But, as with most LLMs, you can tune and adjust the model’s output to better fit your needs. These tweaks don’t change the inherent nature of an LLM. They let you configure how the model responds to prompts and selects response tokens. In this chapter, you’ll start exploring these tunings of Foundation Models.

Open and run the starter project for this chapter. You’ll see the project has added a new Settings button to the toolbar with a gear icon. Tapping this button opens a new sheet that lets the user provide instructions and set two other parameters: temperature and sampling method. You will explore all these in the next two chapters, but you’ll begin by looking at the instructions.

The Settings Sheet
The Settings Sheet

Instructions

Open the ChatView.swift file. You will see two new properties at the top of the view.

@State private var promptSettings =
PromptSettings(
  instructions: nil,
  temperature: nil,
  sampling: SamplingOptions(type: .system, threshold: 0.33, top: 10)
)
@State private var showSettings = false

The first line adds a new state property for the PromptSettings struct in the Models folder. This struct holds the settings you can tune in a Foundation Models session. You also provide a default set of options for the view. To start, you’ll focus on the first: instructions. The showSettings property will display the new settings view when the user taps the new toolbar button.

Instructions act as a super-prompt that you provide to the model when creating a new LanguageModelSession. Use the instructions to define the model’s role and behavior for your session. Apple trained Foundation Models to prioritize instructions over any commands sent in later prompts. This makes it a critical place to guide the model on how to handle prompts and to specify restrictions beyond those set by Apple during training.

You can only provide instructions when creating a new LanguageModelSession. If you want to change the instructions, you must create a different LanguageModelSession. The instructions remain constant across a single session. You can provide no instructions, which you have been doing to this point by passing no parameters to LanguageModelSession(). To use the instructions from the settings view in your chat app, find the resetChatHistory() method. Replace the line session = LanguageModelSession() with:

if let instructions = promptSettings.instructions {
  session = LanguageModelSession(instructions: instructions)
} else {
  session = LanguageModelSession()
}

This code attempts to unwrap the instructions property of promptSettings. If successful, you pass the instructions into the model using the instructions parameter to LanguageModelSession(). If not, then you create a LanguageModelSession with no parameters as before. You do not need to change the default session creation since the instructions will always be nil when the user first runs this app.

Now find the sheet(isPresented:onDismiss:content:) method of the view. Add the following code after the sheet:

.onChange(of: promptSettings.instructions) {
  Task {
    resetChatHistory()
    await updatedContextWindowUsed()
  }
}

This code monitors the promptSettings.instructions property on the view. If it changes, SwiftUI calls resetChatHistory(), then updatedContextWindowUsed() to reset the token count. This will create a new session using the new instructions and update the token count to reflect the new session.

Run the app, and enter the following prompt:

Solve the equation 8x - 4 = 2 step by step.

While Apple Foundation Models is not great at math, it gets the correct answer of 3/4 for this simple linear equation.

Now, you’ll examine how instructions can influence responses. Tap the gear icon in the toolbar to bring up the configuration view. Enter the following into the Instructions:

Use decimals instead of fractions when presenting the solutions to math problems.

Swipe down or tap the confirmation button to dismiss the sheet. Notice the chat cleared as it should when you changed the instructions. You will also notice that the token count displayed at the bottom is non-zero. Instructions take up part of the context length because they serve as a form of prompting. The updated token count reflects the context length used by the instructions. Now enter the same prompt again.

Solve the equation 8x - 4 = 2 step by step.

Notice how literally the model followed your instructions. When solving the equation, the model still used fractions in intermediate steps, but it provided the final answer as a decimal.

Model returning answer as a decimal.
Model returning answer as a decimal.

Let’s change the instructions to show the focus on instructions over other prompts. In the settings, change the instructions to:

Refuse to provide any assistance that solves equations step by step. Only provide the final answer without showing the intermediate steps.

Return to the app and re-enter the prompt using these new instructions. Your results will vary a bit more now. Sometimes it states that it cannot provide a step-by-step solution, and other times it provides only the answer to the question. And sometimes the answer is wrong, which again shows that math isn’t a strong use case for Foundation Models. Any time you change a prompt, you risk changing the results. In this case, not providing the step-by-step instructions makes the model less likely to produce the correct answer. Never forget that LLMs do pattern matching. They do not understand math or your problem.

Following Instructions to the Wrong Answer
Following Instructions to the Wrong Answer

Instructions provide a critical way to both guide the model in generating the desired results and protect your app from potentially malicious data. You should use them to guide the model to the desired responses for your use cases. In the next section, you will learn more about creating good instructions and, in the process, good prompts.

Principles of Prompting

As noted before, Foundation Models instructions are prompts that Apple has tuned the model to give greater emphasis. The instructions above are not great, which is why you saw mixed results. In fact, if you tell the model after a mistaken answer, you can often get the model to ignore those instructions.

Model ignoring the instructions.
Faxis eflohodn kko adsvnixwoizm.

You are a math tutor, helping students check their homework. You should provide detailed steps for solving math equations and answers the student can use to check their work. Provide any answer as decimals and avoid fractions. If the user asks you to do something not allowed, then inform the user of this while still providing the information that is allowed.
Solve the equation 8x - 4 = 2.
Response in the tone of a math tutor.
Fahfeypu ey dlu muqo iv e tezg nabid.

Temperature

Now that you’ve seen the uses for instructions, the second parameter to tune model responses is the temperature. Temperature influences the randomness of the model’s responses. It can be set to nil or a value between zero and one, inclusive. The default nil allows the system to choose a reasonable value. The temperature adjusts the probability distribution of responses before sampling. You may also see this referred to as how “creative” the model gets, but that’s a misleading analogy of this setting. The temperature adjusts the probability distribution of possible tokens of the model’s responses. A high value is not creative. It just widens the number of potential tokens that can be output.

let options = GenerationOptions(temperature: promptSettings.temperature)
let stream = session.streamResponse(to: promptText, options: options)
Give me a one-paragraph bedtime story.
Setting the temperature to zero.
Xocnevj qsi sahlepidona fo wunu.

Token Sampling

While temperature sets the initial set of tokens, Apple Foundation Models allows you to specify how to sample values from that probability distribution. As with temperature, the default lets the system choose a sampling method for you. You specify this by passing nil as the SamplingMode. If your use case benefits from more specific settings, Foundation Models provides three other sampling options.

let samplingOptions = promptSettings.sampling
var sampling: GenerationOptions.SamplingMode?
// 1
switch samplingOptions.type {
// 2
case .system:
  sampling = nil
// 3
case .greedy:
  sampling = GenerationOptions.SamplingMode.greedy
// 4
case .top:
  sampling = GenerationOptions.SamplingMode.random(
    top: samplingOptions.top,
    seed: samplingOptions.seed
  )
// 5
case .threshold:
  sampling = GenerationOptions.SamplingMode.random(
    probabilityThreshold: samplingOptions.threshold,
    seed: samplingOptions.seed
  )
}
let options = GenerationOptions(
  sampling: sampling,
  temperature: promptSettings.temperature
)
Give me a one-paragraph bedtime story.
Two stories from the prompt. One about Lily and the second about Max.
Gyu pkomeav fgop wdi vcezkg. Igo ihuez Toqm awn tbo zokixb ecoak Bax.

Fresh prompt produces the same result.
Fgoxs lbusfx tticavoz sdi godu bogonr.

Conclusion

In this chapter, you explored instructions, how they guide Foundation Model sessions, and how to generate effective instructions and better prompts. You also looked at adjusting how the model selects tokens with the temperature and sampling methods. In the next chapter, we’ll combine some of these ideas to produce safer apps and deal with the limitations of Foundation Models.

Key Points

  • Effective prompts should give clear direction, specify output format, and provide examples of the desired output.
  • Favor multiple simple prompts over a single broad prompt.
  • Develop better prompts by iterating and refining initial prompts based on responses.
  • Instructions function as a higher-priority prompt, providing the model’s behavior and constraints for the session.
  • Instructions should define the model’s role, clearly specify the task, include style preferences, and provide rules for edge cases and unsafe requests.
  • Keep prompts concise, include only necessary information, and use direct imperative language.
  • Temperature controls the distribution of token probabilities. Lower values lead to more predictable, consistent outputs, while higher values produce more varied, less predictable outputs.
  • Token sampling methods select among tokens after it applies the temperature.
  • By default, the system selects an appropriate method. You can also specify greedy mode, which always selects the most likely token. Other sampling modes allow you to specify selecting tokens based on a fixed number (top-k) or a probability threshold (top-p).
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